//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property copyright "mladen"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  LimeGreen
#property indicator_color2  Orange
#property indicator_color3  Orange
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property strict

//
//
//
//
//

extern int                RsiPeriod = 14;           // Rsi period
extern ENUM_APPLIED_PRICE RsiPrice  = PRICE_CLOSE;  // Rsi price

//
//
//
//
//

double rsi[];
double rsida[];
double rsidb[];
double slope[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,rsi);
   SetIndexBuffer(1,rsida);
   SetIndexBuffer(2,rsidb);
   SetIndexBuffer(3,slope);
   
   //
   //
   //
   //
   //
   
   IndicatorShortName("RSI ("+(string)RsiPeriod+")");
   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(Bars-counted_bars,Bars-2);

   //
   //
   //
   //
   //
   
   if (slope[limit]==-1) CleanPoint(limit,rsida,rsidb);
   for(int i=limit; i>=0; i--)
   {
      double trsi = iRSI(NULL,0,RsiPeriod,RsiPrice,i);
      if (High[i]>=High[i+1] && Low[i]<=Low[i+1]) 
            rsi[i] = rsi[i+1];
      else  rsi[i] = trsi;
      rsida[i] = EMPTY_VALUE;
      rsidb[i] = EMPTY_VALUE;
      slope[i] = slope[i+1];
         if (rsi[i]>rsi[i+1]) slope[i] =  1;
         if (rsi[i]<rsi[i+1]) slope[i] = -1;
         if (slope[i]==-1) PlotPoint(i,rsida,rsidb,rsi);
   }      
   return(0);
}
  

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i];  first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] =  from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                           second[i] = EMPTY_VALUE; }
}
