//------------------------------------------------------------------
#property copyright "mladen"
#property link      "mladenfx@gmail.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  LimeGreen
#property indicator_color2  Orange
#property indicator_color3  DarkGray
#property indicator_color4  DarkGray

//
//
//
//
//

extern int    Length    = 14;
extern int    Price     = PRICE_CLOSE;
extern double LevelUp   = 80;
extern double LevelDown = 20;

//
//
//
//
//

double barnu[];
double barnd[];
double bartu[];
double bartd[];

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

int init()
{
      SetIndexBuffer(0,bartu); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,bartd); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,barnu); SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexBuffer(3,barnd); SetIndexStyle(3,DRAW_HISTOGRAM);
         IndicatorShortName("Rsx ("+Length+")");
   return(0);
}
int deinit() { return(0); }

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

double wrkBuffer[][13];

int start()
{
   int i,r,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (ArrayRange(wrkBuffer,0) != Bars) ArrayResize(wrkBuffer,Bars);

   //
   //
   //
   //
   //
   
   double Kg = (3.0)/(2.0+Length); 
   double Hg = 1.0-Kg;
   for(i=limit, r=Bars-i-1; i>=0; i--, r++)
   {
      wrkBuffer[r][12] = iMA(NULL,0,1,0,MODE_SMA,Price,i);

         if (i==(Bars-1)) { for (int c=0; c<12; c++) wrkBuffer[r][c] = 0; continue; }  

      //
      //
      //
      //
      //
      
      double mom = wrkBuffer[r][12]-wrkBuffer[r-1][12];
      double moa = MathAbs(mom);
      for (int k=0; k<3; k++)
      {
         int kk = k*2;
            wrkBuffer[r][kk+0] = Kg*mom                + Hg*wrkBuffer[r-1][kk+0];
            wrkBuffer[r][kk+1] = Kg*wrkBuffer[r][kk+0] + Hg*wrkBuffer[r-1][kk+1]; mom = 1.5*wrkBuffer[r][kk+0] - 0.5 * wrkBuffer[r][kk+1];
            wrkBuffer[r][kk+6] = Kg*moa                + Hg*wrkBuffer[r-1][kk+6];
            wrkBuffer[r][kk+7] = Kg*wrkBuffer[r][kk+6] + Hg*wrkBuffer[r-1][kk+7]; moa = 1.5*wrkBuffer[r][kk+6] - 0.5 * wrkBuffer[r][kk+7];
      }
      if (moa != 0)
           double rsx = MathMax(MathMin((mom/moa+1.0)*50.0,100.00),0.00); 
      else        rsx = 50;
      
      //
      //
      //
      //
      //

      bartu[i] = EMPTY_VALUE;
      bartd[i] = EMPTY_VALUE;
      barnu[i] = EMPTY_VALUE;
      barnd[i] = EMPTY_VALUE;
      int trend = 0;
         if (rsx>LevelUp)   trend =  1;
         if (rsx<LevelDown) trend = -1;
            if (trend== 1) { bartu[i] = High[i]; bartd[i] = Low[i]; }
            if (trend==-1) { bartd[i] = High[i]; bartu[i] = Low[i]; }
            if (trend== 0) { barnu[i] = High[i]; barnd[i] = Low[i]; }
   }
   return(0);
}