Skip to main content

Module cci

Module cci 

Source
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

RegionHabit (classic, not a rule)
CCI > +100Extended / breakout participation screen
CCI < −100Oversold / mean-reversion screen
Zero line crossMomentum shift screen

§vs RSI / Stochastic / WillR

CCIRSIStoch / %R
Centered onTypical price vs its meanClose momentumClose in HH–LL range
BoundsUnbounded (soft ±100 zones)0–1000–100 or −100–0
Best narrative“How stretched vs recent TP?”Overbought/oversold on closesWhere 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

CciParamscci / CciStatecci_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).
ValidatedCci

Functions§

cci
cci_solution
Examples