ranaroussi / yfinance

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

Unable to get open price of a stock on the current day itself, getting yesterday instead #1591

Open anarchy89 opened 1 year ago

anarchy89 commented 1 year ago

I have the latest version of yfinance installed.

I tried the following query, ohlcv = yf.Ticker('AAPL').history(period='1d'), to get the open price of a stock 1 second after the market open at 9:30:01.

I even did

ohlcv = yf.Ticker('AAPL').history(period='1d')
while ohlcv.index[0].date() != datetime.datetime.now(pytz.timezone('US/Eastern'))).date():
    ohlcv = yf.Ticker('AAPL').history(period='1d')
    time.sleep(0.5)

because i wanted to make sure that the date was correct.

I tried this with 6 tickers today, GOOGL, NOW, BABA, BX, APD and SEDG.

Using the code above, and asserting that I get todays date (12 July 2023), I got the following open prices which are actually 11 July 2023. Even though i used datetime.datetime.now(pytz.timezone('US/Eastern'))).date() to assert the date.

GOOGL 116.29 supposed to be 118.80 NOW 567.21 supposed to be 570.86 BABA 91.02 supposed to be 94.11 BX 93.80 supposed to be 99.60 APD 291.28 supposed to be 291.70 SEDG 266.00 supposed to be 272.10

I have no idea why I get yesterdays open even though I am forcing the program to check the date as today. Any idea why it's behaving like this and what I can do to solve this? I am trying to get the open price of a stock on that day itself.

It seems that the date is today's date but the price is yestedays open. I tried it again like an hour later and it showed the correct price.

I have tried this everyday for a week already and it's the same during the market open.

I have tried it on BP.L and RELIANCE.NS during their respective market opens but they seem to work fine. This problem only seems to happen on the US market opens.

sunnycqcn commented 1 year ago

Hello, I get the same error.

sunnycqcn commented 1 year ago

Hello, Did you get any solution way? Thanks

chotaGit commented 11 months ago

Any update on this one? Not sure if this is a locale related issue. I am pulling the data from outside US and the current day data doesn't get refreshed until two hours after market opens.

ValueRaider commented 11 months ago

How are you determining what the price should be? Maybe that's wrong.

anarchy89 commented 11 months ago

By the date.

ValueRaider commented 11 months ago

Review the bug report template form and complete relevant questions/tasks here. Need to correctly identify fault before solving.

chotaGit commented 11 months ago

How are you determining what the price should be? Maybe that's wrong.

20231016: Open price should be 250.05. This is correctly reflected two hours after market open. Values at 9.31 EST, the open price is showing incorrectly as 258.9. This is previous day open price. Other values are ok. ticker type open high low close volume
TSLA CURRD 258.90 250.48 248.48 250.29 2861650
TSLA PREVD 258.90 259.60 250.22 251.12 102073800

Values at 11:00 EST.

ticker type open high low close volume
TSLA CURRD 250.05 254.17 248.48 253.23 37805861
TSLA PREVD 258.90 259.60 250.22 251.12 102073800
ValueRaider commented 11 months ago

Please: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables

chotaGit commented 9 months ago

@anarchy89 did you find a fix/workaround for this issue? Issue persists in the latest release. Thanks!

anarchy89 commented 9 months ago

No I am still waiting for a solution as well

ValueRaider commented 9 months ago

I'm waiting for someone to post debug log ... https://github.com/ranaroussi/yfinance/issues/1591#issuecomment-1766244019

chotaGit commented 9 months ago

Describe Bug

The open price of the ticker is set to yesterdays open price at 9:30 a.m EST and this does not get updated until 10:30 a.m. EST

Sample code that reproduces your problem:

import yfinance as yf
import logging
logging.basicConfig(level=logging.DEBUG)  
data = yf.download("TSLA")
print(data.tail(5).to_string())

Debug log:

DEBUG:yfinance:Entering download()
DEBUG:yfinance:Disabling multithreading because DEBUG logging enabled
DEBUG:yfinance: Entering history()
DEBUG:peewee:('CREATE TABLE IF NOT EXISTS "_kv" ("key" VARCHAR(255) NOT NULL PRIMARY KEY, "value" VARCHAR(255)) WITHOUT ROWID', [])
DEBUG:peewee:('SELECT "t1"."key", "t1"."value" FROM "_kv" AS "t1" WHERE ("t1"."key" = ?) LIMIT ? OFFSET ?', ['TSLA', 1, 0])
DEBUG:yfinance:TSLA: Yahoo GET parameters: {'period1': '1924-12-15 19:00:00-05:00', 'period2': '2023-11-22 04:00:06-05:00', 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'}
DEBUG:yfinance:  Entering get()
DEBUG:yfinance:url=https://query2.finance.yahoo.com/v8/finance/chart/TSLA
DEBUG:yfinance:params={'period1': -1421452800, 'period2': 1700643606, 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'}
DEBUG:yfinance:   Entering _get_cookie_and_crumb()
DEBUG:yfinance:cookie_mode = 'basic'
DEBUG:yfinance:    Entering _get_cookie_and_crumb_basic()
DEBUG:peewee:('CREATE TABLE IF NOT EXISTS "_cookieschema" ("strategy" VARCHAR(255) NOT NULL PRIMARY KEY, "fetch_date" DATETIME NOT NULL, "cookie_bytes" BLOB NOT NULL) WITHOUT ROWID', [])
DEBUG:peewee:('SELECT "t1"."strategy", "t1"."fetch_date", "t1"."cookie_bytes" FROM "_cookieschema" AS "t1" WHERE ("t1"."strategy" = ?) LIMIT ? OFFSET ?', ['basic', 1, 0])
DEBUG:yfinance:loaded persistent cookie
DEBUG:yfinance:reusing cookie
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): query1.finance.yahoo.com:443
DEBUG:urllib3.connectionpool:https://query1.finance.yahoo.com:443 "GET /v1/test/getcrumb HTTP/1.1" 200 None
DEBUG:yfinance:crumb = '/bPWUUV6QNr'
DEBUG:yfinance:    Exiting _get_cookie_and_crumb_basic()
DEBUG:yfinance:   Exiting _get_cookie_and_crumb()
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): query2.finance.yahoo.com:443
DEBUG:urllib3.connectionpool:https://query2.finance.yahoo.com:443 "GET /v8/finance/chart/TSLA?period1=-1421452800&period2=1700643606&interval=1d&includePrePost=False&events=div%2Csplits%2CcapitalGains&crumb=%2FbPWUUV6QNr HTTP/1.1" 200 None
DEBUG:yfinance:  Exiting get()
DEBUG:yfinance:TSLA: yfinance received OHLC data: 2010-06-29 13:30:00 -> 2023-11-21 14:30:00
DEBUG:yfinance:TSLA: OHLC after cleaning: 2010-06-29 09:30:00-04:00 -> 2023-11-21 09:30:00-05:00
DEBUG:yfinance:TSLA: OHLC after combining events: 2010-06-29 00:00:00-04:00 -> 2023-11-21 00:00:00-05:00
DEBUG:yfinance:TSLA: yfinance returning OHLC: 2010-06-29 00:00:00-04:00 -> 2023-11-21 00:00:00-05:00
DEBUG:yfinance: Exiting history()
DEBUG:yfinance:Exiting download()

Bad data proof:

Actual Data 

|Date       | Open       | High       | Low        | Close      | Adj. Close | Volume   |
|-----------| -----------| ---------- | ---------- | ---------- | ---------- | ---------|
|2023-11-15 | 239.289993 | 246.699997 | 236.449997 | 242.839996 | 242.839996 | 150354000|
|2023-11-16 | 239.490005 | 240.880005 | 230.960007 | 233.589996 | 233.589996 | 136816800|
|2023-11-17 | 232.000000 | 237.389999 | 226.539993 | 234.300003 | 234.300003 | 142532800|
|2023-11-20 | 234.039993 | 237.100006 | 231.020004 | 235.600006 | 235.600006 | 116320100|
|2023-11-21 | 234.039993 | 243.619995 | 233.339996 | 241.199997 | 241.199997 | 121987800|

Expected Data 

|Date       | Open       | High       | Low        | Close      | Adj. Close | Volume   |
|-----------| -----------| ---------- | ---------- | ---------- | ---------- | ---------|
|2023-11-15 | 239.289993 | 246.699997 | 236.449997 | 242.839996 | 242.839996 | 150354000|
|2023-11-16 | 239.490005 | 240.880005 | 230.960007 | 233.589996 | 233.589996 | 136816800|
|2023-11-17 | 232.000000 | 237.389999 | 226.539993 | 234.300003 | 234.300003 | 142532800|
|2023-11-20 | 234.039993 | 237.100006 | 231.020004 | 235.600006 | 235.600006 | 116320100|
|2023-11-21 | 235.039993 | 243.619995 | 233.339996 | 241.199997 | 241.199997 | 121987800|

yfinance version:

0.2.32

Python version:

3.10.x

Operating system:

Windows 10
ValueRaider commented 9 months ago

DEBUG:yfinance:TSLA: yfinance received OHLC data: 2010-06-29 13:30:00 -> 2023-11-21 14:30:00

Means data returned by yfinance matches what Yahoo API returns, except timezone change.

And a suggestion: Firefox -> Yahoo Finance -> F12 -> Network tab -> F5. Now you can investigate how Yahoo website getting different data from API than yfinance

anarchy89 commented 9 months ago

Hi,

Maybe it should be changed to include pytz so that it’s in the time zone of where the stock is instead of where the query comes from.

On 22 Nov 2023, at 21:12, ValueRaider @.***> wrote:



DEBUG:yfinance:TSLA: yfinance received OHLC data: 2010-06-29 13:30:00 -> 2023-11-21 14:30:00

Means data returned by yfinance matches what Yahoo API returns, except timezone change.

— Reply to this email directly, view it on GitHubhttps://github.com/ranaroussi/yfinance/issues/1591#issuecomment-1822746795, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABM3J44R26SDUP5QLZ7KKMTYFX23PAVCNFSM6AAAAAA2HVA2WWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRSG42DMNZZGU. You are receiving this because you were mentioned.Message ID: @.***>

chotaGit commented 9 months ago

This may or may not be a timezone issue. It is only the open price that is incorrect and not the HLC values. Let me try to grab the log exactly at 9:30/9.35/10 EST.

ValueRaider commented 9 months ago

@anarchy89 read the log, your comment is redundant.

chotaGit commented 9 months ago

This is the log from today - 20231124 @9.30 EST. The open price for TSLA for 1122 and 1124 are the same. All the other values are different. This is not a TZ issue. The correct open price is 233.75

DEBUG:yfinance:Entering download() DEBUG:yfinance:Disabling multithreading because DEBUG logging enabled DEBUG:yfinance: Entering history() DEBUG:peewee:('CREATE TABLE IF NOT EXISTS "_kv" ("key" VARCHAR(255) NOT NULL PRIMARY KEY, "value" VARCHAR(255)) WITHOUT ROWID', []) DEBUG:peewee:('SELECT "t1"."key", "t1"."value" FROM "_kv" AS "t1" WHERE ("t1"."key" = ?) LIMIT ? OFFSET ?', ['TSLA', 1, 0]) DEBUG:yfinance:TSLA: Yahoo GET parameters: {'period1': '1924-12-17 19:00:00-05:00', 'period2': '2023-11-24 09:30:01-05:00', 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'} DEBUG:yfinance: Entering get() DEBUG:yfinance:url=https://query2.finance.yahoo.com/v8/finance/chart/TSLA DEBUG:yfinance:params={'period1': -1421280000, 'period2': 1700836201, 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'} DEBUG:yfinance: Entering _get_cookie_and_crumb() DEBUG:yfinance:cookie_mode = 'basic' DEBUG:yfinance: Entering _get_cookie_and_crumb_basic() DEBUG:peewee:('CREATE TABLE IF NOT EXISTS "_cookieschema" ("strategy" VARCHAR(255) NOT NULL PRIMARY KEY, "fetch_date" DATETIME NOT NULL, "cookie_bytes" BLOB NOT NULL) WITHOUT ROWID', []) DEBUG:peewee:('SELECT "t1"."strategy", "t1"."fetch_date", "t1"."cookie_bytes" FROM "_cookieschema" AS "t1" WHERE ("t1"."strategy" = ?) LIMIT ? OFFSET ?', ['basic', 1, 0]) DEBUG:yfinance:loaded persistent cookie DEBUG:yfinance:reusing cookie DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): query1.finance.yahoo.com:443 DEBUG:urllib3.connectionpool:https://query1.finance.yahoo.com:443 "GET /v1/test/getcrumb HTTP/1.1" 200 None DEBUG:yfinance:crumb = 'gmBqbkYFkGT' DEBUG:yfinance: Exiting _get_cookie_and_crumb_basic() DEBUG:yfinance: Exiting _get_cookie_and_crumb() DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): query2.finance.yahoo.com:443 DEBUG:urllib3.connectionpool:https://query2.finance.yahoo.com:443 "GET /v8/finance/chart/TSLA?period1=-1421280000&period2=1700836201&interval=1d&includePrePost=False&events=div%2Csplits%2CcapitalGains&crumb=gmBqbkYFkGT HTTP/1.1" 200 None DEBUG:yfinance: Exiting get() DEBUG:yfinance:TSLA: yfinance received OHLC data: 2010-06-29 13:30:00 -> 2023-11-24 14:30:00 DEBUG:yfinance:TSLA: OHLC after cleaning: 2010-06-29 09:30:00-04:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance:TSLA: OHLC after combining events: 2010-06-29 00:00:00-04:00 -> 2023-11-24 00:00:00-05:00 DEBUG:yfinance:TSLA: yfinance returning OHLC: 2010-06-29 00:00:00-04:00 -> 2023-11-24 00:00:00-05:00 DEBUG:yfinance: Exiting history() DEBUG:yfinance:Exiting download() DEBUG:yfinance:Entering download() DEBUG:yfinance:Disabling multithreading because DEBUG logging enabled DEBUG:yfinance: Entering history() DEBUG:peewee:('SELECT "t1"."key", "t1"."value" FROM "_kv" AS "t1" WHERE ("t1"."key" = ?) LIMIT ? OFFSET ?', ['TSLA', 1, 0]) DEBUG:yfinance:TSLA: Yahoo GET parameters: {'range': '1d', 'interval': '5m', 'includePrePost': True, 'events': 'div,splits,capitalGains'} DEBUG:yfinance: Entering get() DEBUG:yfinance:url=https://query2.finance.yahoo.com/v8/finance/chart/TSLA DEBUG:yfinance:params={'range': '1d', 'interval': '5m', 'includePrePost': True, 'events': 'div,splits,capitalGains'} DEBUG:yfinance: Entering _get_cookie_and_crumb() DEBUG:yfinance:cookie_mode = 'basic' DEBUG:yfinance: Entering _get_cookie_and_crumb_basic() DEBUG:yfinance:reusing cookie DEBUG:yfinance:reusing crumb DEBUG:yfinance: Exiting _get_cookie_and_crumb_basic() DEBUG:yfinance: Exiting _get_cookie_and_crumb() DEBUG:urllib3.connectionpool:https://query2.finance.yahoo.com:443 "GET /v8/finance/chart/TSLA?range=1d&interval=5m&includePrePost=True&events=div%2Csplits%2CcapitalGains&crumb=gmBqbkYFkGT HTTP/1.1" 200 1142 DEBUG:yfinance: Exiting get() DEBUG:yfinance:TSLA: yfinance received OHLC data: 2023-11-24 09:00:00 -> 2023-11-24 14:30:00 DEBUG:yfinance:TSLA: OHLC after cleaning: 2023-11-24 04:00:00-05:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance:TSLA: OHLC after combining events: 2023-11-24 04:00:00-05:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance:TSLA: yfinance returning OHLC: 2023-11-24 04:00:00-05:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance: Exiting history() DEBUG:yfinance: Entering history() DEBUG:peewee:('SELECT "t1"."key", "t1"."value" FROM "_kv" AS "t1" WHERE ("t1"."key" = ?) LIMIT ? OFFSET ?', ['NVDA', 1, 0]) DEBUG:yfinance:NVDA: Yahoo GET parameters: {'range': '1d', 'interval': '5m', 'includePrePost': True, 'events': 'div,splits,capitalGains'} DEBUG:yfinance: Entering get() DEBUG:yfinance:url=https://query2.finance.yahoo.com/v8/finance/chart/NVDA DEBUG:yfinance:params={'range': '1d', 'interval': '5m', 'includePrePost': True, 'events': 'div,splits,capitalGains'} DEBUG:yfinance: Entering _get_cookie_and_crumb() DEBUG:yfinance:cookie_mode = 'basic' DEBUG:yfinance: Entering _get_cookie_and_crumb_basic() DEBUG:yfinance:reusing cookie DEBUG:yfinance:reusing crumb DEBUG:yfinance: Exiting _get_cookie_and_crumb_basic() DEBUG:yfinance: Exiting _get_cookie_and_crumb() DEBUG:urllib3.connectionpool:https://query2.finance.yahoo.com:443 "GET /v8/finance/chart/NVDA?range=1d&interval=5m&includePrePost=True&events=div%2Csplits%2CcapitalGains&crumb=gmBqbkYFkGT HTTP/1.1" 200 1241 DEBUG:yfinance: Exiting get() DEBUG:yfinance:NVDA: yfinance received OHLC data: 2023-11-24 09:00:00 -> 2023-11-24 14:30:00 DEBUG:yfinance:NVDA: OHLC after cleaning: 2023-11-24 04:00:00-05:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance:NVDA: OHLC after combining events: 2023-11-24 04:00:00-05:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance:NVDA: yfinance returning OHLC: 2023-11-24 04:00:00-05:00 -> 2023-11-24 09:30:00-05:00 DEBUG:yfinance: Exiting history() DEBUG:yfinance:Exiting download() Hello World *Yahoo Test Start**** Open High Low Close Adj Close Volume Date
2023-11-17 232.000000 237.389999 226.539993 234.300003 234.300003 142532800 2023-11-20 234.039993 237.100006 231.020004 235.600006 235.600006 116320100 2023-11-21 235.039993 243.619995 233.339996 241.199997 241.199997 122288000 2023-11-22 242.039993 244.009995 231.399994 234.210007 234.210007 117950600 2023-11-24 242.039993 233.740005 233.740005 233.740005 233.740005 1206541 *
Yahoo Test ENd****

ValueRaider commented 9 months ago

Debug log not helping because I misunderstood issue, I thought was "today is missing". I have no insight to offer beyond what I said 2 comments ago, was never concerned about yfinance vs website differing.

Maximilian3141 commented 9 months ago

You can fix this by putting the end date one day into the future. When you download the historical prices (in .csv) from the yahoo web page and change the default date, it will also be from yesterday. To get data from today in the .csv file, you have to change the end date to tomorrow.

from datetime import datetime, timedelta
import yfinance as yf 

start_date = date.today()-timedelta(days=7)
end_date = date.today()+timedelta(days=1)

msft = yf.Ticker("MSFT")
hist = msft.history(period="1d", start= start_date, end=end_date)
hist
2023-12-04 00:00:00-05:00   369.100006  369.519989  362.899994  369.140015  32063300    0.0 0.0
2023-12-05 00:00:00-05:00   366.450012  373.079987  365.619995  372.519989  23065000    0.0 0.0
2023-12-06 00:00:00-05:00   373.540009  374.179993  368.029999  368.799988  21182100    0.0 0.0
2023-12-07 00:00:00-05:00   368.230011  371.450012  366.320007  370.950012  23118900    0.0 0.0
2023-12-08 00:00:00-05:00   369.200012  374.440002  368.279999  374.230011  20034491    0.0 0.0
chotaGit commented 9 months ago

@Maximilian3141 The issue is about the data quality. The open price @930 am EST shows yesterdays open price and this continues till 1000 am EST at which point the current days open price is correctly captured. What do you get at 930 EST with the fix that you are proposing?

Maximilian3141 commented 9 months ago

@chotaGit I'm sorry, I just tried it, since European markets are open right now, and it's not fixed by choosing an end date one day in the future. Previously, I couldn't reproduce the error correctly, because no markets were open.

Current Time: 2023-12-11 09:59:06.574482+01:00

2023-12-07 00:00:00+01:00   16623.890625    16655.300781    16595.109375    16628.990234    76803200    0.0 0.0
2023-12-08 00:00:00+01:00   16644.619141    16782.720703    16630.179688    16759.220703    77814500    0.0 0.0
2023-12-11 00:00:00+01:00   16644.619141    16777.460938    16745.560547    16750.140625    0   0.0 0.0

I don't think it is fixable? The problem is that the yahoo API provides wrong data for the first few minutes/hours in a day?