Need your idea how to reset bool flag when a new bar is formed - page 2

 
@Taras Slobodyanik #:

You should not check the bar opening time, but the number of bars using iBars.

If the number of bars has changed by 1, it means a new bar has opened.
If the number of bars has changed by more than 1, it means MT has loaded quotes and formed new bars in history. This means you need to recalculate everything again (update all indicator data or EA).

https://www.mql5.com/en/docs/series/ibars

That is totally incorrect. NEVER use the bar count for detecting new bar. Bar count can change for various reasons and have nothing to do with a new bar.

The CORRECT procedure is to check the bar's open time.


Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:46

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 
Fernando Carreiro #:
That is totally incorrect. NEVER use the bar count for detecting new bar. Bar count can change for various reasons and have nothing to do with a new bar.

Well, if you want your indicator (or EA) to make calculations with leaky history then yes.

 
@Taras Slobodyanik #:Well, if you want your indicator (or EA) to make calculations with leaky history then yes.

The bar count can be used for detecting history changes where you may need to recalculate everything, but it should NEVER be user for detecting a new bar.

The correct way is the open time and that has been discussed in the forum countless times.

 
Fernando Carreiro #:

The bar count can be used for detecting history changes where you may need to recalculate everything, but it should NEVER be user for detecting a new bar.

The correct way is the open time and that has been discussed in the forum countless times.

It's the same thing, you can't expect one bar to appear and not expect many bars to appear.

Moreover, almost all indicators check not the bar time but the number of bars, even variables have been created for this: rates_total and prev_calculated.
But for some reason there is no last time variable)

 
@Taras Slobodyanik #:It's the same thing, you can't expect one bar to appear and not expect many bars to appear. Moreover, almost all indicators check not the bar time but the number of bars, even variables have been created for this: rates_total and prev_calculated. But for some reason there is no last time variable)

"rates_total" and "prev_calculated" serve a different purpose and not for new-bar detection.

Ever since the old MQL4 (and probably before), it has been repeated time and time again, that using the bar count for detecting a new bar is unreliable and only the open time can be relied on.

There is a caveat, however, and that is when using a non standard custom symbol which can have consecutive bars with the same timestamp. In these cases we have to use multiple conditions to check for a new bar. For example, for a Renko one would need to check the Open Time and the Open Price.

Using the "bars" method will inevitably fail especially when you have set a limit on the maximum number of bars for charts and history, where data is shifted in and out to maintain that bar limit. It is even mentioned in the documentation for iBars in MQL5 (same applies in MQL4, as it states "Returns the number of bars on the specified chart.").

Return Value: The number of bars of a corresponding symbol and period, available in history, but no more than allowed by the "Max bars in chart" parameter in platform settings.

Documentation on MQL5: Timeseries and Indicators Access / iBars
Documentation on MQL5: Timeseries and Indicators Access / iBars
  • www.mql5.com
Returns the number of bars of a corresponding symbol and period, available in history. Parameters symbol [in]  The symbol name of the...
 
Fernando Carreiro #:
"rates_total" and "prev_calculated" serve a different purpose and not for new-bar detection.

Ok, all indicators can work with rates_total but user should NEVER do that.

Fernando Carreiro #:

Using the "bars" method will inevitably fail especially when you have set a limit on the maximum number of bars for charts and history, where data is shifted in and out to maintain that bar limit. It is even mentioned in the documentation for iBars.

Return Value: The number of bars of a corresponding symbol and period, available in history, but no more than allowed by the "Max bars in chart" parameter in platform settings.

It is the number of bars that matters, not the limitation of that number.

 
Taras Slobodyanik #: It is the number of bars that matters, not the limitation of that number.

It seems you refuse to acknowledge the obvious. The limitation affects the bar count, so your bar detection will fail when the history is greater than the limit.

Feel free to ignore the advice for yourself, but just don't incentivise others to do it incorrectly.

 
Fernando Carreiro #:

It seems you refuse to acknowledge the obvious. The limitation affects the bar count, so your bar detection will fail when the history is greater than the limit.

Feel free to ignore the advice for yourself, but just don't incentivise others to do it incorrectly.

It seems you don't understand how it works.
The number of bars always increases, but does not decrease and does not remain constant.

Feel free to check it)
 
@Taras Slobodyanik #: It seems you don't understand how it works. The number of bars always increases, but does not decrease and does not remain constant.Feel free to check it)

Very well, I will do so! I will run tests and check it. If you are correct and if the bars count does in fact overflow the max limit, then that will mean that the documentation is incorrect.

I will do a full test on both MQL4 and MQL5 and post the results with log output here later on.

 
@Taras Slobodyanik #: It seems you don't understand how it works. The number of bars always increases, but does not decrease and does not remain constant.Feel free to check it)

In a follow-up question and irrespective of the test results, what is your reasoning for considering the "Open Time" method as incorrect? Where does it fail?

If you are correct about the "bar" method, what is preventing the "open time" method from also being a valid method?