你好,我感觉你这写错了,首先第一步要对close做差分,然后通过这个差分与0的大小关系来进一步计算,我感觉你代码里没有这个判断(我是门外汉,我也不清楚,希望可以帮到你)
def rsi(price,period=6):
import pandas as pd
import numpy as np
clprcChange=price-price.shift(1)
clprcChange=clprcChange.dropna()
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([price,clprcChange,upPrc,downPrc])
rsidata.columns=[\'price\',\'PrcChange\',\'upPrc\',\'downPrc\']
rsidata=rsidata.dropna()
SMUP=[]
SMDOWN=[]
for i in range(period,len(upPrc)+1):
SMUP.append(np.mean(upPrc.values[(i-period):i],dtype=np.float32))
SMDOWN.append(np.mean(downPrc.values[(i-period):i],dtype=np.float32))
rsi=[100*SMUP[i]/(SMUP[i]+SMDOWN[i]) for i in range(0,len(SMUP))]
indexRsi=indexprc[(period-1):]
rsi=pd.Series(rsi,index=indexRsi)
return(rsi)