ranaroussi / yfinance

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

Incorrect handling of historic prices for corporate actions for GCC Stocks #1937

Open BottleNeck2021 opened 6 months ago

BottleNeck2021 commented 6 months ago

Describe bug

Hello,

I am looking at the stock NBK.KW

on 08th April 2024 it had a corporate action of stock dividends/cash bonus etc. as the case may be.

In the Boursa Kuwait stock market the price of the stock is reflected in fils (cents equivalent in US).

once the corporate action takes effect yfinance tries to do a historic price readjustment but ignores that the values are in fils, resulting in incorrect prices for day before the corporate action.

You will have the same problem for all stocks in GCC markets that are reflected in fils or cents equivalent.

Simple code that reproduces your problem

yfinance.download("NBK.KW")

Debug log

DEBUG Entering download() DEBUG Disabling multithreading because DEBUG logging enabled DEBUG Entering history() DEBUG Entering history() DEBUG NBK.KW: Yahoo GET parameters: {'period1': '1925-06-10 03:07:00+03:07', 'period2': '2024-05-16 03:36:03+03:00', 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'} DEBUG Entering get() DEBUG url=https://query2.finance.yahoo.com/v8/finance/chart/NBK.KW DEBUG params={'period1': -1406246400, 'period2': 1715819763, 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'} DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG loaded persistent cookie DEBUG reusing cookie DEBUG crumb = 'iI3kLNAmXIK' DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=200 DEBUG Exiting get() DEBUG NBK.KW: yfinance received OHLC data: 2000-01-03 06:00:00 -> 2024-05-15 06:00:00 DEBUG NBK.KW: OHLC after cleaning: 2000-01-03 09:00:00+03:00 -> 2024-05-15 09:00:00+03:00 DEBUG NBK.KW: OHLC after combining events: 2000-01-03 00:00:00+03:00 -> 2024-05-15 00:00:00+03:00 DEBUG NBK.KW: yfinance returning OHLC: 2000-01-03 00:00:00+03:00 -> 2024-05-15 00:00:00+03:00 DEBUG Exiting history() DEBUG Exiting history() DEBUG Exiting download() DEBUG get_raw_json(): https://query2.finance.yahoo.com/v10/finance/quoteSummary/NBK.KW DEBUG Entering get() DEBUG url=https://query2.finance.yahoo.com/v10/finance/quoteSummary/NBK.KW DEBUG params={'modules': 'financialData,quoteType,defaultKeyStatistics,assetProfile,summaryDetail', 'corsDomain': 'finance.yahoo.com', 'formatted': 'false', 'symbol': 'NBK.KW'} DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG reusing cookie DEBUG reusing crumb DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=200 DEBUG Exiting get() DEBUG Entering get() DEBUG url=https://query1.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/NBK.KW?symbol=NBK.KW&type=trailingPegRatio&period1=1700092800&period2=1715904000 DEBUG params=None DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG reusing cookie DEBUG reusing crumb DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=200 DEBUG Exiting get()

Bad data proof

image

yfinance version

0.2.38

Python version

3.12

Operating system

Windows 10 Home

ValueRaider commented 6 months ago

I might be able to improve price-repair to handle - is this right? Not sure on dividend. And are other Arab exchanges affected?

                                Close    Volume  Dividends  Repaired?
Date                                                                 
2024-04-01 00:00:00+03:00  887.935758    934312      0.000       True
2024-04-02 00:00:00+03:00  891.638815   3488921      0.000       True
2024-04-03 00:00:00+03:00  874.046981   6309698      0.025       True
2024-04-08 00:00:00+03:00  872.000000   3358161      0.000      False
2024-04-14 00:00:00+03:00  866.000000   2829391      0.000      False
2024-04-15 00:00:00+03:00  862.000000   6771672      0.000      False