//+------------------------------------------------------------------+
//|                                                          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 4

#property indicator_color1 clrMagenta
#property indicator_color2 clrBlue //DeepSkyBlue
#property indicator_color3 clrDarkOrange //Gold
#property indicator_color4 clrRed

#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 1

//---- input parameters
//+------------------------------------------------------------------+
extern int       periodAMA=9;
extern int       nfast=2;
extern int       nslow=30;
double    G=2.0;
extern int filterPeriod=20;
extern double filterFactor=0.20;
extern int maxBars=500;
//extern double    dK=2.0;
extern bool Draw_TriggerLine=false;
//+------------------------------------------------------------------+

//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];
double diffAMA[];
double triggerAMA[];

//+------------------------------------------------------------------+

int    cbars=0,prevbars=0,prevtime=0;

double slowSC,fastSC;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   //int iArrow=159; //circle
   int iArrow=115; //romb
   
   SetIndexBuffer(0,kAMAbuffer);  SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexBuffer(1,kAMAupsig);   SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,iArrow);  SetIndexLabel(1,NULL);
   SetIndexBuffer(2,kAMAdownsig); SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,iArrow);  SetIndexLabel(2,NULL);
//SetIndexDrawBegin(0,nslow+nfast);
   SetIndexBuffer(3,triggerAMA); SetIndexStyle(3,DRAW_LINE,0,2); SetIndexLabel(3,"AMA trigger");

   IndicatorBuffers(5);
   SetIndexBuffer(4,diffAMA);

   SetIndexDrawBegin(3,20);

   IndicatorDigits(Digits);

//slowSC=0.064516;
//fastSC=0.2;
//cbars=IndicatorCounted();
//----
//Comment("");
   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;

   if(pos>maxBars) pos=maxBars;

   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;

      //-- signal line --------------------------         
      if(Draw_TriggerLine)
        {
         if(pos<=maxBars-2)
            triggerAMA[pos]=2*AMA-kAMAbuffer[pos+2];
         else
            triggerAMA[pos]=AMA;
        }

      //-- Slope --------------------------------
      if(AMA0!=0)
         ddK=10000*(AMA-AMA0)/AMA0; else ddK=0;

      diffAMA[pos]=ddK;

      //-- Filter -------------------------------
      double filterValue=filterFactor*iStdDevOnArray(diffAMA,0,filterPeriod,0,MODE_SMA,pos);

      if(MathAbs(ddK)>filterValue && ddK>0)
         kAMAupsig[pos]=AMA; else kAMAupsig[pos]=0;
      if(MathAbs(ddK)>filterValue && ddK<0)
         kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0;

      //ddK=(AMA-AMA0);
      //if((MathAbs(ddK))>(dK*Point) && (ddK>0))
      //   kAMAupsig[pos]=AMA; else kAMAupsig[pos]=0;
      //if((MathAbs(ddK))>(dK*Point) && (ddK<0))
      //   kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0;

      AMA0=AMA;
      pos--;
     };
//----
   prevbars=Bars;
   return(0);
  }
//+------------------------------------------------------------------+
