使用熊猫计算相对强弱指数

我正在尝试使用 Pandas 计算相对强度指数 (RSI),但似乎无法正确调整此处提供的解决方案。为什么这不返回 RSI 系列?


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(series):    

    delta = series.diff()

    u = delta * 0 

    d = u.copy()

    i_pos = delta > 0

    i_neg = delta < 0

    u[i_pos] = delta[i_pos]

    d[i_neg] = delta[i_neg]

    rs = moments.ewma(u, span=27) / moments.ewma(d, span=27)

    return 100 - 100 / (1 + rs)


print(rsi(price, n))


慕后森
浏览 139回答 1
1回答

慕哥6287543

这是黑暗中的一个镜头,因为您没有提供太多背景信息。pandas.stats.moment.ewma0.23.0 不再支持。指数加权窗口现在使用pd.Series.ewm. 这将返回指数加权的窗口对象窗口对象,如果不提供滚动窗口的方法,就不能在任何类型的方程中使用该对象。以下是可用方法的列表:rs.agg&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.apply&nbsp; &nbsp; &nbsp; &nbsp;rs.count&nbsp; &nbsp; &nbsp; &nbsp;rs.exclusions&nbsp; rs.max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.median&nbsp; &nbsp; &nbsp; rs.name&nbsp; &nbsp; &nbsp; &nbsp; rs.skew&nbsp; &nbsp; &nbsp; &nbsp; r.sumrs.aggregate&nbsp; &nbsp;rs.corr&nbsp; &nbsp; &nbsp; &nbsp; rs.cov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.kurt&nbsp; &nbsp; &nbsp; &nbsp; rs.mean&nbsp; &nbsp; &nbsp; &nbsp; rs.min&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.quantile&nbsp; &nbsp; rs.std&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.var我假设你从这里复制了上面的函数,它甚至似乎没有回答 OP。如果您想对price.Close具有跨度的系列进行此分析n并计算mean每个指数加权窗口的 :import pandas_datareader.data as webimport datetimeimport pandas as pdewma = pd.Series.ewmstart = datetime.datetime(2018, 2, 8)end = datetime.datetime(2019, 2, 8)stock = 'TNA'price = web.DataReader(stock,'yahoo', start, end)n = 14def RSI(series,n):&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; delta = series.diff()&nbsp; &nbsp; u = delta * 0&nbsp;&nbsp; &nbsp; d = u.copy()&nbsp; &nbsp; i_pos = delta > 0&nbsp; &nbsp; i_neg = delta < 0&nbsp; &nbsp; u[i_pos] = delta[i_pos]&nbsp; &nbsp; d[i_neg] = delta[i_neg]&nbsp; &nbsp; rs = ewma(u, span=n).mean() / ewma(d, span=n).mean()&nbsp; &nbsp; return 100 - 100 / (1 + rs)print(RSI(price.Close,n))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python