//+------------------------------------------------------------------+
//|                                Jim Sloman's natural market slope |
//|                   with Standard Deviation and historical extreme |
//|                                                                  |
//|                                            ocn nmr & SD hist.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DimGray
#property indicator_color2  LimeGreen
#property indicator_color3  Green
#property indicator_color4  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT
#property indicator_width4  2

//
//
//
//
//

extern int    NMS_Period             = 40;
extern int    NMS_Price              = PRICE_CLOSE;
extern int    TEMA_Period            = 10;
extern bool   Show_StandardDeviation = true;
extern int    SD_Period              = 30;
extern double SD_Up                  = 2.0;
extern double SD_Down                = 2.0;
extern bool   Show_HistoricalExtreme = true;
extern int    History_LookBack       = 15;

//
//
//
//
//

double nms[];
double zeroLine[];
double sd[];
double hist[];
double tBuffer[][4];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,hist); SetIndexDrawBegin(1,NMS_Period+SD_Period);
   SetIndexBuffer(2,sd);   SetIndexDrawBegin(2,NMS_Period+SD_Period);
   SetIndexBuffer(3,nms);  SetIndexDrawBegin(3,NMS_Period);
   
   
   //
   //
   //
   //
   //

   string PriceType;
      switch(NMS_Price)
      {
         case PRICE_CLOSE:    PriceType = "Close";    break;  // 0
         case PRICE_OPEN:     PriceType = "Open";     break;  // 1
         case PRICE_HIGH:     PriceType = "High";     break;  // 2
         case PRICE_LOW:      PriceType = "Low";      break;  // 3
         case PRICE_MEDIAN:   PriceType = "Median";   break;  // 4
         case PRICE_TYPICAL:  PriceType = "Typical";  break;  // 5
         case PRICE_WEIGHTED: PriceType = "Weighted"; break;  // 6
      }      

   //
   //
   //
   //
   //
   
      TEMA_Period = MathMax(TEMA_Period,1);
      NMS_Period  = MathMax(NMS_Period ,1);
            alpha = 2.0 /(1.0 + TEMA_Period);

   IndicatorShortName ("nms ("+NMS_Period+","+PriceType+","+TEMA_Period+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iPrc 3

//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    i,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,NMS_Price,i);
      double currPrice = iTema(rawPrice,r);
         if (currPrice > 0)
               tBuffer[r][iPrc] = MathLog(currPrice);
         else  tBuffer[r][iPrc] = 0.00;
      
         //
         //
         //
         //
         //
         
         nms[i]      = iNmsFunction(i);
         zeroLine[i] = 0;
         
         //
         //
         //
         //
         //

         if (Show_StandardDeviation)
         {         
            sd[i] = 0;
            double avg = iSma(nms,SD_Period,i);
            double deviation = iDeviation(nms,avg,SD_Period,i);
               if (nms[i] > 0) sd[i] = avg+deviation*SD_Up;
               if (nms[i] < 0) sd[i] = avg-deviation*SD_Down;
         }
         
         //
         //
         //
         //
         //
         
         if (Show_HistoricalExtreme)
         {
            hist[i] = hist[i+1];
               if (nms[i] >  0) hist[i] = MathMax(0,nms[ArrayMaximum(nms,History_LookBack,i)]);
               if (nms[i] <  0) hist[i] = MathMin(0,nms[ArrayMinimum(nms,History_LookBack,i)]);
               if (nms[i] == 0) hist[i] = 0;
         }               
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//    natural market slope
//
//
//

double iNmsFunction(int tPos)
{
   int    pos   = Bars-tPos-1;
   double sumxy = 0.00;
   double sumy  = 0.00;
   double sum   = 0.00;

   //
   //
   //
   //
   //
   
   for (int period=1; period<=NMS_Period; period++)
   {
      double sumx   = period*(period+1.0)/2.0;
      double sumx2  = period*(period+1.0)*(2.0*period+1.0)/6.0;
      double price  = tBuffer[pos-(period-1)][iPrc];
      
             sumxy += sumy+price;
             sumy  +=      price;
             
      double bot   = period*sumx2 - sumx*sumx;
      double slope = 0.00;
      if (bot != 0) slope = (period*sumxy - sumx*sumy)/bot;
             sum += slope*MathSqrt(period);
   }           
   return(sum*100.00);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iDeviation(double& array[],double dMA, int period, int pos)
{
   double dSum = 0;
         for(int i=0; i<period; i++,pos++) dSum += (array[pos]-dMA)*(array[pos]-dMA);
   return(MathSqrt(dSum/period));
}

//
//
//
//
//

double iSma(double& array[], int period, int pos)
{
   double sum = 0;
         for(int i=0; i<period; i++,pos++) sum += array[pos];
   return(sum/period);
}

//
//
//
//
//

double iTema(double price,int i)
{
   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]);
}