
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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).
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)
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?
of course "Open Time" also works, and it should be used if the task has a time reference (to specific hours or days of the week, etc.)
checking the number of bars makes it possible to see the update of data in history, and the indicators already have built-in variables for this, that is, there is no need to add anything additionally
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.
if the number of bars has changed due to reconnection, there will be more of them - this means that the indicator (or EA) needs to be recalculated completely.
This speaks to the usefulness of using bars rather than time.
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.
why can this only be called once per tick? (open time will not return false?)
why can't the bars variable can't be tested multiple times?
@William Roeder is referring to the fact that a "function", using a static variable to keep track of the change will only report correctly on the first call. During the remainder of the that bar's duration,the function will return an incorrect result. So one should use a variable for the change detection and not rely on repeated calls to the function.
I have carried out the test with both MQL4 and MQL5, and these are my findings:
This is the code I used for the test ...
The bar count does in fact increment beyond the "max limit" set for the chart. How far it will overflow, I am unable to say. It may reset at some unknown point.
there can't be overflow here because otherwise all indicators will work incorrectly
MT5 resets the bar count if there is a limit (so it can decrease).
changing the value of the number of bars matters, MT will return the difference of bars greater than 1, this means that a recalculation is necessary
@William Roeder is referring to the fact that a "function", using a static variable to keep track of the change will only report correctly on the first call. During the remainder of the that bar's duration,the function will return an incorrect result. So one should use a variable for the change detection and not rely on repeated calls to the function.
it is not clear what this is for?
the task is to find out when a new bar will appear, isn't it? (what incorrect result are you talking about?)
This is the code I used for the test ...