//+------------------------------------------------------------------+
//|                                                 RSI Coloured.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, PIPS 2009"
#property indicator_separate_window

#property indicator_maximum 100
#property indicator_minimum 0
#property indicator_levelcolor Gray

#property indicator_buffers 4

#property indicator_color1 White
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 White

//---- input parameters
extern string  RSIPeriod_Desc    =  "Период RSI";
extern int     RSIPeriod         =  14;
extern string  RSIPrice_Desc     =  "Цена RSI: 0-Close, 1-Open, 2-High, 3-Low, 4-Median, 5-Typical, 6-Weighted";
extern int     RSIPrice          =  0;
extern string  HighLevel_Desc    =  "Верхний уровень RSI";
extern int     HighLevel         =  70;
extern string  LowLevel_Desc     =  "Нижний уровень RSI";
extern int     LowLevel          =  30;
extern string  MarkFalse_Desc    =  "Маркировать или нет бары, которые при формировании залазили за уровни";
extern bool    MarkFalse         =  false;
extern string  Transparent_Desc  =  "Рисовать RSI поверх цветных уровней";
extern bool    Transparent       =  false;
extern string  Width_Desc        =  "Ширина сигнальных линий";
extern int     Width             =  1;
extern string  ClrShift_Desc     =  "Сдвиг цветовых линий по горизонтали";
extern int     ColorShift        =  0;
      
//---- buffers
double RSI[];
double Inner[];
double Higher[];
double Lower[];


/// \brief Equal to ternar operator 
/// needed = Condition ? IfTrue : IfFalse;
/// \param IfTrue
/// \param IfFalse
/// \return matching value from parameters
double DoubleIf(bool Condition, double IfTrue, double IfFalse)
{
   if (Condition) return (IfTrue);
   else           return (IfFalse);
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(4);

   SetIndexBuffer(0, Inner);
   SetIndexBuffer(1, Higher);
   SetIndexBuffer(2, Lower);
   SetIndexBuffer(3, RSI);
   
   SetLevelValue(0, HighLevel);
   SetLevelValue(1, LowLevel);
  
   SetIndexLabel(0, "RSI");
   SetIndexLabel(1, "Выше уровня");
   SetIndexLabel(2, "Ниже уровня");

   SetIndexStyle(0, DRAW_NONE);
   SetIndexStyle(1, DRAW_LINE, EMPTY, Width);
   SetIndexStyle(2, DRAW_LINE, EMPTY, Width);
   SetIndexStyle(3, DRAW_LINE);

   if (!Transparent)
   {       
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(3,DRAW_NONE);
   }

   SetIndexShift(1,ColorShift);
   SetIndexShift(2,ColorShift);


   SetIndexDrawBegin(0, RSIPeriod + 1);
   SetIndexDrawBegin(1, RSIPeriod + 1);
   SetIndexDrawBegin(2, RSIPeriod + 1);   
   
   IndicatorShortName("Цветной RSI "+RSIPeriod);
   SetIndexLabel(3,NULL);
   
   IndicatorDigits(2);

   return(0);
}

int start()
{
   int ToCount = Bars - IndicatorCounted();

   for (int cnt = ToCount - 1; cnt >= 0; cnt--)
   {
      RSI[cnt] = iRSI(NULL, 0, RSIPeriod, RSIPrice, cnt);

      int depth = 2;
      if (MarkFalse)
      {
         depth = 1;
      }
      
      for (int i = cnt + depth - 1; i >= cnt; i--)
      {
         Higher[i] = EMPTY_VALUE;
         Lower[i] = EMPTY_VALUE;
      
      
         Inner[i] = RSI[i];
   

         
         if(RSI[i] >= HighLevel)
         {
            Higher[i] = RSI[i];
            Higher[i + 1] = DoubleIf(RSI[i + 1] >= HighLevel, RSI[i + 1], HighLevel);
         }
         else if(RSI[i + 1] >= HighLevel)
         {
            Higher[i] = HighLevel;
            Higher[i + 1] = RSI[i + 1];
         }   


         if(RSI[i] <= LowLevel)
         {
            Lower[i] = RSI[i];
            Lower[i + 1] = DoubleIf(RSI[i + 1] <= LowLevel, RSI[i + 1], LowLevel);
         }
         else if(RSI[i + 1] <= LowLevel)
         {
            Lower[i] = LowLevel;
            Lower[i + 1] = RSI[i + 1];
         }   
      }
   }
   
   return(0);
}