Home » Technical Analysis Functions » Understanding the ta.cci() Function in Pine Script

Understanding the ta.cci() Function in Pine Script

Photo of author
Published on

The ta.cci() function, or Commodity Channel Index, is a versatile tool in Pine Script used for identifying cyclical trends in financial markets. It’s particularly useful in commodities trading, hence its name, but has found wide application across various asset classes. This function calculates the CCI by measuring the variation of a security’s price from its statistical mean, which helps traders to identify when a security is in an overbought or oversold condition.


ta.cci(source, length) → series float


  • source (series int/float): This is the series of values the CCI will process. It can be any series of numbers, typically the closing prices of a security but can also be open, high, or low prices.
  • length (series int): This specifies the number of bars, or periods, over which the CCI will calculate. The length impacts the sensitivity of the CCI indicator; shorter lengths result in a more sensitive CCI that can identify short-term trends, while longer lengths provide a smoother indicator better suited for identifying long-term trends.


  • Series float: The function returns the CCI as a floating-point series, which can be plotted on a chart or used as part of a trading strategy.


  • Handling of na values: It’s important to note that any na (not available) values in the source series are ignored during calculations. This ensures that the CCI is calculated only on available data points, maintaining the accuracy of the indicator.

Practical Example

Let’s apply the ta.cci() function in a practical Pine Script example to calculate and plot the CCI for a given asset over a specific period.

Example Code

indicator("My CCI Indicator", overlay=false)
priceSeries = close
periodLength = 20
myCCI = ta.cci(priceSeries, periodLength)
plot(myCCI, title="CCI", color=color.blue)


  • //@version=5
    • This line specifies the version of Pine Script being used for the script. Version 5 is the latest version, offering the most up-to-date features and functionality.
  • indicator("My CCI Indicator", overlay=false)
    • Declares the script as a custom indicator named “My CCI Indicator”.
    • overlay=false indicates that this indicator will not be plotted directly on the price chart but in a separate pane below it. This is typical for oscillators like the CCI.
  • priceSeries = close
    • Assigns the closing prices of the chart’s security to a variable named priceSeries. The CCI calculation will use this series of closing prices as its source data.
  • periodLength = 20
    • Sets the length of the period over which the CCI will be calculated to 20 bars. This parameter defines the timeframe for the indicator and can be adjusted based on the trader’s analysis requirements.
  • myCCI = ta.cci(priceSeries, periodLength)
    • Calculates the CCI based on the priceSeries and periodLength defined earlier. This line uses the built-in ta.cci() function to perform the calculation.
    • The result of the calculation is stored in a variable called myCCI, representing the CCI value for each bar over the specified period.
  • plot(myCCI, title="CCI", color=color.blue)
    • Plots the calculated CCI values on the chart using the plot() function.
    • The title of the plot is set to “CCI”, and the line color is specified as blue, making the indicator easily identifiable on the chart.

Key Features and Takeaways

  • Function Usability: The ta.cci() function is a powerful tool for trend identification, adaptable across various markets and securities.
  • Syntax and Application: Its syntax is straightforward, requiring a source series and a period length, making it accessible for both novice and experienced traders.
  • Adaptability: By adjusting the length parameter, traders can tailor the sensitivity of the CCI to match their trading strategy and the asset’s volatility.
  • Data Handling: The function’s ability to ignore na values ensures consistent and accurate calculations even with incomplete data series.

This detailed walkthrough of the ta.cci() function in Pine Script highlights its utility in financial analysis and trading strategy development, offering a robust method for identifying market trends.

Leave a Comment