A Keltner Channel is an envelope around an EMA basis, with upper and lower bands set a multiple of Average True Range away. Because it uses ATR rather than standard deviation the channel reacts smoothly to volatility; breaks outside the bands signal continuation while the basis acts as dynamic support and resistance. Copy the version for your platform.
//@version=5
indicator("Keltner Channel", overlay=true)
length = input.int(20, "Length")
mult = input.float(2.0, "ATR Mult")
basis = ta.ema(close, length)
rangema = ta.atr(length)
upper = basis + mult * rangema
lower = basis - mult * rangema
plot(basis, title="Basis", color=color.orange)
plot(upper, title="Upper", color=color.blue)
plot(lower, title="Lower", color=color.blue)#property indicator_chart_window
#property indicator_buffers 3
extern int KC_Period = 20;
extern double KC_Mult = 2.0;
double BasisBuf[], UpperBuf[], LowerBuf[];
int OnInit() {
SetIndexBuffer(0, BasisBuf);
SetIndexBuffer(1, UpperBuf);
SetIndexBuffer(2, LowerBuf);
return 0;
}
int OnCalculate(const int rates_total, const int prev_calculated, const double &close[]) {
for (int i = prev_calculated; i < rates_total; i++) {
double basis = iMA(NULL, 0, KC_Period, 0, MODE_EMA, PRICE_CLOSE, i);
double rng = iATR(NULL, 0, KC_Period, i);
BasisBuf[i] = basis;
UpperBuf[i] = basis + KC_Mult * rng;
LowerBuf[i] = basis - KC_Mult * rng;
}
return rates_total;
}#property indicator_chart_window
#property indicator_buffers 3
input int KC_Period = 20;
input double KC_Mult = 2.0;
double BasisBuf[], UpperBuf[], LowerBuf[];
int OnInit() {
SetIndexBuffer(0, BasisBuf);
SetIndexBuffer(1, UpperBuf);
SetIndexBuffer(2, LowerBuf);
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const datetime &time[], const double &open[], const double &high[],
const double &low[], const double &close[], const long &tick_volume[],
const long &volume[], const int &spread[]) {
for (int i = prev_calculated; i < rates_total; i++) {
double basis = iMA(NULL, 0, KC_Period, 0, MODE_EMA, PRICE_CLOSE, i);
double rng = iATR(NULL, 0, KC_Period, i);
BasisBuf[i] = basis;
UpperBuf[i] = basis + KC_Mult * rng;
LowerBuf[i] = basis - KC_Mult * rng;
}
return rates_total;
}inputs: Length(20), Mult(2.0);
variables: Basis(0), Upper(0), Lower(0);
Basis = XAverage(Close, Length);
Upper = Basis + Mult * AvgTrueRange(Length);
Lower = Basis - Mult * AvgTrueRange(Length);
Plot1(Basis, "Basis");
Plot2(Upper, "Upper");
Plot3(Lower, "Lower");{Keltner Channel - EMA basis +/- 2*ATR}
basis := Mov(C, 20, E);
rng := ATR(20);
upper := basis + 2 * rng;
lower := basis - 2 * rng;
basisTo use it inside dtcharts, paste any version into the Script Converter and it becomes a native indicator on your chart.