Download MetaTrader 5

Libraries: IsNewBar

To add comments, please log in or register
Publish your articles and improve your rating!
Automated-Trading
Admin
99472
Automated-Trading 2012.02.13 07:53 

IsNewBar:

СIsNewBar class allows to determine the moment of a bar change. Usually IsNewBar() function is used for such things instead of a class. But such a function contains a static variable, and therefore we cannot use several calls of this function. To be able to reuse such function repeatedly in Expert Advisor's code, it would be much easier to make it a class member. In this case that has been accomplished using IsNewBar.mqh.

Author: Nikolay Kositsin

Alain Verleyen
Moderator
30739
Alain Verleyen 2013.03.06 20:20  
Automated-Trading:

IsNewBar:

Author: Nikolay Kositsin

There is a little bug in your class (as in most of the functions I saw the code). First time you call you method IsNewBar, it always return true, irrespective of the existence of a real new bar.

But such a function contains a static variable, and therefore we cannot use several calls of this function.

One can also use a two dimensional array to keep last bar open time.

Rafael Caetano Pinto
129
Rafael Caetano Pinto 2017.01.27 13:28  
I made a alternative version of CIsNewBar, without any static variable. I just used the Tick.time_msc to do a idempotent function:


class CIsNewBar{
private:
   long checkedMs;
   datetime lastBarOpenedAt;
   bool lastValue;
   CTickUtils tickUtils;
public:
   CIsNewBar(){}
   ~CIsNewBar(){}

   bool isNewBar(){
      MqlTick tick;
      SymbolInfoTick(_Symbol, tick);
      long tickMs = tick.time_msc;

      
      if(checkedMs >= tickMs){ //already processed this function on this tick?
         return lastValue;     //so, return the buffered value
      }

      datetime time[1];
      CopyTime(_Symbol, _Period, 0, 1, time);


      if(lastBarOpenedAt != time[0]){
         lastBarOpenedAt = time[0];
         lastValue = true;
      } else {
         lastValue = false;
      }

      checkedMs = tickMs;
      return lastValue;
   }
To add comments, please log in or register