Download MetaTrader 5

How to detect NEW Bar?

To add comments, please log in or register
Musngi
387
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??
Marco vd Heijden
Moderator
6138
Marco vd Heijden  

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
     }
whroeder1
17122
whroeder1  
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

TomQL
85
TomQL  

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

honest_knave
Moderator
2342
honest_knave  
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.

Marco vd Heijden
Moderator
6138
Marco vd Heijden  

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.

TomQL
85
TomQL  
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.

To add comments, please log in or register