//+------------------------------------------------------------------+
//|                                                        Bands HULL.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ 

///|  MODIFIED BY SOHOCOOL TO DO BOLLINGER BANDS HULL MOVING AVERAGE august 2012
//+------------------------------------------------------------------+
#property copyright "OPEN SOURCE"
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
//---- indicator parameters
extern int    BandsPeriod=18;
extern int PRICE =5;
extern int   MODE_Band =3;
extern int    BandsShift=0;
extern double BandsDeviations=0.50;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double SignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,LowerBuffer);
//----
   SetIndexDrawBegin(0,BandsPeriod+BandsShift);
   SetIndexDrawBegin(1,BandsPeriod+BandsShift);
   SetIndexDrawBegin(2,BandsPeriod+BandsShift);
   
   SetIndexBuffer(3,SignalBuffer);
   
   IndicatorBuffers(4);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation;
   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;
         UpperBuffer[Bars-i]=EMPTY_VALUE;
         LowerBuffer[Bars-i]=EMPTY_VALUE;
        }
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      SignalBuffer[i]= iMA(NULL,0,MathFloor(BandsPeriod/2),BandsShift,MODE_Band,PRICE,i)*2-iMA(NULL,0,BandsPeriod,BandsShift,MODE_Band,PRICE,i);
     for(i=0; i<limit; i++)  
      MovingBuffer[i]=iMAOnArray(SignalBuffer,Bars,MathFloor(MathSqrt(BandsPeriod)),BandsShift,MODE_Band,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= iMA(NULL,0,1,0,0,PRICE,k)-oldval;           //Close[k]-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
      UpperBuffer[i]=oldval+deviation;
      LowerBuffer[i]=oldval-deviation;
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+