//+------------------------------------------------------------------+
//|         Jim Sloman's natural moving average + standard deviation |
//|                                                 ocn nma & SD.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  DeepSkyBlue
#property indicator_color2  LimeGreen
#property indicator_color3  Red
#property indicator_width1  2
#property indicator_style2  STYLE_DOT
#property indicator_style3  STYLE_DOT


//
//
//
//
//

extern int    NMA_Period  = 40;
extern int    NMA_Price   = PRICE_CLOSE;
extern int    TEMA_Period =  1;
extern int    SD_Period   = 30;
extern double SD_Up       = 2.0;
extern double SD_Down     = 2.0;
extern bool   UseLog      = true;

//
//
//
//
//

double nma[];
double sdUp[];
double sdDo[];
double tBuffer[][5];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,nma);
   SetIndexBuffer(1,sdUp);
   SetIndexBuffer(2,sdDo);
   
      TEMA_Period = MathMax(TEMA_Period,1);
      NMA_Period  = MathMax(NMA_Period ,1);
           alpha = 2.0 /(1.0 + TEMA_Period);
           
   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 deviation = iStdDev(NULL,0,SD_Period,0,MODE_SMA,NMA_Price,i);
      double rawPrice  = iMA(NULL,0,1,0,MODE_SMA,NMA_Price,i);
      double calPrice  = iTema(rawPrice,i);
      
         tBuffer[r][iPrc] = calPrice;
                 if (UseLog && (calPrice>0)) tBuffer[r][iPrc] = MathLog(calPrice);
         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 (SD_Up   !=0) sdUp[i] = nma[i]+deviation*SD_Up;
            if (SD_Down !=0) sdDo[i] = nma[i]-deviation*SD_Down;
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iTema(double price,int pos)
{
   int i = Bars-pos-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]);
}