//+------------------------------------------------------------------+
//|                                                        Bands.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Blue
#property indicator_color4 Blue
#property indicator_color5 Blue
//---- indicator parameters
extern int    ATR=48;
extern int    EMA_Period=216;
extern double space=0.7;
extern double BandsDeviations=4.0;
       int    BandsShift=0;
//---- buffers
double MovingBuffer[];
double UpperBufferA[];
double LowerBufferA[];
double UpperBufferB[];
double LowerBufferB[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,UpperBufferA);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,LowerBufferA);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,UpperBufferB);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,LowerBufferB);
//----
   SetIndexDrawBegin(0,EMA_Period+BandsShift);
   SetIndexDrawBegin(1,EMA_Period+BandsShift);
   SetIndexDrawBegin(2,EMA_Period+BandsShift);
   SetIndexDrawBegin(3,EMA_Period+BandsShift);
   SetIndexDrawBegin(4,EMA_Period+BandsShift);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviationA,deviationB;
   double sum,oldval,newres;
//----
   if(Bars<=EMA_Period) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=EMA_Period;i++)
        {
         MovingBuffer[Bars-i]=EMPTY_VALUE;
         UpperBufferA[Bars-i]=EMPTY_VALUE;
         LowerBufferA[Bars-i]=EMPTY_VALUE;
         UpperBufferB[Bars-i]=EMPTY_VALUE;
         LowerBufferB[Bars-i]=EMPTY_VALUE;
        }
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      MovingBuffer[i]=iMA(NULL,0,EMA_Period,BandsShift,MODE_EMA,PRICE_CLOSE,i);
//----
   i=Bars-EMA_Period+1;
   if(counted_bars>EMA_Period-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      oldval=MovingBuffer[i];
      deviationA=iATR(NULL,0,ATR,i)*BandsDeviations;
      deviationB=iATR(NULL,0,ATR,i)*BandsDeviations*space;
      UpperBufferA[i]=oldval+deviationA;
      LowerBufferA[i]=oldval-deviationA;
      UpperBufferB[i]=oldval+deviationB;
      LowerBufferB[i]=oldval-deviationB;
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+