【问题标题】:Why is my RSI calculation wildly off from Yahoo Finance?为什么我的 RSI 计算与 Yahoo Finance 大相径庭?
【发布时间】:2021-04-05 23:32:09
【问题描述】:

我正在使用yfinance 库每天拉取收盘价并计算各种技术指标。有时,我的 RSI(相对强弱指数,对于那些想知道的人)与我在雅虎财经图表上看到的相匹配。然而,其他时候,它会偏离很多。我会假设雅虎财经知道他们在做什么,是我犯了错误,但我不知道在哪里。

预期行为:我计算出的 RSI 值将与 Yahoo Finance 股票图表上的值匹配。

实际行为:我的 RSI 有时会偏离 10 或 15 个点,但有时却完美匹配。

例如,今天,2020 年 12 月 29 日,我从昨天为 FB 计算的 RSI 为 38。Yahoo 显示为 52。但对于 T(AT&T 的符号),我的 RSI 为 41,而 Yahoo 显示为 42 .

我已验证我的代码与我看到的其他示例相匹配,但除此之外我不知道在这里尝试什么。我不是数学家。

以下是我的确切代码:

import pandas as pd
import yfinance as yf

# Calculate Relative Strength Indicator (RSI) #
    gainz = []
    losses = []

    # Initialize variable for counting rows of prices
    n = 1

    # For each of the last 14 trading sessions...
    while n <= 14:  

        # ... calculate difference between closing price of each day and of the day before it.
        difference = ((df['Close'][-n]) - (df['Close'][-(n+1)]))

        # If difference is positive, add it to the positive list, and add 0 to the losses list 
        if difference > 0:
            gainz.append(difference)
            losses.append(0)

        # If negative, get the absolute value and add to the negative list, and add 0 to the gainz list
        elif difference < 0:
            losses.append(abs(difference))
            gainz.append(0)

        # Otherwise it must be zero, so add 0 to both lists
        else:
            gainz.append(0)
            losses.append(0)

        # Increment n to move to the next row
        n += 1
        
    avg_gainz = (sum(gainz))/14
    avg_losses = (sum(losses))/14

    RSvalue = (avg_gainz/avg_losses)

    RSI = (100 - (100/(1+RSvalue)))
    RSI = int(RSI)

【问题讨论】:

  • RSI 计算与 pandas 已经解释here
  • 我搜索了但由于某种原因没有找到这个......但它有很大帮助,所以谢谢你。我将使用talib 库,因为它似乎很容易进行此计算。

标签: python python-3.x pandas yfinance technical-indicator


【解决方案1】:

首先,我相信 RSI 计算是通过百分比收益/损失而不是原始美元收益/损失完成的。其次,您需要将收益数组和损失数组分别除以收益和损失的数量,以获得计算期间的平均收益或损失(不是 14)。例如。 7 收益和 7 损失意味着将每个数组除以 7。如果我错了,请纠正我,但您可以尝试这些修复,看看它们是否有效。

这个链接对我很有帮助:https://www.investopedia.com/terms/r/rsi.asp

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2021-03-20
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多