//+------------------------------------------------------------------+
//|                        Smoothed Rsi Inverse Fisher Transform.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  PaleVioletRed
#property indicator_width1  2
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  12
#property indicator_level2  88
#property indicator_levelcolor DimGray

//
//
//
//
//

extern int  RsiPeriod  = 4;
extern int  EmaPeriod  = 4;
extern int  RwmaLength = 2;
extern int  Depth      = 10;
extern int  Price      = PRICE_CLOSE;

//
//
//
//
//

#define MAX_depth 50
double  inv[];
double  rwm[];
double  ema1[];
double  ema2[];
double  pBuffer[][MAX_depth];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
      SetIndexBuffer(0,inv);
      SetIndexBuffer(1,rwm);
      SetIndexBuffer(2,ema1);
      SetIndexBuffer(3,ema2);
      
      //
      //
      //
      //
      //
      
         RwmaLength = MathMax(RwmaLength,2);
         Depth      = MathMax(MathMin(Depth,MAX_depth),2);
         
      //
      //
      //
      //
      //
               
   IndicatorShortName("Smoothed Rsi Inverse Fisher Transform ("+RsiPeriod+","+EmaPeriod+","+RwmaLength+","+Depth+")");
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,l,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           if (ArrayRange(pBuffer,0) != Bars) ArrayResize(pBuffer,Bars);

   //
   //
   //
   //
   //
   
   double weight = (RwmaLength+1.0)*RwmaLength*0.5;
      for(i=limit, r=Bars-limit-1; i>=0; i--,r++)
      {
         double price = iMA(NULL,0,1,0,MODE_SMA,Price,i);
      
         //
         //
         //
         //
         //

         double coeff  = 5;
         double sum    = 0;
         double sumc   = 0;
         for (int k=0; k<Depth; k++)
         {
            pBuffer[r][k] = price;
            if (r>RwmaLength)
            {
               for (l=0, price=0; l<RwmaLength; l++) price += (RwmaLength-l)*pBuffer[r-l][k];
                                                     price /= weight;
            }
            sum  += coeff*price;
            sumc += coeff;
            coeff = MathMax(coeff-1,1);
         }
         rwm[i] = sum/sumc;
      }
   
      //
      //
      //
      //
      //
   
      double alpha = 2.0 / (1+EmaPeriod);
      for(i=limit; i>=0; i--)
      {
         double rsi     = 0.1 * (iRSIOnArray(rwm,0,RsiPeriod,i)-50.0);
                ema1[i] = ema1[i+1]+alpha*(rsi    -ema1[i+1]);
                ema2[i] = ema2[i+1]+alpha*(ema1[i]-ema2[i+1]);
         double zlema   = 2.0*ema1[i]-ema2[i];
                inv[i]  = 50.0*(1+(MathExp(2*zlema)-1)/(MathExp(2*zlema)+1));
      }      

   //
   //
   //
   //
   //
         
   return(0);
}