Develop your own indicator for use in an EA

 

Hello!

I have developed an "Efficiency Ratio indicator" that I want to put in the format of a class to be able to use it in an EA.

To do this I use the tools that Mr Andrew YOUNG describes in his book and makes available for free download.

Here I will share with you my source code for my indicator and also the class I am having trouble with.

Here is the indicator code (there is no problem and everything works correctly):

 #property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 1
#property indicator_plots    1

#property indicator_level1 0.5

#property indicator_type1    DRAW_LINE
#property indicator_color1   DodgerBlue
#property indicator_width1    3

sinput string ER;                                   	//Efficient Ratio Settings
input int InpERperiod = 10 ;                         	//Efficient Ratio Period
input ENUM_APPLIED_PRICE price = PRICE_CLOSE ;       	//Applied price

int ExtERperiod;

double ExtERBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit ()
  {
   if (InpERperiod< 1 ) ExtERperiod= 10 ;
   else ExtERperiod=InpERperiod;
   
//--- indicator buffers mapping
   SetIndexBuffer ( 0 ,ExtERBuffer, INDICATOR_DATA );
   IndicatorSetString ( INDICATOR_SHORTNAME , "ER(" + string (ExtERperiod)+ ")" );
   PlotIndexSetInteger ( 0 , PLOT_DRAW_BEGIN ,ExtERperiod- 1 );
//--- sets drawing line empty value
   PlotIndexSetDouble ( 0 , PLOT_EMPTY_VALUE , 0.0 );
//--- digits
   IndicatorSetInteger ( INDICATOR_DIGITS , 3 );
   
//---
   return ( 0 );
  }
//+------------------------------------------------------------------+
//| Efficiency Ratio Indicator                                       |
//+------------------------------------------------------------------+
int OnCalculate ( const int rates_total,
                 const int prev_calculated,
                 const datetime &time[],
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 const long &tick_volume[],
                 const long &volume[],
                 const int &spread[])
  {
//---
   if ( IsStopped ()) return ( 0 );
   if (rates_total<=ExtERperiod) return ( 0 );
   
   ArraySetAsSeries (ExtERBuffer, true );
   ArraySetAsSeries (close, true );
   
   int bars = 0 ;
   double nominator = 0.0 ;
      
   if (prev_calculated > 0 ){
      bars = rates_total - prev_calculated - 1 ;
   }
   else {
      bars = rates_total - 1 ;
   }
   
   //Main boucle
   for ( int i = bars; i > 0 ; i--){
       if (i+ExtERperiod- 1 <= bars){
         nominator = MathAbs (close[i] - close[i + (ExtERperiod- 1 )]);
         double denominator = 0.0 ;
         
         for ( int index = i; index < i + (ExtERperiod- 1 ); index++){
            denominator = denominator + MathAbs (close[index] - close[index+ 1 ]);
         }
         ExtERBuffer[i] = (nominator/denominator);
      }   
   }
   
//--- return value of prev_calculated for next call
   return (rates_total);
  }
//+------------------------------------------------------------------+

So far all is well and it is working very well. I have no worries. So I want to write all of this as a class to us it in my EA.

Here is my code for the CiEfficientRatio class:

CiEfficiencyRatio ER;
ER.Init( _Symbol , _Period , ERPeriod, ERPrice);
ER.Main();

class CiEfficiencyRatio : public CIndicator
{
         public :
                 int Init( string pSymbol, ENUM_TIMEFRAMES pTimeframe, int pERPeriod, ENUM_APPLIED_PRICE pERPrice);
}; 


int CiEfficiencyRatio::Init( string pSymbol, ENUM_TIMEFRAMES pTimeframe, int pERPeriod, ENUM_APPLIED_PRICE pERPrice)
{
        handle =iEfficencyRatio(pSymbol,pTimeframe, pERPeriod, pERPrice);
         return (handle);
}

//My problem : I don't know how define iEfficiencyRatio(...)
//Solution 1
int iEfficiencyRatio( string pSymbol, ENUM_TIMEFRAMES pTimeframe, int pERPeriod, ENUM_APPLIED_PRICE pERPrice)
{
   //my code
}

//Solution 2
int CiEfficiencyRatio::iEfficiencyRatio( string pSymbol, ENUM_TIMEFRAMES pTimeframe, int pERPeriod, ENUM_APPLIED_PRICE pERPrice)
{
   //my code
}

/*
   I don't know how to implement this function. What I need to do ? And which value this function need to return (0,1 or other)   
*/

My class implements CIndicator class from Mr Andrew YOUNG's book:

 class CIndicator
{
         protected :
                 int handle;
                 double main[];
                
         public :
                CIndicator( void );
                 double Main( int pShift= 0 );
                 void Release();
                 virtual int Init() { return (handle); }
};

CIndicator::CIndicator( void )
{
         ArraySetAsSeries (main, true );
}

double CIndicator::Main( int pShift= 0 )
{
         CopyBuffer (handle, 0 , 0 ,MAX_COUNT,main);
         double value = NormalizeDouble (main[pShift], _Digits );
         return (value);
}

void CIndicator::Release( void )
{
         IndicatorRelease (handle);
}

Thank you in advance for the help you give me!