TimeDayOfWeek() issue

 

I have a weird one below: The if statement should only be able to return 'true' on Fridays, yet i have this trigger every day for some odd reason and positions close as a result.

void OnTick()
 {
<snip>
  if(TimeHour(TimeLocal())==23 && TimeDayOfWeek(TimeLocal()==5))
     {
      Alert("Reason to close: It's Weekend");
      ClosePositions();
     }
 }

 Any thoughts? I'm seeing this happen 3 days in a row now and i'm puzzled. 

 

Check your brackets:

if(TimeHour(TimeLocal())==23 && TimeDayOfWeek(TimeLocal()==5))

 TimeLocal() will not be equal to 5, so you have written:

TimeDayOfWeek(TimeLocal()==5)

TimeDayOfWeek(false)

TimeDayofWeek(0)

 Time started on 01 January 1970, which happened to be a Thursday. So this will evaluate to 4.

 

Oh darn - i can't believe i missed that! 

if(TimeHour(TimeLocal())==23 && TimeDayOfWeek(TimeLocal())==5)

 is what it should have been of course!!   Thanks!

 (my assumption that the compiler would choke on any kind of bracket mistake has now also proven wrong) 

 

Glad you got it sorted.

Technically, there is nothing wrong with your statement so the compiler didn't have a problem.

The second part will just always evaluate as true because the number 4 is greater than 0 (because 0 would be false). It is the same as writing:

if(TimeHour(TimeLocal())==23 && true)

 which is pointless but technically a valid statement.

 
The second question is why are you using TimeLocal? The market closes 5 PM ET. Unless you live east of Germany and have the same DST (NY+7) the market will not close your time midnight.
Reason: