//+------------------------------------------------------------------+
//|                                                      3 Bands.mq4 |
//|                     Copyright © 2005, forexmargin Software Corp. |
//|                                      http://www.forexmargin.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, forexmargin Software Corp."
#property link      "http://www.forexmargin.net/"

#property indicator_chart_window
#property indicator_buffers 7
/*#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue
#property indicator_color4 DeepSkyBlue
#property indicator_color5 DeepSkyBlue
#property indicator_color6 DeepSkyBlue
#property indicator_color7 DeepSkyBlue
*/
//---- indicator parameters
extern int    BandsPeriod=120;
extern int    BandsShift=0;
extern double BandsDeviations1=1.0;
extern double BandsDeviations2=2.0;
extern double BandsDeviations3=3.0;
extern color  BandColor=DeepSkyBlue;
//---- buffers
double MovingBuffer[];

double UpperBuffer1[];
double UpperBuffer2[];
double UpperBuffer3[];

double LowerBuffer1[];
double LowerBuffer2[];
double LowerBuffer3[];

int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,3,2,BandColor);
   SetIndexBuffer(0,MovingBuffer);
   
   SetIndexStyle(1,DRAW_LINE,2,0,BandColor);
   SetIndexBuffer(1,UpperBuffer1);
   
   SetIndexStyle(2,DRAW_LINE,2,0,BandColor);
   SetIndexBuffer(2,LowerBuffer1);
   
   SetIndexStyle(3,DRAW_LINE,1,0,BandColor);
   SetIndexBuffer(3,UpperBuffer2);
   
   SetIndexStyle(4,DRAW_LINE,1,0,BandColor);
   SetIndexBuffer(4,LowerBuffer2);
   
   SetIndexStyle(5,DRAW_LINE,4,0,BandColor);
   SetIndexBuffer(5,UpperBuffer3);
   
   SetIndexStyle(6,DRAW_LINE,4,0,BandColor);
   SetIndexBuffer(6,LowerBuffer3);

//----
   SetIndexDrawBegin(0,BandsPeriod+BandsShift);
   SetIndexDrawBegin(1,BandsPeriod+BandsShift);
   SetIndexDrawBegin(2,BandsPeriod+BandsShift);
   SetIndexDrawBegin(3,BandsPeriod+BandsShift);
   SetIndexDrawBegin(4,BandsPeriod+BandsShift);
   SetIndexDrawBegin(5,BandsPeriod+BandsShift);
   SetIndexDrawBegin(6,BandsPeriod+BandsShift);
//----
   return(0);
  }

int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation1,deviation2,deviation3;
   double sum,oldval,newres;

   if(Bars<=BandsPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=BandsPeriod;i++)
        {
         MovingBuffer[Bars-i]=EMPTY_VALUE;
         UpperBuffer1[Bars-i]=EMPTY_VALUE;
         LowerBuffer1[Bars-i]=EMPTY_VALUE;
         UpperBuffer2[Bars-i]=EMPTY_VALUE;
         LowerBuffer2[Bars-i]=EMPTY_VALUE;
         UpperBuffer3[Bars-i]=EMPTY_VALUE;
         LowerBuffer3[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,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
//----
   i=Bars-BandsPeriod+1;
   if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      sum=0.0;
      k=i+BandsPeriod-1;
      oldval=MovingBuffer[i];
      while(k>=i)
        {
         newres=Close[k]-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation1=BandsDeviations1*MathSqrt(sum/BandsPeriod);
      deviation2=BandsDeviations2*MathSqrt(sum/BandsPeriod);
      deviation3=BandsDeviations3*MathSqrt(sum/BandsPeriod);      
      
      UpperBuffer1[i]=oldval+deviation1;
      LowerBuffer1[i]=oldval-deviation1;

      UpperBuffer2[i]=oldval+deviation2;
      LowerBuffer2[i]=oldval-deviation2;

      UpperBuffer3[i]=oldval+deviation3;
      LowerBuffer3[i]=oldval-deviation3;
      
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+