//+------------------------------------------------------------------+
//|                                          !Retracement Finder.mq4 |
//|                                  Copyright © 2011, John Wustrack |
//|                                        john_wustrack@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, John Wustrack"
#property link      "john_wustrack@hotmail.com"

#property indicator_separate_window

#property indicator_buffers 3
#property indicator_color1 LightGray
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2

extern int xi_Period = 8;
extern double xd_Level = 50;

double IB_Wait[];
double IB_Bull[];
double IB_Bear[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 

   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(0,IB_Wait);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(1,IB_Bull);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(2,IB_Bear);
   IndicatorDigits(Digits-1);
   IndicatorShortName("Retracement Finder ("+xi_Period+")");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   int    counted_bars=IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars-counted_bars;
   double ld_CCI;
   
   for (int i=limit; i>=0; i--)
      {
      IB_Wait[i]=EMPTY_VALUE;
      IB_Bull[i]=EMPTY_VALUE;
      IB_Bear[i]=EMPTY_VALUE;
      
      ld_CCI = iCCI(NULL,0,xi_Period,PRICE_TYPICAL,i);
      if (ld_CCI>=xd_Level)
         IB_Bull[i]=ld_CCI;
      else
         if (ld_CCI<=xd_Level*(-1))
            IB_Bear[i]=ld_CCI;
         else
            IB_Wait[i]=ld_CCI;
      }
      
//----
   return(0);
  }
//+------------------------------------------------------------------+