0

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

1 回答 1

1

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

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

于 2021-01-28T06:30:50.053 回答