//+------------------------------------------------------------------+
//|                                                          RSIMA.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#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_buffers 3
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 DodgerBlue   //RSI color
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_width1  1
#property indicator_width2  2
#property indicator_width3  2
#property indicator_level1 30      //SetLevelValue(0,50);
#property indicator_level2 50      //SetLevelValue(1,68);
#property indicator_level3 70      //SetLevelValue(2,32);
#property indicator_levelcolor DimGray      //color indicator value

//--- input parameters
extern int RSI_Period    = 14;    //8-25
extern int RSI_Price     =  0;     /*PRICE_CLOSE    0   Close price. 
                                     PRICE_OPEN     1   Open price. 
                                     PRICE_HIGH     2   High price. 
                                     PRICE_LOW      3   Low price. 
                                     PRICE_MEDIAN   4   Median price, (high+low)/2. 
                                     PRICE_TYPICAL  5   Typical price, (high+low+close)/3. 
                                     PRICE_WEIGHTED 6   Weighted close price, (high+low+close+close)/4. */
extern int RSI_MA_Period = 21; 
extern int RSI_MA_Type = 1;       // 0 SMA , 1 EMA , 2 SMMA , 3 LWMA
//--- buffers
double RSIBuf[];
double Uptrend[];
double Dntrend[];
double MaBuf[];


int OnInit()
{
IndicatorBuffers(4);
//---- drawing settings  RSI  (0,......)
SetIndexBuffer(0,RSIBuf);
SetIndexLabel(0,"RSI("+string(RSI_Period)+")");                    //SetIndexLabel(0,NULL); wrong
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);  //SetIndexStyle(0,DRAW_NONE);wrong this is draw nothing

//---- drawing settings of Moving Average on RSI
SetIndexBuffer(1,Uptrend);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(2,Dntrend);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(3,MaBuf);
ArraySetAsSeries(MaBuf,true);
//SetIndexLabel(1, "MA("+ string(RSI_MA_Period) +") Period MA of RSI");       //SetIndexLabel(1,"MA of the RSI");
//SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);                       //SetIndexStyle(1,DRAW_LINE);
IndicatorShortName("RSI"+"("+string(RSI_Period)+ ")"+"=>" + "MA"+"("+ string(RSI_MA_Period)+")");


return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
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[])
{
   double trend[];
   double MA,RSI[];
   ArrayResize(RSI,RSI_MA_Period);
   int counted_bars=IndicatorCounted();
   int limit = Bars-counted_bars - 1;
   if(limit>Bars) limit=Bars;
   
   ArrayResize(trend,limit);
   ArraySetAsSeries(trend,true);
   
   for(int i= limit; i>=0 ; i--)
   {
      RSIBuf[i] = iRSI(NULL,0,RSI_Period,RSI_Price,i); 
      MA = 0;
      for(int x=i; x<i; x++) 
      {
         RSI[x-i] = RSIBuf[x];
         MA += RSIBuf[x]/RSI_MA_Period;
      }
   }
   for(int j=limit; j >= 0; j--)       //was missing
   {
      MaBuf[j] = (2*iMAOnArray(RSIBuf,0,RSI_MA_Period/2,0,RSI_MA_Type,j)- iMAOnArray(RSIBuf,0,RSI_MA_Period,0,RSI_MA_Type,j));
   }
   for(int x=limit-RSI_MA_Period; x>=0; x--)
     {
      trend[x]=trend[x+1];
      if(MaBuf[x]> MaBuf[x+1]) trend[x] =1;
      if(MaBuf[x]< MaBuf[x+1]) trend[x] =-1;

      if(trend[x]>0)
        {
         Uptrend[x]=MaBuf[x];
         if(trend[x+1]<0) Uptrend[x+1]=MaBuf[x+1];
         Dntrend[x]=EMPTY_VALUE;
         Comment ("Uptrend");
        }
      else
      if(trend[x]<0)
        {
         Dntrend[x]=MaBuf[x];
         if(trend[x+1]>0) Dntrend[x+1]=MaBuf[x+1];
         Uptrend[x]=EMPTY_VALUE;
         Comment ("Downtrend");
        }

     }
   
//----
return(rates_total);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
