BB Order Blocks
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MensaTrader


//@version=5
indicator(shorttitle="OB", title="BB Order Blocks", overlay=true, timeframe="", timeframe_gaps=true)

//Inputs
length      = input.int(20, minval=1, title="Bollinger Band Length", group="=== Bollinger Bands ===", step=5, tooltip="Lengths 20,50,100 & 200 Give interesting levels of confluence")
src         = input(close, title="Band Source", group="=== Bollinger Bands ===")
mult        = input.float(2.0, minval=0.001, maxval=50, title="Band StdDev", group="=== Bollinger Bands ===")

//OB In
lookback    = input.int(100, title="Lookback Length", step=10, group="=== Order Block ===", tooltip="Range from 20-200")


plotBands   = input.bool(false, title="Plot Bollinger Bands", group="=== Plots ===")
plotOb      = input.bool(true, title="Plot OB Levels", group="=== Plots ===")
plotCon     = input.bool(true, title="Plot Solid OB only", group="=== Plots ===")

//Calc Bands
basis       = ta.sma(src, length)
dev         = mult * ta.stdev(src, length)
upper       = basis + dev
lower       = basis - dev
offset      = 0

reachDev    = (mult + 0.1) * ta.stdev(src, length)
upper2      = basis + reachDev
lower2      = basis - reachDev

//Order Blocks
obHigh      = ta.highest(upper, lookback)
obLow       = ta.lowest(lower, lookback)

obHigh2     = ta.highest(upper2, lookback)
obLow2      = ta.lowest(lower2, lookback)


//Plots
//Upper Order Block
o1 = plot(plotOb ? plotCon ? obHigh==obHigh[1]  ? obHigh : na : obHigh :na, title="Order Block High", color=#FF0000, style=plot.style_linebr)
o2 = plot(plotOb ? plotCon ? obHigh==obHigh[1]  ? obHigh2 : na : obHigh2 :na, title="Order Block High 2", color=#FF0000, style=plot.style_linebr)

fill(o1,o2, color=color.new(#FF0000,80), title="Sell Fill")

//Lower Order Block
o3 = plot(plotOb ? plotCon ? obLow==obLow[1]  ? obLow : na : obLow :na, title="Order Block Low", color=#00E600, style=plot.style_linebr)
o4 = plot(plotOb ? plotCon ? obLow==obLow[1]  ? obLow2 : na : obLow2 :na, title="Order Block Low 2", color=#00E600, style=plot.style_linebr)

fill(o3,o4, color=color.new(#00E600,80), title="Buy Fill")


//Plot bands
plot(plotBands ? basis : na, "Basis", color=#FF6D00, offset = offset)
p1 = plot(plotBands ? upper : na, "Upper", color=#2962FF, offset = offset)
p2 = plot(plotBands ? lower : na, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))