DadBod's BB with Squeeze
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © future_dadbod

//@version=5
indicator(shorttitle="BB", title="DadBod's BB", overlay=true, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
comp_len = input.int(125, "BBW Squeeze Look Back Period")
offset = input.int(0, "Offset", minval = -500, maxval = 500)
basis = ta.sma(src, length)

// Inner BB Bands
mult_A = input.float(2.0, minval=0.001, maxval=50, title="StdDev_A")
dev_A = mult_A * ta.stdev(src, length)
upper_A = basis + dev_A
lower_A = basis - dev_A

// Outter BB Bands
mult_B = input.float(3.0, minval=0.001, maxval=50, title = "StdDev_B")
dev_B = mult_B * ta.stdev(src, length)
upper_B = basis + dev_B
lower_B = basis - dev_B

// BB Squeeze
bb_width(basis, upper, lower) =>
    (upper-lower)/basis

bbw_A = bb_width(basis, upper_A, lower_A) 
std_sqz = bbw_A == ta.lowest(bbw_A, comp_len)
// bbw_B = bb_width(basis, upper_B, lower_B)
// dev_sqz = bbw_B == ta.lowest(bbw_B, comp_len)

// BB Plots
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper_A, "A-Upper", color=#2962FF, offset = offset)
p2 = plot(lower_A, "A-Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "A-Background", color=color.rgb(33, 150, 243, 95))
p3 = plot(upper_B, "B-Upper", color=#2962FF, offset = offset)
p4 = plot(lower_B, "B-Lower", color=#2962FF, offset = offset)
fill(p3, p4, title = "B-Background", color=color.rgb(150, 150, 150, 125))

// Squeeze Plot and Alerts
barcolor(std_sqz ? color.yellow : na)
bgcolor(std_sqz ? color.new(color.fuchsia,75) : na)
plotshape(std_sqz, style=shape.arrowup, location=location.belowbar, color=color.aqua)
plotshape(std_sqz, style=shape.arrowdown, location=location.abovebar, color=color.aqua)

alertcondition(std_sqz, "BB Squeeze", "BB Squeeze")
// alertcondition(dev_sqz, "Developing BB Squeeze", "Developing BB Squeeze")
