Volatility
Keltner Channel
An exponential moving average with bands set a multiple of average true range either side, so the channel reacts to the range of recent bars rather than to the dispersion of their closes.
indicator
//@version=6
indicator("Keltner Channel", "Keltner", overlay = true)
length = input.int(20, "EMA length", minval = 2, group = "Settings")
atrLen = input.int(10, "ATR length", minval = 1, group = "Settings")
mult = input.float(2.0, "ATR multiplier", minval = 0.1, step = 0.1, group = "Settings")
bandCol = input.color(#787B86, "Channel", group = "Style")
aboveCol = input.color(#26A69A, "Closed above", group = "Style")
belowCol = input.color(#EF5350, "Closed below", group = "Style")
basis = ta.ema(close, length)
atr = ta.atr(atrLen)
upper = basis + atr * mult
lower = basis - atr * mult
pUpper = plot(upper, "Upper", color = bandCol)
pLower = plot(lower, "Lower", color = bandCol)
plot(basis, "Basis", color = color.new(bandCol, 20))
fill(pUpper, pLower, color = color.new(bandCol, 94), title = "Channel")
brokeUp = ta.crossover(close, upper)
brokeDown = ta.crossunder(close, lower)
plotshape(brokeUp, "Closed above", location = location.abovebar, style = shape.triangleup, color = aboveCol, size = size.tiny)
plotshape(brokeDown, "Closed below", location = location.belowbar, style = shape.triangledown, color = belowCol, size = size.tiny)
alertcondition(brokeUp, "Closed above the channel", "Price closed above the upper Keltner band")
alertcondition(brokeDown, "Closed below the channel", "Price closed below the lower Keltner band")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.
What it will not do
- The basis is an EMA, so the whole channel lags. It describes where price has been travelling, not where it is going next.
- ATR uses the true range, which includes gaps. On instruments that gap the channel is wider than the intraday movement justifies.
- A close outside the channel is common in a trend and is not evidence that the move is finished.
Written from this description
Draw a Keltner channel from an EMA basis with upper and lower bands a configurable ATR multiple away, shade the channel, and mark closes through either band.
Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.