OPEN-SOURCE SCRIPT

Close Outside BB Without Touching

44
//version=5
indicator("Close Outside BB Without Touching", overlay=true)

// Input parameters
length = input.int(20, title="BB Length")
mult = input.float(2.0, title="BB Standard Deviation")
src = input(close, title="Source")

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

// Check if candle closed outside BB
closedAbove = close > upper
closedBelow = close < lower

// Check if candle didn't touch the BB during its formation
// For a candle closing above: low must be greater than upper band
// For a candle closing below: high must be less than lower band
noTouchAbove = low > upper
noTouchBelow = high < lower

// Final conditions
validAbove = closedAbove and noTouchAbove
validBelow = closedBelow and noTouchBelow

// Plot Bollinger Bands
plot(basis, "Basis", color=color.orange)
u = plot(upper, "Upper", color=color.blue)
l = plot(lower, "Lower", color=color.blue)

// Fill between Bollinger Bands
fill(u, l, color=color.new(color.blue, 95), title="Background")

// Highlight valid candles
barcolor(validAbove ? color.green : validBelow ? color.red : na)

// Plot markers for valid signals
plotshape(validAbove, title="Valid Above BB", color=color.green,
style=shape.triangleup, location=location.belowbar, size=size.small)
plotshape(validBelow, title="Valid Below BB", color=color.red,
style=shape.triangledown, location=location.abovebar, size=size.small)

// Alert conditions
alertcondition(validAbove, title="Valid Close Above BB",
message="Candle closed above BB without touching")
alertcondition(validBelow, title="Valid Close Below BB",
message="Candle closed below BB without touching")

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.