Master these dynamic volatility envelopes in TradingView's Pine Script for identifying overbought/oversold conditions, potential trend reversals, and market volatility shifts.
Bollinger Bands (BB) are a widely popular and versatile technical analysis indicator developed by John Bollinger. They consist of three lines:
The calculation of Bollinger Bands involves a Simple Moving Average (SMA) and Standard Deviation (StdDev) of price:
Middle Band = SMA(close, length)StdDev = ta.stdev(close, length)Upper Band = Middle Band + (StdDev * multiplier)Lower Band = Middle Band - (StdDev * multiplier)//@version=5
indicator("My Bollinger Bands", overlay=true)
// Inputs for Bollinger Bands parameters
length = input.int(20, title="BB Length", minval=1)
stdDevMultiplier = input.float(2.0, title="StdDev Multiplier", minval=0.1, step=0.1)
// Calculate Bollinger Bands using the built-in function
[middleBand, upperBand, lowerBand] = ta.bb(close, length, stdDevMultiplier)
// Plot the Middle Band
plot(middleBand, title="Middle Band", color=color.blue, linewidth=2)
// Plot the Upper and Lower Bands
plot(upperBand, title="Upper Band", color=color.red, linewidth=1)
plot(lowerBand, title="Lower Band", color=color.green, linewidth=1)
// Fill the area between the bands for visual appeal
fill(upperBand, lowerBand, color=color.new(color.blue, 90), title="BB Background")
Unlike fixed channels, Bollinger Bands adapt to current volatility, providing a more context-aware view of price extremes.
A "squeeze" occurs when the bands contract significantly, indicating a period of low volatility and consolidation that often precedes a strong price breakout.
//@version=5
strategy("Bollinger Band Squeeze Breakout", overlay=true)
length = input.int(20, title="BB Length", minval=1)
stdDevMultiplier = input.float(2.0, title="StdDev Multiplier", minval=0.1, step=0.1)
[middleBand, upperBand, lowerBand] = ta.bb(close, length, stdDevMultiplier)
plot(middleBand, "Middle Band", color.blue)
plot(upperBand, "Upper Band", color.red)
plot(lowerBand, "Lower Band", color.green)
fill(upperBand, lowerBand, color.new(color.blue, 90))
bandWidth = (upperBand - lowerBand) / middleBand * 100
squeezeThreshold = input.float(5.0, title="Squeeze Percentage Threshold", minval=0.1, step=0.1)
isSqueezing = bandWidth < squeezeThreshold
longBreakout = isSqueezing[1] and close > upperBand and close[1] <= upperBand[1]
shortBreakout = isSqueezing[1] and close < lowerBand and close[1] >= lowerBand[1]
if (longBreakout)
strategy.entry("Long Breakout", strategy.long)
if (shortBreakout)
strategy.entry("Short Breakout", strategy.short)
strategy.exit("Long Exit", from_entry="Long Breakout", stop=lowerBand)
strategy.exit("Short Exit", from_entry="Short Breakout", stop=upperBand)
In strong trends, price can "walk" along the Upper or Lower Band without touching the Middle Band for extended periods, indicating strong directional momentum.
//@version=5
strategy("Bollinger Band Walk Strategy", overlay=true)
length = input.int(20, title="BB Length", minval=1)
stdDevMultiplier = input.float(2.0, title="StdDev Multiplier", minval=0.1, step=0.1)
[middleBand, upperBand, lowerBand] = ta.bb(close, length, stdDevMultiplier)
plot(middleBand, "Middle Band", color.blue)
plot(upperBand, "Upper Band", color.red)
plot(lowerBand, "Lower Band", color.green)
fill(upperBand, lowerBand, color.new(color.blue, 90))
isBullishWalk = close > middleBand and close > close[1] and close[1] > middleBand[1] and middleBand > middleBand[1] and close >= upperBand
isBearishWalk = close < middleBand and close < close[1] and close[1] < middleBand[1] and middleBand < middleBand[1] and close <= lowerBand
if (isBullishWalk)
strategy.entry("Riding Bull", strategy.long)
if (isBearishWalk)
strategy.entry("Riding Bear", strategy.short)
strategy.close("Riding Bull", when=close < middleBand or close[1] < middleBand[1])
strategy.close("Riding Bear", when=close > middleBand or close[1] > middleBand[1])
In sideways or consolidating markets, price often reverts to the Middle Band after touching one of the outer bands.
//@version=5
strategy("Bollinger Band Mean Reversion", overlay=true)
length = input.int(20, title="BB Length", minval=1)
stdDevMultiplier = input.float(2.0, title="StdDev Multiplier", minval=0.1, step=0.1)
[middleBand, upperBand, lowerBand] = ta.bb(close, length, stdDevMultiplier)
plot(middleBand, "Middle Band", color.blue)
plot(upperBand, "Upper Band", color.red)
plot(lowerBand, "Lower Band", color.green)
fill(upperBand, lowerBand, color.new(color.blue, 90))
isRanging = (upperBand - lowerBand) / middleBand * 100 > 1.0 and (upperBand - lowerBand) / middleBand * 100 < 5.0
longReversion = isRanging and close > lowerBand and close[1] <= lowerBand[1]
shortReversion = isRanging and close < upperBand and close[1] >= upperBand[1]
if (longReversion)
strategy.entry("Buy Reversion", strategy.long)
if (shortReversion)
strategy.entry("Sell Reversion", strategy.short)
strategy.close("Buy Reversion", when=close > middleBand or close < lowerBand)
strategy.close("Sell Reversion", when=close < middleBand or close > upperBand)
To get the most from Bollinger Bands in Pine Script:
Price touching or slightly exceeding the bands is not always a direct buy/sell signal. It indicates relative overextension. The subsequent price action is what provides the signal.
Bollinger Bands are an exceptionally powerful and dynamic technical analysis tool in Pine Script for TradingView. By creating a visual representation of price volatility around a central moving average, they provide traders with invaluable insights into relative overbought/oversold conditions, potential trend reversals, and shifts in market volatility. Whether you are looking to capitalize on volatility squeezes, ride strong trends, or execute mean-reversion strategies, understanding and effectively applying Bollinger Bands, ideally in conjunction with other indicators and robust risk management, can significantly enhance your trading strategies and overall performance.
Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.
This strategy runs in live mode on TradingView, helping you identify potential opportunities.
Get Pine Script Strategy