EA to trade only on certain time periods

 

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! 

 
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 (https://www.mql5.com/en/docs/dateandtime) especially TimeCurrent() (https://www.mql5.com/en/docs/dateandtime/timecurrent) and TimeLocal() (https://www.mql5.com/en/docs/dateandtime/timelocal)

 

Documentation on MQL5: Date and Time
Documentation on MQL5: Date and Time
  • www.mql5.com
Date and Time - Documentation on MQL5
 
onewithzachy:

Hi polymath,

Please read Date and Time (https://www.mql5.com/en/docs/dateandtime) especially TimeCurrent() (https://www.mql5.com/en/docs/dateandtime/timecurrent) and TimeLocal() (https://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! 

 

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! 

 
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)

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

}
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type
  • www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type - Documentation on MQL5
 

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! 

 
polymath:

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:

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

Thanks again! 

Polymath, well done for coming back with this - you saved me time! 

 
strontiumDog:

Polymath, well done for coming back with this - you saved me time! 

where to add this code? I mean to say in which part because I tried but I am getting error.

 
dhrums82:

where to add this code? I mean to say in which part because I tried but I am getting error.

OnTick

 
polymath:

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:

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

Thanks again! 

Thank you very much, +1
Reason: