//+------------------------------------------------------------------+
//|                                                          %BB.mq4 |
//|                                    Copyright ? 2008, Walter Choy |
//|                                                                  |
//+------------------------------------------------------------------+
//mod2009fxtsd
#property copyright "Copyright ? 2008, Walter Choy"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Teal
#property indicator_color3 Teal
#property indicator_level1  50
#property indicator_level2 -50

extern int    Bands_period = 20;
extern double Bands_deviation = 2;

//---- buffers
double PercentBB[];
double BuB[];
double BlB[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(0, PercentBB);
   SetIndexBuffer(1, BuB);
   SetIndexBuffer(2, BlB);
   SetIndexBuffer(0, PercentBB);
   SetIndexLabel(0, "%BB");
   SetIndexLabel(1, "uB");
   SetIndexLabel(2, "lB");
   SetIndexDrawBegin(0, Bands_period);
   SetIndexDrawBegin(1, Bands_period);
   SetIndexDrawBegin(2, Bands_period);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   double LB, UB;
   int limit;
   
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars-1;
      
   for(int i=0; i<limit; i++){
      LB = iBands(NULL,0,Bands_period,Bands_deviation,0,PRICE_CLOSE,MODE_LOWER,i);
      UB = iBands(NULL,0,Bands_period,Bands_deviation,0,PRICE_CLOSE,MODE_UPPER,i);

      PercentBB[i] = (iClose(NULL,0,i) - LB)/(UB-LB) * 100-50;
      BuB[i]= iStdDev(NULL,0,Bands_period,0,0,PRICE_CLOSE,i)*10000;
      BlB[i]= -BuB[i];
      
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+