Search code examples
pythonyfinance

yfinance and yahoo finance data are very different


I use the yfinance package in the Python code below to get 5 years of daily price data for LGEN.L (Legal & General, a company listed on the London Stock Exchange for over 100 yrs). The result is the first figure below.

Then I go to the yahoo finance website and look up LGEN.L and click on the 5 yrs button: see second figure below (note: if you look up the share price from other resources, you get a very similar profile)

Although the most recent data (on the right hand side of the two figures) match at around 280, the older data (on the left hand side of the two figures) do not: the yfinance data starts at around 150, whereas the yfinance data starts at around 210; a huge difference

What am I doing wrong?

Python code:

import yfinance as yf
import matplotlib.pyplot as plt

isin = "LGEN.L"

# Extract 5 years of daily data
df = yf.download(tickers=isin, period="5y", interval="1d", auto_adjust=True, prepost=False)
print(df)

#Extract time index
indx = df.index.to_numpy()
indx = indx.astype(str)
indx = [elem[:16] for elem in indx]
indx = [elem.replace(" ", "T") for elem in indx]

# Extract price (as average of openPrice, highPrice, lowPrice and closePrice
openPrice = df['Open'].to_numpy()
highPrice = df['High'].to_numpy()
lowPrice = df['Low'].to_numpy()
closePrice = df['Close'].to_numpy()
price = (openPrice + highPrice + lowPrice + closePrice) / 4.0
for i in range(len(openPrice)): print(indx[i] + ' / ' + str(price[i]))

# Plot
fig = plt.scatter(indx, price)
plt.title(isin)
plt.show()

This code gives this figure:

enter image description here

And the yahoo finance figure:

enter image description here


Solution

  • The yahoo website is showing strict close price, whereas you have plotted the ADJUSTED closing price. So the further back you go in time, the more they diverge.

    Use

    df = yf.download(tickers=isin, period="5y", interval="1d", auto_adjust=False, prepost=False)