//-----------------------------------------------------------------------------
//                                         Mn Squawk Rsi                      |
//-----------------------------------------------------------------------------

#property copyright "Mn"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Blue 
#property indicator_color2 Blue
#property indicator_color3 Blue 
#property indicator_color4 Blue
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  4
#property indicator_width4  4
#property indicator_color5 Red 
#property indicator_color6 Red
#property indicator_color7 Red
#property indicator_color8 Red
#property indicator_width5  1
#property indicator_width6  1
#property indicator_width7  4
#property indicator_width8  4

extern int mEarliestBar = 1000,
           mRsiPrd      = 14,
           mUpperLvl    = 70,
           mLowerLvl    = 30;

double SigBarLo[], SigBarHi[], SigBarCl[], SigBarOp[],
       SigBarLo2[], SigBarHi2[], SigBarCl2[], SigBarOp2[];

//-----------------------------------------------------------------------------
void init() {
  SetIndexBuffer(0, SigBarLo);
  SetIndexBuffer(1, SigBarHi);
  SetIndexBuffer(2, SigBarCl);
  SetIndexBuffer(3, SigBarOp);
  SetIndexBuffer(4, SigBarLo2);
  SetIndexBuffer(5, SigBarHi2);
  SetIndexBuffer(6, SigBarCl2);
  SetIndexBuffer(7, SigBarOp2);
    
  SetIndexEmptyValue(0, 0);
  SetIndexEmptyValue(1, 0);
  SetIndexEmptyValue(2, 0);
  SetIndexEmptyValue(3, 0);
  
  SetIndexStyle(0, DRAW_HISTOGRAM);
  SetIndexStyle(1, DRAW_HISTOGRAM);
  SetIndexStyle(2, DRAW_HISTOGRAM);
  SetIndexStyle(3, DRAW_HISTOGRAM);
  SetIndexStyle(4, DRAW_HISTOGRAM);
  SetIndexStyle(5, DRAW_HISTOGRAM);
  SetIndexStyle(6, DRAW_HISTOGRAM);
  SetIndexStyle(7, DRAW_HISTOGRAM);

  SetIndexDrawBegin(0, Bars - mEarliestBar);
  SetIndexDrawBegin(1, Bars - mEarliestBar);
  SetIndexDrawBegin(2, Bars - mEarliestBar);
  SetIndexDrawBegin(3, Bars - mEarliestBar);
  SetIndexDrawBegin(4, Bars - mEarliestBar);
  SetIndexDrawBegin(5, Bars - mEarliestBar);
  SetIndexDrawBegin(6, Bars - mEarliestBar);
  SetIndexDrawBegin(7, Bars - mEarliestBar);
  
  return(0);  
}

//-----------------------------------------------------------------------------
void start() 
{
  int mCounted = IndicatorCounted();
  if(mCounted < 0) return (-1); 
  if(mCounted > 0) mCounted--;
  int mLimit = Bars - mCounted;
  for (int i = mLimit; i >= 0; i--) 
   {
     double mRsi1 = iRSI(NULL, 0, mRsiPrd, PRICE_CLOSE, i+1);
     double mRsi2 = iRSI(NULL, 0, mRsiPrd, PRICE_CLOSE, i+2);
     
     if(mRsi1 > mUpperLvl && mRsi2 < mUpperLvl)
      {     
         SigBarLo[i] = High[i]; 
         SigBarHi[i] = Low[i]; 
         SigBarCl[i] = Open[i];
         SigBarOp[i] = Close[i];
      }
     if(mRsi1 < mLowerLvl && mRsi2 > mLowerLvl)
      {     
         SigBarLo2[i] = High[i]; 
         SigBarHi2[i] = Low[i]; 
         SigBarCl2[i] = Open[i];
         SigBarOp2[i] = Close[i];
      }
   }  // for i 

  return(0);  
}

//-----------------------------------------------------------------------------
void deinit()
 {

  return(0);  
 }

//-----------------------------------------------------------------------------


