i-BandsWidth

Download script
Author: Copyright � 2008, Talex
i-BandsWidth

This tool is designed to measure market volatility by calculating the relative "width" of a Bollinger Band—a popular technical analysis indicator. Instead of showing you the price bands themselves, this script provides a simplified numerical value that represents how much the price is stretching or compressing at any given moment.

How the Logic Works

The underlying process follows three main steps to arrive at the final visual result:

1. Measuring Market Variance The script begins by looking at a specific number of recent price candles (defined by the user's period settings). It calculates the average price and then determines how far each individual candle's closing price sits away from that average. By calculating this distance, the tool identifies the "Standard Deviation," which is a mathematical way to express how much the market is swinging compared to its recent norm.

2. Determining the Band Width Once the script knows the deviation, it identifies the upper and lower boundaries of the price movement (the "Bands"). It then subtracts the lower boundary from the upper boundary to find the total width of the price range. To make this data meaningful regardless of whether the asset is expensive (like Gold) or cheap (like a low-priced currency), the tool divides this width by the average price. This creates a percentage-style measurement of volatility.

3. Smoothing the Data Raw volatility data can often be "noisy" and jump around erratically, making it difficult to read clearly on a chart. To solve this, the script performs a final smoothing pass on the calculated widths. It takes the results from the previous step and applies a moving average filter. This rounds off sharp spikes and provides a cleaner, more flowing line that highlights long-term trends in market activity.

What This Means for the User

By using this logic, you can visually identify different market "moods":

  • Expansion: When the line on the chart rises sharply, it indicates that price volatility is increasing (the market is moving aggressively).
  • Contraction: When the line drops toward the bottom of the window, it suggests the market is becoming quiet or consolidating.

Essentially, this script filters out the "noise" of price action to show you a dedicated pulse of market energy.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
i-BandsWidth
//+------------------------------------------------------------------+
//|                                                 i-BandsWidth.mq4 |
//|                                          Copyright © 2008, Talex |
//|                                                 tan@gazinter.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Talex"
#property link      "tan@gazinter.net"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

extern int    BandsPeriod=20;
extern int    BandsShift=0;
extern double BandsDeviations=2.0;
extern int    Slow=3;

double BWBuffer[],Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() {
   
   IndicatorBuffers(2);
   IndicatorShortName("i-BandsWidth ("+BandsPeriod+", "+DoubleToStr(BandsDeviations,1)+", "+Slow+")");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Buffer);
   SetIndexBuffer(1,BWBuffer);

   return;
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() {

   return;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start() {
   int i,j,limit,cbars;
   double sum,oldval,newres,deviation,upband,dwband;
//---------------------------------------------------   
   cbars=IndicatorCounted();
   if(Bars<=BandsPeriod) return;
   if(cbars<0) return;
   if(cbars>0) limit=Bars-cbars-1;
      else limit=Bars-BandsPeriod-1;

   for(i=0;i<=limit;i++) {
      sum=0.0;
      oldval=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
      for(j=0;j<BandsPeriod;j++) {
         newres=Close[i+j]-oldval;
         sum+=newres*newres;
      }
      deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
      upband=oldval+deviation;
      dwband=oldval-deviation;
      if(oldval!=0)
      BWBuffer[i]=(upband-dwband)/oldval;
   }
   for(i=0;i<=limit;i++) {
      Buffer[i]=iMAOnArray(BWBuffer,0,Slow,0,0,i);
   }
   return;
}
//+------------------------------------------------------------------+

Comments