Closed alexnwk closed 4 months ago
Try setting end to tomorrow.
This successfully fixed the issue. I would argue the underlying bug still exists as it is odd the program will flip back and forth between the end date, however the solution is simple enough where a further dive into the issue is not warranted. Here is example code I used to overcome the issue when repeatedly pulling data through today's current date.
newYorkTz = pytz.timezone("America/New_York")
tomorrow = (datetime.now(newYorkTz) + timedelta(days=1)).strftime('%Y-%m-%d')
data = yf.Ticker('^DJI').history(interval='1d', start='2023-01-02', end=tomorrow)```
You could change default to tomorrow #1084
Describe bug
When using .history or .download on specific tickers, the resulting OHLC data will be of inconsistent size with the incorrect current date. The issue is inconsistent and will flip from correct to incorrect within seconds. For example, today is 7/17/2024. Running the below loop code with a start date of 1/2/2023 will result in a dataframe from 1/2/2023 -> 7/17/2024 OR 1/2/2023 -> 7/16/2024. I have noticed this issue with only specific tickers including ^DJI and ^GSPC, Dow Jones Index and S&P500 Index, respectively. I have not seen this issue on company-specific tickers (e.g. AAPL, NVDA), or other index-like tickers (e.g. ^TNX (10-year treasury yield), ^VIX (volatility)). However, because of the problem's infrequent occurrence inconsistent results could have missed with other tickers.
To mitigate this issue to ensure correct downloads through to the current date, I wrote a separate application which simply queries the data frame's end-date, and if it does not match the current date will re-download via YFinance after waiting 20 seconds. It is a bandaid for the underlying issue.
I am uncertain if this issue is similar to 1982. That issue appears more with data inconsistency across the data frame, whereas this is how YF is calculating (or pulling) the end-date.
Simple code that reproduces your problem
Debug log
Bad data proof
yfinance
version0.2.40
Python version
3.9
Operating system
No response