ama indicator mql4 porting to mql5 help

 

MQL4 code:

//+------------------------------------------------------------------+
//|                                                          AMA.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, by konKop,wellx"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Sienna
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Gold

//---- input parameters
extern int       periodAMA=9;
extern int       nfast=2;
extern int       nslow=30;
extern double    G=2.0;
extern double    dK=2.0; 

//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];

double myPoint;


//+------------------------------------------------------------------+

int    cbars=0, prevbars=0, prevtime=0;

double slowSC,fastSC;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,159);
   //SetIndexDrawBegin(0,nslow+nfast);
   SetIndexBuffer(0,kAMAbuffer);
   SetIndexBuffer(1,kAMAupsig);
   SetIndexBuffer(2,kAMAdownsig);
   
   
   IndicatorDigits(4);
   myPoint = SetPoint();
   
   //slowSC=0.064516;
   //fastSC=0.2;
   //cbars=IndicatorCounted();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i,pos=0;
   double noise=0.000000001,AMA,AMA0,signal,ER;
   double dSC,ERSC,SSC,ddK;
   
   if (prevbars==Bars) return(0);
    
//---- TODO: add your code here
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   cbars=IndicatorCounted();
   if (Bars<=(periodAMA+2)) return(0);
//---- check for possible errors
   if (cbars<0) return(-1);
//---- last counted bar will be recounted
   if (cbars>0) cbars--;
   pos=Bars-periodAMA-2;
   AMA0=Close[pos+1];
   while (pos>=0)
     {
      if(pos==Bars-periodAMA-2) AMA0=Close[pos+1];
      signal=MathAbs(Close[pos]-Close[pos+periodAMA]);
      noise=0.000000001;
      for(i=0;i<periodAMA;i++)
       {
        noise=noise+MathAbs(Close[pos+i]-Close[pos+i+1]);
       };
      ER =signal/noise;
      dSC=(fastSC-slowSC);
      ERSC=ER*dSC;
      SSC=ERSC+slowSC;
      AMA=AMA0+(MathPow(SSC,G)*(Close[pos]-AMA0));
      kAMAbuffer[pos]=AMA;

      ddK=(AMA-AMA0);
      if ((MathAbs(ddK)) > (dK*myPoint) && (ddK > 0)) kAMAupsig[pos] =AMA; else kAMAupsig[pos]=0;
      if ((MathAbs(ddK)) > (dK*myPoint) && (ddK < 0)) kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0; 

     
      AMA0=AMA;
      pos--;
     };
//----
   prevbars=Bars;
   return(0);
  }


double SetPoint()
{
   double mPoint;
   
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

my try on MQL5 porting of the above code:

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot kAMAbuffer
#property indicator_label1  "kAMAbuffer"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot kAMAupsig
#property indicator_label2  "kAMAupsig"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrLime
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot kAMAdownsig
#property indicator_label3  "kAMAdownsig"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- input parameters
input int      periodAMA=9;
input int      nfast=2;
input int      nslow=30;
input double   G=2.0;
input double   dK=2.0;

double slowSC,fastSC;
double myPoint;
//--- indicator buffers
double         kAMAbuffer[];
double         kAMAupsig[];
double         kAMAdownsig[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,kAMAbuffer,INDICATOR_DATA);
   SetIndexBuffer(1,kAMAupsig,INDICATOR_DATA);
   SetIndexBuffer(2,kAMAdownsig,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   PlotIndexSetInteger(2,PLOT_ARROW,159);
   
   myPoint = SetPoint();
//---
   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[])
{
//---
   int begin=!prev_calculated?rates_total-periodAMA-nfast-1:rates_total-prev_calculated;
   int i = begin;
   
   double noise=0.000000001,AMA,AMA0,signal,ER;
   double dSC,ERSC,SSC,ddK;
   
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   
   i = rates_total-periodAMA-nfast;
   AMA0 = close[i+1];
//--- draw begin may be corrected
   if(begin!=0)
      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,periodAMA+nfast+begin);
//--- main cycle
   for(i=begin; i>=0 && !IsStopped(); i--)
   {
     if(i==rates_total-periodAMA-nfast) AMA0=close[i+1];
     signal=MathAbs(close[i]-close[i+periodAMA]);
     noise=0.000000001;
     for(int j=0;j<periodAMA;j++)
     {
       noise=noise+MathAbs(close[i+j]-close[i+j+1]);
     };
     ER =signal/noise;
     dSC=(fastSC-slowSC);
     ERSC=ER*dSC;
     SSC=ERSC+slowSC;
     AMA=AMA0+(MathPow(SSC,G)*(close[i]-AMA0));
     kAMAbuffer[i]=AMA;

     ddK=(AMA-AMA0);
     if ((MathAbs(ddK)) > (dK*myPoint) && (ddK > 0)) kAMAupsig[i] =AMA; else kAMAupsig[i]=0;
     if ((MathAbs(ddK)) > (dK*myPoint) && (ddK < 0)) kAMAdownsig[i]=AMA; else kAMAdownsig[i]=0; 

     
      AMA0=AMA;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+

double SetPoint()
{
   double mPoint;
   
   if (_Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

but the end result of the indicator on chart appeared very different. I have no doubt i have messed up some key elements in the code. Any help appreciated.

 
Wilson Wong: I have no doubt i have messed up some key elements in the code.
     if(i==rates_total-periodAMA-nfast) AMA0=close[i+1];
     signal=MathAbs(close[i]-close[i+periodAMA]);

You are accessing the array as-series, but you didn't set it that way.


OnCalculate - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Note

To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.

 
William Roeder #:

You are accessing the array as-series, but you didn't set it that way.


OnCalculate - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Thank you very much for this pointer.
 
with the modified code
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot kAMAbuffer
#property indicator_label1  "kAMAbuffer"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot kAMAupsig
#property indicator_label2  "kAMAupsig"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrLime
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot kAMAdownsig
#property indicator_label3  "kAMAdownsig"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- input parameters
input int      periodAMA=9;
input int      nfast=2;
input int      nslow=30;
input double   G=2.0;
input double   dK=2.0;

double slowSC,fastSC;
double myPoint;
//--- indicator buffers
double         kAMAbuffer[];
double         kAMAupsig[];
double         kAMAdownsig[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,kAMAbuffer,INDICATOR_DATA);
   SetIndexBuffer(1,kAMAupsig,INDICATOR_DATA);
   SetIndexBuffer(2,kAMAdownsig,INDICATOR_DATA);

   ArraySetAsSeries(kAMAbuffer,true);
   ArraySetAsSeries(kAMAupsig,true);
   ArraySetAsSeries(kAMAdownsig,true);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,DRAW_TYPE,DRAW_LINE);  PlotIndexSetInteger(0PLOT_SHOW_DATA,true);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   PlotIndexSetInteger(2,PLOT_ARROW,159);
   
   myPoint = SetPoint();
//---
   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[])
{
//---
   int begin=!prev_calculated?rates_total-periodAMA-nfast-1:rates_total-prev_calculated;
   int i = begin;
   
   double noise=0.000000001,AMA,AMA0,signal,ER;
   double dSC,ERSC,SSC,ddK;
   
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   
   ArraySetAsSeries(close,true);

   i = rates_total-periodAMA-nfast;
   AMA0 = close[i+1];


//--- main cycle
   for(i=begin; i>=0 && !IsStopped(); i--)
   {
     if(i==rates_total-periodAMA-nfast) AMA0=close[i+1];
     signal=MathAbs(close[i]-close[i+periodAMA]);
     noise=0.000000001;
     for(int j=0;j<periodAMA;j++)
     {
       noise=noise+MathAbs(close[i+j]-close[i+j+1]);
     };
     ER =signal/noise;
     dSC=(fastSC-slowSC);
     ERSC=ER*dSC;
     SSC=ERSC+slowSC;
     AMA=AMA0+(MathPow(SSC,G)*(close[i]-AMA0));
     kAMAbuffer[i]=AMA;

     ddK=(AMA-AMA0);
     if ((MathAbs(ddK)) > (dK*myPoint) && (ddK > 0)) kAMAupsig[i] =AMA; else kAMAupsig[i]=0;
     if ((MathAbs(ddK)) > (dK*myPoint) && (ddK < 0)) kAMAdownsig[i]=AMA; else kAMAdownsig[i]=0; 

     
      AMA0=AMA;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+

double SetPoint()
{
   double mPoint;
   
   if (_Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

I am getting this unexpected downward plunging line of the indicator when there's a new bar appearing and the same goes when running it on tester.

https://charts.mql5.com/31/896/eurusd-d1-metaquotes-software-corp-2.png

Any advice?

 
did you find the answer of your question? do you have the solution now?