相對強弱指數(RSI)是通過比較一段時期內的平均收盤漲數和平均收盤跌數來分析市場買沽盤的意向和實力,從而作出未來市場的走勢。RSI在1978年6月由Wells Wider創制的一種通過特定時期內股價的變動情況計算市場買賣力量對比,來判斷股票價格內部本質強弱、推測價格未來的變動方向的技術指標。可以參考相對強弱指標,以及talib推薦的RSI介紹。
計算方法:RSI=[上升平均數÷(上升平均數+下跌平均數)]×100
上升平均數:在某一段日子里升幅數的平均;下跌平均數:在同一段日子里跌幅數的平均。
使用方法:
當RSI高于70時,股票可以被視為超買,是賣出的時候。
當RSI低于30時,股票可以被視為超賣,是買入的時候。
import pandas as pd
import numpy as np
import talib as ta
import tushare as ts
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
import seaborn as sns
sns.set_style('white')
from matplotlib import dates
import matplotlib as mpl
%matplotlib inline
myfont =mpl.font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=14)
plt.rcParams["figure.figsize"] = (20,10)
dw = ts.get_k_data("600600")
dw = dw[300:]
dw.index = range(len(dw))
close = dw.close.values
dw["rsi"] = ta.RSI(close, timeperiod=14)
#dw[["close","rsi"]].plot()
fig = plt.figure(figsize=(20,10))
fig.set_tight_layout(True)
ax1 = fig.add_subplot(111)
#fig.bar(dw.index, dw.volume, align='center', width=1.0)
ax1.plot(dw.index, dw.close, '-', color='g')
ax2 =ax1.twinx()
ax2.plot(dw.index, dw.rsi, '-', color='r')
ax2.plot(dw.index, [70]*len(dw), '-', color='r')
ax2.plot(dw.index, [30]*len(dw), '-', color='r')
ax1.set_ylabel(u"股票價格(綠色)",fontproperties=myfont, fontsize=16)
ax2.set_ylabel(u"RSI參數",fontproperties=myfont, fontsize=16)
ax1.set_title(u"綠色是股票價格,紅色(右軸)為RSI參數",fontproperties=myfont, fontsize=16)
# plt.xticks(bar_data.index.values, bar_data.barNo.values)
ax1.set_xlabel(u"RSI參數圖",fontproperties=myfont,fontsize=16)
ax1.set_xlim(left=-1,right=len(dw))
ax1.grid()
RSI