diff --git a/tests/test_tradingcalendar.py b/tests/test_tradingcalendar.py index b9a7b832..c297bec1 100644 --- a/tests/test_tradingcalendar.py +++ b/tests/test_tradingcalendar.py @@ -21,7 +21,7 @@ import datetime class TestTradingCalendar(TestCase): - def test_tradingcalendar(self): + def test_newyears(self): """ Check whether tradingcalendar contains certain dates. """ @@ -110,3 +110,51 @@ If the day after NYE falls during the week, {0} \ is the first trading day. """.strip().format(first_trading_day_after_new_years) ) + + def test_thanksgiving(self): + """ + Check tradingcalendar Thanksgiving dates. + """ + # November 2005 + # Su Mo Tu We Th Fr Sa + # 1 2 3 4 5 + # 6 7 8 9 10 11 12 + # 13 14 15 16 17 18 19 + # 20 21 22 23 24 25 26 + # 27 28 29 30 + thanksgiving_with_four_weeks = datetime.datetime( + 2005, 11, 24, tzinfo=pytz.utc) + + self.assertNotIn(thanksgiving_with_four_weeks, + tradingcalendar.trading_days, + """ +If Nov has 4 Thursdays, {0} Thanksgiving is the last Thursady. +""".strip().format(thanksgiving_with_four_weeks) + ) + + # November 2006 + # Su Mo Tu We Th Fr Sa + # 1 2 3 4 + # 5 6 7 8 9 10 11 + # 12 13 14 15 16 17 18 + # 19 20 21 22 23 24 25 + # 26 27 28 29 30 + thanksgiving_with_five_weeks = datetime.datetime( + 2006, 11, 23, tzinfo=pytz.utc) + + self.assertNotIn(thanksgiving_with_five_weeks, + tradingcalendar.trading_days, + """ +If Nov has 5 Thursdays, {0} Thanksgiving is not the last week. +""".strip().format(thanksgiving_with_five_weeks) + ) + + first_trading_day_after_new_years_sunday = datetime.datetime( + 2012, 1, 3, tzinfo=pytz.utc) + + self.assertIn(first_trading_day_after_new_years_sunday, + tradingcalendar.trading_days, + """ +If NYE falls on a weekend, {0} the Tuesday after is the first trading day. +""".strip().format(first_trading_day_after_new_years_sunday) + ) diff --git a/zipline/utils/tradingcalendar.py b/zipline/utils/tradingcalendar.py index bef27702..d5065fec 100644 --- a/zipline/utils/tradingcalendar.py +++ b/zipline/utils/tradingcalendar.py @@ -128,7 +128,7 @@ def get_non_trading_days(start, end): thanksgiving = rrule.rrule( rrule.MONTHLY, bymonth=11, - byweekday=(rrule.TH(-1)), + byweekday=(rrule.TH(4)), cache=True, dtstart=start, until=end