CCI(Commodity Channel Index) Technical Indicators Build in Python

CCI refered as Commodity Channel Index is one of the famous Oscillator it was discovered by Donald Lambert in 1980.CCI is mainly discovered for commodity but this channel is aslo famous for it's oversold/overbought levels so that it's used in another segment such as ETF's,Currencies and Equities.

CCI is same as Bollinger Bands we mainly consider to found out the Bullish and Bearish Outlook.

We consider +100 for overbought levels and -100 for oversold levels it is to be considered that in +100 overbought levels will give the opportunity for selling and vice versa but it is not taken guaranteed that after these levels it won't get cross these levels further also.

in this we mainly consider Typical price which is of contained High,Low and Close after this we take the consideration of Mean and of it's Standard Deviation.


Mean is nothing but getting the Averages of ndays by dividing them with the same value datasets of ndays for further explanation please refer this page as of all explanation is done about mean and moving average which is as follows : SMA(Simple Moving Average) Technical Indicators Build in Python.


To find out the standard deviation we will take an example of datasets say for example 1,2 and 4 the ndays in this is 3 and after that we add those datsets i.e 7 and then after we will divide this by 3 which is ndays of datasets i.e the value will be 2.3 and here we got our mean i.e M=2.3 and n=3 and then again we considered those datsets and subtract with the mean i.e 1.3,0.3 and -1.7.


Then those datasets need to be squared i.e 1.69,0.09 and 2.89 and then it is sumed up and divide by n-1 i.e. 2.335 and then we exactly findout the variance by finding square root of it which is 1.528 that's it you achieve here CCI std values which is need to get divide by further with the mean value of typical price and in this end we need to considere a 0.015 constant an inverse factor for the index so that we can have readable numbers


Extraction is done from yahoo finance and the symbol considerd is Nifty .To achieve this Technical Indicator value in csv and visualize through charting please follow the Vide for Proper Instruction and then Proceed for code input.


from pandas_datareader import data as web
import matplotlib.pyplot as plt
import fix_yahoo_finance
import pandas as pd

# Commodity Channel Index 
def CCI(data, ndays): 
 TP = (data['High'] + data['Low'] + data['Close']) / 3 
 CCI = pd.Series((TP - pd.rolling_mean(TP, ndays)) / (0.015*pd.rolling_std(TP, ndays)),
 name = 'CCI') 
 data = data.join(CCI) 
 return data

# Retrieve the Nifty data from Yahoo finance:
data = web.get_data_yahoo("^NSEI", start="2014-01-01", end="2017-07-24") 
data = pd.DataFrame(data)

# Compute the Commodity Channel Index(CCI) for NIFTY based on the 20-day Moving average
n = 20
NI = CCI(data, n)
CCI = NI['CCI']

# Plotting the Price Series chart and the Commodity Channel index below
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(2, 1, 1)
ax.set_xticklabels([])
plt.plot(data['Close'],lw=1)
plt.title('NSE Price Chart')
plt.ylabel('Close Price')
plt.grid(True)
bx = fig.add_subplot(2, 1, 2)
plt.plot(CCI,'k',lw=0.75,linestyle='-',label='CCI')
plt.legend(loc=2,prop={'size':9.5})
plt.ylabel('CCI values')
plt.grid(True)
plt.setp(plt.gca().get_xticklabels(), rotation=30)
plt.show()

---End of Code Input--

If you have any doubt regarding this please feel free to comment or whatsapp to the given number for further Queries.