BBandWidthRatio_v3
Download scriptOverview of the BBandWidthRatio Indicator
This technical tool is designed to measure and visualize the relative "expansion" or "contraction" of market volatility. Instead of simply showing you the price lines, it focuses on calculating a specific ratio that helps traders understand how wide the current trading range is compared to the average price.
How the Logic Works
To produce its results, the script performs a multi-step calculation based on standard market statistics:
1. Establishing the Baseline The tool first looks at a set number of recent price candles (defined by the "Period" setting). It calculates a Simple Moving Average (SMA), which represents the "fair value" or the center point of the price action over that timeframe.
2. Measuring Volatility (Standard Deviation) Beyond just finding the average, the script calculates how much the price is currently swinging away from that average. It determines the "Standard Deviation"—a mathematical measure of how far prices are straying from the norm. This tells the tool whether the market is moving calmly or experiencing large, aggressive swings.
3. Defining the Range The script uses these two pieces of information (the average and the deviation) to create a theoretical "boundary" for the price. By multiplying the deviation by a specific factor (the "Deviation" setting), it identifies the total width of the market's current reach.
4. The Final Ratio Calculation This is where the unique logic of this specific script happens. Rather than showing you the boundaries themselves, it calculates a Ratio. It takes the total width of that volatility range and divides it by the average price.
What This Means for the User
The final output you see on your chart is a single line representing this ratio. Here is how to interpret the logic:
- High Values: When the line moves upward, it indicates that the market's volatility (the "stretch" of the price) is becoming large relative to its average price. This suggests a period of high activity or significant expansion.
- Low Values: When the line moves downward, it suggests that the price is staying very close to its average. This indicates a "squeezed" market with low volatility and less aggressive movement.
By following this ratio, you can visually track whether the market is currently in a state of expansion or contraction, helping you identify periods of high-intensity price action versus calmer, more stable trading environments.
//+------------------------------------------------------------------+
//| 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[];
//+------------------------------------------------------------------+
//| 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