Home > Enterprise >  Python Pandas: Most recent date there was trading on the NYSE exchange
Python Pandas: Most recent date there was trading on the NYSE exchange

Time:11-17

I need to get the most recent date that there was trading on the NYSE exchange. For example, there is no trading on 11/25/2021, on the NYSE exchange. Therefore on November 26, the most recent trading day should be November 24. This is what I have so far but it's not working.

#import pandas market trading days calendar
import pandas_market_calendars as mcal

#create calendar of NYSE trading days
trading_days = mcal.get_calendar('NYSE').valid_days(start_date='2021-11-24', end_date='2021-11-26')

#print trading_days
print(trading_days)

DatetimeIndex(['2021-11-24 00:00:00 00:00', '2021-11-26 00:00:00 00:00'], dtype='datetime64[ns, UTC]', freq='C')

#subtract 1 trading day from November 26, 2021
pd.to_datetime('11/26/2021') - pd.tseries.offsets.CustomBusinessDay(1, calendar = trading_days)

This should be the output

Timestamp('2021-11-24 00:00:00')

Instead I'm getting:

Timestamp('2021-11-25 00:00:00')

CodePudding user response:

nyse = mcal.get_calendar('NYSE')
date =  pd.to_datetime('11/26/2021') - pd.tseries.offsets.CustomBusinessDay(1, holidays = nyse.holidays().holidays)

2021-11-24 00:00:00
  • Related