BBandWidthRatio

Download script
Author: Maji
BBandWidthRatio

This tool is designed to measure and visualize the relative "stretch" or volatility of price action by calculating a specific ratio based on Bollinger Bands. Instead of showing you the standard bands themselves, it focuses on the relationship between the volatility of the market and its average price level.

How the Logic Works

1. Establishing the Foundation (Moving Average) The script begins by looking at a set number of previous price candles (the "Period"). It calculates a Simple Moving Average (SMA), which represents the "baseline" or equilibrium price of the asset over that timeframe.

2. Measuring Volatility (Standard Deviation) Simultaneously, the script looks at how much the price is fluctuating away from that baseline. It calculates the "Standard Deviation," which is a mathematical way to determine if the market is currently calm or experiencing wide, aggressive swings. By multiplying this deviation by a specific factor (the "Deviation" setting), it determines the boundaries of the current price movement.

3. Calculating the Ratio The core logic of this script lies in its final calculation. Rather than simply plotting the width of the bands, it creates a ratio. It takes the total width of the volatility (the distance between the upper and lower boundaries) and divides it by the baseline average price.

What This Means for the User

Because the script produces a ratio, it helps normalize the data. In many markets, a "wide" price move might simply be because the asset has a high face value (like a stock worth $500 vs a penny stock). By dividing the volatility by the moving average, this script filters out that "size" factor.

The resulting line on your chart represents the intensity of the price movement relative to its current value. When the line is high, it indicates that the market is expanding significantly compared to its average; when the line is low, it suggests the market is consolidating or moving in a very tight, stable range.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
BBandWidthRatio
//+------------------------------------------------------------------+
//|                                              BBandWidthRatio.mq4 |
//|                                                             Maji |
//+------------------------------------------------------------------+
#property copyright "Maji"
#property link      "None"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue

//---- input parameters
extern int       BB_Period=20;
extern double    Deviation=2.0;

double buf1[];
//double buf2[];
//double buf3[];
//double buf4[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0, DRAW_LINE, EMPTY, 2);
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
   SetIndexDrawBegin(0,BB_Period);
   SetIndexLabel(0,"BBandWidthRatio");
   SetIndexBuffer(0, buf1);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i, j;
   double ave, sko, sum;
   int counted_bars=IndicatorCounted();
   double MA, Up, Dn;
   
   if(Bars<=BB_Period) return(0);
      
   i=Bars-BB_Period;
   if(counted_bars>BB_Period) i=Bars-counted_bars-1;
   

   if (Bars<=BB_Period) return;
   
   for (i=Bars-BB_Period; i>=0; i--) 
   {
//    buf2[i] = iMA(NULL,0,BB_Period,0,MODE_SMA,PRICE_CLOSE,i);
    MA = iMA(NULL,0,BB_Period,0,MODE_SMA,PRICE_CLOSE,i);
    sum = 0;
    for (j=0; j<BB_Period; j++) sum+=Close[i+j];
    ave = sum / BB_Period;
    sum = 0;
    for (j=0; j<BB_Period; j++) sum+=(Close[i+j]-ave)*(Close[i+j]-ave);
    sko = MathSqrt(sum / BB_Period);
//    buf4[i] = buf2[i]+(Deviation*sko);
//    buf3[i] = buf2[i]-(Deviation*sko);
    Up = MA+(Deviation*sko);
    Dn = MA-(Deviation*sko);
//    buf1[i] = 2*(Deviation*sko)/buf2[i];
    buf1[i] = 2*(Deviation*sko)/MA;
  
   }
  
    return(0);
  
  }
//+------------------------------------------------------------------+

Comments