为什么我的 RSI 计算结果与雅虎财经相差很大?

问题描述 投票:0回答:2

我正在使用

yfinance
库每天拉收盘股票价格并计算各种技术指标。有时,我的 RSI(相对强弱指数,对于那些想知道的人来说)与我在雅虎财经图表上看到的相符。然而,其他时候,情况却相差很多。我会假设雅虎财经知道他们在做什么,是我犯了错误,但我不知道错在哪里。

预期行为:我计算出的 RSI 值将与雅虎财经股票图表上看到的值相匹配。

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

例如,今天,2020 年 12 月 29 日,我从昨天计算出的 FB 的 RSI 是 38。雅虎显示为 52。然而,对于 T(AT&T 的符号),我的 RSI 是 41,而雅虎显示为 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)
python python-3.x pandas yfinance technical-indicator
2个回答
1
投票

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

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


0
投票

您正在使用简单移动平均线,请尝试使用怀尔德平滑。使用这个我能够获得正确的值。

© www.soinside.com 2019 - 2024. All rights reserved.