RSI标志买方和卖方的相对力量强弱
R S I = 100 ∗ U P U P + D O W N RSI=100 * \frac{UP}{UP+DOWN} RSI=100∗UP+DOWNUP
UP表示t期内股价上涨的平均值,DOWN表示t期内股价下跌的平均值
以交通银行为例进行分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
BOCM=pd.read_csv('BOCM.csv')
BOCM.index=BOCM.iloc[:,1]
BOCM=BOCM.iloc[:,2:]
BOCM.head()
Open | High | Low | Close | Volume | |
---|---|---|---|---|---|
Date | |||||
2014-01-02 | 3.82 | 3.84 | 3.80 | 3.82 | 57317900 |
2014-01-03 | 3.81 | 3.83 | 3.76 | 3.79 | 64039600 |
2014-01-06 | 3.79 | 3.79 | 3.72 | 3.75 | 73494700 |
2014-01-07 | 3.73 | 3.79 | 3.72 | 3.77 | 48477500 |
2014-01-08 | 3.77 | 3.84 | 3.76 | 3.80 | 47952000 |
BOCMclp=BOCM.Close
clprcChange=BOCMclp-BOCMclp.shift(1)
clprcChange=clprcChange.dropna()
clprcChange[0:6]
Date
2014-01-03 -0.03
2014-01-06 -0.04
2014-01-07 0.02
2014-01-08 0.03
2014-01-09 -0.01
2014-01-10 0.01
Name: Close, dtype: float64
#upPrc 表示上涨
#downPrc 表示下跌
indexprc=clprcChange.index
upPrc=pd.Series(0,index=indexprc)
upPrc[clprcChange>0]=clprcChange[clprcChange>0]
downPrc=pd.Series(0,index=indexprc)
downPrc[clprcChange<0]=-clprcChange[clprcChange<0]
rsidata=pd.concat([BOCMclp,clprcChange,upPrc,downPrc],axis=1)
rsidata.columns=['Close','PrcChange','upPrc','downPrc']
rsidata=rsidata.dropna()
rsidata.head()
/Users/yaochenli/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass 'sort=False'.
To retain the current behavior and silence the warning, pass 'sort=True'.
Close | PrcChange | upPrc | downPrc | |
---|---|---|---|---|
2014-01-03 | 3.79 | -0.03 | 0.00 | 0.03 |
2014-01-06 | 3.75 | -0.04 | 0.00 | 0.04 |
2014-01-07 | 3.77 | 0.02 | 0.02 | 0.00 |
2014-01-08 | 3.80 | 0.03 | 0.03 | 0.00 |
2014-01-09 | 3.79 | -0.01 | 0.00 | 0.01 |
# 使用简单平均数计算交通银行收盘价6日的上涨力度和下跌力度
SMUP=[]
SMDOWN=[]
for i in range(6,len(upPrc)+1):
SMUP.append(np.mean(upPrc.values[(i-6):i],dtype=np.float32))
SMDOWN.append(np.mean(downPrc.