Download MetaTrader 5
To add comments, please log in or register
Interested in the Market activity? Come and track!
Route206
211
Route206 2016.01.14 15:49 

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. 

honest_knave
963
honest_knave 2016.01.14 18:49  

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.

Route206
211
Route206 2016.01.15 09:14  

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) 

honest_knave
963
honest_knave 2016.01.15 10:28  

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.

whroeder1
13613
whroeder1 2016.01.15 12:50  
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.
/
To add comments, please log in or register