//+------------------------------------------------------------------+
//|                                                  ATRbandsV02.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Mark Bass"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 DarkGreen
#property indicator_color2 DarkGreen
#property indicator_color3 Yellow
#property indicator_color4 DarkGreen
#property indicator_color5 DarkGreen

//---- input parameters
extern int       ATRtf     = 0;
extern int       ATRper    = 14;
extern int       ATRshift  = 0;
extern double    ATRband   = 1.0;
extern bool      showMidBand = false;

//---- buffers
double upperBand1[], upperBand2[];
double midBand[];
double lowerBand1[], lowerBand2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0, upperBand2);   
   SetIndexLabel (0, ATRper + " ATR band 2");   
   SetIndexStyle (0, DRAW_LINE, STYLE_DOT, 1);

   SetIndexBuffer(1, upperBand1);   
   SetIndexLabel (1, ATRper + " ATR band 1");   
   SetIndexStyle (1, DRAW_LINE, STYLE_DOT, 1);

   SetIndexBuffer(2, midBand);   
   SetIndexLabel (2, ATRper + " ATR mid band");   
   SetIndexStyle (2, DRAW_LINE, STYLE_DOT, 1);

   SetIndexBuffer(3, lowerBand1);   
   SetIndexLabel (3, ATRper + " ATR band -1");   
   SetIndexStyle (3, DRAW_LINE, STYLE_DOT, 1);

   SetIndexBuffer(4, lowerBand2);   
   SetIndexLabel (4, ATRper + " ATR band -2");   
   SetIndexStyle (4, DRAW_LINE, STYLE_DOT, 1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars = IndicatorCounted();
   int    limit        = Bars - counted_bars;
   int    i = 0;
   
//----Check for possible errors
   if ( counted_bars < 0 ) return ( -1 );
      
//----Last counted bar will be recounted
   if ( counted_bars > 0 ) counted_bars-- ;
   
   double ATR =0.00;
   double theTypical;
   
//---Main calculation Loop
   for (i = 0; i < limit; i++ )
      
       { 
         theTypical = iMA(NULL,ATRtf,1,0,0,PRICE_TYPICAL,i);
         ATR = iATR(NULL, ATRtf, ATRper, i+ATRshift);
              
         upperBand2[i] = theTypical + (ATR * ATRband *2);
         upperBand1[i] = theTypical + ATR * ATRband;
         if (showMidBand) midBand[i] = theTypical;
         lowerBand1[i] = theTypical - ATR * ATRband;
         lowerBand2[i] = theTypical - (ATR * ATRband *2);
       }                         

   
   return(0);
  }
//+------------------------------------------------------------------+