//+------------------------------------------------------------------+
//|                                                 Jim Sloman's nst |
//|                                                      ocn nst.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  DimGray
#property indicator_color3  DimGray
#property indicator_color4  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT
#property indicator_style3  STYLE_DOT

//
//
//
//
//

extern string _           =  "NST settings";
extern int    NST_Period  =  20;
extern int    TEMA_Period =  10;
extern string __          =  "Levels settings";
extern int    OverBought  =  50;
extern int    OverSold    = -50; 

//
//
//
//
//

double nst[];
double obLine[];
double osLine[];
double zeroLine[];
double tBuffer[][3];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,obLine);   SetIndexLabel(1,NULL);
   SetIndexBuffer(2,osLine);   SetIndexLabel(2,NULL);
   SetIndexBuffer(3,nst);

   //
   //
   //
   //
   //

   TEMA_Period = MathMax(TEMA_Period,1);
   NST_Period  = MathMax(NST_Period ,1);
        alpha = 2.0 /(1.0 + TEMA_Period);
           
   IndicatorShortName ("nst ("+NST_Period+","+TEMA_Period+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    i,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-NST_Period-1);
         if (ArrayRange(tBuffer,0) != Bars) ArrayResize(tBuffer,Bars);

   //
   //
   //
   //
   //

   for(i=limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      double sumSto = 0;
      double sumDen = 0;
      double max    = High[i];
      double min    = Low[i];
      
         //
         //
         //
         //
         //
         
         for (int k=0; k < NST_Period; k++)
         {
            if (max < High[i+k]) max = High[i+k];
            if (min >  Low[i+k]) min =  Low[i+k];
            if (max!=min)
                  double stoch = (Close[i]-min)/(max-min);
            else         stoch = 0;
            double coeff = 1.0/MathSqrt(k+1.0);
          
            sumSto += coeff*stoch;
            sumDen += coeff;
         }
         
         //
         //
         //
         //
         //

         double nstTemp = iTema((200.0*sumSto/sumDen)-100.0,r);
            if (nstTemp > 85) nstTemp =  85+( nstTemp-85)/2.0;
            if (nstTemp <-85) nstTemp = -85-(-nstTemp-85)/2.0;

      //
      //
      //
      //
      //
      
      nst[i]      = nstTemp;
      obLine[i]   = OverBought;
      osLine[i]   = OverSold;
      zeroLine[i] = 0;
   }
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

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.0*tBuffer[i][0] - 3.0*tBuffer[i][1] + tBuffer[i][2]);
}