WelchBBWidth
Download scriptThis script is designed to analyze market behavior by identifying whether a financial instrument is currently "ranging" (moving sideways in a tight pattern) or trending significantly. It achieves this by measuring the volatility and physical space between price boundaries over specific timeframes.
Core Mechanics
The logic operates through three primary stages:
1. Volatility Mapping (Bollinger Bands) The script first establishes a baseline of market movement using a standard moving average. It then calculates "Bands" above and below this average based on the standard deviation of price action. Essentially, it creates a visual "container" for the price; the wider this container is, the higher the current volatility.
2. Dynamic Width Analysis
Rather than just looking at how wide the bands are at any single moment, the script performs a historical comparison. It looks back over a user-defined period (the WidthCalcPeriod) to find the maximum and minimum width the bands have reached during that time.
By comparing the current width of the bands against these historical highs and lows, the script determines where the market currently sits on the spectrum between "extreme volatility" and "extreme stability."
3. Market State Classification The final step is the classification logic, which categorizes the market into three distinct states based on the "closeness" of the current price action to its historical minimum width:
- Ranging: If the current market width is very close to the historical minimum (falling within a specific percentage), the script identifies this as a "rangebound" market. This suggests low volatility and sideways movement.
- Transitionary (Start/End of Range): If the market is slightly wider than the absolute minimum but still relatively narrow, it flags this as a potential transition zone where a range may be starting to break or end.
- Not Ranging: If the width significantly exceeds the narrow parameters, the script identifies this as a trending or high-volatility state.
Visual Representation
The tool translates these mathematical findings into a histogram format. Instead of complex lines, it provides a clear visual signal (colored bars) that allows traders to see at a glance whether the market is currently behaving in a "squishy" (rangebound) manner or if it has broken out into a more active trend.
//+------------------------------------------------------------------+
//| WelchHMABB.mq4 |
//| Copyright © 2011, Timothy Welch |
//| http://www.timothy-welch.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Timothy Welch"
#property link "http://www.timothy-welch.com"
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_width1 4
#property indicator_color2 Yellow
#property indicator_width2 4
#property indicator_color3 Red
#property indicator_width3 4
#property indicator_color4 DodgerBlue
#property indicator_width4 2
#property indicator_minimum 0
#property indicator_maximum 300
//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=2.0;
extern int MinRangePercent=20;
extern string Note1="How far back to look for Max/Min width";
extern int WidthCalcPeriod=100;
extern string Note2="If TRUE, shows the the Width on a line chart";
extern bool ShowWidthLine=false;
//---- buffers
double devBuffer[];
double squishyBuffer1[];
double squishyBuffer2[];
double squishyBuffer3[];
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double dVal=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(7);
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,squishyBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(1,squishyBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexBuffer(2,squishyBuffer3);
if (ShowWidthLine) {
SetIndexStyle(3,DRAW_LINE);
} else {
SetIndexStyle(3,DRAW_NONE);
}
SetIndexBuffer(3,devBuffer);
SetIndexBuffer(4,MovingBuffer);
SetIndexBuffer(5,UpperBuffer);
SetIndexBuffer(6,LowerBuffer);
//----
SetIndexDrawBegin(0,BandsPeriod+BandsShift);
SetIndexDrawBegin(1,BandsPeriod+BandsShift);
SetIndexDrawBegin(2,BandsPeriod+BandsShift);
//----
// If we have 5 digits, or 4 digits and the symbol is a JPY currency, then dVal is 10.
if ( Digits==5 || (Digits==4 && StringFind(Symbol(),"JPY",0)>0) ) { dVal = 10; }
if (BandsPeriod==0) { BandsPeriod=20; }
if (MinRangePercent==0) { MinRangePercent=10; }
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;
}
//----
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;
devBuffer[i]=MathAbs(UpperBuffer[i]-LowerBuffer[i])/Point/dVal;
int tMaxIdx = ArrayMaximum(devBuffer, WidthCalcPeriod, i);
int tMinIdx = ArrayMinimum(devBuffer, WidthCalcPeriod, i);
// This will give us the percentage of closeness compared to each of tMin and tMax.
// If we are < MinRangePercent away from the tMin value, we are considered in a rangebound market.
// i.e. tMin=30, tMax=150, devBuffer[0]=42. In that case, tLow=10, tHigh=90. tLow <15% from tMin
double tLow = (MathAbs(devBuffer[tMinIdx]-devBuffer[i])/MathAbs(devBuffer[tMinIdx]-devBuffer[tMaxIdx])) * 100;
double tHigh = (MathAbs(devBuffer[tMaxIdx]-devBuffer[i])/MathAbs(devBuffer[tMinIdx]-devBuffer[tMaxIdx])) * 100;
if (tLow<tHigh && tLow<=MinRangePercent) {
squishyBuffer1[i]=300; // Ranging
} else if (tLow<tHigh && tLow<=(MinRangePercent*2)) {
squishyBuffer2[i]=300; // Start/End of Range
} else {
squishyBuffer3[i]=300; // Not ranging
}
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments