//+-------------------------------------------------------------------+
//|                                                    Bandswidth.mq4 |
//|                                         by Linuxser for Forex TSD |
//|                                                                   |
//| John Bollinger original formula is:                               |
//| (Upper BB - Lower BB)/middle BB                                   |
//+-------------------------------------------------------------------+
#property copyright "www.ForexGuideline.com"
#property link      "https://www.forexguideline.com/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  clrYellow
#property indicator_color2  clrCornflowerBlue


//---- input parameters
extern int            BBPeriod     = 20;
extern int            StdDeviation = 2;
extern int            MaPeriod     = 10;
extern ENUM_MA_METHOD MaMode       = MODE_EMA;
//---- buffers
double BLGBuffer[],bwMa[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0,BLGBuffer); SetIndexStyle(0,DRAW_LINE);  SetIndexLabel(0,"Bandswidth("+BBPeriod+","+StdDeviation+")");
   SetIndexBuffer(1,bwMa);      SetIndexStyle(1,DRAW_LINE);
   
   IndicatorShortName("Bandswidth("+BBPeriod+","+StdDeviation+")");
return(0);
}
//+------------------------------------------------------------------+
//| Momentum                                                         |
//+------------------------------------------------------------------+
int start()
{
  int i,counted_bars = IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            int limit=MathMin(Bars-counted_bars,Bars-1);
            
     for (i=limit;i>=0;i--)
     { 
        double ub = iBands(NULL,0,BBPeriod,StdDeviation,0,PRICE_CLOSE,MODE_UPPER,i); 
        double lb = iBands(NULL,0,BBPeriod,StdDeviation,0,PRICE_CLOSE,MODE_LOWER,i);
        double cl = iMA(NULL,0,BBPeriod,0,MODE_SMA,PRICE_CLOSE,i);
        if (ub!=lb)
             BLGBuffer[i] = (ub-lb)/cl;
        else BLGBuffer[i] = 0; 
     }
     for (i=limit;i>=0;i--) bwMa[i] = iMAOnArray(BLGBuffer,0,MaPeriod,0,MaMode,i);
return(0);
}
//+------------------------------------------------------------------+