New bar arrived

 

Hi

mql5 EA

Will this code do well in checking if a new bar arrives?


Thanks

datetime currBarTime;

int OnInit() {
   currBarTime = SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE);

}

void OnTick()
  {
    if (currBarTime != SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE)) {
      // NEW BAR ARRIVED
    // DO STUFF
    }
 
samjesse:

Hi

mql5 EA

Will this code do well in checking if a new bar arrives?


Thanks

No, it won't.

 
Andrey Barinov:

No, it won't.


Why? is it because SERIES_LASTBAR_DATE gives error sometimes? is so, what about this?

By Malden https://www.mql5.com/en/forum/5762/page2


static datetime prevTime=0;
datetime lastTime[1];

OnTick(){

   if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
   {
      prevTime=lastTime[0];
    
      // ...new bar arrived, do stuff
   }
}       
How to detect a new bar
How to detect a new bar
  • 2017.01.26
  • www.mql5.com
Hi All, I'm a complete newbie to MQL5 but I've done a fair bit of coding for other platforms...
Reason: