RSI指标解释:baike.baidu.com/item/rsi顺势指标
talib_rsi.py
# -*- 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_rsi.py stockcode ')
sys.exit(1)
if len(code) !=6:
print('stock code length: 6')
sys.exit(2)
df = ts.get_k_data(code)
df = df[ df['date'] > '2020-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)
dw = pd.DataFrame()
# baike.baidu.com/item/rsi顺势指标
dw['rsi6'] = talib.RSI(df.close, timeperiod=6)
dw['rsi14'] = talib.RSI(df.close, timeperiod=14)
print("rsi6={0:.1f} , rsi14={1:.1f}".format(dw['rsi6'][-1], dw['rsi14'][-1]))
# 画股票收盘价图
fig,axes = plt.subplots(2,1)
df[['close', 'ma10']].plot(ax=axes[0], grid=True, title=code)
# 画 RSI 曲线图
dw[['rsi6', 'rsi14']].plot(ax=axes[1], grid=True)
plt.legend(loc='best', shadow=True)
plt.show()
运行 python talib_rsi.py 000001