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

 
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).

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)

 
Fernando Carreiro #:

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

 
William Roeder #:
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.

William Roeder #:
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?

 
@Taras Slobodyanik #: 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.

 
@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).

I have carried out the test with both MQL4 and MQL5, and these are my findings:

  1. 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.
  2. After a network connection failure:
    1. MT5 resets the bar count if there is a limit (so it can decrease).
    2. while MT4 instead simply jumps the bar count (increases more than one) but does not reset to the limit.

This is the code I used for the test ...

void OnTick() {
   // Check for new bar based on open time
      static datetime dtBarCurrent   = WRONG_VALUE;
             datetime dtBarPrevious  = dtBarCurrent;
                      dtBarCurrent   = iTime( _Symbol, _Period, 0 );
             bool     bNewBarEvent   = ( dtBarCurrent != dtBarPrevious );

   // Check for new bar based on bar count
      static int      nCountCurrent  = WRONG_VALUE;
             int      nCountPrevious = nCountCurrent;
                      nCountCurrent  = iBars( _Symbol, _Period );
             bool     bNewCountEvent = ( nCountCurrent != nCountPrevious );

   // React to a new bar event and handle it.
      if( bNewBarEvent || bNewCountEvent ) {
         PrintFormat( "Event (time/bar): %s/%s, Open time: %s/%s, Bars (prev/curr): %d / %d",
            bNewBarEvent ? "True" : "False", bNewCountEvent ? "True" : "False",
            TimeToString( dtBarPrevious ), TimeToString( dtBarCurrent ),
            nCountPrevious, nCountCurrent );
      };
};
 
Fernando Carreiro #:
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

Fernando Carreiro #:
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

 
Fernando Carreiro #:
@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?)

 
Fernando Carreiro #:
This is the code I used for the test ...
if(rates_total-prev_calculated==0)
      {
      //no new bar
      }
else if(rates_total-prev_calculated==1)
      {
      //new bar
      }
else
      {
      //recalculate all
      }