VariablePeriodCCI
Background
The Commodity Channel Index (CCI), developed by Donald Lambert, measures how far price has moved away from its statistical mean. It compares the current price to a moving average and scales that difference by the average mean deviation, so most readings fall within a band of roughly −100 to +100. Traders use it to spot overbought and oversold conditions and to flag the start of new trends when price pushes beyond those bands.
A normal CCI uses a fixed look-back. VariablePeriodCCI takes a
period for each bar instead, so it can tighten or widen its window
as market conditions change. The per-bar periods usually come from an adaptor such as
the DominantCycle, or from any array you supply.
Included formula
The toolbox ships a ready-made Adaptive Smoothed CCI indicator built on this function, so you can use it without writing any code. In AmiBroker open the Charts window, expand the WiseTraderToolbox group and drag Adaptive Smoothed CCI onto a chart.
It plots the CCI in its own pane below price, with optional overbought/oversold threshold lines and a signal line. A built-in system marks buys and sells — you choose whether they come from the signal-line crossovers or from the CCI crossing the thresholds — and the same formula can be run as an Exploration from the Analysis window to scan a watchlist for the latest signal. The Adaptor you pick produces the per-bar period this function needs, an optional Smoother cleans up the input first, and the colours, thresholds and bounds are all adjustable.
The exported function
If you would rather build your own formula, the toolbox exports the calculation as a single AFL function you can call directly — it is the engine inside that indicator.
VariablePeriodCCI(Price, Periods)
| Parameter | Description |
|---|---|
| Price | The price array the indicator is calculated on. |
| Periods | An array (or value) giving the CCI period to use at each bar. Most often this
is fed from an adaptor such as the DominantCycle. |
Usage
periods = DominantCycle( Close );
cci = VariablePeriodCCI( ( High + Low + Close ) / 3, periods );
Plot( cci, "Adaptive CCI", colorRed );
Plot( 100, "", colorGrey40, styleDashed | styleNoLabel );
Plot( -100, "", colorGrey40, styleDashed | styleNoLabel );
CCI is often read against the ±100 bands: a security is generally considered oversold below −100 and overbought above +100. A buy signal might be given as CCI climbs back above −100, and a sell signal as it drops back below +100.