Skip to content

Instantly share code, notes, and snippets.

@jckantor
Last active February 9, 2023 13:22
Show Gist options
  • Save jckantor/d100a028027c5a6b8340 to your computer and use it in GitHub Desktop.
Save jckantor/d100a028027c5a6b8340 to your computer and use it in GitHub Desktop.
Python dateutil rule sets for NYSE trading days and holiday observances.
from dateutil import rrule
import datetime
# Generate ruleset for holiday observances on the NYSE
def NYSE_holidays(a=datetime.date.today(), b=datetime.date.today()+datetime.timedelta(days=365)):
rs = rrule.rruleset()
# Include all potential holiday observances
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth=12, bymonthday=31, byweekday=rrule.FR)) # New Years Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 1, bymonthday= 1)) # New Years Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 1, bymonthday= 2, byweekday=rrule.MO)) # New Years Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 1, byweekday= rrule.MO(3))) # Martin Luther King Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 2, byweekday= rrule.MO(3))) # Washington's Birthday
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, byeaster= -2)) # Good Friday
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 5, byweekday= rrule.MO(-1))) # Memorial Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 7, bymonthday= 3, byweekday=rrule.FR)) # Independence Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 7, bymonthday= 4)) # Independence Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 7, bymonthday= 5, byweekday=rrule.MO)) # Independence Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth= 9, byweekday= rrule.MO(1))) # Labor Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth=11, byweekday= rrule.TH(4))) # Thanksgiving Day
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth=12, bymonthday=24, byweekday=rrule.FR)) # Christmas
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth=12, bymonthday=25)) # Christmas
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=a, until=b, bymonth=12, bymonthday=26, byweekday=rrule.MO)) # Christmas
# Exclude potential holidays that fall on weekends
rs.exrule(rrule.rrule(rrule.WEEKLY, dtstart=a, until=b, byweekday=(rrule.SA,rrule.SU)))
return rs
# Generate ruleset for NYSE trading days
def NYSE_tradingdays(a=datetime.date.today(), b=datetime.date.today()+datetime.timedelta(days=365)):
rs = rrule.rruleset()
rs.rrule(rrule.rrule(rrule.DAILY, dtstart=a, until=b))
# Exclude weekends and holidays
rs.exrule(rrule.rrule(rrule.WEEKLY, dtstart=a, byweekday=(rrule.SA,rrule.SU)))
rs.exrule(NYSE_holidays(a,b))
return rs
# Examples
# List all NYSE holiday observances for the coming year
print "NYSE Holidays\n"
for dy in NYSE_holidays():
print dy.strftime('%b %d %Y')
# Count NYSE trading days in next 5 years
print "\n\nTrading Days\n"
for yr in range(2015,2020):
tdays = len(list(NYSE_tradingdays(datetime.datetime(yr,1,1),datetime.datetime(yr,12,31))))
print "{0} {1}".format(yr,tdays)
@meGregV
Copy link

meGregV commented Nov 6, 2018

can be done with pandas as well:

from pandas.tseries.holiday import AbstractHolidayCalendar,Holiday, USMemorialDay,  USMartinLutherKingJr, USPresidentsDay, GoodFriday, \
    USLaborDay, USThanksgivingDay, nearest_workday
 
class NYSECalendar(AbstractHolidayCalendar):
    rules = [
        Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
        USMartinLutherKingJr,
        USPresidentsDay,
        GoodFriday,
        USMemorialDay,
        Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
        USLaborDay,
        USThanksgivingDay,
        Holiday('Christmas', month=12, day=25, observance=nearest_workday),
        ]
 
cdr = NYSECalendar()
non_trading_days = cdr.holidays(datetime(2017, 1, 1), datetime(2017, 12, 31))

per https://pandas.pydata.org/pandas-docs/stable/timeseries.html#holidays-holiday-calendars

@masayang
Copy link

I added a quick and dirty patch for George H.W. Bush holiday. https://gist.github.com/masayang/797f379067c6c8f397a1d72e3a32f3a1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment