//+------------------------------------------------------------------+
//|                                                    RSI Color.mq5 |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3

#property indicator_label1  "RSI Green"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  Green
#property indicator_width1  1

#property indicator_label2  "RSI Red"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  Red
#property indicator_width2  1

#property indicator_label3  "RSI Blue"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3  Blue
#property indicator_width3  1

#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  40
#property indicator_level2  50
#property indicator_level3  60

input int    RSIPeriod   = 14;
input ENUM_APPLIED_PRICE RSIPrice = PRICE_CLOSE;
input double GreenLimit  = 60;
input double RedLimit    = 40;

double RSIG[];
double RSIR[];
double RSIB[];

int rsiHandle = INVALID_HANDLE;

int OnInit()
  {
   SetIndexBuffer(0,RSIG,INDICATOR_DATA);
   SetIndexBuffer(1,RSIR,INDICATOR_DATA);
   SetIndexBuffer(2,RSIB,INDICATOR_DATA);

   IndicatorSetString(INDICATOR_SHORTNAME,"RSI Color("+IntegerToString(RSIPeriod)+")");
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,0);

   ArraySetAsSeries(RSIG, true);
   ArraySetAsSeries(RSIR, true);
   ArraySetAsSeries(RSIB, true);

   rsiHandle = iRSI(_Symbol, _Period, RSIPeriod, RSIPrice);

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   if(rsiHandle != INVALID_HANDLE)
      IndicatorRelease(rsiHandle);
  }

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[])
  {
   if(GreenLimit<RedLimit) return(0);

   static double rsiBuffer[];
   ArrayResize(rsiBuffer, rates_total);
   ArraySetAsSeries(rsiBuffer, true);

   int copied = CopyBuffer(rsiHandle, 0, 0, rates_total, rsiBuffer);

   for(int i=0; i<rates_total; i++)
     {
      RSIG[i]=RSIR[i]=RSIB[i]=EMPTY_VALUE;
      if(copied > 0 && MathIsValidNumber(rsiBuffer[i]))
      {
         double rsi = rsiBuffer[i];
         if(rsi>GreenLimit)      RSIG[i]=rsi;
         else if(rsi<RedLimit)   RSIR[i]=rsi;
         else                    RSIB[i]=rsi;
      }
     }
   return(rates_total);
  }