All indicators
Momentum / Own pane

Commodity Channel Index

How far the typical price is from its average, in units of its mean deviation, with +100 and -100 bands.

  • CCI
  • Built into Spectral
  • Updates every tick
  • cci
  • commodity channel index
  • oscillator
  • overbought
  • oversold
  • momentum
  • mean deviation

By Spectral · v1.0.0

MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.

Already in Spectral: add it from the Indicators toolbox. The file here is the same indicator written as a custom indicator, to change or learn from. Installed, it appears next to the built-in one, marked Custom.

Commodity Channel Index on a Spectral chart
Spectral chart with sample market data, default settings.

Overview

The Commodity Channel Index (Donald Lambert) compares the typical price with its simple average over the last Length bars and divides the difference by the mean absolute deviation, scaled by 0.015 so that most values fall between +100 and -100. Zero means price is at its average.

How to read it

Above +100 price is unusually far above its average for the recent variability, below -100 unusually far below. A move beyond +100 can mark the start of a strong advance rather than an overbought market, and a fall back inside the band shows that the push is fading.

The zero line separates price above and below its average. Divergences between the CCI and price, and turns near the bands, are context for price structure, not standalone signals.

Settings and limitations

Length defaults to 20 bars with the typical price, as on TradingView; 14 is also common, and longer lengths such as 50 suit trend filtering. The optional smoothing line is a simple average of the CCI. The first value appears on bar Length (bar 20 with the default). When every price in the window is the same, there is no value.

Notes

Matches the CCI on TradingView and most platforms: (price - average) / (0.015 x mean deviation), with a simple average and mean absolute deviation over the same window.

Settings

Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.

SettingTypeDefaultRange or optionsWhat it does
LengthWhole number201 – 500Bars in the average and the mean deviation.
SourcePrice sourcehlc3open, high, low, close, hlc3Price the index is calculated from; the typical price (high + low + close) / 3 is standard.
Show smoothing lineOn / offOff—Draw a simple average of the CCI over it.
Smoothing lengthWhole number141 – 500Bars in the average of the smoothing line.
Upper bandNumber1000 – 500, step 10Level of the upper reference band.
Lower bandNumber-100-500 – 0, step 10Level of the lower reference band.
Fill between bandsOn / offOn—Shade the area between the upper and lower band.

Presets

Each preset is its own entry in the Indicators toolbox, with these settings changed.

Toolbox entryShort nameSettings
CCI (14)CCI 14Length: 14
CCI (50)CCI 50Length: 50

What it draws

Lines

  • CCIsolid · 1.5 px · primary#8AA9C3
  • Smoothingsolid · 1 px#C4A46A

Levels

  • Upper banddashed · 1 px#788493
  • Zero linedotted · 1 px#788493
  • Lower banddashed · 1 px#788493

Zones

  • Band fillfilled at 10% opacity#8AA9C3

Source

Save it as cci.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.

// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt

/** Mean absolute deviation of the last `length` values from their average, per bar; null until the window is full. */
function meanDeviation(values, averages, length) {
  return values.map((value, index) => {
    const average = averages[index];
    if (average === null || index < length - 1) return null;
    let total = 0;
    for (let offset = 0; offset < length; offset += 1) total += Math.abs(values[index - offset] - average);
    return total / length;
  });
}

/** @type {import('./spectral-indicator').IndicatorV2} */
export default {
  schemaVersion: 2,
  id: 'cci',
  name: 'Commodity Channel Index',
  shortName: 'CCI',
  version: '1.0.0',
  author: 'Spectral',
  description: 'How far the typical price is from its average, in units of its mean deviation, with +100 and -100 bands.',
  pane: 'oscillator',
  live: { lookback: 'length' },
  inputs: {
    length: { type: 'integer', label: 'Length', default: 20, min: 1, max: 500, description: 'Bars in the average and the mean deviation.' },
    source: { type: 'source', label: 'Source', default: 'hlc3', description: 'Price the index is calculated from; the typical price (high + low + close) / 3 is standard.' },
    showSmoothing: { type: 'boolean', label: 'Show smoothing line', default: false, description: 'Draw a simple average of the CCI over it.' },
    smoothingLength: { type: 'integer', label: 'Smoothing length', default: 14, min: 1, max: 500, description: 'Bars in the average of the smoothing line.' },
    upperBand: { type: 'number', label: 'Upper band', default: 100, min: 0, max: 500, step: 10, description: 'Level of the upper reference band.' },
    lowerBand: { type: 'number', label: 'Lower band', default: -100, min: -500, max: 0, step: 10, description: 'Level of the lower reference band.' },
    showFill: { type: 'boolean', label: 'Fill between bands', default: true, description: 'Shade the area between the upper and lower band.' },
  },
  plots: {
    cci: { label: 'CCI', color: '#8AA9C3', lineWidth: 1.5, primary: true },
    smoothing: { label: 'Smoothing', color: '#C4A46A', lineWidth: 1 },
  },
  levels: {
    upper: { label: 'Upper band', color: '#788493', lineStyle: 'dashed' },
    zero: { label: 'Zero line', color: '#788493', lineStyle: 'dotted' },
    lower: { label: 'Lower band', color: '#788493', lineStyle: 'dashed' },
  },
  zones: {
    fill: { label: 'Band fill', color: '#8AA9C3', opacity: 0.1 },
  },
  presets: [
    { id: 'short', name: 'CCI (14)', shortName: 'CCI 14', inputs: { length: 14 } },
    { id: 'long', name: 'CCI (50)', shortName: 'CCI 50', inputs: { length: 50 } },
  ],
  docs: {
    overview: 'The Commodity Channel Index (Donald Lambert) compares the typical price with its simple average over the last Length bars and divides the difference by the mean absolute deviation, scaled by 0.015 so that most values fall between +100 and -100. Zero means price is at its average.',
    howToRead: 'Above +100 price is unusually far above its average for the recent variability, below -100 unusually far below. A move beyond +100 can mark the start of a strong advance rather than an overbought market, and a fall back inside the band shows that the push is fading.\n\nThe zero line separates price above and below its average. Divergences between the CCI and price, and turns near the bands, are context for price structure, not standalone signals.',
    settings: 'Length defaults to 20 bars with the typical price, as on TradingView; 14 is also common, and longer lengths such as 50 suit trend filtering. The optional smoothing line is a simple average of the CCI. The first value appears on bar Length (bar 20 with the default). When every price in the window is the same, there is no value.',
    notes: 'Matches the CCI on TradingView and most platforms: (price - average) / (0.015 x mean deviation), with a simple average and mean absolute deviation over the same window.',
  },
  calculate(ctx) {
    const { bars, inputs, ta } = ctx;
    const source = ta.source(bars, inputs.source);
    const average = ta.sma(source, inputs.length);
    const deviation = meanDeviation(source, average, inputs.length);
    const cci = source.map((value, index) => {
      const mean = average[index];
      const spread = deviation[index];
      // A flat window leaves only rounding noise in both terms; treat it as no range instead of a ratio of noise.
      return mean === null || spread === null || spread <= 1e-10 * Math.max(1, Math.abs(mean)) ? null : (value - mean) / (0.015 * spread);
    });
    const smoothing = inputs.showSmoothing ? ta.sma(cci, inputs.smoothingLength) : bars.map(() => null);
    const band = (value) => bars.map(() => (inputs.showFill ? value : null));
    return {
      plots: { cci, smoothing },
      levels: { upper: inputs.upperBand, zero: 0, lower: inputs.lowerBand },
      zones: { fill: { upper: band(inputs.upperBand), lower: band(inputs.lowerBand) } },
    };
  },
};

Adapt it with an LLM

Paste this into your assistant and replace the second-to-last line with the change you want.

Start from this Spectral custom indicator: https://spectral.trade/indicators/cci.js
Format reference: https://spectral.trade/docs/custom-indicators.llm.md
Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match.
Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow.
Change I want: <describe the change>
Answer with the complete file in one code block, ready to save as <id>.js.