//+------------------------------------------------------------------+
//|   #3LB_v2.mq4
//+------------------------------------------------------------------+
#property  indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red

extern int RevBars = 3;

double RevUp, RevDn, PrevHi, PrevLo;
int BarCount, Trend;
//double Reversal1, Reversal2, Reversal3;
 
double UpBuffer[];
double DnBuffer[];

double Reversal[];

int init()
{
   IndicatorBuffers(2);

   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, UpBuffer);

   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(1,DnBuffer);

   return(0);
}

int deinit()
{
   return(0);
}

void RollPrices()
{
   int r1;
   for (r1=RevBars-1;r1>0;r1--)
   {
      Reversal[r1] = Reversal[r1-1];
   }
}
   
int start()
{
   int i,j,limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1); //---- check for possible errors
   if(counted_bars>0) counted_bars--; //---- last counted bar will be recounted
   limit=Bars-counted_bars;
//   limit = 50;
   ArrayResize(Reversal,RevBars);
   
   for (i=limit;i>=1;i--)
   {
      if (Close[i] > PrevHi)
      {
         if (Trend < 1)
         {
            Trend = 1;
            BarCount = 0;
         }
         UpBuffer[i] = Close[i];
         DnBuffer[i] = Open[i];
         PrevHi = Close[i];
         BarCount++;
         RollPrices();  
         Reversal[0] = Open[i];
         if (BarCount >= RevBars)
         {PrevLo = Reversal[RevBars-1];}
      }

      if (Close[i] < PrevLo)
      {
         if (Trend > -1)
         {
            Trend = -1;
            BarCount = 0;
         }
         DnBuffer[i] = Open[i];
         UpBuffer[i] = Close[i];
         PrevLo = Close[i];
         BarCount++;
         RollPrices();  
         Reversal[0] = Open[i];
         if (BarCount >= RevBars)
         {PrevHi = Reversal[RevBars-1];}
      }

   }

   Comment("Bars = ",BarCount," Rev up =  ",PrevHi," Rev down = ",PrevLo);

   return(0);
}
//+------------------------------------------------------------------+

