Check if there are any new bar – library MetaTrader 5

1、There is only one class function without additional variables. Please declare class CCheck before use

class CCheck
  {
public:
   bool              isNewBar(const string symbol,const ENUM_TIMEFRAMES period);
   
  };

2、Program variables are used to store corresponding data, while historical bar variables need to be statically modified for data storage

//--- Static variables used to save history bar time
   static datetime time_OldBar = 0;   
   
//--- used to save the latest bar time
   datetime time_NewBar = 0;    //Latest bar time
   long     var = 0;            //Temporary variables

 

3、This is the function to obtain the latest bar time. Using this function, the system runs the fastest, so it is used. When it returns False, the function will not exit, and if necessary, check the error code.

//--- SeriesInfoInteger() this function runs the fastest, so use it
   if(!SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE,var))
      {
      ResetLastError();
      Print("Error in obtaining the latest bar time. Code:",GetLastError());
      }
      else time_NewBar = datetime(var);

4、When used for the first time, initial values will be assigned to the storage location of historical data for subsequent comparison

Alternative:  AsymmetricStochNR - indicator MetaTrader 5
//--- First function call, assign a value
   if(time_OldBar == 0)		//static datetime time_OldBar
     {
      time_OldBar = time_NewBar;
      return(false); 
     }

5、Comparing the old and new time columns, if there is a difference, it indicates that a new bar has been generated and returns true. If the time is the same, there are no new bar, return false

//--- The time of new and old bar is different, it means there is a new bar
   if(time_OldBar != time_NewBar)
     {
      time_OldBar = time_NewBar;
      return(true); 
     }
     else return(false);
    📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks