# Limit trading to given hours and days

86

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

7111

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;```
Moderator
2328

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
```
Moderator
2328

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

15607

 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;```
Moderator
2328

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.

86

Folks, thank you all for your input and help.  Its much appreciated.  :)