//+------------------------------------------------------------------+
//|                              Jim Sloman's natural moving average |
//|                                                 ocn nma fast.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  DeepSkyBlue
#property indicator_width1  2


//
//
//
//
//

extern int    NMA_Period  = 40;
extern int    NMA_Price   = PRICE_CLOSE;
extern int    TEMA_Period =  1;

//
//
//
//
//

double nma[];
double tBuffer[][9];
double alpha;
int    oneFifth;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,nma);
   
      TEMA_Period = MathMax(TEMA_Period,1);
      NMA_Period  = MathMax(NMA_Period ,1);
           oneFifth = MathMax(MathRound(NMA_Period/5),1);
           alpha = 2.0 /(1.0 + TEMA_Period);
           
   IndicatorShortName ("NMA");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iPrc 6
#define iPrp 7
#define iMom 8

//
//
//
//
//

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;
            int    tperiod  = iNmaPeriodLen(1,oneFifth,i);
      
            for (k = 0; k<tperiod; 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);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int iNmaPeriodLen(int startPeriod, int minPeriod,int tPos)
{
   int    endPeriod = startPeriod*NMA_Period;
   int    pos       = Bars-tPos-1;
   double maxmomen  = 0.00;
   double maxlookb  = 0.00;
   double currPrice = iTema(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,tPos),tPos,3);
      
   
   tBuffer[pos][iPrp] = currPrice;

      for (int i= startPeriod; i <= endPeriod; i++)
      {
         double tMomentum  = MathAbs((currPrice-tBuffer[pos-i][iPrp])/MathSqrt(i));
            if (tMomentum > maxmomen)
            {
               maxmomen = tMomentum;
               maxlookb = i;
            }
         i += (startPeriod-1);
      }

   return(MathMax(maxlookb,minPeriod));
}

//
//
//
//
//

double iTema(double price,int pos,int sbuf=0)
{
   int i  = Bars-pos-1;
   int ia = sbuf+0;
   int ib = sbuf+1;
   int ic = sbuf+2;
   
   if (i < 1)
      {
         tBuffer[i][ia] = price;
         tBuffer[i][ib] = price;
         tBuffer[i][ic] = price;
      }
   else
      {
         tBuffer[i][ia] = tBuffer[i-1][ia]+alpha*(price         -tBuffer[i-1][ia]);
         tBuffer[i][ib] = tBuffer[i-1][ib]+alpha*(tBuffer[i][ia]-tBuffer[i-1][ib]);
         tBuffer[i][ic] = tBuffer[i-1][ic]+alpha*(tBuffer[i][ib]-tBuffer[i-1][ic]);
      }
   return(3*tBuffer[i][ia] - 3*tBuffer[i][ib] + tBuffer[i][ic]);
}