Download MetaTrader 5

Limit trading to given hours and days

To add comments, please log in or register
jmb1
86
jmb1  

Hello

I'd be very grateful if someone could confirm that I have my logic correct here:

   // only trade Monday to Friday inclusive
   if(DayOfWeek()==0 || DayOfWeek()==6) return;
   // don't trade during the hours of 0, 4, 18 to 19 inclusive, 22 to 23 inclusive
   if((Hour()>=0 && Hour()<1) || (Hour()>=4 && Hour()<5) || (Hour()>=18 && Hour()<20) || (Hour()>=22) )return;

 

Sorry for such a basic query, but I'm still learning!

Thank you.   :) 

Carl Schreiber
6969
Carl Schreiber  

Seems to be ok, but can be simplified:

//if((Hour()>=0 && Hour()<1) || (Hour()>=4 && Hour()<5) || (Hour()>=18 && Hour()<20) || (Hour()>=22) )return;
if( Hour()<1 || Hour()==4  || (Hour()>=18 && Hour()<20) || Hour()>=22 ) return;
honest_knave
Moderator
2373
honest_knave  

That should work fine. A couple of finesse points you may be interested in 

 // only trade Monday to Friday inclusive
 if(DayOfWeek()==0 || DayOfWeek()==6) return;
 // don't trade during the hours of 0, 4, 18 to 19 inclusive, 22 to 23 inclusive
 int hr=Hour(); // this is more efficient than calling Hour() each time
 if(hr==0 || hr==4 || (hr>=18 && hr<20) || hr>=22)return; // this is simpler
honest_knave
Moderator
2373
honest_knave  

Sorry gooly, looks like we posted at the same time.

whroeder1
15304
whroeder1  
gooly :Seems to be ok, but can be simplified:
honest_knave: this is more efficient than calling Hour() each time
 // only trade Monday to Friday inclusive
 if(DayOfWeek()==0 || DayOfWeek()==6) return;
 // don't trade during the hours of 0, 4, 18 to 19 inclusive, 22 to 23 inclusive
 int hr=Hour(); // this is more efficient than calling Hour() each time
 if(hr==0 || hr==4 || (hr>=18 && hr<20) || hr>=22)return; // this is simpler
Even simplifier and efficient
int DOW[] = { 0,1,1,1,1,1,0};
//            0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int HR[]  = { 0,1,1,1,0,1,1,1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0};
if(DOW[DayOfWeek()] == 0 || HR[Hour()] == 0)return;
honest_knave
Moderator
2373
honest_knave  
You could simplify it further using bool array
int DOW[] = { 0,1,1,1,1,1,0};
//            0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int HR[]  = { 0,1,1,1,0,1,1,1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0};
if(DOW[DayOfWeek()] == 0 || HR[Hour()] == 0)return;

--or--

bool DOW[] = { 0,1,1,1,1,1,0};
//             0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
bool HR[]  = { 0,1,1,1,0,1,1,1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0};
if(!DOW[DayOfWeek()] || !HR[Hour()])return;

 

Interestingly, using GetTickCount() over 100,000,000 iterations show the array method to be twice as slow as the method not accessing arrays. Which surprised me (I was bored). The arrays were declared outside of the loop.

jmb1
86
jmb1  
Folks, thank you all for your input and help.  Its much appreciated.  :)
To add comments, please log in or register