在R语言中获取纽约证券交易所(NYSE)的计划及非计划闭市日期
Great question! The holidayNYSE() function from the timeDate package works perfectly for scheduled NYSE holidays, but it doesn’t cover unplanned closures like the 9/11 attacks (Sept 11-14, 2001) or Hurricane Sandy (Oct 29-30, 2012). Unfortunately, there’s no out-of-the-box R function that automatically includes all historical unplanned exchange closures—here’s how you can tackle this:
1. Manually Merge Scheduled Holidays with Unplanned Closures
Since unplanned closures are relatively rare, the most straightforward approach is to curate a list of these dates and combine them with the scheduled holidays from timeDate. Here’s a quick example:
library(timeDate) # Fetch scheduled NYSE holidays for a target year scheduled_holidays <- holidayNYSE(2012) # Define your list of unplanned closure dates (adjust as needed) unplanned_closures <- timeDate(c("2012-10-29", "2012-10-30")) # Combine and deduplicate the two sets, then sort all_nyse_closures <- sort(unique(c(scheduled_holidays, unplanned_closures))) # View the full list of closures all_nyse_closures
2. Use the bizdays Package for Custom Calendars
The bizdays package lets you build custom business day calendars that include both scheduled and unplanned closures. This is useful if you need to repeatedly check business days against the full closure list:
library(bizdays) # Create a custom NYSE calendar with unplanned closures included nyse_full_calendar <- create.calendar( name = "NYSE_Full", holidays = c(as.character(holidayNYSE(2012)), "2012-10-29", "2012-10-30"), weekdays = c("saturday", "sunday") ) # Check if a specific date is a closure (returns FALSE for closures) is.bizday("2012-10-29", nyse_full_calendar)
Key Note
To keep your closure list accurate, you’ll need to reference official historical records (like the NYSE’s archived closure announcements) to add any other unplanned dates. No R package maintains a fully up-to-date, automatically updated list of these one-off closures—manual curation is the way to go for now.
内容的提问来源于stack exchange,提问作者jmuhlenkamp

