Skip to content

Instantly share code, notes, and snippets.

@masayang
Forked from jckantor/NYSE_tradingdays.py
Last active December 22, 2018 11:08
Show Gist options
  • Save masayang/797f379067c6c8f397a1d72e3a32f3a1 to your computer and use it in GitHub Desktop.
Save masayang/797f379067c6c8f397a1d72e3a32f3a1 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
rs.rrule(rrule.rrule(rrule.YEARLY, dtstart=datetime.date(2018, 12, 1), until=datetime.date(2018, 12, 31), bymonth=12, bymonthday=5)) # George H.W. Bush
# 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
if __name__ == '__main__':
# 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'))
print("\nNYSE Holidays in 2017\n")
for dy in NYSE_holidays(datetime.date(2017, 1, 1), datetime.date(2017, 12, 31)):
print(dy.strftime('%b %d %Y'))
print("\nNYSE Holidays in 2018\n")
for dy in NYSE_holidays(datetime.date(2018, 1, 1), datetime.date(2018, 12, 31)):
print(dy.strftime('%b %d %Y'))
print("\nNYSE Holidays in 2019\n")
for dy in NYSE_holidays(datetime.date(2019, 1, 1), datetime.date(2019, 12, 31)):
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment