//+------------------------------------------------------------------+
//|                              Jim Sloman's natural moving average |
//|                                                      ocn nma.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  SteelBlue
#property indicator_width1  2

//
//
//
//
//

extern int  NMA_Period    = 40;
extern int  NMA_Price     = PRICE_CLOSE;
extern int  TEMA_Period   = 1;
extern int  Smooth_Period = 8;

//
//
//
//
//

double nma[];
double nmas[];
double tBuffer[][5];
double alpha;
double coef1;
double coef2;
double coef3;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define Pi 3.141592653589793238462643

int init()
{
   IndicatorBuffers(2);
   SetIndexBuffer(0,nmas);
   SetIndexBuffer(1,nma);
   
      TEMA_Period = MathMax(TEMA_Period,1);
      NMA_Period  = MathMax(NMA_Period ,1);
           alpha = 2.0 /(1.0 + TEMA_Period);
           
      //
      //
      //
      //
      //
   
      Smooth_Period = MathMax(Smooth_Period,1);
         double a = MathExp(-1.414*Pi/Smooth_Period);
         double b = 2*a*MathCos(1.414*Pi/Smooth_Period);

            coef2 = b;
            coef3 = -a*a;
            coef1 = 1-coef2-coef3;
   
   IndicatorShortName ("NMA ("+NMA_Period+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iPrc 3
#define iMom 4

//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    i,k,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;
         if (ArrayRange(tBuffer,0) != Bars) ArrayResize(tBuffer,Bars);

   //
   //
   //
   //
   //
        
   for(i=limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      double rawPrice = iMA(NULL,0,1,0,MODE_SMA,NMA_Price,i);
      
         tBuffer[r][iPrc] = iTema(rawPrice,i);
         tBuffer[r][iMom] = tBuffer[r][iPrc]-tBuffer[r-1][iPrc];
   
         //
         //
         //
         //
         //
                      
            double momRatio = 0.00;
            double sumMomen = 0.00;
            double ratio    = 0.00;
      
            for (k = 0; k<NMA_Period; k++)
            {
               sumMomen += MathAbs(tBuffer[r-k][iMom]);
               momRatio +=         tBuffer[r-k][iMom]*(MathSqrt(k+1)-MathSqrt(k));
            }
            if (sumMomen != 0) ratio = MathAbs(momRatio)/sumMomen;
      
         //
         //
         //
         //
         //

      nma[i] = nma[i+1]+ratio*(rawPrice-nma[i+1]);
      if (i >= Bars-2)
            nmas[i] = nma[i];
      else  nmas[i] = coef1*nma[i]+coef2*nmas[i+1]+coef3*nmas[i+2];         
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iTema(double price,int shift)
{
   int i = Bars-shift-1;
   if (i < 1)
      {
         tBuffer[i][0] = price;
         tBuffer[i][1] = price;
         tBuffer[i][2] = price;
      }
   else
      {
         tBuffer[i][0] = tBuffer[i-1][0]+alpha*(price        -tBuffer[i-1][0]);
         tBuffer[i][1] = tBuffer[i-1][1]+alpha*(tBuffer[i][0]-tBuffer[i-1][1]);
         tBuffer[i][2] = tBuffer[i-1][2]+alpha*(tBuffer[i][1]-tBuffer[i-1][2]);
      }
   return(3*tBuffer[i][0] - 3*tBuffer[i][1] + tBuffer[i][2]);
}