Download MetaTrader 5

New Class Creation Isuues !!! 'HELP'

To add comments, please log in or register
surubabs Suresh Kakkattil
815
surubabs Suresh Kakkattil  

hi all,

I am just step in to class, i am trying to make a class with BAnds,MA,MACD,

The problem i face with the code, m_ma_Applied, bcoz, it comes with all this three indicators, and all this indicators have a MA,

i have the same problem with m_ma_period,

 

Please tell me how do we code if i want to create a class with multiple indicators,which has a commen parameater like moving average,

my coding got stoped with this confussion,please help me out, 

//+------------------------------------------------------------------+
//|                                                   Band_Cross.mqh |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include "..\ExpertSignal.mqh"   // CExpertSignal is in the file ExpertSignal
// wizard description start
//+---------------------------------------------------------------------------------+
//| Description of the class                                                        |
//| Title=Signals at the intersection of Bands                                      |
//| Type=SignalAdvanced                                                             |
//| Name=My_Band_Cross                                                              |
//| ShortName=BandCross                                                             |
//| Class=Band_Cross                                                                |
//| Page=Not needed                                                                 |
//| Parameter=BANDPeriod,int,20,Periof of Bollinger Bands                           |
//| Parameter=BANDDeviation,double,2.000,Deviation of Bollinger Bands               |
//| Parameter=BANDshift,int,0,Time shift                                            |
//| Parameter=BandApplied,ENUM_APPLIED_PRICE,PRICE_CLOSE                            |
//| Parameter=MACDPeriodFast,int,12,Period of fast EMA                              |
//| Parameter=MACDPeriodSlow,int,24,Period of slow EMA                              |
//| Parameter=MACDPeriodSignal,int,9,Period of averaging of difference              |
//| Parameter=MACDApplied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series              |
//| Parameter=PeriodMA,int,12,Period of averaging                                   |
//| Parameter=Shift,int,0,Time shift                                                |
//| Parameter=Method,ENUM_MA_METHOD,MODE_SMA,Method of averaging                    |
//| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series                  |
//+---------------------------------------------------------------------------------+
class Band_Cross : public CExpertSignal
  {
private:
      CiBands           m_Bands;
      //--- Configurable module parameters
      int                             m_Bands_period;
      int                             m_Bands_shift;
      double                          m_deviation;
      ENUM_APPLIED_PRICE              m_applied;
      
      CiMACD            m_MACD;           // object-oscillator
      //--- adjusted parameters
      int               m_period_fast;    // the "period of fast EMA" parameter of the oscillator
      int               m_period_slow;    // the "period of slow EMA" parameter of the oscillator
      int               m_period_signal;  // the "period of averaging of difference" parameter of the oscillator
      
      //--- Configurable module parameters
      CiMA              m_ma;             // object-indicator
      //--- adjusted parameters
      int               m_ma_period;      // the "period of averaging" parameter of the indicator
      int               m_ma_shift;       // the "time shift" parameter of the indicator
      ENUM_MA_METHOD    m_ma_method;      // the "method of averaging" parameter of the indicator
      
      
public:
     //--- Constructor of class
                     Band_Cross(void);
     //--- Destructor of class
                    ~Band_Cross(void);
     //--- Methods for setting
     //--- methods of setting adjustable parameters
     void              BandsPeriod(int value)             { m_Bands_period=value;        }
     void              BandsShift(int value)             { m_Bands_shift=value;           }
     void              Deviation(int value)           { m_deviation=value;         }
     void              Applied(ENUM_APPLIED_PRICE value) { m_applied=value;         }
     //--- methods of setting adjustable parameters
     void              PeriodMA(int value)                 { m_ma_period=value;          }
     void              Shift(int value)                    { m_ma_shift=value;           }
     void              Method(ENUM_MA_METHOD value)        { m_ma_method=value;          }
     void              MAApplied(ENUM_APPLIED_PRICE value)   { m_ma_applied=value;         }           
     //--- methods of setting adjustable parameters
     void              PeriodFast(int value)             { m_period_fast=value;           }
     void              PeriodSlow(int value)             { m_period_slow=value;           }
     void              PeriodSignal(int value)           { m_period_signal=value;         }
     //--- Checking correctness of input data
     bool              ValidationSettings(); 
     //--- Creating indicators and timeseries for the module of signals
     bool              InitIndicators(CIndicators *indicators);
     //--- Creating MA indicators
    bool              CreateBands(CIndicators *indicators);
    bool              InitMACD(CIndicators *indicators);
    bool              InitMA(CIndicators *indicators);
  };

  
  
  
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
Band_Cross::Band_Cross(void):  m_Bands_period(-1),
                               m_Bands_shift(-1),
                               m_deviation(EMPTY_VALUE),
                               m_applied(PRICE_CLOSE),
                              //-----------//
                              m_ma_period(8),
                              m_ma_shift(0),
                              m_ma_method(MODE_EMA),
                              m_ma_applied(PRICE_CLOSE), 
                              //-----------//
                              m_period_fast(12),
                              m_period_slow(24),
                              m_period_signal(9)
                              
{
}
//+------------------------------------------------------------------+
//| Checks input parameters and returns true if everything is OK     |
//+------------------------------------------------------------------+
bool Band_Cross :: ValidationSettings()
  {
      //--- validation settings of additional filters
   if(!CExpertSignal::ValidationSettings())return(false);
      
//--- initial data checks MACD
   if(m_period_fast>=m_period_slow)
     {
      printf(__FUNCTION__+": slow period must be greater than fast period");
      return(false);
     }
    
//--- initial data checks MA
   if(m_ma_period<=0)
     {
      printf(__FUNCTION__+": period MA must be greater than 0");
      return(false);
     }
//--- initial data checks Bands
   if(m_Bands_period<=0)
     {
      printf(__FUNCTION__+": Bands period must be greater than 0");
      return(false);
     }   
//--- All checks are completed, everything is ok
   return true;
  }   
//+------------------------------------------------------------------+
//| Creates indicators                                               |
//| Input:  a pointer to a collection of indicators                  |
//| Output: true if successful, otherwise false                      |
//+------------------------------------------------------------------+
bool Band_Cross ::InitIndicators(CIndicators* indicators)
  {
//--- Standard check of the collection of indicators for NULL
   if(indicators==NULL)                           return(false);
//--- Initializing indicators and timeseries in additional filters
   if(!CExpertSignal::InitIndicators(indicators)) return(false);
//--- Creating our MA indicators  
   if(!CreateBands(indicators))                  return(false);
   if(!InitMACD(indicators))                  return(false);   
   if(!InitMA(indicators))                  return(false);
   //--- Reached this part, so the function was successful, return true
   return(true);
  }
//+------------------------------------------------------------------+
//| Creates the "BANDS" indicator                                  |
//+------------------------------------------------------------------+
bool Band_Cross::CreateBands(CIndicators *indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
//--- add object to collection
   if(!indicators.Add(GetPointer(m_Bands)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_Bands.Create(m_symbol.Name(),m_period,m_Bands_period,m_Bands_shift,m_deviation,m_applied))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Creates the "MA" indicator                                       |
//+------------------------------------------------------------------+
bool Band_Cross::InitMA(CIndicators *indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
//--- add object to collection
   if(!indicators.Add(GetPointer(m_ma)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_ma.Create(m_symbol.Name(),m_period,m_ma_period,m_ma_shift,m_ma_method,m_ma_applied))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Creates the "MACD" indicator                                     |
//+------------------------------------------------------------------+
bool Band_Cross::InitMACD(CIndicators *indicators)
  {
//--- add object to collection
   if(!indicators.Add(GetPointer(m_MACD)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_MACD.Create(m_symbol.Name(),m_period,m_period_fast,m_period_slow,m_period_signal,m_applied))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
//--- ok
   return(true);
  }                                                         

//|                                                                  |
//+------------------------------------------------------------------+
Band_Cross::~Band_Cross()
  {
  }
//+------------------------------------------------------------------+
Create Your Own Trading Robot in 6 Steps!
Create Your Own Trading Robot in 6 Steps!
  • 2012.03.22
  • MetaQuotes Software Corp.
  • www.mql5.com
If you don't know how trade classes are constructed, and are scared of the words "Object Oriented Programming", then this article is for you. In fact, you do not need to know the details to write your own module of trading signals. Just follow some simple rules. All the rest will be done by the MQL5 Wizard, and you will get a ready-to-use trading robot!
Alain Verleyen
Moderator
31478
Alain Verleyen  
surubabs:

hi all,

I am just step in to class, i am trying to make a class with BAnds,MA,MACD,

The problem i face with the code, m_ma_Applied, bcoz, it comes with all this three indicators, and all this indicators have a MA,

i have the same problem with m_ma_period,

 

Please tell me how do we code if i want to create a class with multiple indicators,which has a commen parameater like moving average,

my coding got stoped with this confussion,please help me out, 

You have to give different names to your properties. What's the problem ?
surubabs Suresh Kakkattil
815
surubabs Suresh Kakkattil  
angevoyageur:
You have to give different names to your properties. What's the problem ?

ok,If i did so does it call the indicator correctly ? for example in // wizard description start i givena diffrent name to Bands

// wizard description start
//+---------------------------------------------------------------------------------+
//| Description of the class                                                        |
//| Title=Signals at the intersection of Bands                                      |
//| Type=SignalAdvanced                                                             |
//| Name=My_Band_Cross                                                              |
//| ShortName=BandCross                                                             |
//| Class=Band_Cross                                                                |
//| Page=Not needed                                                                 |
//| Parameter=BANDPeriod,int,20,Periof of Bollinger Bands                           |
//| Parameter=BANDDeviation,double,2.000,Deviation of Bollinger Bands               |
//| Parameter=BANDshift,int,0,Time shift                                            |
//| Parameter=BandApplied,ENUM_APPLIED_PRICE,PRICE_CLOSE                            |
//| Parameter=MACDPeriodFast,int,12,Period of fast EMA                              |
//| Parameter=MACDPeriodSlow,int,24,Period of slow EMA                              |
//| Parameter=MACDPeriodSignal,int,9,Period of averaging of difference              |
//| Parameter=MACDApplied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series              |
//| Parameter=PeriodMA,int,12,Period of averaging                                   |
//| Parameter=Shift,int,0,Time shift                                                |
//| Parameter=Method,ENUM_MA_METHOD,MODE_SMA,Method of averaging                    |
//| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series                  |
//+---------------------------------------------------------------------------------+

 So when i call the indicator what should i use like here

//+------------------------------------------------------------------+
//| Creates the "BANDS" indicator                                  |
//+------------------------------------------------------------------+
bool Band_Cross::CreateBands(CIndicators *indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
//--- add object to collection
   if(!indicators.Add(GetPointer(m_Bands)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_Bands.Create(m_symbol.Name(),m_period,m_Bands_period,m_Bands_shift,m_deviation,m_applied))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
   return(true);
  }

 please make it clear to me sir.

To add comments, please log in or register