唐纳德·蓝伯特于上世纪80年代提出比较新颖的顺势指标CCI,其引进了价格与固定期间的股价平均区间的偏离程度的概念,强调股价平均绝对偏差在股市技术分析中的重要性。CCI有两个与大多数常见的分析指标不一样的特点,第一个是其并非只利用股票的单一数据特征,第二个是指标波动于正无穷与负无穷之间而0并不代表它的中轴线。计算方法:
CCI(N日)=(TP-MA)÷MD÷0.015
其中,
TP=(最高价+最低价+收盘价)÷3
MA=近N日收盘价的累计之和÷N
MD=近N日(MA-收盘价)的累计之和÷N
0.015为计算系数,N为计算周期
某些分析过程可以参考前面的博文,量化交易——传统技术分析相对强弱指数RSI的原理及实现,这里不细讲。
相关代码:
import numpy as np
import math
import random
import json
import matplotlib.pyplot as plt
import sys
sys.setrecursionlimit(10000)
#date|open|high|low|close|volume|adjsuted
def get_stock_hist(num):
s_his=np.genfromtxt('C:/Users/Haipeng/Desktop/python/Korea/Korea_{:03d}.csv'.format(num), delimiter=',')
s_hi=s_his[1:][:]
days=s_hi.shape[0]
this_stock = []
for i in range(1,days,1):
this_day = [i]
for k in range(1,7):
this_day.append(s_hi[i][k])
this_stock.append(this_day)
print 'Maximum date is ',len(this_stock)
return this_stock
def get_price(D, p_tpe):
if p_tpe=='close':
pos=4;
elif p_tpe=='open':
pos=1;
elif p_tpe=='high':
pos=2;
elif p_tpe=='low':
pos=3;
else:
pos=5
price=stock_hist[D-1][pos];
return price
def get_ma(D, N):
p_used=np.zeros(N);
for i in range(1,N+1,1):
p_used[i-1]=stock_hist[(D-1)-(i-1)][4];
ma=np.mean(p_used);
return ma
def get_mar(fro,to,N):
ma = []
for i in range(fro,to+1):
ma.append(get_ma(i,N))
return ma
#Date\Open\High\Low\Close
def get_tuples(fro,to):
res =[]
for d in range(fro,to+1):
tmp = []
tmp.append(d)
tmp.append(get_price(d,'open'))
tmp.append(get_price(d,'high'))
tmp.append(get_price(d,'low'))
tmp.append(get_price(d,'close'))
res.append(tmp)
return res
def get_volume(fro,to):
res = []
for d in range(fro,to+1):
num = 1
try:
if get_price(d,'close')1,'close'):
num = -1
except:
pass
res.append(num*get_price(d,'volume'))
return res
#CCI实现
def get_CCI(D,N):
TP = (get_price(D,'high')+get_price(D,'low')+get_price(D,'close'))/3.0
MA = 0.0
MD = 0.0
for i in range(N):
MA += get_price(D-i,'close')
MA = MA/N
for i in range(N):
MD += abs(get_price(D-i,'close')-MA)
MD = MD/N
return (TP - MA)/MD/0.015
def get_cci(fro,to,N):
res = []
for d in range(fro,to+1):
res.append(get_CCI(d,N))
return res
绘图代码:
def plot_CCI(fro,to):
volume = get_volume(fro,to)
cci7 = get_cci(fro,to,7)
cci14 = get_cci(fro,to,14)
ma5 = get_mar(fro,to,5)
ma10 = get_mar(fro,to,10)
ma20 = get_mar(fro,to,20)
tuples = get_tuples(fro,to)
date = [d for d in range(fro,to+1)]
fig = plt.figure(figsize=(8,5))
p1 = plt.subplot2grid((5,4),(0,0),rowspan=3,colspan=4,axisbg='k')
p1.set_title("Commodity Channel Index(CCI)")
p1.set_ylabel("Price")
p1.plot(date,ma5,'m')
p1.plot(date,ma10,'b')
p1.plot(date,ma20,'y')
p1.legend(('MA5','MA10','MA20'))
p1.grid(True,color='w')
candlestick_ohlc(p1, tuples, width=0.7,colorup='r',colordown="g")
p2 = plt.subplot2grid((5,4),(3,0),colspan=4,axisbg='c')
p2.set_ylabel("Volume")
colors = []
for i in range(len(volume)):
if volume[i]<0:
colors.append('green')
volume[i] = -volume[i]
else:
colors.append('red')
p2.bar(date,volume,color=colors)
p3 = plt.subplot2grid((5,4),(4,0),colspan=4,axisbg='y')
p3.set_ylabel("CCI")
p3.set_xlabel("Dates")
p3.plot(date,cci7, 'm-')
p3.plot(date,cci14, 'g-')
p3.legend(('CCI7','CCI14'),loc='upper left')
plt.subplots_adjust(hspace=0)
plt.show()# show the plot on the screen
stock_hist = get_stock_hist(18)
plot_CCI(1000,1200)
#得图2
plot_CCI(1000,1080)