just declare a datetime variable and store the bar open time, then compare it to the live value and if it has changed , you know there was a new bar.
if(D1!=iTime(Symbol(),PERIOD_D1,0)) // new candle on D1
D1=iTime(Symbol(),PERIOD_D1,0); // overwrite old with new value
For a new bar test, Bars is unreliable (a refresh/reconnect can change
number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable
(duplicate prices and The == operand. - MQL4 and MetaTrader 4 -
MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called
once per tick. A variable can be tested multiple times.
Volume works fine. " Volume==1 " means the first tick of a new bar.
Nope, volume works really badly. Your terminal will miss ticks so sometimes the new bar will start on >1
Also realize that when a new bar arrives for your chart symbol, it does not mean that there will be new bars for all symbols.
This is important when you are building multi symbol robots.
After detecting the first new bar for a symbol, it can take quite some time before all symbols will return the new bar time.
So then you check all bar open times in a loop until they are all returning the new open time.
I've never had a problem using the ==1. But I understand that the terminal might miss a tick or two and there is a better way.
You are right. Time is better and more reliable.