Pine Script indicators

Keltner Channels: Formula Versions and Squeeze

A Keltner Channel places range-based bands around a moving average. Before interpreting a breakout, identify the basis, range smoothing, length and multiplier. Two channels labeled 20/2 can produce different boundaries.

An illustrative ATR-based version

Basis = EMA(close, 20)
Upper = Basis + 2 × ATR(10)
Lower = Basis - 2 × ATR(10)

If the basis is 100 and ATR is 1.5, the bands are 103 and 97. This is arithmetic, not a forecast or a confidence interval.

Which Keltner Channel formula are you using?

TradingView exposes the basis MA type, source, band style and ATR length separately. Its Keltner indicator settings and the Pine function ta.kc are different interfaces. The reference implementation of ta.kc smooths the chosen range with an EMA.

Keltner formula variants and their parameters
VersionBasisHalf-widthLength control
ATR-based example hereEMA of sourceMultiplier × RMA of true rangeSeparate EMA and ATR lengths
ta.kc with true rangeEMA of sourceMultiplier × EMA of true rangeOne length for both EMAs
ta.kc without true rangeEMA of sourceMultiplier × EMA of high minus lowOne length for both EMAs

True range is the largest of high minus low, the absolute distance from high to the previous close, and the absolute distance from low to the previous close. A high-low-only range leaves out the overnight gap. Wilder ATR uses RMA smoothing; an EMA of true range generally gives a different value.

Sources: TradingView KC settings, ATR calculation and ta.kc reference.

Change one parameter at a time

  • The basis length changes the center line. A shorter EMA gives more weight to recent prices.
  • The ATR length changes the range estimate. It does not need to match the EMA length in the custom example.
  • The multiplier scales width directly. With the same basis and ATR, changing 2.0 to 1.5 makes each band 25% closer to the basis.
  • Keep source, session, chart timeframe and smoothing fixed when comparing results. There is no universal best setting for an asset class.

Keltner Channels vs Bollinger Bands

An ATR channel measures bar ranges and gaps. Bollinger Bands use standard deviation to measure dispersion around a mean. A series of wide candles with similar closes can therefore affect the two widths differently. Neither method is always smoother or more profitable. See TradingView's Bollinger Bands formula.

Define the squeeze before testing it

For these examples, use a 20-bar SMA of close with two standard deviations for BB, and the EMA(20), ATR(10), 2.0 KC above. A squeeze means the BB upper band is below the KC upper band and the BB lower band is above the KC lower band. Equality counts as outside this strict definition. Use the same symbol, timeframe and source; record each indicator's basis and width settings.

Suppose KC spans 97 to 103 and BB spans 98 to 102. The bands are contained. If BB then spans 96.5 to 103.5, containment has ended. That change alone gives no long or short direction. A squeeze can persist, release without follow-through, or return immediately. Merely seeing a narrow KC is a different condition.

Browse TradingView indicator guides, or read the Bollinger Bands guide for the companion indicator and the ATR guide for range measurement.

Three setups with explicit failure conditions

These are hypothetical long-side test specifications, not measured trading edges. Evaluate signals after the bar closes, allow one position at a time, and model entry at the next bar open. Fix costs and position sizing before testing.

1. Squeeze release with directional confirmation

Require squeeze containment on the previous closed bar. On the current close, require containment to end, price to be above the upper KC band, and the basis to be higher than three bars earlier. If those conditions do not occur together, skip the setup.

For this test, exit after the first close below the basis, or after ten bars in the position, whichever occurs first. Execute the exit at the next open. An immediate return inside the channel is a failed expansion; record it rather than changing the squeeze definition afterward.

2. Channel breakout without the squeeze filter

Require the previous close to be at or below its upper band and the current close to be above its upper band, with the same rising-basis condition. Use the same entry timing, size and exits as setup 1.

This is the baseline for deciding whether the squeeze filter adds value. A wick above the band does not qualify. A close can still be followed by a gap down, and a large gap up can make the next-open entry much less attractive than the signal close.

3. Pullback toward a rising basis

Require the basis to be above its value three bars earlier and the previous close above its basis. The current bar must touch or cross the basis with its low, then close back above it. Enter at the next open only if there is no existing position.

Exit on the first close below the basis, a close at or above the upper band, or ten bars after entry. This tests a resumption hypothesis. A rising average can lag a reversal, so the basis is not guaranteed support. Close-based exits also leave intrabar and overnight risk.

Pine Script with separate EMA and ATR lengths

Paste this indicator into TradingView's Pine Editor and add it to a standard candlestick chart. It plots the ATR-based version discussed above. It has no entry logic, alerts or backtest orders; the three setups remain separate experiments.

Pine Script v6
//@version=6
indicator("Keltner Channels: separate EMA and ATR lengths", overlay=true)

source = input.source(close, "Source")
emaLength = input.int(20, "EMA length", minval=1)
atrLength = input.int(10, "ATR length", minval=1)
multiplier = input.float(2.0, "ATR multiplier", minval=0.1, step=0.1)

basis = ta.ema(source, emaLength)
rangeValue = ta.atr(atrLength)
upper = basis + multiplier * rangeValue
lower = basis - multiplier * rangeValue

plot(basis, "EMA basis", color=color.blue)
upperPlot = plot(upper, "Upper", color=color.blue)
lowerPlot = plot(lower, "Lower", color=color.blue)
fill(upperPlot, lowerPlot, color=color.new(color.blue, 92), title="Channel")

Indicator example. It does not place orders.

This example deliberately uses ta.atr rather than ta.kc. When comparing it with another indicator, match the source, EMA length, ATR length and multiplier, then compare closed-bar values in the Data Window. A visual resemblance is not formula equivalence.

Confirmation changes when a signal is available

On an open bar, price and the channel can move together. A temporary cross may disappear before close. Closed-bar evaluation avoids acting on that temporary condition, but the signal arrives later. If you add higher-timeframe data, confirm that timeframe separately. See TradingView's repainting documentation.

Compare against a baseline with the same costs

Compare the squeeze-filtered entry with the plain breakout on the same dates, instrument and timeframe. Keep position sizing, commission, slippage, exits and fill timing identical. Report trade count, net return, drawdown and average trade together; fewer trades can change apparent performance substantially.

Choose a small parameter grid on a training period, then freeze it before evaluating a later holdout period. Inspect nearby parameter results, not only the top row. An isolated winning setting or a result based on very few trades is weak evidence. Use standard OHLC prices for execution tests and check the TradingView strategy fill and cost settings.

Build the version you actually intend to test

Pineify's Visual Pine Script Editor has a KC block with source, length, multiplier and a true-range toggle. It uses ta.kc and exposes middle, upper and lower outputs. For an independently adjustable ATR length, ask the AI Coding Agent for the custom EMA-plus-ATR version and inspect the generated formula.

Once you have a strategy with explicit orders and numeric inputs, the Strategy Optimizer can run parameter comparisons in TradingView. Optimization ranks historical results; it does not establish future profitability. Product access and usage limits depend on your plan.

Example request: Create a Pine v6 strategy using an EMA(20) basis and a separate ATR(10) with a 2.0 multiplier. Expose all three inputs. Implement the closed-bar breakout and exit rules above, with a switch for the BB/KC squeeze filter, so I can compare both versions with the same costs and position size.
Build a custom Keltner script

Educational material, not investment advice. Examples are hypothetical and have no claimed performance. Review generated code, test execution assumptions and paper trade before risking capital. Backtests do not guarantee future results.

Keltner Channel FAQ

A Keltner Channel is a moving average with upper and lower bands set a multiple of a price-range measure away. A common modern version uses an EMA basis and ATR width. The name alone does not specify the averaging method or every length.

The example starts with a 20-bar EMA of close, a separate 10-bar ATR using Wilder smoothing, and a 2.0 multiplier. These are illustrative inputs, not recommended or optimized settings. Record all three values when comparing channels.

The built-in ta.kc function uses an EMA for its basis and an EMA of the selected range, with the same length for both. With useTrueRange enabled it smooths true range; otherwise it smooths high minus low. A channel built with ta.atr uses Wilder RMA smoothing and can have a separate ATR length, so matching the visible length and multiplier is insufficient.

An ATR-based Keltner Channel measures true range, including gaps from the previous close. Bollinger Bands measure price dispersion with standard deviation. Their widths can diverge even when their basis lengths match. Neither width determines whether a trade will be profitable.

One explicit BB/KC squeeze definition puts both Bollinger Bands inside the Keltner boundaries. A squeeze ends when that containment condition becomes false. The state does not specify breakout direction or guarantee expansion, so a strategy needs a separate entry condition.

No. It describes price relative to the selected basis and range. A trend can continue outside the band. A reversal strategy needs its own evidence and exit rule; a band touch alone is not a complete trade.

The live bar can change the close, high, low, EMA and range estimate, so a cross can disappear before the bar closes. Evaluate closed bars for close-based rules. Higher-timeframe inputs require their own confirmation policy; waiting for the chart bar alone does not confirm a higher-timeframe bar.

Sources and verification

Official documentation reviewed October 16, 2026. Trading rules and numeric illustrations on this page are hypothetical.