MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Automated Trading and Strategy Testing Forum

Custom Panel DemoCustom Panel Demo Try product
Custom Panel Demo
Author: markon
Event handling in MQL5: Changing MA period on-the-fly Event handling in MQL5: Changing MA period on-the-fly AlfOs Indicator
AlfOs
Author: FoxRex
Screenshot
GBPUSD, M30
Real
Subscribe to signal
Hedge
57.69%, 15 664.55 USD

// EA to trade only on certain time periods

To add comments, please log in or register
polymath
177
polymath 2012.08.08 15:41

Hi guys,

I want to make an EA only trade in certain time ranges? I can't seem to locate any tutorial on timers in the article section.  I'm also confused regarding the time here in MQL5, what time does all our platforms follow? our individual local time, or is there a universal time being used? Appreciate if you could point me to anything that can help me learn the ropes regarding how to use and understand the time function of MQL5.

Thanks in advance! 

Advanced Adaptive Indicators Theory and Implementation in MQL5
This article will describe advanced adaptive indicators and their implementation in MQL5: Adaptive Cyber Cycle, Adaptive Center of Gravity and Adaptive RVI. All indicators were originally presented in "Cybernetic Analysis for Stocks and Futures" by John F. Ehlers.
onewithzachy
954
onewithzachy 2012.08.08 16:25
polymath:

Hi guys,

I want to make an EA only trade in certain time ranges? I can't seem to locate any tutorial on timers in the article section.  I'm also confused regarding the time here in MQL5, what time does all our platforms follow? our individual local time, or is there a universal time being used? Appreciate if you could point me to anything that can help me learn the ropes regarding how to use and understand the time function of MQL5.

Thanks in advance! 

Hi polymath,

Please read Date and Time (http://www.mql5.com/en/docs/dateandtime) especially TimeCurrent() (http://www.mql5.com/en/docs/dateandtime/timecurrent) and TimeLocal() (http://www.mql5.com/en/docs/dateandtime/timelocal)

 

polymath
177
polymath 2012.08.11 07:11
onewithzachy:

Hi polymath,

Please read Date and Time (http://www.mql5.com/en/docs/dateandtime) especially TimeCurrent() (http://www.mql5.com/en/docs/dateandtime/timecurrent) and TimeLocal() (http://www.mql5.com/en/docs/dateandtime/timelocal)

 

Hi onewithzachy,

Thanks for the advise! I've also stumbled across this article:

MQL5 Wizard - Trade Signals Based on Crossover of Two EMA with intraday time filter 

but I find it very difficult to understand since all the links to  CSignalITF    and CSignal2EMA_ITF  lead to a 404 error page.

I've resorted to digging up the source codes in the include folder instead and I'm trying to  dissect the classes line by line.

Thanks again! 

polymath
177
polymath 2012.08.11 10:37

Hi guys,

I'm trying to create and backtest an EA that trades 2:00 to 22:00,I'm using this code below:

datetime starttime = D'02:00:00';

datetime endtime = D'22:00:00'; 

bool checktime(datetime start,datetime end)

  {

   datetime dt=TimeCurrent();                          // current time

   if(start<end) if(dt>=start && dt<end) return(true); // check if we are in the range

   if(start>=end) if(dt>=start|| dt<end) return(true);

   return(false);

  } 

It always returns false so I printed  out the value of the starttime, endtime and TimeCurrent() , from one sample, this is what I got:

starttime = 2012.08.11 02:00:00

endtime =  2012.08.11 22:00:00

TimeCurrent = 2012.07.18 11:11:40

This is why the function always returns false, because the date that starttime and endtime displays is the date that the code is compiled.

Is there a way to get the backtesting date instead of the current date that the code is run?

Thanks in advance! 

onewithzachy
954
onewithzachy 2012.08.11 13:03
polymath:

Hi guys,

I'm trying to create and backtest an EA that trades 2:00 to 22:00,I'm using this code below:

It always returns false so I printed  out the value of the starttime, endtime and TimeCurrent() , from one sample, this is what I got:

starttime = 2012.08.11 02:00:00

endtime =  2012.08.11 22:00:00

TimeCurrent = 2012.07.18 11:11:40

This is why the function always returns false, because the date that starttime and endtime displays is the date that the code is compiled.

Is there a way to get the backtesting date instead of the current date that the code is run?

Thanks in advance! 

Hi polymath,

You probably need to specify only hour and minute of each start and end. Extract time component using TimeToStruct (click that), with this example MqlDateTime (click that again please)

enbo lu
455
luenbo 2012.08.11 14:18
input datetime Mtime_begin = D'2012.08.08 06:00';
input datetime Mtime_end = D'2012.08.08 16:00';

int CurrentTimeCompare()
{
datetime Datetime = TimeCurrent();
MqlDateTime str_current;
MqlDateTime str_begin;
MqlDateTime str_end;
TimeToStruct(Datetime,str_current);
TimeToStruct(Mtime_begin,str_begin);
TimeToStruct(Mtime_end,str_end);

if (str_current.hour >= str_begin.hour && str_current.hour < str_end.hour)
{
return(1);
}
else
return(2);

}
polymath
177
polymath 2012.08.11 17:02

Thanks a lot onewithzachy, Luenbo!  Your suggestions worked perfectly.

I've actually solved this one while waiting for somebody to reply.  I got this idea from the book "Expert Advisor Programming" by Andrew Young:

datetime starttime()
{ string currentdatestr=TimeToString(TimeCurrent(),TIME_DATE);
  string datetimenow=currentdatestr+" 02:00:00";
  return StringToTime(datetimenow);
}
datetime endtime()
{ string currentdatestr=TimeToString(TimeCurrent(),TIME_DATE);
  string datetimenow=currentdatestr+" 22:00:00";
  return StringToTime(datetimenow);
}

bool checktime(datetime start,datetime end)
  {
   datetime dt=TimeCurrent();                          // current time
   if(start<end) if(dt>=start && dt<end) return(true); // check if we are in the range
   if(start>=end) if(dt>=start|| dt<end) return(true);
   return(false);
  }

 Just thought of sharing my code in case somebody might stumble across the same task.

Thanks again! 

/
To add comments, please log in or register