//+------------------------------------------------------------------+
//|                          Bollinger bands multiple deviations.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 LimeGreen
#property indicator_color2 DimGray
#property indicator_color3 Orange
#property indicator_color4 DeepSkyBlue
#property indicator_style2 STYLE_DOT
#property indicator_width4 2

//
//
//
//
//

extern int    MaPeriod       =   100;
extern int    MaPrice        = PRICE_CLOSE;
extern int    MaMethod       = MODE_SMA;
extern int    BandsLength    =   200;
extern double BandsDeviation =     2; 

//
//
//
//
//

double MaBuffer1[];
double UpperBand1[];
double LowerBand1[];
double MaBuffer2[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,UpperBand1);
   SetIndexBuffer(1,MaBuffer1);
   SetIndexBuffer(2,LowerBand1);
   SetIndexBuffer(3,MaBuffer2);
   return(0);
}
int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         
   //
   //
   //
   //
   //

   for (int i=limit;i>=0;i--) MaBuffer2[i] = iMA(NULL,0,MaPeriod,0,MaMethod,MaPrice,i);
   for (    i=limit;i>=0;i--) MaBuffer1[i] = iMAOnArray(MaBuffer2,0,BandsLength,0,MODE_SMA,i);
   for (    i=limit;i>=0;i--)
      {
         double deviation   = iStdDevOnArray(MaBuffer2,0,BandsLength,0,MODE_SMA,i);
                UpperBand1[i] = MaBuffer1[i]+BandsDeviation*deviation;
                LowerBand1[i] = MaBuffer1[i]-BandsDeviation*deviation;
      }               
   return(0);
}