this is a lower study, that uses BollingerBandwidth , bandwidth data
...1. look for bandwidth staying below the % min number (10), for x bars {30)
...2. followed by increasing bandwidth for x bars (3)
...3. draw a vertical green line, on the first bar of #2
...4. draw arrows when #3 happens, based on price movement.
i don't scan, so i don't make scans.
to make this a scan,
...change all plots to def and add # to plot parameter code lines
.then remove the # from this line, at the end of code
...# plot buy2 = buy;
Code:
# bband_sqz_out_01
#==============================
def na = double.nan;
def bn = barnumber();
#=================================
# BollingerBandwidth
# TD Ameritrade IP Company, Inc. (c) 2008-2021
declare lower;
input averageType = AverageType.SIMPLE;
def price = close;
def displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_Up = 2.0;
input BulgeLength = 150;
input SqueezeLength = 150;
def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).LowerBand;
def midLine = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).MidLine;
plot Bandwidth = (upperBand - lowerBand) / midLine * 100;
Bandwidth.SetDefaultColor(GetColor(1));
input show_bulge_squeeze_lines = no;
plot Bulge = if show_bulge_squeeze_lines then Highest(Bandwidth, BulgeLength) else na;
Bulge.SetDefaultColor(GetColor(8));
Bulge.SetStyle(Curve.SHORT_DASH);
plot Squeeze = if show_bulge_squeeze_lines then Lowest(Bandwidth, SqueezeLength) else na;
Squeeze.SetDefaultColor(GetColor(8));
Squeeze.SetStyle(Curve.SHORT_DASH);
#==================================
#def bbb = BollingerBandwidth().Bandwidth;
def bbb = Bandwidth;
#---------------------------------
# draw a reference line
plot z0 = 0;
z0.setdefaultcolor(color.gray);
# a % number, that bandwidth has to stay under, to be considered in a squeeze
input squeeze_max_percent = 10;
def sqzper = squeeze_max_percent;
# qty of consecutive bars, to define a squeeze
input squeeze_min_bars = 30;
def sqzb = squeeze_min_bars;
# qty of bars after a squeeze, of increasing bandwidth, to define a breakout
input post_sqz_rising_bars = 3;
def sqzrise = post_sqz_rising_bars;
# qty of bars in series +1
def series_bars = (sqzb + sqzrise + 1);
input show_labels = yes;
addlabel(show_labels, "a yellow line, when bandwidth is < " + squeeze_max_percent + "%.", color.yellow);
addlabel(show_labels, "a white dot, in a squeeze. after " + squeeze_min_bars + " bars are < " + squeeze_max_percent + "%." , color.white);
addlabel(show_labels, "a green dot, after " + post_sqz_rising_bars + " rising bars, after a squeeze" , color.green);
#---------------------------------------------------------
# draw a line at the percent level , when bandwidth is < squeeze_max_percent
def sqzbar = (bbb < sqzper);
input show_sqz_bars = yes;
def v2 = sqzper;
plot z2 = if ( show_sqz_bars and sqzbar ) then v2 else na;
z2.SetDefaultColor(Color.yellow);
z2.hidebubble();
# ----------------------------------------------------------
# draw points under the min percent line, after the squeeze_min_bars have passed
# set a var to 1 if a squeeze, the past x bars (30) were < the % min (10)
def sqz = if Sum( sqzbar, squeeze_min_bars) == squeeze_min_bars then 1 else 0;
def v3 = (sqzper * 0.8);
input show_sqzs = yes;
plot z3 = if ( show_sqzs and sqz ) then v3 else na;
z3.SetPaintingStrategy(PaintingStrategy.POINTS);
z3.SetDefaultColor(Color.white);
#z3.setlineweight(2);
z3.hidebubble();
# ----------------------------------------------------------
# check for 3 rising bars after a sqz
# put a comparison in a sum, to look for x increasing bars
def incr = bbb > bbb[1];
# find just the 1st sqz_out in a series
# if a sqz 3+1 bars ago, then did it rise for 3 bars in a row?
def after_sqz = if (sqz[sqzrise + 1] and ( Sum(incr, sqzrise) == sqzrise ) ) then 1 else 0;
def v4 = (sqzper * 0.6);
input show_sqz_rise = yes;
plot z4 = if ( show_sqz_rise and after_sqz ) then v4 else na;
z4.SetPaintingStrategy(PaintingStrategy.POINTS);
z4.SetDefaultColor(Color.green);
#z4.setlineweight(2);
z4.hidebubble();
# draw vertical line, 1st bar there is a sqz of 30 bars then a rise of 3 bars
def t = ( !after_sqz[1] and after_sqz );
#addverticalline( t, sqzb + " BAR SQUEEZE, " + post_sqz_rising_bars + " RISING BARS" , color.green);
addverticalline( t, "-" , color.green);
# ----------------------------------------------------------
# smooth out price movements with an average
def pr = Average( close, 3);
def prup = ( pr > pr[1] );
def prdwn = ( pr < pr[1] );
# prup , prdwn , are avgs
def buy = (t and prup);
def sell = (t and prdwn);
plot buyx = if buy then sqzper else na;
buyx.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buyx.SetDefaultColor(Color.green);
buyx.setlineweight(2);
buyx.hidebubble();
plot sellx = if sell then sqzper else na;
sellx.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sellx.SetDefaultColor(Color.red);
sellx.setlineweight(2);
sellx.hidebubble();
# ---------------------------------------------
# to make this a scan,
# change all plots to def and add # to plot parameter lines
# the enable this line
# plot buy2 = buy;
# ----------------------------------------------------------
# test stuff
#input test5_after_sqz = no;
#addchartbubble(test5_after_sqz, 0, after_sqz, color.cyan, yes);
#addchartbubble(t, (sqzper * 0.4), bn, color.cyan, no);
#
Thank you very much. This worked very well when added as study on chart.
Your instructions to change this to scan were very simple and helped with no coding experience.
I changed plots to def and I think I commented out parameter lines but the scan didn't always show tickers that broke out!
Can you take a quick look to see I commented out right lines?
# bband_sqz_out_01
#==============================
def na = double.nan;
def bn = barnumber();
#=================================
# BollingerBandwidth
# TD Ameritrade IP Company, Inc. (c) 2008-2021
declare lower;
input averageType = AverageType.SIMPLE;
def price = close;
def displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_Up = 2.0;
input BulgeLength = 150;
input SqueezeLength = 150;
def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).LowerBand;
def midLine = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).MidLine;
plot Bandwidth = (upperBand - lowerBand) / midLine * 100;
Bandwidth.SetDefaultColor(GetColor(1));
input show_bulge_squeeze_lines = no;
plot Bulge = if show_bulge_squeeze_lines then Highest(Bandwidth, BulgeLength) else na;
Bulge.SetDefaultColor(GetColor(8));
Bulge.SetStyle(Curve.SHORT_DASH);
plot Squeeze = if show_bulge_squeeze_lines then Lowest(Bandwidth, SqueezeLength) else na;
Squeeze.SetDefaultColor(GetColor(8));
Squeeze.SetStyle(Curve.SHORT_DASH);
#==================================
#def bbb = BollingerBandwidth().Bandwidth;
def bbb = Bandwidth;
#---------------------------------
# draw a reference line
Def z0 = 0; #replace def with plot to see this on chart as a study
#z0.setdefaultcolor(color.gray);
# a % number, that bandwidth has to stay under, to be considered in a squeeze
input squeeze_max_percent = 10;
def sqzper = squeeze_max_percent;
# qty of consecutive bars, to define a squeeze
input squeeze_min_bars = 30;
def sqzb = squeeze_min_bars;
# qty of bars after a squeeze, of increasing bandwidth, to define a breakout
input post_sqz_rising_bars = 3;
def sqzrise = post_sqz_rising_bars;
# qty of bars in series +1
def series_bars = (sqzb + sqzrise + 1);
input show_labels = yes;
addlabel(show_labels, "a yellow line, when bandwidth is < " + squeeze_max_percent + "%.", color.yellow);
addlabel(show_labels, "a white dot, in a squeeze. after " + squeeze_min_bars + " bars are < " + squeeze_max_percent + "%." , color.white);
addlabel(show_labels, "a green dot, after " + post_sqz_rising_bars + " rising bars, after a squeeze" , color.green);
#---------------------------------------------------------
# draw a line at the percent level , when bandwidth is < squeeze_max_percent
def sqzbar = (bbb < sqzper);
input show_sqz_bars = yes;
def v2 = sqzper;
def z2 = if ( show_sqz_bars and sqzbar ) then v2 else na; #replace def with plot to see this on chart as a study
#z2.SetDefaultColor(Color.yellow);
#z2.hidebubble();
# ----------------------------------------------------------
# draw points under the min percent line, after the squeeze_min_bars have passed
# set a var to 1 if a squeeze, the past x bars (30) were < the % min (10)
def sqz = if Sum( sqzbar, squeeze_min_bars) == squeeze_min_bars then 1 else 0;
def v3 = (sqzper * 0.8);
input show_sqzs = yes;
def z3 = if ( show_sqzs and sqz ) then v3 else na; #replace def with plot to see this on chart as a study
# z3.SetPaintingStrategy(PaintingStrategy.POINTS);
#z3.SetDefaultColor(Color.white);
#z3.setlineweight(2);
#z3.hidebubble();
# ----------------------------------------------------------
# check for 3 rising bars after a sqz
# put a comparison in a sum, to look for x increasing bars
def incr = bbb > bbb[1];
# find just the 1st sqz_out in a series
# if a sqz 3+1 bars ago, then did it rise for 3 bars in a row?
def after_sqz = if (sqz[sqzrise + 1] and ( Sum(incr, sqzrise) == sqzrise ) ) then 1 else 0;
def v4 = (sqzper * 0.6);
input show_sqz_rise = yes;
def z4 = if ( show_sqz_rise and after_sqz ) then v4 else na; #replace def with plot to see this on chart as a study
#z4.SetPaintingStrategy(PaintingStrategy.POINTS);
#z4.SetDefaultColor(Color.green);
#z4.setlineweight(2);
#z4.hidebubble();
# draw vertical line, 1st bar there is a sqz of 30 bars then a rise of 3 bars
def t = ( !after_sqz[1] and after_sqz );
#addverticalline( t, sqzb + " BAR SQUEEZE, " + post_sqz_rising_bars + " RISING BARS" , color.green);
addverticalline( t, "-" , color.green);
# ----------------------------------------------------------
# smooth out price movements with an average
def pr = Average( close, 3);
def prup = ( pr > pr[1] );
def prdwn = ( pr < pr[1] );
# prup , prdwn , are avgs
def buy = (t and prup);
def sell = (t and prdwn);
def buyx = if buy then sqzper else na; #replace def with plot to see this on chart as a study
#buyx.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#buyx.SetDefaultColor(Color.green);
#buyx.setlineweight(2);
#buyx.hidebubble();
def sellx = if sell then sqzper else na; #replace def with plot to see this on chart as a study
#sellx.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#sellx.SetDefaultColor(Color.red);
#sellx.setlineweight(2);
#sellx.hidebubble();
# ---------------------------------------------
# to make this a scan,
# change all plots to def and add # to plot parameter lines
# the enable this line
plot buy2 = buy;
# ----------------------------------------------------------
# test stuff
#input test5_after_sqz = no;
#addchartbubble(test5_after_sqz, 0, after_sqz, color.cyan, yes);
#addchartbubble(t, (sqzper * 0.4), bn, color.cyan, no);
#