by damianpitt· code credited to Capital41 | MIT License
Capital41 ATR Volatility Bands v2 draws ATR-based bands around a customizable basis moving average on the price chart to visualize potential volatility ranges.
// Copyright (c) 2026 Capital41 | MIT License
// CAPITAL41 ATR VOLATILITY BANDS v2
// What it does:
// - Draws ATR-based bands around a moving average to visualize volatility range.
// - Optional recent-range box highlights the last N bars' high/low range.
// - Optional expansion highlight when ATR is rising above its average.
// - Alerts on volatility expansion and band crosses.
//
// v2 changes:
// - ADDED: Alert conditions for volatility expansion start and band crosses,
// gated by barstate.isconfirmed.
//
// TradingView setup:
// 1) Open TradingView -> Pine Editor.
// 2) Paste this script, Save, and Add to chart.
// 3) Use 30m or 4h on BTC or US stocks.
// 4) Adjust ATR Multiplier to widen or tighten the bands.
//
// Usage guide (30m/4h):
// - Bands help spot expanding volatility and mean-reversion edges.
// - If price rides the band, trend is strong; if it rejects, expect pullback.
// - Volatility box shows recent range; use it with sweeps or session context.
//@version=6
indicator("Capital41 ATR Volatility Bands v2", overlay = true, max_boxes_count = 20)
// Inputs
basisType = input.string("EMA", "Basis type", options = ["EMA", "SMA"])
basisLen = input.int(50, "Basis length", minval = 5)
atrLen = input.int(14, "ATR length", minval = 1)
atrMult = input.float(1.5, "ATR multiplier", minval = 0.1, maxval = 5.0)
showBasis = input.bool(false, "Show basis")
showBands = input.bool(true, "Show ATR bands")
showVolBox = input.bool(true, "Show recent-range box")
boxLookback = input.int(20, "Range lookback bars", minval = 5, maxval = 200)
showExpansion = input.bool(false, "Highlight volatility expansion")
expFactor = input.float(1.2, "Expansion factor", minval = 1.0, maxval = 3.0)
colorBand = color.new(color.blue, 0)
colorBasis = color.new(color.gray, 0)
colorBox = color.new(color.gray, 90)
// Basis + ATR
basis = basisType == "EMA" ? ta.ema(close, basisLen) : ta.sma(close, basisLen)
atr = ta.atr(atrLen)
upper = basis + atr * atrMult
lower = basis - atr * atrMult
plot(showBasis ? basis : na, title = "Basis", color = colorBasis, linewidth = 1)
plot(showBands ? upper : na, title = "ATR Upper", color = color.new(colorBand, 40), linewidth = 1)
plot(showBands ? lower : na, title = "ATR Lower", color = color.new(colorBand, 40), linewidth = 1)
// Recent-range box
var box volBox = na
if showVolBox
hi = ta.highest(high, boxLookback)
lo = ta.lowest(low, boxLookback)
left = bar_index - boxLookback + 1
if na(volBox)
volBox := box.new(left = left, right = bar_index, top = hi, bottom = lo, border_width = 0, bgcolor = colorBox)
else
box.set_left(volBox, left)
box.set_right(volBox, bar_index)
box.set_top(volBox, hi)
box.set_bottom(volBox, lo)
else
if not na(volBox)
box.delete(volBox)
volBox := na
// Expansion highlight
atrAvg = ta.sma(atr, atrLen)
volExp = atrAvg > 0 and atr > atrAvg * expFactor
bgcolor(showExpansion and volExp ? color.new(color.orange, 92) : na)
// Alerts (crosses hoisted so ta.* runs every bar, then confirmed on bar close)
expStart = volExp and not volExp[1]
upperTagCross = ta.crossover(close, upper)
lowerTagCross = ta.crossunder(close, lower)
alertcondition(expStart and barstate.isconfirmed, title = "Volatility Expansion",
message = "ATR Bands: volatility expansion started on {{ticker}} ({{interval}})")
alertcondition(upperTagCross and barstate.isconfirmed, title = "Upper Band Cross",
message = "ATR Bands: price closed above the upper band on {{ticker}} ({{interval}})")
alertcondition(lowerTagCross and barstate.isconfirmed, title = "Lower Band Cross",
message = "ATR Bands: price closed below the lower band on {{ticker}} ({{interval}})")
Overview
The basis can be set to EMA or SMA, with adjustable lengths for both basis and ATR. It optionally plots a recent-range box showing the last N bars high-low range and highlights volatility expansion when ATR exceeds its average by a factor. Band crosses and expansion starts trigger alerts gated on confirmed bars. Main settings control basis type, lengths, multipliers, and visibility toggles for basis, bands, box, and expansion. It draws directly on the price chart for intraday and swing timeframes.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Basis type
string
EMA
basisType
Basis length
int
50
basisLen
ATR length
int
14
atrLen
ATR multiplier
float
1.5
atrMult
Show basis
bool
false
showBasis
Show ATR bands
bool
true
showBands
Show recent-range box
bool
true
showVolBox
Range lookback bars
int
20
boxLookback
Highlight volatility expansion
bool
false
showExpansion
Expansion factor
float
1.2
expFactor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This indicator plots an ATR-based Chandelier Exit trailing stop on the price chart. It tracks a long stop under recent highs and a short stop above recent lows, then switches direction when price crosses the opposite stop. Buy and sell labels mark those flips.
This overlay indicator marks ICT liquidity void fills on the price chart and can show long and short signals. It places a Fibonacci level inside a displacement candle, defaulting to the midpoint, and draws lines in a chosen style. Optional filters limit the fi
This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre
This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos
This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre
This indicator calculates an outlier-filtered moving average based on statistical filtering concepts. It stores close prices in a rolling array over a user-defined length, calculates the mean and standard deviation, and excludes values falling within a specifi