Limit trading to given hours and days

Back to topics list To post a new topic, please log in or register
avatar
27
jmb 2015.02.06 17:01
 

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.   :) 

avatar
2017
gooly 2015.02.06 17:23 #
 

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;


avatar
736
honest_knave 2015.02.06 17:23 #
 

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
avatar
736
honest_knave 2015.02.06 17:27 #
 

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

avatar
13008
WHRoeder 2015.02.06 18:34 #
 
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;

avatar
736
honest_knave 2015.02.06 19:09 #
 

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.

 

avatar
27
jmb 2015.02.08 00:02 #
 
Folks, thank you all for your input and help.  Its much appreciated.  :)
Back to topics list  

To add comments, please log in or register