# CCI Stock Technical Indicator with Python

Last Update: February 21, 2020

Stock technical indicators are calculated by applying certain formula to stock prices and volume data. They are used to alert on the need to study stock price action with greater detail, confirm other technical indicators’ signals or predict future stock prices direction.

This topic is part of Stock Technical Analysis with Python course. Feel free to take a look at Course Curriculum.

This tutorial has an educational and informational purpose and doesn’t constitute any type of trading or investment advice. All content, including code and data, is presented for personal educational use exclusively and with no guarantee of exactness of completeness. Past performance doesn’t guarantee future results. Please read full Disclaimer.

An example of stock technical indicators is commodity channel index CCI  which consists of bounded oscillator that measures a stock price variation from its statistical mean. Twenty days and constant factor are commonly used to make sure most values fall within bands.

1. Technical indicator calculation.

1.1. Typical price calculation.

Where  = current period stock typical price,  = current period stock high price,  = current period stock low price,  = current period stock close price.

1.2. Typical prices smoothing calculation.

Where  = current period stock typical prices twenty days simple moving average,  = current period stock typical price.

1.3. Typical prices mean absolute deviation or average deviation calculation.

Where  = current period stock typical prices twenty days mean absolute deviation or average deviation,  = current period stock typical price,  = current period stock typical prices twenty days rolling arithmetic mean.

1.4. Commodity channel index calculation.

Where  = current period stock typical prices twenty days and constant factor commodity channel index,  = current period stock typical price,  = current period stock typical prices twenty days simple moving average,  = current period stock typical prices twenty days mean absolute deviation or average deviation,  = constant factor.

2. Python code example.

2.1. Import Python packages .

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import talib as ta

2.2. CCI stock technical indicator data reading.

• Data: S&P 500® index replicating ETF (ticker symbol: SPY) daily open, high, low, close, adjusted close prices and volume (2016).
SPY = pd.read_csv('Data//CCI-Stock-Technical-Indicator-Data.txt', index_col='Date', parse_dates=True)

2.3. CCI stock technical indicator calculation and chart.

• CCI technical indicator number of periods and constant factor not fixed and only included for educational purposes.
SPY['cci'] = ta.CCI(np.asarray(SPY['SPY.High']), np.asarray(SPY['SPY.Low']),
np.asarray(SPY['SPY.Close']), timeperiod=20)
fig1, ax = plt.subplots(2, sharex=True)
ax.plot(SPY['SPY.Close'])
ax.legend(loc='upper left')
ax.plot(SPY['cci'])
ax.legend(loc='upper left')
plt.suptitle('SPY Close Prices and CCI(20,0.015) Stock Technical Indicator')
plt.show()

 Donald R. Lambert. “Commodity Channel Index: Tool for Trading Cyclic Trends”. Commodities Magazine (now Futures Magazine). 1980.

 Travis E, Oliphant. “A guide to NumPy”. USA: Trelgol Publishing. 2006.

Stéfan van der Walt, S. Chris Colbert and Gaël Varoquaux. “The NumPy Array: A Structure for Efficient Numerical Computation”. Computing in Science & Engineering. 2011.

Wes McKinney. “Data Structures for Statistical Computing in Python.” Proceedings of the 9th Python in Science Conference. 2010.

John D. Hunter. “Matplotlib: A 2D Graphics Environment.” Computing in Science & Engineering. 2007.

Mario Fortier, TicTacTec LLC. “TA-Lib: Technical Analysis Library”. 1999, 2007.

Quantopian. “TA-Lib: Technical Analysis Library”. Python package version 0.4.9. 2018.

+