Passing parameters in the constructor

 

Hi

Using the code in this article https://www.mql5.com/en/articles/159 to detect the new bar arrival. In the file lib_cisnewbar.mqh

Is there a way to "using the code in the link" to detect the arrival of one day bar when the chart in a smaller time frame? i.e. arrival of a new bar in a different time frame?

I figured if I change the constructor as below and pass in the _Period I like, but I get the compiler error CisNewBar Wrong parameters count. Any suggestions? Thanks


Original:

class CisNewBar 
  {
   protected:
      string            m_symbol;         // Symbol name
      ENUM_TIMEFRAMES   m_period;         // Chart period
     
   public:
      void              CisNewBar();      // CisNewBar constructor      
        // more stuff
  };
void CisNewBar::CisNewBar()
  {
   m_retcode=0;         // Result code of detecting new bar 
   m_lastbar_time=0;    // Time of opening last bar
   m_new_bars=0;        // Number of new bars
   m_comment="";        // Comment of execution
   m_symbol=Symbol();   // Symbol name, by default - symbol of current chart
   m_period=Period();   // Chart period, by default - period of current chart    
  }


My modified versioin with the compiler complaining

class CisNewBar 
  {
   protected:
      string            m_symbol;         // Symbol name
      ENUM_TIMEFRAMES   m_period;         // Chart period
     
   public:
      void              CisNewBar(string symbol, int period);    //<<<<<<<<<<<<<<<    
        // more stuff
  };
void CisNewBar::CisNewBar(string symbol, int period) //<<<<<<<<<<<<<<<<<
  {
   m_retcode=0;         // Result code of detecting new bar 
   m_lastbar_time=0;    // Time of opening last bar
   m_new_bars=0;        // Number of new bars
   m_comment="";        // Comment of execution
   m_symbol=symbol);   //<<<<<<<<<<<<<<<<<
   m_period=period;   // <<<<<<<<<<<<<<<<<   
  }

And in the file.mq5 the call is outside the OnInt() at the start of the file like this


CisNewBar current_chart(_Symbol, _Period); // instance of the CisNewBar class: current chart
The "New Bar" Event Handler
The "New Bar" Event Handler
  • www.mql5.com
Authors of indicators and experts have always been interested in writing the compact code in terms of execution time. You can approach to this problem from different angles. From this broad topic in this article we will cover the problem, that is seemingly already have been solved: check for a new bar. This is quite a popular way to limit the...
 
You're calling the constructor wrong when instantiating the object. Show us how you are trying to do it. 
 
nicholi shen:
You're calling the constructor wrong when instantiating the object. Show us how you are trying to do it. 

I updated the question to include the call.

 

The problem is the way you're trying to define your constructor. Constructors don't have return types. 


class CisNewBar 
  {
   protected:
      string            m_symbol;         // Symbol name
      ENUM_TIMEFRAMES   m_period;         // Chart period
     
   public:
      //void              CisNewBar(string symbol, int period);    //<<<<<<<<<<<<<<<    
      CisNewBar(string symbol, int period);
  };
CisNewBar::CisNewBar(string symbol, int period) //<<<<<<<<<<<<<<<<<
  {
   m_retcode=0;         // Result code of detecting new bar 
   m_lastbar_time=0;    // Time of opening last bar
   m_new_bars=0;        // Number of new bars
   m_comment="";        // Comment of execution
   m_symbol=symbol);   //<<<<<<<<<<<<<<<<<
   m_period=period;   // <<<<<<<<<<<<<<<<<   
  }
 
nicholi shen:

The problem is the way you're trying to define your constructor. Constructors don't have return types. 


Still gives the same error.

 
samjesse:

Still gives the same error.

Except your typo (bracket behind symbol) I didn't get any compiler errors.
class CisNewBar 
  {
   protected:
      datetime          m_lastbar_time;   // Time of opening last bar
      uint              m_retcode;        // Result code of detecting new bar
      int               m_new_bars;       // Number of new bars
      string            m_comment;        // Comment of execution
      string            m_symbol;         // Symbol name
      ENUM_TIMEFRAMES   m_period;         // Chart period
     
   public:
      void              CisNewBar(string symbol, int period);    //<<<<<<<<<<<<<<<    
        // more stuff
  };
void CisNewBar::CisNewBar(string symbol, int period) //<<<<<<<<<<<<<<<<<
  {
   m_retcode=0;         // Result code of detecting new bar 
   m_lastbar_time=0;    // Time of opening last bar
   m_new_bars=0;        // Number of new bars
   m_comment="";        // Comment of execution
   m_symbol=symbol;   //<<<<<<<<<<<<<<<<<
   m_period=(ENUM_TIMEFRAMES)period;   // <<<<<<<<<<<<<<<<<   
  }

void OnStart()
  {
   CisNewBar current_chart(_Symbol, _Period); 
  }
 
samjesse:

Still gives the same error.

I'm not sure what you're doing then. Don't put a type in front of the constructor. It should look exactly like this. 

class CisNewBar 
{
   protected:
      string            m_symbol;         // Symbol name
      int               m_period;         // Chart period
   public:   
      CisNewBar(string symbol, int period);
};
CisNewBar::CisNewBar(string symbol, int period) //<<<<<<<<<<<<<<<<<
{
   m_symbol=symbol;   
   m_period=period;   
}
 
samjesse:

Hi

Using the code in this article https://www.mql5.com/en/articles/159 to detect the new bar arrival. In the file  lib_cisnewbar.mqh

Is there a way to "using the code in the link" to detect the arrival of one day bar when the chart in a smaller time frame? i.e. arrival of a new bar in a different time frame?

I figured if I change the constructor as below and pass in the _Period I like, but I get the compiler error CisNewBar Wrong parameters count. Any suggestions? Thanks


Original:


My modified versioin with the compiler complaining

And in the file.mq5 the call is outside the OnInt() at the start of the file like this


There is a ")" on "m_symbol=symbol);", i think it should be "m_symbol=symbol;".

Reason: