Expand description
§Commodity Channel Index (CCI)
TP = (high + low + close) / 3
SMA = mean(TP over N)
MD = mean( |TP − SMA| over N )
CCI = (TP − SMA) / (0.015 × MD)When mean deviation is zero (flat typical prices), CCI is None for that bar
(undefined scale), not a fake 0.
Default: period 20 (CciParams::period_20).
§Trading perspective
| Region | Habit (classic, not a rule) |
|---|---|
| CCI > +100 | Extended / breakout participation screen |
| CCI < −100 | Oversold / mean-reversion screen |
| Zero line cross | Momentum shift screen |
§vs RSI / Stochastic / WillR
| CCI | RSI | Stoch / %R | |
|---|---|---|---|
| Centered on | Typical price vs its mean | Close momentum | Close in HH–LL range |
| Bounds | Unbounded (soft ±100 zones) | 0–100 | 0–100 or −100–0 |
| Best narrative | “How stretched vs recent TP?” | Overbought/oversold on closes | Where is close in the range? |
Use CCI for channel stretch on HLC; RSI for pure close momentum; Stoch/%R for range position. They often agree at extremes but diverge in trends (CCI can stay > +100).
§Pairs well with
- Donchian / Bollinger / Keltner — breakout when CCI already extended.
- ADX — high ADX + CCI > +100 → trend continuation more than fade.
- ATR — size stops in price units, not CCI points.
§Engineering
CciParams → cci / CciState → cci_solution. Batch via CciState.
Mean deviation uses the current window mean, so each ready bar is O(period) over
the ring (fine for N≈20; not the same O(1) slide as Bollinger’s Σx² identity).
§Word problem
Constant H=L=C=100 for 20 bars. Is CCI defined?
No: MD = 0 → None (zero width).
use finance_solution::stocks::ta::{cci, CciParams};
let x = vec![100.0; 25];
let s = cci(&x, &x, &x, CciParams::period_20()).unwrap();
assert!(s.cci[19].is_none());Structs§
- CciParams
- CCI lookback (on typical price).
- CciSeries
- CciSolution
- CciState
- Incremental CCI. After warm-up each push is O(period) (mean absolute deviation).
- Validated
Cci
Functions§
- cci
- cci_
solution - Examples