#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Relative Strength Index"
#property strict

#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_buffers    6
#property indicator_color1     DodgerBlue
#property indicator_color4     clrYellow
#property indicator_color5     clrRed
#property indicator_color6     clrLime
#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameters
input int InpRSIPeriod=14; // RSI Period
//--- buffers

#include <MovingAverages.mqh>

double ExtRSIBuffer[];
double ExtPosBuffer[];
double ExtNegBuffer[];
double ExtSignalBuffer1[];
double ExtSignalBuffer2[];
double ExtSignalBuffer3[];

input int InpSignalSMA1=9;  // Signal SMA Period
input int InpSignalSMA2=22;  // Signal SMA Period
input int InpSignalSMA3=53;  // Signal SMA Period
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(1,ExtPosBuffer);
   SetIndexBuffer(2,ExtNegBuffer);
   SetIndexBuffer(3,ExtSignalBuffer1);
   SetIndexBuffer(4,ExtSignalBuffer2);
   SetIndexBuffer(5,ExtSignalBuffer3);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtRSIBuffer);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtSignalBuffer1);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtSignalBuffer2);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,ExtSignalBuffer3);
//--- name for DataWindow and indicator subwindow label
   short_name="ABO-MOHAMMED, RSI("+string(InpRSIPeriod)+"),,,SIGNAL1("+string(InpSignalSMA1)+")"+"),,,SIGNAL2("+string(InpSignalSMA2)+")"+"),,,SIGNAL3("+string(InpSignalSMA3)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input
   if(InpRSIPeriod<2)
     {
      Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpRSIPeriod);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int    i,pos;
   double diff;
//---
   if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtRSIBuffer,false);
   ArraySetAsSeries(ExtPosBuffer,false);
   ArraySetAsSeries(ExtNegBuffer,false);
   ArraySetAsSeries(close,false);
//--- preliminary calculations
   pos=prev_calculated-1;
   if(pos<=InpRSIPeriod)
     {
      //--- first RSIPeriod values of the indicator are not calculated
      ExtRSIBuffer[0]=0.0;
      ExtPosBuffer[0]=0.0;
      ExtNegBuffer[0]=0.0;
      double sump=0.0;
      double sumn=0.0;
      for(i=1; i<=InpRSIPeriod; i++)
        {
         ExtRSIBuffer[i]=0.0;
         ExtPosBuffer[i]=0.0;
         ExtNegBuffer[i]=0.0;
         diff=close[i]-close[i-1];
         if(diff>0)
            sump+=diff;
         else
            sumn-=diff;
        }
      //--- calculate first visible value
      ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;
      ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;
      if(ExtNegBuffer[InpRSIPeriod]!=0.0)
         ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
      else
        {
         if(ExtPosBuffer[InpRSIPeriod]!=0.0)
            ExtRSIBuffer[InpRSIPeriod]=100.0;
         else
            ExtRSIBuffer[InpRSIPeriod]=50.0;
        }
      //--- prepare the position value for main calculation
      pos=InpRSIPeriod+1;
     }
//--- the main loop of calculations
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      diff=close[i]-close[i-1];
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
      if(ExtNegBuffer[i]!=0.0)
         ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
      else
        {
         if(ExtPosBuffer[i]!=0.0)
            ExtRSIBuffer[i]=100.0;
         else
            ExtRSIBuffer[i]=50.0;
        }
     }

   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA1,ExtRSIBuffer,ExtSignalBuffer1);
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA2,ExtRSIBuffer,ExtSignalBuffer2);
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA3,ExtRSIBuffer,ExtSignalBuffer3);
   return(rates_total);
  }
//+------------------------------------------------------------------+
