Commodity Channel Index CCI
The typical price's distance from its own moving average, divided by 0.015 times the mean absolute deviation of the same window, which scales the result so that roughly the middle of the distribution falls inside plus or minus 100.
It measures deviation from the mean in units of that window's own dispersion. Readings: beyond plus or minus 100 as a stretched move, the zero line as the mean, and divergence. This is the one OHLC indicator with a single convention: MT5 and TA-Lib give the same values.
01The study, live
Computed by Bentrix’s own code over the same synthetic bars on every page — an illustration of how the study behaves, not a market.
What this figure shows
The figure is computed over 300 sample bars. CCI has no value on the first 13 — the bars it needs before it can answer — and its first value stands on bar 14. Its line runs between -247.5 and 237.5 on these bars, and the last value is 41.95. Counting the conditions the study declares on these bars: crosses above -100 12 times; crosses below -100 12 times; crosses above 100 15 times; crosses below 100 16 times. Press a condition under the figure to see each place marked. As states rather than moments: is above -100 on 213 bars; is below -100 on 74 bars; is above 100 on 70 bars; is below 100 on 217 bars.
02Its parts, named
03What you see
An oscillator around zero, usually with lines at +100 and −100, measuring how far the typical price stands from its average in units of its average deviation.
04Where it comes from
Donald Lambert introduced it in Commodities magazine in 1980 for commodity cycles. Its constant 0.015 was chosen so that about 70–80 % of values would fall between −100 and +100.
05The formula
TP = (H+L+C)/3, CCI = (TP − SMA(TP, N))/(0.015 · MD)
where is the mean absolute deviation of TP from its SMA over bars (14 or 20).
06A worked example
The sample bars used here are eight: closes 20, 21, 23, 22, 24, 25, 24, 26; highs 21, 22, 24, 24, 25, 26, 26, 27; lows 18, 19, 20, 21, 21, 23, 23, 23; opens 19, 20, 21, 23, 22, 24, 25, 24.
Take a period of 5. The typical price of each of the last five bars, (high + low + close) / 3, is 22.33, 23.33, 24.67, 24.33, 25.33. Their average is 24.00.
The mean deviation is the average distance of the five from that average, ignoring sign: 0.93.
CCI = (25.33 − 24.00) / (0.015 × 0.93) = 95.24
The newest typical price is 1.33 above the average, which is 1.43 mean deviations; dividing by 0.015 turns that into the CCI's scale, on which ±100 is ±1.5 mean deviations.
07Built in Assay
The same study written in Assay, Bentrix’s script language, and drawn by Bentrix itself as the script’s graph: each block is one step of the formula. On the ladder the steps stand in the script’s order, one a row; on the board each wire carries a value from the block that gives it to the block that reads it, left to right. The script computes exactly what the study computes — Bentrix checks the two against each other, candle by candle.
//@version=6 indicator("Commodity Channel Index") length = input.int(14, "Length") typical = hlc3 average = ta.sma(typical, length) // The window's mean absolute distance from that average. deviation = ta.dev(typical, length) // The 0.015 scales the result so that most of it falls between -100 and +100. cci = deviation == 0 ? 0.0 : (typical - average) / (0.015 * deviation) plot(cci, "CCI") hline(100) hline(-100)
Paste it into the Assay IDE to put it on a chart, or to change it. Looking at the graph is free in every package; editing it as blocks is Plus.
08Readings — what traders look for
Above +100 / below −100 is read as the typical price unusually far above (below) its average for this window.
Crossing zero is read as the typical price moving above or below its average.
Divergence with price, as with other oscillators.
How the ±100 reading fails. Lambert chose the constant 0.015 so that most values fall inside ±100; a move outside says price is unusually far from its average for this window. In a strong trend the line stays at one end of its scale for a long time; a high reading there describes the trend and does not mark its end.
How the zero line fails. The line crosses zero whenever price crosses its own average, which in a sideways market is every few bars.
09In different markets and timeframes
The CCI measures distance from an average in units of typical deviation, so it serves both ways: in a range its returns from beyond ±100 mark the swings; in a trend its stay beyond +100 or −100 marks the trend's force, which was Lambert's original reading. It is unbounded, and its extreme values differ by market and timeframe.
10With other studies
Read beside Bollinger Bands or %B, which measure the same distance in standard deviations; and beside a trend measure, to know which of its two readings applies.
11What it does not tell you
The CCI is unbounded; values of ±300 occur in strong moves, and a reading above +100 can persist through a trend.
12Inputs and defaults
- 1Period — 14The window of the average and of the mean deviation. Lambert suggested about a third of the cycle length he expected. A short CCI passes ±100 often; a long one seldom.
- 2Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
- 3Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.
The settings in use
- In use14 and 20 are the usual windows. Lambert suggested a third of the expected cycle length; short windows (5 to 9) pass ±100 constantly.
13Limits and common misreadings
Unbounded. There is no ceiling: readings of ±300 occur, and their size cannot be compared across instruments or periods.
A flat window divides by almost nothing. When the last N typical prices are nearly equal, the mean deviation is tiny and a small move gives a huge reading.
14Conditions an alert or a strategy can watch
an event happens on one bar a state holds on every bar while it lasts
These are the conditions the study itself declares — the same rules the alert editor and the strategy engine read, and the ones the figure's chips show.
15Practise it in the laboratory
Put CCI 20 on a chart and replay a trend. Count the bars the line spends beyond +100 before price first pulls back to its 20-bar average.
Open the laboratory17In Bentrix
- From the menu: a chart's Indicators button › Oscillators › CCI, or type CCI in its search.
- Its settings: the settings card opens when it is added; its legend's name opens it again.
18From an agent or a program
Two tools reach it. study_values computes it over a workbench’s candles up to the clock — no chart needed — and study_attach puts it on a chart, where it is drawn and saved with the chart ({"chart": "<chart uri>", "kind": "cci"}).
The study, as a chart files it
This is a new CCI’s entry — its kind, its calculation, the price it reads and its settings. Change a setting here to read it with other inputs.
{
"applied": "close",
"convention": "mt5",
"kind": "cci",
"settings": {
"period": 14,
"shape": "period"
}
}An agent
An agent calls the MCP tool with it:
{
"name": "study_values",
"arguments": {
"bench": "bentrix://lab/\u2026/bench/\u2026",
"symbol": "EURUSD",
"timeframe": "H1",
"limit": 5,
"study": {
"applied": "close",
"convention": "mt5",
"kind": "cci",
"settings": {
"period": 14,
"shape": "period"
}
}
}
}A program
A program does the same in its language — bench and market as in the built-in indicators guide:
Python
values = client.study.values(bench, market, "H1", {"applied": "close", "convention": "mt5", "kind": "cci", "settings": {"period": 14, "shape": "period"}}, limit=5)
print("times:", values["times"])
for line in values["lines"]:
print("line:", line["line"])
print(" newest values:", line["values"])JavaScript
const values = await client.study.values({ bench, symbol: market, timeframe: "H1", study: { "applied": "close", "convention": "mt5", "kind": "cci", "settings": { "period": 14, "shape": "period" } }, limit: 5 });
console.log("times:", values.times);
for (const line of values.lines) {
console.log("line:", line.line);
console.log(" newest values:", line.values);
}C#
var values = await client.Study.Values(bench, market, "H1", new Dictionary<string, object?> { ["applied"] = "close", ["convention"] = "mt5", ["kind"] = "cci", ["settings"] = new Dictionary<string, object?> { ["period"] = 14, ["shape"] = "period" } }, limit: 5);
Console.WriteLine($"times: {values!["times"]!}");
foreach (var line in values!["lines"]!.AsArray())
{
Console.WriteLine($"line: {line!["line"]!}");
Console.WriteLine($" newest values: {line!["values"]!}");
}Go
values := must(client.Study.Values(ctx, mcp.StudyValuesArgs{Bench: bench, Symbol: market, Timeframe: "H1", Study: map[string]any{"applied": "close", "convention": "mt5", "kind": "cci", "settings": map[string]any{"period": 14, "shape": "period"}}, Limit: mcp.Ptr[int64](5)}))
fmt.Println("times:", at(values, "times"))
for _, line := range at(values, "lines").([]any) {
fmt.Println("line:", at(line, "line"))
fmt.Println(" newest values:", at(line, "values"))
}Java
Object values = client.study.values(new Bentrix.StudyApi.ValuesArgs(bench, market, "H1", obj("applied", "close", "convention", "mt5", "kind", "cci", "settings", obj("period", 14, "shape", "period"))).limit(5L));
System.out.println("times:" + " " + at(values, "times"));
for (Object line : (List<Object>) at(values, "lines")) {
System.out.println("line:" + " " + at(line, "line"));
System.out.println(" newest values:" + " " + at(line, "values"));
}C++
auto values = client.study.values_(StudyApi::ValuesArgs(bench, market, "H1", Json::Object{{"applied", "close"}, {"convention", "mt5"}, {"kind", "cci"}, {"settings", Json::Object{{"period", 14}, {"shape", "period"}}}}).limit(5));
std::cout << "times:" << " " << values["times"].dump() << std::endl;
for (const Json &line : values["lines"].arr()) {
std::cout << "line:" << " " << line["line"].dump() << std::endl;
std::cout << " newest values:" << " " << line["values"].dump() << std::endl;
}Julia
values = client.study.values(bench, market, "H1", Dict{String,Any}("applied" => "close", "convention" => "mt5", "kind" => "cci", "settings" => Dict{String,Any}("period" => 14, "shape" => "period")); limit = 5)
println("times:", " ", values["times"])
for line in values["lines"]
println("line:", " ", line["line"])
println(" newest values:", " ", line["values"])
endR
values <- client$study$values(bench, market, "H1", list(applied = "close", convention = "mt5", kind = "cci", settings = list(period = 14, shape = "period")), limit = 5)
cat("times:", format(values$times), "\n")
for (line in values$lines) {
cat("line:", format(line$line), "\n")
cat(" newest values:", format(line$values), "\n")
}Rust
let values = client.study().values(mcp::StudyValuesArgs::new(bench.clone(), market.clone(), "H1", serde_json::json!({"applied": "close", "convention": "mt5", "kind": "cci", "settings": {"period": 14, "shape": "period"}})).limit(5))?;
println!("times: {}", values["times"]);
for line in values["lines"].as_array().into_iter().flatten() {
println!("line: {}", line["line"]);
println!(" newest values: {}", line["values"]);
}Reading the answer
times— the candles’ open times, in milliseconds since 1970 (UTC), oldest first.lines— one entry per line, in this order, each with itslinename and itsvalues;values[i]belongs to the candle attimes[i].- The last value is the candle at the clock’s, which may still be forming: it changes until that candle closes, as on the chart. Nothing after the clock is ever computed.
nullis a candle with no value yet — the warm-up, about 1 × the period at the start of the history.- Its fixed levels, drawn on the chart:
[-100.0, 100.0].
line | What it is |
|---|---|
Main | the indicator's own value |
In a strategy
Filed under a key of your choice in a strategy document’s studies — here cci: {"cci": {"applied": "close", "convention": "mt5", "kind": "cci", "settings": {"period": 14, "shape": "period"}}} — its rules can name:
| Write | What it is |
|---|---|
cci | its main line, as a value in a compare |
{"cond": "cci:level.cross_up:-100"} | a condition it declares itself |
{"cond": "cci:level.cross_down:-100"} | a condition it declares itself |
{"cond": "cci:level.above:-100"} | a condition it declares itself |
{"cond": "cci:level.below:-100"} | a condition it declares itself |
{"cond": "cci:level.cross_up:100"} | a condition it declares itself |
{"cond": "cci:level.cross_down:100"} | a condition it declares itself |
{"cond": "cci:level.above:100"} | a condition it declares itself |
{"cond": "cci:level.below:100"} | a condition it declares itself |
19Sources
- Lambert, Donald R. "Commodity Channel Index: Tools for Trading Cyclic Trends." Commodities, October 1980.
Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed