我正在通过导入数据并对其进行操作进行编码。在最后一步中,我将pd.rolling_apply
与三个参数一起使用。该代码有错误,在该行上使用#
会使该代码不返回任何内容。
我已经尝试过多次在google上查看该功能是否对其他人有用。我也尝试过注释该行,并用价格和什么都没有替换pd,但无济于事。
import pandas_datareader.data as web
import datetime
start = datetime.datetime(2018, 2, 8)
end = datetime.datetime(2019, 2, 8)
stock = 'TNA'
price = web.DataReader(stock,'yahoo', start, end)
n = 14
def rsi(price, n):
''' rsi indicator '''
gain = (price-price.shift(1)).fillna(0) # calculate price gain with
previous day, first row nan is filled with 0
def rsiCalc(p):
# subfunction for calculating rsi for one lookback period
avgGain = p[p>0].sum()/n
avgLoss = -p[p<0].sum()/n
rs = avgGain/avgLoss
return 100 - 100/(1+rs)
# run for all periods with rolling_apply
return pd.rolling_apply(gain,n,rsiCalc)
print(rsi(price, n=14))
期望代码在给定的时间内打印出rsi。 遇到错误,那不是结果。