Is there an occassional delay updating the buffer as a new candle starts?

 

Hi,


I have an EA which uses the iClose function with a shift value of 1 to get the close price of the last candle. I have noticed that occassionally, when using the H1 timeframe, the EA has opened trades one hour late!! It seems that the close price information of the candle that has just closed has not been updated to the buffer at the moment that the EA is looking for the close price of the last candle. Therefore it uses the info from the second last candle which it is still reading as the last candle.


Is this a know problem in MT4?


If so, is it advisable to include a small time delay into the EA at the start of a new hour in order to give the buffer sufficient time to update? What length of small time delay is recommended?


Thanks in advance for any help and/or insight into this matter.


~DV

 

That could depend on how your code defines "candle that has just closed".


How does it check for the condition?

 
blogzr3:

That could depend on how your code defines "candle that has just closed".


How does it check for the condition?

It checks for the condition by determining if Close1 > Close2, where these variables are defined as follows:


Close1 = iClose(Symbol(), PERIOD_H1, 1);
Close2 = iClose(Symbol(), PERIOD_H1, 2);


So, as the time moves from, for example, 10:59 AM to 11:00 AM, a new candle for 11 AM should start to form on the chart. Close1 should be the closing price of the candle that started at 10 AM (and has just closed) and Close2 should be the closing price for the candle that started at 9 AM.


What I have noticed is that occassionally a few seconds after the new hour mark, the EA is not picking up the closing price of the candle that has just closed. Hence my question about the need for a small time delay in the EA to overcome this problem.


Does anyone know anything definitive about this problem?


Thanks for help.


~DV

 

Comparing Close1/2 is not going to give you what you want because it is possible 1>2 or 2>1 or even 1=2, and it has no relation to whether a new bar has formed or not.


Neither can you depend on your "time" for the formation of a new bar, that is why you are seeing "EA is not picking up the closing price of the candle that has just closed" - because it has not-yet closed as you assumed.


You need to check for sure whether a new bar has actually just formed by:


if (LastTime!=Time[0])

{

Print("now we have new bar, do something here...");
LastTime=Time[0];

...

}

 
blogzr3:

Comparing Close1/2 is not going to give you what you want because it is possible 1>2 or 2>1 or even 1=2, and it has no relation to whether a new bar has formed or not.


Neither can you depend on your "time" for the formation of a new bar, that is why you are seeing "EA is not picking up the closing price of the candle that has just closed" - because it has not-yet closed as you assumed.


You need to check for sure whether a new bar has actually just formed by:


if (LastTime!=Time[0])

{

Print("now we have new bar, do something here...");
LastTime=Time[0];

...

}




Thanks for the info: it helped a lot. I think I have resolved the problem my doing a check of the hour of the last closing bar using a combination of the TimeHour() and iTime() functions.


Cheers,


~DV