python:talib 计算 RSI

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)
if df.empty ==True:
    print(" df is empty ")
    sys.exit(2)

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()
# 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

000001_RSI.png

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值