pythonpython-3.xyfinance

yfinance not returning yesterday data


Yfinance doesnt return yesterday data

import yfinance as yf
import pandas as pd

def get_last_two_days_ohlc(ticker):
    # Fetch historical data for the last 5 days to ensure we get at least two closed days
    data = yf.download(ticker, period="5d")
    
    # Drop rows with missing data
    data.dropna(inplace=True)
    
    # Get the last two closed days
    last_two_days = data.iloc[-2:]
    
    # Return the OHLC data
    return last_two_days[['Open', 'High', 'Low', 'Close']]

# Example usage
ticker = "EURUSD=X"
ohlc_data = get_last_two_days_ohlc(ticker)
print(ohlc_data)

This is the result

    [*********************100%***********************]  1 of 1 completed
                Open      High       Low     Close
         Date                                              
  2024-08-29  1.112496  1.114020  1.105632  1.112496
  2024-08-30  1.108045  1.109570  1.105400  1.108045
  2024-09-02  1.104484  1.107763  1.104252  1.104484
  2024-09-04  1.104606  1.106562  1.104240  1.105583

I've tried running different symbols but its happening with all of them.


Solution

  • You're getting the correct data.

    Because of Labor day in America, Exchange was closed.

    List of Holidays: https://www.nasdaq.com/market-activity/stock-market-holiday-schedule

    Y-Finance Official site Data is not available for 9/3/2024.

    https://finance.yahoo.com/quote/EURUSD=X/history/

    enter image description here