Indie Script Developer Portal
Commodity Channel Index
OscillatorsBuilt-in Indie v5 1 source file

Source files1

Click a file to open it in the viewer
README.md

Commodity Channel Index (CCI) - Built-in Indicator Guide

Computes the Commodity Channel Index from a price source and plots it with fixed ±100 and 0 levels.

   
Language Indie Script v5
Platform TakeProfit
Category Oscillators
Type Built-in indicator
Author TakeProfit
License MIT
Documentation Built-in indicators
Source file Commodity Channel Index.indie5

Overview

The Commodity Channel Index measures the current value of a chosen price source relative to its average over length bars, using a mean-deviation scaling. It is an oscillator meant to show how far price has moved from a simple average and can help identify cyclical or overextended moves.

On the chart, a blue line shows the CCI value. A light aqua background fill is drawn between -100 and +100, with gray boundary lines, and a gray line is drawn at 0. The band and center line are static chart decorations; they are not modified by the CCI calculation.

How it works

  1. Main receives length and src from the settings generated by @param.int and @param.source.
  2. Cci.new(src, length) creates a rolling CCI series that keeps the required sums inside the algorithm.
  3. For each bar the algorithm uses the last length values of src to compute a simple moving average and the mean absolute deviation from that average.
  4. It forms the CCI value: current src minus the moving average, divided by 0.015 times the mean deviation.
  5. [0] selects the current bar’s CCI value, and Main returns it so @plot.line can draw it as a blue line.
  6. The @band and @level decorations are drawn independently on every bar.

Mathematical model

The Cci algorithm computes the standard CCI formula for the selected source P (default HLC3) with period n = length:

\[P = \frac{H + L + C}{3}\] \[A_t = \frac{1}{n} \sum_{i=0}^{n-1} P_{t-i}\] \[D_t = \frac{1}{n} \sum_{i=0}^{n-1} |P_{t-i} - A_t|\] \[CCI_t = \frac{P_t - A_t}{0.015 \cdot D_t}\]

Parameters

Parameter Type Default Range Description
length int 20 ≥ 1  
src source source.HLC3   Source

Code walkthrough

Indicator declaration and inputs

Lines 6-8 of Commodity Channel Index.indie5:

@indicator('CCI', format=format.PRICE)  # Commodity Channel Index
@param.int('length', default=20, min=1)
@param.source('src', default=source.HLC3, title='Source')

The @indicator decorator names the script ‘CCI’ and requests price-style formatting. @param.int('length', default=20, min=1) publishes an integer input, while @param.source('src', default=source.HLC3, title='Source') publishes a source selector. The defaults make the indicator start with period 20 and use HLC3.

Fixed chart layers

Lines 9-10 of Commodity Channel Index.indie5:

@band(-100, 100, line_color=color.GRAY, fill_color=color.AQUA(0.1), title='Background')
@level(0, line_color=color.GRAY(0.5), title='Middle Band')

@band(-100, 100, ...) draws fixed horizontal boundaries at -100 and 100 with a light aqua fill between them. @level(0, ...) adds a 50% gray center line at zero. These layers do not depend on src or length.

Plot and calculation

Lines 11-13 of Commodity Channel Index.indie5:

@plot.line(color=color.BLUE, title='CCI')
def Main(self, length, src):
    return Cci.new(src, length)[0]

Cci.new is a series-returning algorithm. The [0] index returns the value for the current bar; using [1] would access the previous bar. The function returns this single number, which the @plot.line decorator maps to a blue chart line.

Reading the chart

  • The blue line is the current CCI reading; its height tells how far src is from its rolling average in mean-deviation units.
  • The aqua-filled area between -100 and 100 is the @band background; a CCI inside it is between the two fixed reference lines.
  • Values above 100 go above the upper band boundary; values below -100 go below the lower boundary.
  • The gray line at 0 is the Middle Band; CCI crossing above or below it corresponds to src being above or below its rolling average.

Implementation notes

  • Cci.new(src, length) encapsulates the whole calculation; Main contains no manual indicator math.
  • Indexing the returned series with [0] reads the current bar. The same series can be indexed with [1] to inspect the previous value in a modified script.
  • length has a UI minimum of 1 and a default of 20; changing it affects the number of bars included in the average and deviation.
  • format=format.PRICE controls display formatting only; it does not affect the CCI calculation.

FAQ

How is HLC3 calculated?

HLC3 is (High + Low + Close) / 3. It is the default source fed into Cci.new; the Source setting can replace it with another series.

Can I plot a smoothed CCI?

This built-in script does not apply smoothing. It returns Cci.new(src, length)[0] directly; to smooth, you would need to wrap the returned series in an additional algorithm and plot that.

What do the -100 and +100 lines mean in this script?

They are fixed values passed to @band and are only visual reference levels; the script does not use them for alerts or signals.

Full source code

Indie Script v5, the built-in indicator as shipped with TakeProfit. Add it from the indicators menu or copy the code into the platform’s script editor.

# indie:lang_version = 5
from indie import indicator, format, param, source, band, color, level, plot
from indie.algorithms import Cci


@indicator('CCI', format=format.PRICE)  # Commodity Channel Index
@param.int('length', default=20, min=1)
@param.source('src', default=source.HLC3, title='Source')
@band(-100, 100, line_color=color.GRAY, fill_color=color.AQUA(0.1), title='Background')
@level(0, line_color=color.GRAY(0.5), title='Middle Band')
@plot.line(color=color.BLUE, title='CCI')
def Main(self, length, src):
    return Cci.new(src, length)[0]
    # TODO: implement smoothing when display.none is supported