ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
12.94k stars 2.29k forks source link

Last hour of trade is missing in Saudi stock market #1585

Open Abdullah-Jannadi opened 1 year ago

Abdullah-Jannadi commented 1 year ago

I'm using the yfinance library to retrieve historical data for the Saudi stock market, but I've noticed that the last hour of trade is missing from the data. Specifically, the data ends at 14:00:00+03:00 , even though the market is open until 15:20:00+03:00 , so we must have a row of data for 15:00:00+03:00

To reproduce the issue, I'm using the following code to retrieve data for ARAMCO (2222) :

import yfinance as yf

aramco = yf.download('2222.SR' , period='1mo' , interval='1h')

aramco.head(10).round(2)

And the result was:

Datetime Open High Low Close Adj Close Volume
2023-06-07 10:00:00+03:00 32.5 32.5 32.35 32.45 32.45 562639
2023-06-07 11:00:00+03:00 32.45 32.5 32.4 32.45 32.45 360020
2023-06-07 12:00:00+03:00 32.45 32.5 32.35 32.4 32.4 392409
2023-06-07 13:00:00+03:00 32.4 32.45 32.35 32.4 32.4 396132
2023-06-07 14:00:00+03:00 32.4 32.45 32.3 32.4 32.4 1205491
2023-06-08 10:00:00+03:00 32.3 32.4 32.25 32.35 32.35 620175
2023-06-08 11:00:00+03:00 32.3 32.4 32.3 32.35 32.35 941143
2023-06-08 12:00:00+03:00 32.4 32.4 32.3 32.35 32.35 400662
2023-06-08 13:00:00+03:00 32.4 32.45 32.3 32.35 32.35 514886
2023-06-08 14:00:00+03:00 32.4 32.5 32.4 32.45 32.45 496968

Furthermore, I compared the total volume from yfinance with the official data from Tadawul and found that there is a discrepancy. So if we sum up all the hours' volume, the official data shows a higher total volume for the day than the yfinance data.

I would appreciate any assistance in resolving this issue.

Thank you!

ValueRaider commented 1 year ago

2 things going on here - 1 my fault, 2nd appears Yahoo's fault.

1) When prepost=False (default), then yfinance uses the trading schedule returned by Yahoo to remove any pre/post data. Yahoo thinks Saudi regular trading ending at 3pm, so data was being removed. Easy to fix ...

2) ... but after fixing yfinance, volume for 3pm is 0, so Yahoo may have fundamental problem with 3pm interval.

Date Close Volume
2023-07-06 14:00:00+03:00 32.250000 1450407
2023-07-06 15:00:00+03:00 32.299999 0

Try the fix and report back, it's in branch fix/saudi-intraday-lost-hour. You'll also need branch to see schedule, I found & fixed a bug: yf.Ticker('2222.SR').history_metadata['tradingPeriods']