To get the last New York Stock Exchange (NYSE) open hour timestamp in Unix milliseconds, we need to account for the NYSE trading hours and holidays. The NYSE typically operates from 9:30 AM to 4:00 PM Eastern Time (ET) Monday through Friday, but it's also closed on certain holidays. Here's a Python function that calculates the last NYSE open hour timestamp in Unix milliseconds: /// python from datetime import datetime, timedelta import pytz import holidays def get_last_nyse_open_hour_timestamp(): # Define the NYSE timezone eastern = pytz.timezone('US/Eastern') # Get the current date and time in NYSE timezone now = datetime.now(eastern) # Define NYSE trading hours market_open_hour = 9 market_open_minute = 30 market_close_hour = 16 market_close_minute = 0 # Fetch US holidays us_holidays = holidays.US() # Adjust the current time if the market is closed while True: if now.weekday() >= 5 or now.date() in us_holidays: # NYSE is closed on weekends and certain holidays now -= timedelta(days=1) now = now.replace(hour=market_close_hour - 1, minute=0, second=0, microsecond=0) elif now.time() < datetime.time(now.replace(hour=market_open_hour, minute=market_open_minute, second=0)) or now.time() > datetime.time(now.replace(hour=market_close_hour, minute=market_close_minute, second=0)): now = now.replace(hour=market_close_hour - 1, minute=0, second=0, microsecond=0) else: break # Convert the datetime back to Unix milliseconds unix_timestamp = int(now.timestamp() * 1000) return unix_timestamp # Example usage print(get_last_nyse_open_hour_timestamp()) /// **Explanation:** 1. **Timezone Handling**: We use `pytz` to handle timezone conversion to Eastern Time. 2. **Trading Hours**: Define standard trading hours, keeping in mind holidays and weekends. 3. **Holidays**: Use the `holidays` library to check if today is a US holiday when the market is closed. 4. **Timestamp Calculation**: Adjust the datetime object as necessary, then convert it back to Unix milliseconds. **Dependencies**: - You’ll need to install the `pytz` and `holidays` libraries: /// shell pip install pytz holidays /// This function ensures that you always get the last NYSE open hour timestamp considering weekends, holidays, and trading hours.
Here's a Python function that retrieves the last NYSE open hour timestamp in unix milliseconds: /// python import datetime import pytz def get_last_nyse_open_hour_timestamp(): # Create a timezone object for Eastern Time (ET) eastern = pytz.timezone('US/Eastern') # Get the current date and time in Eastern Time now = datetime.datetime.now(eastern) # Set the time to the NYSE open hour (9:30 AM ET) nyse_open = now.replace(hour=9, minute=30, second=0, microsecond=0) # If the current time is before 9:30 AM ET, subtract 1 day if now < nyse_open: nyse_open -= datetime.timedelta(days=1) # Convert the NYSE open hour to unix milliseconds nyse_open_timestamp = int(nyse_open.timestamp() * 1000) return nyse_open_timestamp /// You can call this function to retrieve the last NYSE open hour timestamp in unix milliseconds like this: /// python print(get_last_nyse_open_hour_timestamp()) ///
Loading...