BandsBandwidth2
Download scriptThis tool is designed to provide traders with a visual and numerical representation of market volatility by calculating the "width" of Bollinger Bands in real-time. Instead of just showing you where price might be heading, it helps you measure how much the market is stretching or compressing at any given moment.
How the Logic Works
The script operates by processing price data through three distinct layers of calculation:
1. Establishing the Baseline (Moving Average) First, the tool calculates a Simple Moving Average (SMA). This acts as the "center" of your price action over a specific number of bars (defined by your period settings). It smooths out minor price fluctuations to show you the general trend.
2. Measuring the Variance (Standard Deviation) Once the baseline is established, the script looks at how far the current market prices are drifting away from that average. It calculates the "Standard Deviation"—a mathematical way of expressing how volatile the price is. Based on your desired deviation setting, it creates an upper boundary and a lower boundary around the moving average.
3. Calculating the Bandwidth This is the core logic unique to this script. While standard Bollinger Bands show you the "tunnel" that price stays within, this tool specifically calculates the distance between the top and bottom boundaries.
What This Means for the User
- Volatility Tracking: When the "Bandwidth" value is high, it indicates that the price is swinging widely, suggesting a period of high activity or intense market emotion.
- Squeeze Identification: When the "Bandwidth" value drops significantly (the bands get tight), it signals a "squeeze." This often suggests that the market is becoming quiet and may be preparing for a breakout in either direction.
- Real-Time Display: The script automatically updates a text label on your chart, giving you a constant numerical readout of the current bandwidth. This allows you to see exactly how much the market is expanding or contracting without having to manually measure the distance between lines on your screen.
In short, this tool takes complex volatility mathematics and simplifies it into a single, easy-to-read metric that tells you how "stretched" or "compressed" the current market environment is.
//+------------------------------------------------------------------+
//| BandsBandwidth2.mq4 |
//| Copyright © 2009, Investors Haven |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Investors Haven"
#property link ""
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Purple
#property indicator_color3 Purple
#property indicator_width1 2
#property indicator_width2 3
#property indicator_width3 3
//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=2.0;
extern color TextColor = Purple;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double Bandwidth[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
// IndicatorBuffers(1);
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MovingBuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpperBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,LowerBuffer);
SetIndexStyle(3, 12);
SetIndexBuffer(3,Bandwidth);
//----
SetIndexDrawBegin(0,BandsPeriod+BandsShift);
SetIndexDrawBegin(1,BandsPeriod+BandsShift);
SetIndexDrawBegin(2,BandsPeriod+BandsShift);
SetIndexDrawBegin(3,BandsPeriod+BandsShift);
ObjectCreate( "BollingerBandwidth", OBJ_LABEL, 0,0,0,0,0,0,0);
ObjectSet( "BollingerBandwidth", OBJPROP_CORNER, 0);
ObjectSet( "BollingerBandwidth", OBJPROP_XDISTANCE, 5);
ObjectSet( "BollingerBandwidth", OBJPROP_YDISTANCE, 15);
ObjectSetText( "BollingerBandwidth", "", 12, "Arial", TextColor);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ObjectDelete("BollingerBandwidth");
//----
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;
Bandwidth[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,MODE_SMA,PRICE_CLOSE,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=BandsDeviations*MathSqrt(sum/BandsPeriod);
UpperBuffer[i]=oldval+deviation;
LowerBuffer[i]=oldval-deviation;
Bandwidth[i]=MathFloor((UpperBuffer[i] - LowerBuffer[i])*10000);
i--;
}
string BollText = StringConcatenate("Bollinger Bandwidth: ", Bandwidth[0]);
ObjectSetText("BollingerBandwidth", BollText, 12, "Arial", TextColor );
//----
return(0);
}
//+------------------------------------------------------------------+
Comments