//+------------------------------------------------------------------+
//|                              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 3
#property indicator_color1  LimeGreen
#property indicator_color2  DarkGray
#property indicator_color3  Orange
#property indicator_style1  STYLE_DOT
#property indicator_style3  STYLE_DOT

//
//
//
//
//

extern int    NMAPeriod  =  40;
extern int    NMAPrice   =   PRICE_CLOSE;
extern int    TEMAPeriod =  10;
extern double Deviation  = 0.1;

//
//
//
//
//

double nma[];
double nmu[];
double nmd[];
double tBuffer[][5];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,nmu);
   SetIndexBuffer(1,nma);
   SetIndexBuffer(2,nmd);
   
      TEMAPeriod = MathMax(TEMAPeriod,1);
      NMAPeriod  = MathMax(NMAPeriod ,1);
           alpha = 2.0 /(1.0 + TEMAPeriod);
           
   IndicatorShortName ("NMA");
   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,NMAPrice,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<NMAPeriod; 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]);
      nmu[i] = (1+Deviation/100)*nma[i];
      nmd[i] = (1-Deviation/100)*nma[i];
   }
   
   //
   //
   //
   //
   //
   
   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]);
}