#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_width1 1
#property indicator_style1 STYLE_SOLID
//---- input parameters
extern int  RSIPeriod   = 14;
extern ENUM_APPLIED_PRICE  ApplyTo     = PRICE_CLOSE;
extern bool AlertMode   = true;
extern int  OverBought  = 70;
extern int  OverSold    = 30;
//---- buffers
double RSIBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   string short_name;
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,RSIBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="RSI-Alert("+RSIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetLevelValue(0, OverBought);
   SetLevelValue(1, OverSold);
//----
   SetIndexDrawBegin(0,RSIPeriod);
//----
   return(INIT_SUCCEEDED);
}
int i;
//+------------------------------------------------------------------+
//| 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[])
{
   i = (prev_calculated < RSIPeriod) ? rates_total - prev_calculated-RSIPeriod-2 : rates_total - prev_calculated;
   while(i >= 0)
   {
      RSIBuffer[i]=iRSI(NULL,0,RSIPeriod,ApplyTo,i);
      if(AlertMode && i == 1)
      {
         if(RSIBuffer[i+1]<OverBought && RSIBuffer[i]>=OverBought)
            Alert(_Symbol," RSI = "+ RSIBuffer[i]+ ", Sell.");
         else if(RSIBuffer[i+1]>OverSold && RSIBuffer[i]<=OverSold)
            Alert(_Symbol," RSI = "+ RSIBuffer[i]+ ", Buy.");
      }
      i--;
   }
//----
   return(rates_total);
}
//+------------------------------------------------------------------+