//+------------------------------------------------------------------+
//|                                        Fisher RSI Oscillator.mq4 |
//|                                                          Belomor |
//|                                                 belomor@inbox.ru |
//+------------------------------------------------------------------+
#property copyright "Belomor"
#property link      "belomor@inbox.ru"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Red
//---- input parameters
extern int       FR=34;
extern int       SlowEMA=13;
extern int       FastEMA=5;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   if(FR<2)
   FR=2;
   if(SlowEMA<2)
   SlowEMA=2;
   if(FastEMA<1)
   FastEMA=1;
   IndicatorShortName("Fisher RSI Oscillator ("+FR+","+SlowEMA+","+FastEMA+")");
   SetIndexDrawBegin(0,FR+SlowEMA);
   SetIndexDrawBegin(1,FR+SlowEMA);
   SetIndexLabel(0,"FR");
   SetIndexLabel(1,"Signal");
   IndicatorDigits(4);
//----
   return(0);
  }

double NormalizedRSI(int R_period, int i)
   {
   double result;
   double R;
   if(i<Bars-R_period)
      {
      R=iRSI(NULL,0,R_period,PRICE_CLOSE,i);
      result=(R/50)-1;
      }
   else
   result=0;
   return(result);
   }
   
double FisherNormalizedRSI(int FR_period, int i)
   {
   double result;
   double R;
   if(i<Bars-FR_period)
      {
      R=NormalizedRSI(FR_period,i);
      if(R>0.999)
      R=0.999;
      if(R<-0.999)
      R=-0.999;
      result=0.5*MathLog((1+R)/(1-R));
      }
   else
   result=0;
   return(result);
   }
   
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   for(int i=0; i<limit; i++)
      ExtMapBuffer3[i]=FisherNormalizedRSI(FR,i);
   for(i=0; i<limit; i++)
      {
      ExtMapBuffer1[i]=iMAOnArray(ExtMapBuffer3,Bars,FastEMA,0,MODE_EMA,i);
      ExtMapBuffer2[i]=iMAOnArray(ExtMapBuffer3,Bars,SlowEMA,0,MODE_EMA,i);
      }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+