How to detect NEW Bar?

 
How to detect NEW Bar

I want to reset my indicator variables if there's a new bar. 
I'm thinking to use the volumes data to detect it but I don't know if that's right formula or best code to do it??
 

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.

datetime D1;
   if(D1!=iTime(Symbol(),PERIOD_D1,0)) // new candle on D1
     {
      //Do Something...
      D1=iTime(Symbol(),PERIOD_D1,0);    // overwrite old with new value
     }
 
Musngi:
How to detect NEW Bar? 

I want to reset my indicator variables if there's a new bar. 
I'm thinking to use the volumes data to detect it but I don't know if that's right formula or best code to do it??
  1. 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.

    New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. Indicators do not need to be "reset." If your indicator is broken, fix it. Likely you are using a variable value from the previous iteration, which means you can't process bar zero more than once, ever. Put it in a buffer so you can restore it, or only process bar zero once, or save and restore them when processing bar zero.

  3. See How to do your lookbacks correctly.


Marco vd Heijden: just declare
D1=iTime(Symbol(),PERIOD_D1,0);
Unless the chart is that specific pair/TF, you must handle 4066/4073 errors. Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

 

Volume works fine. " Volume[0]==1 " means the first tick of a new bar.

 
TomQL:

Volume works fine. " Volume[0]==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

Use time.

 

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.

 
honest_knave:

Nope, volume works really badly. Your terminal will miss ticks so sometimes the new bar will start on >1

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

 
Musngi:
How to detect NEW Bar? 

I want to reset my indicator variables if there's a new bar. 
I'm thinking to use the volumes data to detect it but I don't know if that's right formula or best code to do it??

I guess you can also monitor previous open candle open prices. If the open prices are no longer the same , it means that a new candle has opened and the candle index numbers have moved up. So there is a very slight chance that the open price might be the same, so i would pick any two random candles and  monitor for change. The problem with this method is, you are dependent on the next tick , so its not good for a time sensitive function, unless you run on a timer.

// Create in global scope to map the initial open prices of candle 0 and candle 5 (Any random candles)

double barOpen1 = Open[0]; 
double barOpen2 = Open[5];


void OnTick()
  {
        if(barOpen1 != Open[0] || barOpen2 != Open[5]) //If any of the statements pass, a new candle must have opened changing the index number.
        {
         
            barOpen1 = Open[0]; // Update variables with current prices
            barOpen2 = Open[5];
           
        }
}
                

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
 
rjwswanepoel:

I guess you can also monitor previous open candle open prices.

But the open price can be the same as the previous bar while the open time can not be the same so time is the preferred method. 

 

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.
          New candle - MQL4 programming forum #3 2014.04.04

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

 
I prefer using static datetime  prev_time and Time[0]
If prevTime==Time[0] return;  

Normally, I have a problem with counting bars on chart, cause what if your chart was out dated and upon initial running of function there are 100 bars then bars change to 200 then to 85 K all happening in the same current bar period. That will mean the function that must run o ce per bar will run several time per bar