//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  White
#property indicator_width1  3
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  20
#property indicator_level2  80
#property indicator_levelcolor DarkGray

//
//
//
//
//

extern int    Price           = PRICE_CLOSE; 
extern int    StartFrom       = 2;
extern int    EndWith         = 30;
extern int    Filter          = 5;
extern int    FilterMethod    = 3;
extern double Smooth          = 5;
extern double SmoothPhase     = 0;
extern int    BarsToDraw      = 150;
extern int    BarsWidth       = 4;
extern string UniqueID        = "Swami rapid RSI";
extern bool   GenericVisible  = true;
extern color  ColorUp         = LimeGreen;
extern color  ColorDown       = HotPink;

double rsi[];
double step;
double totalSteps;
int    window;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int init()
{
   int type = DRAW_NONE; if (GenericVisible) type = DRAW_LINE;
   SetIndexBuffer(0,rsi); SetIndexStyle(0,type);
      
         //
         //
         //
         //
         //
         
         StartFrom  = MathMax(StartFrom, 6);
         EndWith    = MathMin(EndWith  ,99);
         totalSteps = EndWith-StartFrom+1.0;
         step       = 100.0/(totalSteps-1);
      IndicatorShortName(UniqueID);
   return(0);
}

//
//
//
//
//

int deinit()
{
   string lookFor       = UniqueID+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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);
         window = WindowFind(UniqueID);

         //
         //
         //
         //
         //

         for (i=limit; i>=0 ; i--)
         {
            double price = iMA(NULL,0,Filter,0,FilterMethod,Price,i);
            rsi[i] = 0;         
            for (int k=StartFrom; k<EndWith; k++)
            {
               double trsi = iRapidRsi(price,k,i,k);
               if (i<BarsToDraw)
               {
                     plot(k,(k-StartFrom)*step,(k-StartFrom+1)*step,i,i,gradientColor(trsi,101,ColorDown,ColorUp),BarsWidth);
               }                        
               rsi[i] += trsi;
            }
            rsi[i] = iSmooth(rsi[i]/totalSteps,Smooth,SmoothPhase,i);
         }
         
   //
   //
   //
   //
   //

   SetIndexDrawBegin(0,Bars-BarsToDraw);
   return(0);
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workRRsi[][100];
double iRapidRsi(double price, int period, int i, int instanceNo)
{
   if (ArrayRange(workRRsi,0) != Bars) ArrayResize(workRRsi,Bars); i=Bars-i-1;
   
   //
   //

   workRRsi[i][instanceNo] = price;
   
      double up   =0;
      double down =0;
      for(int k=0;  k<period && (i-k-1)>=0; k++)
      {
         double diff = workRRsi[i-k][instanceNo]-workRRsi[i-k-1][instanceNo];
         if(diff>0)
               up   += diff;
         else  down -= diff;
      }
      double rsi = 0; if((up+down) != 0) rsi = 100.0 * up / (up + down);
      return(rsi);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void plot(string namex,double valueA, double valueB, int shiftA, int shiftB, color theColor, int width=0,int style=STYLE_SOLID)
{
   string   name = UniqueID+":"+namex+Time[shiftA];
   
   //
   //
   //
   //
   //
   
   if (ObjectFind(name) == -1) 
   {
       ObjectCreate(name,OBJ_TREND,window,Time[shiftA],valueA,Time[shiftB],valueB);
          ObjectSet(name,OBJPROP_RAY,false);
          ObjectSet(name,OBJPROP_BACK,true);
          ObjectSet(name,OBJPROP_STYLE,style);
          ObjectSet(name,OBJPROP_WIDTH,width);
   }
   ObjectSet(name,OBJPROP_COLOR,theColor);
   ObjectSet(name,OBJPROP_PRICE1,valueA);
   ObjectSet(name,OBJPROP_PRICE2,valueB);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

color gradientColor(int step, int totalSteps, color from, color to)
{
   step = MathMax(MathMin(step,totalSteps-1),0);
      color newBlue  = getColor(step,totalSteps,(from & 0XFF0000)>>16,(to & 0XFF0000)>>16)<<16;
      color newGreen = getColor(step,totalSteps,(from & 0X00FF00)>> 8,(to & 0X00FF00)>> 8) <<8;
      color newRed   = getColor(step,totalSteps,(from & 0X0000FF)    ,(to & 0X0000FF)    )    ;
      return(newBlue+newGreen+newRed);
}
color getColor(int stepNo, int totalSteps, color from, color to)
{
   double step = (from-to)/(totalSteps-1.0);
   return(MathRound(from-step*stepNo));
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double wrk[][10];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax+s];
      double del2   = price - wrk[r-1][bsmin+s];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
            if (wrk[r][avolty+s] > 0)
               double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}