#inside_and_bbline_cross
#https://usethinkscript.com/threads/bollinger-band-squeeze-scan.19715/
#Bollinger Band Squeeze Scan
def bn = BarNumber();
def na = Double.NaN;
def o = open;
def h = high;
def l = low;
def c = close;
def up = close > open;
def dwn = close < open;
DefineGlobalColor("c1", Color.YELLOW);
# GlobalColor("c1")
#----------------------------
# BollingerBands
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;
def sDev = StDev(data = price[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
def xup_bbupper = (high > UpperBand and low[1] < UpperBand[1]);
#----------------------------
#inside_bars
input minimum_inside_bars = 2;
input show_inside_count_bubbles = yes;
input show_inside_count_values_above = no;
input draw_a_box_around_inside_bar = yes;
input show_horizontal_lines = yes;
input show_inside_bars_within_larger_range = yes;
input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = h;
lowx = l;
case "body":
highx = Max(o, c);
lowx = Min(o, c);
}
def loop1 = 100;
# count how many future bars are smaller than the current bar
def incnt1 = fold k = 1 to loop1
with p
while (highx >= GetValue(highx, -k) and lowx <= GetValue(lowx, -k))
do p + 1;
def incnt2 = if incnt1 >= minimum_inside_bars then incnt1 else 0;
# count down the bars in the inside range , cnt+1 to 1
def incountdown = if bn == 1 then 0
else if incountdown[1] <= 1 and incnt2 > 0 then incnt2 + 1
else if incountdown[1] > 0 then (incountdown[1] - 1)
else 0;
# did an inside bar cross above upper bb line
def inside_xup_bbupper = (xup_bbupper and incnt2 > 0 and up);
# disable this with # if using as a chart study
#plot scan = inside_xup_bbupper;
# ///////////////////////////////////////////
# delete stuff after this line when using this study as scan code
def yoff = 0.001;
def y6 = h * (1 + yoff);
AddChartBubble(show_inside_count_bubbles and inside_xup_bbupper and incountdown[1] <= 1 and incountdown > 0 , y6, incnt2 , GlobalColor("c1"), yes);
# plot horz lines during inside range
def in_hi = if incountdown[1] <= 1 and incountdown > 0 then highx
else if incountdown > 0 then in_hi[1]
else 0;
def in_lo = if incountdown[1] <= 1 and incountdown > 0 then lowx
else if incountdown > 0 then in_lo[1]
else 0;
plot zinhi = if inside_xup_bbupper and show_horizontal_lines and in_hi > 0 then in_hi else na;
plot zinlo = if inside_xup_bbupper and show_horizontal_lines and in_lo > 0 then in_lo else na;
zinhi.SetDefaultColor(GlobalColor("c1"));
zinhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinhi.HideBubble();
zinlo.SetDefaultColor(GlobalColor("c1"));
zinlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinlo.HideBubble();
#