//+------------------------------------------------------------------+
//|                                                trading hours.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
#property indicator_color2 Green
#property indicator_color3 Red

#property indicator_maximum 90
#property indicator_minimum 10

#property indicator_level1 70
#property indicator_level2 50
#property indicator_level3 30
#property indicator_levelcolor DarkGray


extern int RSIPeriod=14;

//--- buffers
double buf_RSI[];
double buf_buy[];
double buf_sell[];
double buf_pos[];
double buf_neg[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(5);
   string short_name = "RSI("+RSIPeriod+")";
//---- indicators
   SetIndexBuffer(0,buf_RSI);
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexLabel(0,short_name);
   SetIndexBuffer(1,buf_buy);
   SetIndexStyle(1,DRAW_ARROW,0,0);
   SetIndexLabel(1,NULL);
   SetIndexBuffer(2,buf_sell);
   SetIndexStyle(2,DRAW_ARROW,0,0);
   SetIndexLabel(2,NULL);
   SetIndexBuffer(3,buf_pos);
 //  SetIndexBuffer(3,DRAW_NONE);
 //  SetIndexLabel(3,NULL);
   SetIndexBuffer(4,buf_neg);
 //  SetIndexBuffer(4,DRAW_NONE);
 //  SetIndexLabel(4,NULL);
   
   IndicatorShortName(short_name);
   IndicatorDigits(Digits);
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   int    i,counted_bars=IndicatorCounted();
   double rel,negative,positive;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++)
         {
         buf_RSI[Bars-i]=0.0;
         }
//----
   i=Bars-RSIPeriod-1;
   if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=Close[k]-Close[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=Close[i]-Close[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(buf_pos[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(buf_neg[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
        }
      buf_pos[i]=positive;
      buf_neg[i]=negative;
      if(negative==0.0) buf_RSI[i]=0.0;
      else buf_RSI[i]=100.0-100.0/(1+positive/negative);
      i--;
     }
     
   i=Bars-RSIPeriod-2;
   while(i>=0)
      {
      if(buf_RSI[i+1]<70 && buf_RSI[i] >= 70)
         {
         buf_sell[i] = buf_RSI[i];
         }
         
      if(buf_RSI[i+1]>30 && buf_RSI[i] <= 30)
         {
         buf_buy[i] = buf_RSI[i];
         }
      i--;
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+