//@version=5
strategy(title = "BB%B Strat", shorttitle = "BB%B Strat", format=format.price, precision=2, default_qty_type=strategy.percent_of_equity, default_qty_value=20)
length = input.int(21, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
ob = input.float(1.2, "Overbought Line", step=0.1)
ob_close = input.float(1.0, "Overbought Close", step=0.1)
os = input.float(-0.2, "Oversold Line", step=0.1)
os_close = input.float(0.2, "Oversold Close", step=0.1)
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
bbr = (src - lower)/(upper - lower)
p = plot(bbr, "Bollinger Bands %B", color=#26A69A)
ob_hline = hline(ob, "Overbought", color=color.red, linestyle=hline.style_dashed)
obc_hline = hline(ob_close, "Overbought Close", color=color.red, linestyle=hline.style_dashed)
os_hline = hline(os, "Oversold", color=color.green, linestyle=hline.style_dashed)
osc_hline = hline(os_close, "Oversold Close", color=color.green, linestyle=hline.style_dashed)
fill(ob_hline, obc_hline, color=color.new(color.red, 80), title="Overbought")
fill(os_hline, osc_hline, color=color.new(color.green, 80), title="Overbought")
bgcolor(bbr > ob ? color.new(color.fuchsia, 80) : (bbr < os ? color.new(color.lime, 80) : na))
if bbr < os and strategy.position_size == 0
strategy.entry("L", strategy.long)
if bbr >= os_close and strategy.position_size > 0
strategy.close_all()
if bbr > ob and strategy.position_size == 0
strategy.entry("S", strategy.short)
if bbr <= ob_close and strategy.position_size < 0
strategy.close_all()
Expand (32 lines)