You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.
I disagree with creating a new bar function, because it can only be called once per tick (the second call returns false). A variable can be checked several times.
I managed to make a generic function IsNewBar(symbol, timeframe), which works correctly within one EA for different combinations of symbol and timeframe, and can be called multiple times within the processing of one tick, returning the same thing the second time (and subsequent times) as the first time.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
1. Improperly initialized previous_time, causing false alarms immediately upon code startup.
2. Expensive access to iTime at each tick.
3. Not all ticks form a bar (in rare cases, a tick can occur on an off day and is not a transaction, but just some internal server procedure).
You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.
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 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)
I disagree with creating a new bar function, because it can only be called once per tick (the second call returns false). A variable can be checked several times.
I managed to make a generic function IsNewBar(symbol, timeframe), which works correctly within one EA for different combinations of symbol and timeframe, and can be called multiple times within the processing of one tick, returning the same thing the second time (and subsequent times) as the first time.