Function of "trading at certain hours" strategy

 

Do you have a Function of "trading at certain hours" strategy?

I would like to test if EA works better 24hrs or during certain hours.

 

There is no standard function in mql4 to do this.

You will need to write your own function.

If you have problems, post your code for your function and I  am sure that someone will help you.

 

Use hour function with if statement.

 

https://docs.mql4.com/dateandtime/hour

 

maybe something like this:

 

input int StartHour=0;

input int StartMinute=0;

input int EndHour=2;

input int EndMinute=0; 

 

if IsTime(StartHour,EndHour,StartMinute,EndMinute) ... open trade here 

 

 

bool IsTime (int startHour, int endHour, int startMinute, int endMinute)

{

   if (startHour < 0 || startHour > 23 || endHour < 0 || endHour > 23 ||

       startMinute < 0 || startMinute > 59 || endMinute < 0 || endMinute > 59)

       return false;

   

   int startTime = startHour*60 + startMinute;

   int endTime = endHour*60 + endMinute;

   int time = Hour()*60 + Minute();

   

   if (startTime < endTime)

      return (time >= startTime && time <= endTime);

   else if (startTime > endTime)

      return (time >= startTime || time <= endTime);

   else

      return (time == startTime);

}

 

Something like this, you just need to offset the server time on depending where you live. The hour function does work as I have used it.

 

bool tradetime;

tradetime=false;
if(Hour()>=12 && Hour()<24){
tradetime=true;}
Reason: