OPEN-SOURCE SCRIPT

Bollinger Band Reversal

//version=5
indicator("Bollinger Band Reversal", overlay=true)

// Bollinger Bands Parameters
length = 20
src = close
mult = 2.0

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

// Conditions
close_outside_upper = close > upper_band
close_outside_lower = close < lower_band

// Detect reversal
candle_reversal = (close_outside_upper[1] and close < upper_band) or (close_outside_lower[1] and close > lower_band)

// Plot Bollinger Bands
plot(upper_band, color=color.red, linewidth=2, title="Upper Band")
plot(lower_band, color=color.green, linewidth=2, title="Lower Band")
plot(basis, color=color.blue, linewidth=1, title="Basis")

// Mark the condition on the chart
bgcolor(candle_reversal ? color.new(color.green, 90) : na, title="Candle Reversal Background")

// Display alerts
alertcondition(candle_reversal, title="Bollinger Band Reversal Alert", message="Candle closed outside BB, next candle closed inside BB")
Bands and Channels

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer