Download MetaTrader 5

Function of "trading at certain hours" strategy

To add comments, please log in or register
jon
465
jon 2016.09.15 21:29 

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.

Keith Watford
Moderator
9642
Keith Watford 2016.09.15 21:36  

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.

alexlearnmql
41
alexlearnmql 2016.09.18 13:31  

Use hour function with if statement.

 

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

FXreedom
43
FXreedom 2016.09.18 16:44  

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

}

alexlearnmql
41
alexlearnmql 2016.09.18 18:43  

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;}
To add comments, please log in or register