//+------------------------------------------------------------------+
//|                                                  OnChart Rsi.mq4 |
//|                                                           mladen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DarkSlateGray
#property indicator_color2 DarkSlateGray
#property indicator_color3 DarkSlateGray
#property indicator_color4 DarkOrange
#property indicator_width4 2
#property indicator_style1 STYLE_DOT

//---- parameters
//
//
//
//

extern int RSIPeriod    = 14;
extern int RSIPriceType =  0;
extern int MinMaxPeriod = 20;
extern int overBought   = 70;
extern int overSold     = 30;
extern string timeFrame = "Current time frame";

//---- buffers
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
int    TimeFrame;
int    atrTimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);


   TimeFrame = stringToTimeFrame(timeFrame);
   atrTimeFrame=PERIOD_D1;
   if (TimeFrame >= atrTimeFrame)
      switch (TimeFrame)
         {
            case PERIOD_D1: atrTimeFrame = PERIOD_W1; break;
            default:        atrTimeFrame = PERIOD_MN1;
         }
   return(0);
}
int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit=MathMin(MathMax(Bars-counted_bars,TimeFrame/Period()),Bars-1);   
      
   //
   //
   //
   //
   
      for (int i=limit; i>=0; i--)
         {
            int y = iBarShift(NULL,TimeFrame,Time[i]);
               double rsiValue = iRSI(NULL,TimeFrame,RSIPeriod,RSIPriceType,y);
               double max      = iHigh(NULL,TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH,MinMaxPeriod,y));
               double min      = iLow (NULL,TimeFrame,iLowest (NULL,TimeFrame,MODE_LOW ,MinMaxPeriod,y));
               double rng      = max-min;
                  ExtMapBuffer1[i] = (min+max)/2;
                  ExtMapBuffer2[i] = min+rng*overBought/100;
                  ExtMapBuffer3[i] = min+rng*overSold  /100;
                  ExtMapBuffer4[i] = min+rng*rsiValue  /100;
         }
   return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      tchar;
   
   while(lenght >= 0)
      {
         tchar = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                  s = StringSetChar(s, lenght, tchar - 32);
          else 
              if(tchar > -33 && tchar < 0)
                  s = StringSetChar(s, lenght, tchar + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}