Keltner Channels (KC)
Summary
Keltner Channels: three overlap lines around price. The centre line is an exponential moving average of the typical price; the outer bands sit a multiple of the Average True Range above and below it. The band width tracks volatility, so the channel widens in fast markets and narrows in quiet ones.
Chart
Chart metadata i
Outputs
outRealUpperBand:upper_limitoutRealMiddleBand:lineoutRealLowerBand:lower_limit
Formula
TP = (High + Low + Close) / 3 Middle = EMA(TP, N) Band = ATR(M) Upper = Middle + Deviations * Band Lower = Middle - Deviations * Band
Notes
- Several incompatible indicators are published under the name "Keltner Channel", disagreeing by percent rather than by rounding. This is the typical-price centre line with a Wilder-smoothed Average True Range band, the form implemented by TTR and ta4j.
- Chester Keltner's 1960 original smooths the typical price with a simple moving average and takes the band from the plain daily range; the widely charted modern variant centres on the close instead. Expect a visible difference against a package plotting either.
- TTR ties the Average True Range period to the centre line's period. Here the two are independent, so the band width can be tuned separately.
- The centre line and the band are separate recursions, each with its own warm-up. They are entered at their own lookbacks, so a caller who wants either one converged sets that function's unstable period —
TA_FUNC_UNST_EMAfor the centre line,TA_FUNC_UNST_ATRfor the band.
Inputs
inHigh— High price of each barinLow— Low price of each barinClose— Close price of each bar
Outputs
outRealUpperBand— Centre line plus the scaled Average True RangeoutRealMiddleBand— Exponential moving average of the typical priceoutRealLowerBand— Centre line minus the scaled Average True Range
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 20 | 2–100000 | Smoothing period of the typical price moving average |
optInATRPeriod | integer | 10 | 1–100000 | Smoothing period of the Average True Range |
optInNbDev | real | 2 | any real | Multiplier applied to the Average True Range |
Properties
Numerical Stability: Initial Unstable Period — Inherited from ATR and EMA, which KC computes internally; tunable via ATR and EMA's unstable period.
| ✅ Overlap Input i |
| ☐ Independent Y-Axis |
| ☐ Candlestick |
| ☐ Display Shift |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double high[252]; /* your data, oldest first */
double low[252]; /* your data, oldest first */
double close[252]; /* your data, oldest first */
double upperBand[252], middleBand[252], lowerBand[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_KC( 0, n - 1, high, low, close, 20, 10, 2.0,
&outBegIdx, &outNBElement, upperBand, middleBand, lowerBand );
printf( "%d values from bar %d:\n", outNBElement, outBegIdx );
printf( "upperBand = [%.4f, ..., %.4f]\n", upperBand[0], upperBand[outNBElement - 1] );
printf( "middleBand = [%.4f, ..., %.4f]\n", middleBand[0], middleBand[outNBElement - 1] );
printf( "lowerBand = [%.4f, ..., %.4f]\n", lowerBand[0], lowerBand[outNBElement - 1] );use ta_lib::Core;
let high = vec![0.0; 252]; // your data, oldest first
let low = vec![0.0; 252]; // your data, oldest first
let close = vec![0.0; 252]; // your data, oldest first
let mut upper_band = vec![0.0; 252];
let mut middle_band = vec![0.0; 252];
let mut lower_band = vec![0.0; 252];
let core = Core::new();
let range = core.kc(0, high.len() - 1, &high, &low, &close, 20, 10, 2.0,
&mut upper_band, &mut middle_band, &mut lower_band)?;
println!("{} values from bar {}:", range.count, range.beg_idx);
println!("upper_band = [{:.4}, ..., {:.4}]", upper_band[0], upper_band[range.count - 1]);
println!("middle_band = [{:.4}, ..., {:.4}]", middle_band[0], middle_band[range.count - 1]);
println!("lower_band = [{:.4}, ..., {:.4}]", lower_band[0], lower_band[range.count - 1]);import io.github.talib.Core;
import io.github.talib.OutRange;
import java.util.Locale;
double[] high = new double[252]; // your data, oldest first
double[] low = new double[252]; // your data, oldest first
double[] close = new double[252]; // your data, oldest first
double[] upperBand = new double[252];
double[] middleBand = new double[252];
double[] lowerBand = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.kc(0, high.length - 1, high, low, close, 20, 10, 2.0,
upperBand, middleBand, lowerBand);
System.out.printf(Locale.ROOT, "%d values from bar %d:%n", r.count(), r.begIdx());
System.out.printf(Locale.ROOT, "upperBand = [%.4f, ..., %.4f]%n", upperBand[0], upperBand[r.count() - 1]);
System.out.printf(Locale.ROOT, "middleBand = [%.4f, ..., %.4f]%n", middleBand[0], middleBand[r.count() - 1]);
System.out.printf(Locale.ROOT, "lowerBand = [%.4f, ..., %.4f]%n", lowerBand[0], lowerBand[r.count() - 1]);using System;
using TALib;
var high = new double[252]; // your data, oldest first
var low = new double[252]; // your data, oldest first
var close = new double[252]; // your data, oldest first
var upperBand = new double[252];
var middleBand = new double[252];
var lowerBand = new double[252];
var core = Core.Default;
OutRange r = core.Kc(0, high.Length - 1, high, low, close, 20, 10, 2.0,
upperBand, middleBand, lowerBand);
Console.WriteLine($"{r.Count} values from bar {r.BegIdx}:");
Console.WriteLine(FormattableString.Invariant($"upperBand = [{upperBand[0]:F4}, ..., {upperBand[r.Count - 1]:F4}]"));
Console.WriteLine(FormattableString.Invariant($"middleBand = [{middleBand[0]:F4}, ..., {middleBand[r.Count - 1]:F4}]"));
Console.WriteLine(FormattableString.Invariant($"lowerBand = [{lowerBand[0]:F4}, ..., {lowerBand[r.Count - 1]:F4}]"));import { kc } from 'ta-lib';
const high = new Float64Array(252); // your data, oldest first
const low = new Float64Array(252); // your data, oldest first
const close = new Float64Array(252); // your data, oldest first
const {
begIdx,
outUpperBand,
outMiddleBand,
outLowerBand,
} = kc({ high, low, close }, { timePeriod: 20, atrPeriod: 10, nbDev: 2 });
console.log(outUpperBand.length + ' values from bar ' + begIdx + ':');
console.log('outUpperBand = [' + outUpperBand[0].toFixed(4) + ', ..., ' + outUpperBand[outUpperBand.length - 1].toFixed(4) + ']');
console.log('outMiddleBand = [' + outMiddleBand[0].toFixed(4) + ', ..., ' + outMiddleBand[outMiddleBand.length - 1].toFixed(4) + ']');
console.log('outLowerBand = [' + outLowerBand[0].toFixed(4) + ', ..., ' + outLowerBand[outLowerBand.length - 1].toFixed(4) + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
high := make([]float64, 252) // your data, oldest first
low := make([]float64, 252) // your data, oldest first
close := make([]float64, 252) // your data, oldest first
upperBand := make([]float64, 252)
middleBand := make([]float64, 252)
lowerBand := make([]float64, 252)
core := talib.NewCore()
r, err := core.KC(0, len(high)-1, high, low, close, 20, 10, 2, upperBand, middleBand, lowerBand)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d:\n", r.NBElement, r.BegIdx)
fmt.Printf("upperBand = [%.4f, ..., %.4f]\n", upperBand[0], upperBand[r.NBElement-1])
fmt.Printf("middleBand = [%.4f, ..., %.4f]\n", middleBand[0], middleBand[r.NBElement-1])
fmt.Printf("lowerBand = [%.4f, ..., %.4f]\n", lowerBand[0], lowerBand[r.NBElement-1])Implementation
Added in 0.8.1 (ta-lib-python 0.8.1).
TA-Lib Definition: kc.c · kc.yaml
| Native | File |
|---|---|
| C | ta_KC.c |
| Rust | kc.rs |
| Java | Core_KC.java |
| C# | Core_KC.cs |
| TypeScript | kc.ts |
| Go | kc.go |
Python: talib.KC (ta-lib-python 0.8.1 or later), see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Keltner Channel
See Also
EMA · ATR · TYPPRICE · BBANDS · ACCBANDS