//+------------------------------------------------------------------+
//|                              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;

//
//
//
//
//

double nma[];
double tBuffer[][5];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,nma);
   
      TEMA_Period = MathMax(TEMA_Period,1);
      NMA_Period  = MathMax(NMA_Period ,1);
           alpha = 2.0 /(1.0 + TEMA_Period);
           
   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]);
   }
   
   //
   //
   //
   //
   //
   
   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]);
}