I've been trying to calculate the 14 RSI of stocks but yfinance keeps giving me wrong numbers
import time
import pprint as pp
import pandas as pd
import pandas as pd
import datetime as dt
import pandas_datareader as web
ticker = 'TSLA'
start = dt.datetime(2018, 1, 1)
end = dt.datetime.now()
data = web.DataReader(ticker, 'yahoo', start, end)
delta = data['Adj Close'].diff(1)
delta.dropna(inplace=True)
positive = delta.copy()
negative = delta.copy()
positive[positive < 0] = 0
negative[negative > 0] = 0
days = 14
average_gain = positive.rolling(window=days).mean()
average_loss = abs(negative.rolling(window=days).mean())
relative_strenght = average_gain / average_loss
rsi = 100.0 - (100.0 / (1.0 + relative_strenght))
print(ticker + str(rsi))
It ends up giving me 77.991564 (14 days RSI) when I should be getting 70.13 (14 days RSI), does any know if there's a fix for it, or is there another library that would be better?
It's well known that frequently there are some wrong data in yf. Nevertheless, verify that you are calculating your RSI on the same close in both way. Should be Bid, Ask ou mid in both case.
The best thing is to get a pro account in a broker. And even in that situation, I used to had some bad surprise.
You should try to use https://alpaca.markets/docs/ because data are quite safe there.
I think that you should be satisfied anyway.