Download MetaTrader 5

Libraries: IsNewBar

To add comments, please log in or register
Automated-Trading
Admin
110959
Automated-Trading  

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
35118
Alain Verleyen  
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
170
Rafael Caetano Pinto  
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