talib_cci.py  计算 CCI 求发出买入信号日期

Buy:买入,close:收盘价,rate:收益率

# -*- coding: utf-8 -*-
import os, sys
import tushare as ts
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import talib

if len(sys.argv) ==2:
    code = sys.argv[1]
else:
    print('usage: python talib_cci.py stockcode ')
    sys.exit(1)

if len(code) !=6:
    print('stock code length: 6')
    sys.exit(2)

df = ts.get_k_data(code)
if df.empty ==True:
    print(" df is empty ")
    sys.exit(2)

df = df[ df['date'] > '2021-01-01']
if len(df) <10:
    print(" len(df) <10 ")
    sys.exit(2)

df['ma10'] = df['close'].rolling(window=10).mean()
df.index = pd.to_datetime(df.date)
#  baike.baidu.com/item/CCI顺势指标
cci = talib.CCI(df.high, df.low, df.close, timeperiod=12)
print(cci.tail(10))
close = df['close'].values
# 求买入信号发出日期
for i in range(-30,-1):
	if cci.iloc[i]< -100 and (cci.iloc[i] < cci.iloc[i+1]):
		print(df[i:i+1])
		print('Buy {0} ,close:{1} , rate: {2:.2f}%'.format(close[i],close[-1],(close[-1]/close[i]-1)*100))
		print('cci minus: {0:.1f} {1:.1f} {2:.1f}'.format(cci.iloc[i-1],cci.iloc[i],cci.iloc[i+1]))

# 画股票收盘价图
fig,axes = plt.subplots(2,1)
df[['close', 'ma10']].plot(ax=axes[0], grid=True, title=code)
# 画 cci 曲线图
cci.plot(ax=axes[1], grid=True, label='CCI')
plt.legend(loc='best', shadow=True)
plt.show()

运行 python talib_cci.py 002555

                  date   open  close  high    low    volume    code    ma10
date
2021-08-30  2021-08-30  17.08  16.24  17.2  16.06  383144.0  002555  17.422
Buy 16.24 ,close:20.55 , rate: 26.54%
cci minus: -119.6 -218.6 -126.4
                  date   open  close   high    low    volume    code    ma10
date
2021-08-31  2021-08-31  15.87  17.05  17.38  15.87  660879.0  002555  17.366
Buy 17.05 ,close:20.55 , rate: 20.53%
cci minus: -218.6 -126.4 130.2

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐