为什么我的 RSI 计算与雅虎财经相差甚远?

问题描述

我正在使用 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)

解决方法

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

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