【发布时间】: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