//+------------------------------------------------------------------+
//|                                  Kaufman Adaptive Moving Average |
//|                                            Kaufman AMA basic.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  LimeGreen
#property indicator_color2  Orange
#property indicator_color3  Orange
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2

//
//
//
//
//

extern int    AMAPeriod           = 10;
extern int    AMAPrice            = PRICE_CLOSE;
extern int    Nfast               = 2;
extern int    Nslow               = 30;
extern double GCoeff              = 2;
extern double PriceFilter         = 15;
extern double PriceFilterSpeed    = 3;
extern bool   PriceFilterAdaptive = true;

//
//
//
//
//

double kAMAbuffer[];
double kAMAbufferda[];
double kAMAbufferdb[];
double wAMAbuffer[];
double diff[];
double slope[];

//
//
//
//
//

double fastend;
double slowend;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,kAMAbuffer);
   SetIndexBuffer(1,kAMAbufferda);
   SetIndexBuffer(2,kAMAbufferdb);
   SetIndexBuffer(3,wAMAbuffer);
   SetIndexBuffer(4,diff);
   SetIndexBuffer(5,slope);
   
   //
   //
   //
   //
   //
   
      fastend   = (2.0 /(Nfast + 1));
      slowend   = (2.0 /(Nslow + 1));
   
   //
   //
   //
   //
   //
   
   IndicatorShortName("Kaufman AMA ("+AMAPeriod+")");
   return(0);
}
int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   if (slope[limit]==-1) CleanPoint(limit,kAMAbufferda,kAMAbufferdb);
   for(int i=limit; i>=0; i--)
   {
      kAMAbuffer[i]   = iKama(kAMAbuffer,diff,AMAPeriod,AMAPrice,GCoeff,i);
      kAMAbufferda[i] = EMPTY_VALUE;
      kAMAbufferdb[i] = EMPTY_VALUE;
      slope[i]        = slope[i+1];
         if (kAMAbuffer[i]>kAMAbuffer[i+1]) slope[i] =  1;
         if (kAMAbuffer[i]<kAMAbuffer[i+1]) slope[i] = -1;
         if (slope[i]==-1) PlotPoint(i,kAMAbufferda,kAMAbufferdb,kAMAbuffer);
   }      
   return(0);
}
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

double price[];
double iKama(double& amabuffer[],double& diffbuffer[],int period,int priceType,double gCoeff,int i)
{
   double efratio  = 1.00;
   double AMA      = 0.00;
   if (ArraySize(price)!=Bars) ArrayResize(price,Bars); int r = Bars-i-1;
      price[r]    = iOma(iMA(NULL,0,1,0,MODE_SMA,priceType,i),PriceFilter,PriceFilterSpeed,PriceFilterAdaptive,i);


   if (i > Bars-period) return(price[r]);
   
   //
   //
   //
   //
   //
   
   double smooth;
   double signal;
   double noise;
          signal        = MathAbs(price[r]-price[r-period]);
          diffbuffer[i] = MathAbs(price[r]-price[r-1]);
          for (int k=0;k<period;k++)
                  noise += diffbuffer[i+k];

          //
          //
          //
          //
          //

          if (noise != 0) efratio = signal/noise;
                          smooth  = MathPow(efratio*(fastend-slowend)+slowend,gCoeff);
                          AMA     = amabuffer[i+1] + smooth*(price[r]-amabuffer[i+1]);
   //
   //
   //
   //
   //

   return(AMA);          
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}

//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

double workOma[][7];
#define F01 0
#define F02 1
#define F03 2
#define F04 3
#define F05 4
#define F06 5
#define prc 6

//
//
//
//
//

double iOma(double tprice, double averagePeriod, double constant, bool adaptive, int r, int s=0)
{
   if (averagePeriod <=1) return(tprice);
   if (ArrayRange(workOma,0) != Bars) ArrayResize(workOma,Bars); r=Bars-r-1; s *=7;
   if (r<=1) 
   {
      for (int i=0; i<6; i++) workOma[r][i  +s] = 0;
                              workOma[r][prc+s] = tprice;
                              return(tprice);
   }      
   double f01=workOma[r-1][F01+s];  double f02=workOma[r-1][F02+s];
   double f03=workOma[r-1][F03+s];  double f04=workOma[r-1][F04+s];
   double f05=workOma[r-1][F05+s];  double f06=workOma[r-1][F06+s];

   //
   //
   //
   //
   //

      if (adaptive && (averagePeriod > 1))
      {
         double minPeriod = MathMin(averagePeriod,r)/2.0;
         double maxPeriod = MathMin(minPeriod*5.0,r);
         int    endPeriod = (int)MathCeil(maxPeriod);
         double signal    = MathAbs((tprice-workOma[r-endPeriod][prc+s]));
         double noise     = 0.00000000001;

            for(i=1; i<endPeriod; i++) noise=noise+MathAbs(tprice-workOma[r-i][prc+s]);

         averagePeriod = ((signal/noise)*(maxPeriod-minPeriod))+minPeriod;
      }
      
      //
      //
      //
      //
      //
      
      double Kg = (2.0+constant)/(1.0+constant+averagePeriod);
      double Hg = 1.0-Kg;

      f01 = Kg * tprice + Hg * f01; f02 = Kg * f01 + Hg * f02; double v01 = 1.5 * f01 - 0.5 * f02;
      f03 = Kg * v01    + Hg * f03; f04 = Kg * f03 + Hg * f04; double v02 = 1.5 * f03 - 0.5 * f04;
      f05 = Kg * v02    + Hg * f05; f06 = Kg * f05 + Hg * f06; double v03 = 1.5 * f05 - 0.5 * f06;

   //
   //
   //
   //
   //

   workOma[r][F01+s] = f01;  workOma[r][F02+s] = f02;
   workOma[r][F03+s] = f03;  workOma[r][F04+s] = f04;
   workOma[r][F05+s] = f05;  workOma[r][F06+s] = f06;
   workOma[r][prc+s] = tprice;
   return(v03);
}