//+------------------------------------------------------------------+
//|                                                      Bands Q.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 Yellow
#property indicator_color3 Yellow
#property indicator_color4 Red
#property indicator_color5 Red

extern double BandsDeviations1 = 1.0;
extern double BandsDeviations2 = 2.0;
extern int    Price_Type      = 6;

int    BandsShift = 0;
string text       = "";
int    MA_Mode    = MODE_SMA;
double BandsPeriod;
double MovingBuffer[];

double UpperBuffer[];
double LowerBuffer[];
double UpperBuffer1[];
double LowerBuffer1[];

string short_name;
string short_name1;
string short_name2;

int init()
  {
   IndicatorBuffers(10);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle (1,DRAW_LINE);
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle (2,DRAW_LINE);
   SetIndexBuffer(2,LowerBuffer);
   SetIndexStyle (3,DRAW_LINE,2,0);
   SetIndexBuffer(3,UpperBuffer1);
   SetIndexStyle (4,DRAW_LINE,2,0);
   SetIndexBuffer(4,LowerBuffer1);

   SetIndexDrawBegin(0,BandsPeriod+BandsShift);
   SetIndexDrawBegin(1,BandsPeriod+BandsShift);
   SetIndexDrawBegin(2,BandsPeriod+BandsShift);
   SetIndexDrawBegin(3,BandsPeriod+BandsShift);
   SetIndexDrawBegin(4,BandsPeriod+BandsShift);

   short_name ="Period ("+BandsPeriod+")";
   short_name1="Std dev ("+BandsDeviations2+")";
   short_name2="Std dev ("+BandsDeviations2+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,short_name1);   
   SetIndexLabel(2,short_name2);
   return(0);
  }

int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation,deviation1;
   double sum,oldval,newres;
   
   if(Period()==PERIOD_M15) {BandsPeriod = 96; text="1 Hari";}
   if(Period()==PERIOD_M30) {BandsPeriod = 48; text="1 Hari";}
   if(Period()==PERIOD_H1)  {BandsPeriod = 24; text="1 Hari";}
   if(Period()==PERIOD_H4)  {BandsPeriod = 30; text="1 Minggu";}
   if(Period()==PERIOD_D1)  {BandsPeriod = 20; text="1 Bulan";}
   if(Period()==PERIOD_W1)  {BandsPeriod = 24; text="6 Bulan";}   
      
   if(Bars<=BandsPeriod) return(0);

   if(counted_bars<1)
      for(i=1;i<=BandsPeriod;i++)
        {
         MovingBuffer[Bars-i]=EMPTY_VALUE;
         UpperBuffer[Bars-i]=EMPTY_VALUE;
         LowerBuffer[Bars-i]=EMPTY_VALUE;
         UpperBuffer1[Bars-i]=EMPTY_VALUE;
         LowerBuffer1[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,MA_Mode,Price_Type,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--;
        }
      deviation=BandsDeviations2*MathSqrt(sum/BandsPeriod);
      UpperBuffer[i]=oldval+deviation;
      LowerBuffer[i]=oldval-deviation;

      deviation1=BandsDeviations1*MathSqrt(sum/BandsPeriod);
      UpperBuffer1[i]=oldval+deviation1;
      LowerBuffer1[i]=oldval-deviation1;

      i--;
     }
   Comment("BB ",text
           );
   return(0);
  }

