# Automated Trading and Strategy Testing Forum

Try product
Custom Panel Demo
Author: markon
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

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.

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.

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)

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,

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!

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?

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?

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)

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);

}

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!

/