//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//
//
//

extern double RsiPeriod  = 14;
extern int    RsiPrice   = 0;
extern int    RsiDepth   = 3;
extern bool   RsiFast    = false;
extern double LevelUp    = 0.85;
extern double LevelDown  = 0.15;

//
//
//
//
//

double rsi[];
double UpH[];
double DnH[]; 
double slope[];

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,UpH); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,DnH); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,rsi);
   SetIndexBuffer(3,slope);
      RsiDepth = MathMax(MathMin(RsiDepth,25),2);
   IndicatorShortName("Composite RSI ("+DoubleToStr(RsiPeriod,1)+","+RsiDepth+")");
   return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars - counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
      for(int i = limit; i >= 0 ; i--)
      {
         rsi[i]   = iCompRsi(iMA(NULL,0,1,0,MODE_SMA,RsiPrice,i),RsiPeriod,RsiDepth,RsiFast,i);
         UpH[i]   = EMPTY_VALUE;
         DnH[i]   = EMPTY_VALUE;
         slope[i] = slope[i+1];
            if (rsi[i] > rsi[i+1]) slope[i] =  1;
            if (rsi[i] < rsi[i+1]) slope[i] = -1;
            if (slope[i] == 1) UpH[i] = 1;
            if (slope[i] ==-1) DnH[i] = 1;
      }
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workCompRsi[][26];

//
//
//
//
//

double iCompRsi(double price, double period, int depth, bool fast, int i, int instanceNo=0)
{
   if (ArrayRange(workCompRsi,0) !=Bars) ArrayResize(workCompRsi,Bars);
   if (!fast)
        double alpha = 2.0/(1.0 + period);
   else        alpha = 2.0/(2.0 + (period-1.0)/2.0);
   instanceNo *= 26; i = Bars-i-1;
   
   //
   //
   //
   //
   //
   
   double CU = 0;
   double CD = 0;
   for (int k=0; k<=depth; k++)
   {
      if (i == 0)
            workCompRsi[i][instanceNo+k] = price;
      else  workCompRsi[i][instanceNo+k] = workCompRsi[i-1][instanceNo+k]+alpha*(price-workCompRsi[i-1][instanceNo+k]);

      //
      //
      //
      //
      //
         
      price = workCompRsi[i][k+instanceNo];
      if (k>0)
         if (workCompRsi[i][instanceNo+k-1] >= workCompRsi[i][instanceNo+k])
              CU += workCompRsi[i][instanceNo+k-1] - workCompRsi[i][instanceNo+k  ];
         else CD += workCompRsi[i][instanceNo+k  ] - workCompRsi[i][instanceNo+k-1];
   }
   double rsi = 0; if (CU + CD != 0) rsi = CU / (CU + CD); 
   return(rsi);
}


