ExponentialMA (please help)

 
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[])
  {
//---
   for(i = 200; i < rates_total; i++)
     {
      n5Buffer[i] = maS(i, close);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
int maS(int _shift, const double &_close[])
  {
   double cMA1, cMA2 ;
   int maSD = 0;

//double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
  //cMA1 = ExponentialMA(_shift, ema1, _shift - 1, _close);
  //cMA2 = ExponentialMA(_shift, ema2, _shift - 1, _close);


//double SimpleMA(const int position,const int period,const double &price[])
   cMA1 = SimpleMA(_shift, ema1,  _close);
   cMA2 = SimpleMA(_shift, ema2,  _close);

//---
   if(cMA1 > cMA2)
     {maSD = 1;}
   if(cMA1 < cMA2)
     { maSD = -1;}
   return(maSD);
  }

hello to everyone. I am asking your help regarding "ExponentionalMA". because when I am using SimpleMA the Histogram display correct. BUT by using ExponentialMA. with above code the histogram display incorrect. I don't know how to get the Prev_value. please help me. the attched jpeg are refer to SimpleMA

Files:
aaa777.jpg  224 kb
 
ExponentialMA(const int position,const int period,const double prev_value,const double &price[])

please share how to get PREV_VALUE.

 
Doesn't compile. Attach the complete MQL5 file.
 
//+------------------------------------------------------------------+
//|                                                       ema000.mq5 |
//|                                                  Michael Macasil |
//|                                https://www.facebook.com/macasili |
//+------------------------------------------------------------------+
#include <MovingAverages.mqh>
#property copyright "Michael Macasil"
#property link      "https://www.facebook.com/macasili"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum -1
#property indicator_maximum 1
#property indicator_buffers 1
#property indicator_plots   1
//--- plot bsSignal
#property indicator_label1  "bsSignal"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      ma1 = 7;
input int      ma2 = 10;
//--- indicator buffers
double         bsSignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, bsSignalBuffer, INDICATOR_DATA);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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(rates_total < ma2)
     {
      return(0);
     }
   int start, i;
   if(prev_calculated == 0)
     {
      start = ma2;
     }
   else
     {
      start = prev_calculated - 1;
     }
   for(i = start; i < rates_total; i++)
     {
      bsSignalBuffer[i] = maS(i, close);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
int maS(int _shift, const double &_close[])
  {
   double xma1, xma2 ;
   int maSD = 0;
//+------------------------------------------------------------------+
//---//---SimpleMA(const int position,const int period,const double &price[])
// xma1 = SimpleMA(_shift, ma1, _close); //No Problem found
// xma2 = SimpleMA(_shift, ma2, _close); //No Problem found
//+------------------------------------------------------------------+
//---//---ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
   xma1 = ExponentialMA(_shift, ma1, ma1 - 1, _close); //I dont know what is the correct value of "prev_value"
   xma2 = ExponentialMA(_shift, ma2, ma2 - 1, _close); //
//+------------------------------------------------------------------+
   if(xma1 > xma2)
     {maSD = 1;}
   if(xma1 < xma2)
     { maSD = -1;}
   return(maSD);
  }
//+------------------------------------------------------------------+
when I used the ExponentialMA . the result is always 1. unable to get -1. i know that there is wrong with my "ma1-1". I don't know how to solve it. i need any help to fix above.
 

Just have a look at ...\MQL5\Indicators\Examples\MACD.mq5. It is using 2 different exp. MA.

Study how a handle of an indicator is defined in OnInit() and hoe it is accessed in OnCalculate().

 
thank you Mr. Moderator( Carl Schreiber ). Your guide are the perfect solution of what I need. And now I can continue my MT5 development. see attached jpeg (almost perfect). thanks a lot. happy coding to all.
Carl Schreiber
Carl Schreiber
  • 2021.10.01
  • www.mql5.com
Trader's profile
Files:
emaH.jpg  204 kb
Reason: