Download MetaTrader 5
To add comments, please log in or register
Do you have Mac OS? Find out how to install MetaTrader on it!
Proximus
760
Proximus 2014.01.04 10:05 

I`ve just wanted to put a time filter in my EA to only trade between 7 Am and 16 PM (london- ny session) and for some reason, it took trades even after 20 PM yesterday, so it might be a glitch:

if (Hour()>=7 && Hour()<=16 && STATUS="BUY") //BUY....

if (Hour()>=7 && Hour()<=16 && STATUS="SELL") //SELL....

Its probably the simplest if() in my whole EA and this doesnt work too.... So please help me.

Simon Gniadkowski
Moderator
17996
Simon Gniadkowski 2014.01.04 10:08  
Proximus:

I`ve just wanted to put a time filter in my EA to only trade between 7 Am and 16 PM (london- ny session) and for some reason, it took trades even after 20 PM yesterday, so it might be a glitch:

Its probably the simplest if() in my whole EA and this doesnt work too.... So please help me.

Use the search and you would have seen recommendations to replace Hour() with TimeHour(TimeCurrent()) what is Hour ?
Proximus
760
Proximus 2014.01.04 10:13  
RaptorUK:
Use the search and you would have seen recommendations to replace Hour() with TimeHour(TimeCurrent()) what is Hour ?

Well i thought the Hour() function should return the current hour time of the broker.Isnt that obvious, otherwise why does a Hour() function exist if it doesnt show the current hour...?
Simon Gniadkowski
Moderator
17996
Simon Gniadkowski 2014.01.04 10:17  
Proximus:
Well i thought the Hour() function should return the current hour time of the broker.Isnt that obvious, otherwise why does a Hour() function exist if it doesnt show the current hour...?

OK then, it does what you want so use it . . . but you have said it doesn't . . . write a test script and test it, then you will know when it works and when it doesn't, then when you have found out when t doesn't you can find out why . . . or just use the code I have suggested.

whroeder1
14457
whroeder1 2014.01.04 12:40  
  1. RaptorUK: Use the search and you would have seen recommendations to replace Hour() with TimeHour(TimeCurrent()) what is Hour ?
    This was based on a post that Hour(), DayOfWeek(), etc didn't work properly in the tester. IIRC there are a test of that on build 5xx that showed it has been corrected.
  2. Proximus: I`ve just wanted to put a time filter in my EA to only trade between 7 Am and 16 PM (london- ny session) and for some reason, it took trades even after 20 PM yesterday,
    If your broker's time UTC+1? Otherwise you need to post the broken code.
Simon Gniadkowski
Moderator
17996
Simon Gniadkowski 2014.01.04 13:09  
WHRoeder:
  1. RaptorUK: Use the search and you would have seen recommendations to replace Hour() with TimeHour(TimeCurrent()) what is Hour ?
    This was based on a post that Hour(), DayOfWeek(), etc didn't work properly in the tester. IIRC there are a test of that on build 5xx that showed it has been corrected.
And yet people still report issues . . . I know TimeHour(TimeCurrent()) will work.
Proximus
760
Proximus 2014.01.07 08:45  

My broker has GMT +0 timezone it is based in London, but i dont know how that makes a difference, i have build 509

@ RaptorUK, i dont know, it seems that TimeHour(TimeCurrent()) doesnt work either, i just recoded it and replaced every Hour() with TimeHour(TimeCurrent()) and it doesnt work, i put the EA yesterday on forward test at around 8 AM GMT +0 and let it run until 23PM, but my filter clearly said that it shouldn't take trades after 17 PM, the last trade must be no later than 16:59, yet it took trades at even 22:30 PM, so i dont know if it works...

Simon Gniadkowski
Moderator
17996
Simon Gniadkowski 2014.01.07 09:24  
Proximus:

My broker has GMT +0 timezone it is based in London, but i dont know how that makes a difference, i have build 509

@ RaptorUK, i dont know, it seems that TimeHour(TimeCurrent()) doesnt work either, i just recoded it and replaced every Hour() with TimeHour(TimeCurrent()) and it doesnt work, i put the EA yesterday on forward test at around 8 AM GMT +0 and let it run until 23PM, but my filter clearly said that it shouldn't take trades after 17 PM, the last trade must be no later than 16:59, yet it took trades at even 22:30 PM, so i dont know if it works...

I can assure you 100% that TimeHour(TimeCurrent()) will give you the correct hour, Print("Hour number - current time: ", TimeHour(TimeCurrent()) ) and see for yourself. Perhaps your STATUS variable/constant ? was not set to what you thought it should be . . . or perhaps your OrderSend() has an issue ?
Proximus
760
Proximus 2014.01.07 15:13  
RaptorUK:
I can assure you 100% that TimeHour(TimeCurrent()) will give you the correct hour, Print("Hour number - current time: ", TimeHour(TimeCurrent()) ) and see for yourself. Perhaps your STATUS variable/constant ? was not set to what you thought it should be . . . or perhaps your OrderSend() has an issue ?


On backtest both the Hour() and the TimeHour(TimeCurrent()) works, i printed the outcomes of both and they worked, however in real time trading the Hour() doesnt work anymore, i think because of this:

int Hour( )
Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).
Note: At the testing, the last known server time is modelled.


Which is pretty silly, but whatever, but the TimeHour(TimeCurrent()) should work on real time trading, i printed it out, it works if i printed it, but if i tested it in an IF() then it doesnt work property, i dont know why?

Simon Gniadkowski
Moderator
17996
Simon Gniadkowski 2014.01.07 15:20  
Proximus:


Which is pretty silly, but whatever, but the TimeHour(TimeCurrent()) should work on real time trading, i printed it out, it works if i printed it, but if i tested it in an IF() then it doesnt work property, i dont know why?

Because your code is wrong . . . it cannot print correctly and then not work as part of an if unless your code is wrong . .
GreenMoney
379
GreenMoney 2014.01.07 15:46  
Proximus:

On backtest both the Hour() and the TimeHour(TimeCurrent()) works, i printed the outcomes of both and they worked, however in real time trading the Hour() doesnt work anymore, i think because of this:

int Hour( )
Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).
Note: At the testing, the last known server time is modelled.


Which is pretty silly, but whatever, but the TimeHour(TimeCurrent()) should work on real time trading, i printed it out, it works if i printed it, but if i tested it in an IF() then it doesnt work property, i dont know why?


I believe you have misinterpreted the text you've highlighted from the documentation. See my detailed explanation here.

From my experience, Hour() works both live on a chart and in the tester. I use it in my live EA, and I've tested it in the strategy tester. See my explanation link above.

Also, you should split your IF statement into a more manageable statement--remember: simpler is better.

if (Hour()>=7 && Hour()<=16) {
   if (STATUS="BUY") 
      //BUY....
   else if (STATUS="SELL")
      //SELL....
}

The above code snippet will first evaluate whether the hour is between 7 and 16, inclusively, and then (only if Hour() is >= 7 and <=16) the STATUS variable is evaluated to determine whether it is BUY or SELL. This has the added benefit of allowing you to better and more effectively debug the statement to determine if it is working correctly (and if not, where the problem is likely to be).

/ /12
To add comments, please log in or register