OPEN-SOURCE SCRIPT
Close Outside BB Without Touching

//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")
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")
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
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.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
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.