I personally love involving bollinger bands into my studies, and I've found bollinger band width intervals to be a great method of staying away from poor trades. I've seen several examples of a "Bollinger Band Oscillator" in pictures on TOS, though I've never seen code for any on this site, so I ported over one that I found interesting on Tradingview: https://www.tradingview.com/script/1ax5ZncJ/
The Bollinger Band Width Oscillator is a great way to see when price is expanding again, and can help with both entries and exits.
The ideal scenario for taking a TRADE using this oscillator is to find price that has contracted (red) all the way down into the Squeeze line on the bottom of the indicator. This suggests that the bollinger bands have contracted harshly, implying that price is being squeezed and a big move may happen soon. When the oscillator moves off of the Squeeze line and turns green, enter a trade with trims/exits when the oscillator hits the Bulge line at the top.
When not taking a trade off this indicator, it is still a great reference to see whether price is potentially overextended or being constrained at all. Remember, with this indicator Green does not mean "bullish", it means expanding BB regardless of direction.
The Bollinger Band Width Oscillator is a great way to see when price is expanding again, and can help with both entries and exits.
The ideal scenario for taking a TRADE using this oscillator is to find price that has contracted (red) all the way down into the Squeeze line on the bottom of the indicator. This suggests that the bollinger bands have contracted harshly, implying that price is being squeezed and a big move may happen soon. When the oscillator moves off of the Squeeze line and turns green, enter a trade with trims/exits when the oscillator hits the Bulge line at the top.
When not taking a trade off this indicator, it is still a great reference to see whether price is potentially overextended or being constrained at all. Remember, with this indicator Green does not mean "bullish", it means expanding BB regardless of direction.
Code:
#//@version=4
#study("Bollinger Bands Wide & Narrow",shorttitle="Bollinger Bands W&N",overlay=false)
#//Bollinger Bands
# Original author: inno14 on Tradingview --https://www.tradingview.com/script/1ax5ZncJ/
# Ported by Chemmy for usethinkscript.com
declare lower;
input length = 20;
input src = close;
input mult = 2.0;
input avgtype = AverageType.SIMPLE;
input showlines = no;
input show_switch = no;
def basis = MovingAverage(avgtype, src, length);
def dev = mult * StandardDeviation(src, length);
def upper = basis + dev;
def lower = basis - dev;
def bbw = (upper - lower) / basis;
def bbw_ma = MovingAverage(avgtype, bbw, length);
## Gradient Line
def osc = AbsValue(bbw - bbw_ma);
def l1 = if (bbw > bbw_ma) then bbw_ma + (0.9 * osc) else bbw_ma - (0.9 * osc);
def l2 = if (bbw > bbw_ma) then bbw_ma + (0.8 * osc) else bbw_ma - (0.8 * osc);
def l3 = if (bbw > bbw_ma) then bbw_ma + (0.7 * osc) else bbw_ma - (0.7 * osc);
def l4 = if (bbw > bbw_ma) then bbw_ma + (0.6 * osc) else bbw_ma - (0.6 * osc);
def l5 = if (bbw > bbw_ma) then bbw_ma + (0.5 * osc) else bbw_ma - (0.5 * osc);
def l6 = if (bbw > bbw_ma) then bbw_ma + (0.4 * osc) else bbw_ma - (0.4 * osc);
def l7 = if (bbw > bbw_ma) then bbw_ma + (0.3 * osc) else bbw_ma - (0.3 * osc);
def l8 = if (bbw > bbw_ma) then bbw_ma + (0.2 * osc) else bbw_ma - (0.2 * osc);
def l9 = if (bbw > bbw_ma) then bbw_ma + (0.1 * osc) else bbw_ma - (0.1 * osc);
# Line Plotting
## Labels
#line_col = color.new(color.white,100)
#p1 = plot(bbw, "Bollinger Bands Width", color=line_col)
#p2 = plot(bbw_ma, "SMA of BBW", color=line_col)
plot f1 = l1;
f1.SetHiding(!showlines);
plot f2 = l2;
f2.SetHiding(!showlines);
plot f3 = l3;
f3.SetHiding(!showlines);
plot f4 = l4;
f4.SetHiding(!showlines);
plot f5 = l5;
f5.SetHiding(!showlines);
plot f6 = l6;
f6.SetHiding(!showlines);
plot f7 = l7;
f7.SetHiding(!showlines);
plot f8 = l8;
f8.SetHiding(!showlines);
plot f9 = l9;
f9.SetHiding(!showlines);
AddCloud(f1, f2, CreateColor(0, 255, 0), CreateColor(255, 0, 0));
AddCloud(f2, f3, CreateColor(20, 255, 0), CreateColor(255, 0, 20));
AddCloud(f3, f4, CreateColor(50, 255, 0), CreateColor(255, 0, 50));
AddCloud(f4, f5, CreateColor(80, 255, 0), CreateColor(255, 0, 80));
AddCloud(f5, f6, CreateColor(110, 255, 0), CreateColor(255, 0, 110));
AddCloud(f6, f7, CreateColor(140, 255, 0), CreateColor(255, 0, 140));
AddCloud(f7, f8, CreateColor(180, 255, 0), CreateColor(255, 0, 180));
AddCloud(f8, f9, CreateColor(210, 255, 0), CreateColor(255, 0, 210));
plot upsignal = if ((bbw - bbw_ma)>0) and ((bbw[1] - bbw_ma[1])<=0) then f1 else double.NaN;
upsignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
upsignal.SetHiding(!show_switch);
plot dnsignal = if ((bbw - bbw_ma)<0) and ((bbw[1] - bbw_ma[1])>=0) then f1 else double.Nan;
dnsignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dnsignal.SetHiding(!show_switch);
#Bandwith section
def num_dev_dn = -2.0;
def num_dev_up = 2.0;
input displace = 0;
input BulgeLength = 150;
input SqueezeLength = 150;
def upperBand = BollingerBands(src, displace, length, Num_Dev_Dn, Num_Dev_Up, avgType).UpperBand;
def lowerBand = BollingerBands(src, displace, length, Num_Dev_Dn, Num_Dev_Up, avgType).LowerBand;
def midLine = BollingerBands(src, displace, length, Num_Dev_Dn, Num_Dev_Up, avgType).MidLine;
plot Bandwidth = (upperBand - lowerBand) / midLine ;
Bandwidth.SetDefaultColor(GetColor(1));
plot Bulge = Highest(Bandwidth, BulgeLength);
Bulge.SetDefaultColor(GetColor(8));
Bulge.setStyle(Curve.SHORT_DASH);
plot Squeeze = Lowest(Bandwidth, SqueezeLength);
Squeeze.SetDefaultColor(GetColor(8));
Squeeze.setStyle(Curve.SHORT_DASH);
#AddLabel(yes, if (SMAFast >= SMASlow and zp < SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast >= SMASlow and zp < SMASlow) then color.green else color.red);
AddLabel(yes, if bandwidth < squeeze then "Price Squeezing" else if bandwidth > bulge then "Price Bulging" else if bandwidth <= ((bulge + squeeze)/2) then "No Squeeze" else "", if bandwidth < squeeze then color.Red else if bandwidth > bulge then Color.Orange else if bandwidth <= ((bulge + squeeze)/2) then Color.Green else Color.Yellow);
def pct = (upperband-lowerband)/lowerband;
AddLabel(1,
if pct > .05 then "Strong " +AsPercent(Pct) else
if pct > .015 then "Expansion " +AsPercent(Pct) else
if 0.005 <= pct <= .015 then "Tight " +AsPercent(Pct) else
"Very Tight " +AsPercent(Pct));