//+------------------------------------------------------------------+
//|                                              stochastic momentum |
//|                                                           mladen |
//|                                                                  |
//| Stochastic momentum developed by William Blau                    |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Red
#property indicator_color2  Goldenrod
#property indicator_minimum -100
#property indicator_maximum  100
#property indicator_level1   0
#property indicator_level2   50
#property indicator_level3  -50
#property indicator_levelcolor DarkSlateGray


//
//
//
//
//

extern int  Length   = 13;
extern int  Smooth1  = 25;
extern int  Smooth2  =  2;
extern int  Signal   =  5;
extern int mode = 0;
extern int retard = 0;
extern int  Price    = PRICE_CLOSE;

//
//
//
//
//

double smo[];
double sms[];
double tBuffer[][6];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,smo); SetIndexLabel(0,"Stochastic momentum");
   SetIndexBuffer(1,sms); SetIndexLabel(1,"Stochastic momentum signal");

   //
   //
   //
   //
   //
   
   string PriceType;
      switch(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
      }      

   //
   //
   //
   //
   //

   Length  = MathMax(Length ,1);
   Smooth1 = MathMax(Smooth1,1);
   Smooth2 = MathMax(Smooth2,1);
   IndicatorShortName (" Stochastic momentum ("+Length+","+Smooth1+","+Smooth2+","+PriceType+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define ema10 0
#define ema11 1
#define ema12 2
#define ema20 3
#define ema21 4
#define ema22 5

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-1);
                 if (ArrayRange(tBuffer,0) != Bars) ArrayResize(tBuffer,Bars);

         double alpha1 = 2.0 /(1.0+Smooth1);
         double alpha2 = 2.0 /(1.0+Smooth2);
         double alphas = 2.0 /(1.0+Signal);
         
   //
   //
   //
   //
   //
   //

   for(i=limit,r=Bars-i-1; i >= 0; i--,r++)
   {
      double hh = High[iHighest(NULL,0,MODE_HIGH,Length,i)];
      double ll =  Low[ iLowest(NULL,0,MODE_LOW ,Length,i)];
      double pr = iMA(NULL,0,1,0,MODE_SMA,Price,i);

         tBuffer[r][ema10] = pr - 0.5*(hh+ll);
         tBuffer[r][ema20] =           hh-ll;
         if (i>=Bars-3)
         {
            tBuffer[r][ema11] = tBuffer[r][ema10];
            tBuffer[r][ema12] = tBuffer[r][ema10];
            tBuffer[r][ema21] = tBuffer[r][ema20];
            tBuffer[r][ema22] = tBuffer[r][ema20];
            continue;
         }
      
      //
      //
      //
      //
      //
      
      tBuffer[r][ema11] = tBuffer[r-1][ema11] + alpha1*(tBuffer[r][ema10]-tBuffer[r-1][ema11]);
      tBuffer[r][ema12] = tBuffer[r-1][ema12] + alpha2*(tBuffer[r][ema11]-tBuffer[r-1][ema12]);
      tBuffer[r][ema21] = tBuffer[r-1][ema21] + alpha1*(tBuffer[r][ema20]-tBuffer[r-1][ema21]);
      tBuffer[r][ema22] = tBuffer[r-1][ema22] + alpha2*(tBuffer[r][ema21]-tBuffer[r-1][ema22]);

         smo[i] = 100.00 * tBuffer[r][ema12] / (0.5 * tBuffer[r][ema22]);
         if (Signal>1)
            if(mode==0) sms[i] = sms[i+1]+alphas*(smo[i]-sms[i+1]);
             if(mode==1) sms[i] = smo[i+retard];
   }
   return(0);
}