diff --git a/metrik/trading_days.py b/metrik/trading_days.py new file mode 100644 index 0000000..e179789 --- /dev/null +++ b/metrik/trading_days.py @@ -0,0 +1,21 @@ +from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, \ + nearest_workday, USMartinLutherKingJr, USPresidentsDay, GoodFriday,\ + USMemorialDay, USLaborDay, USThanksgivingDay +from pandas.tseries.offsets import CustomBusinessDay + + +class USTradingCalendar(AbstractHolidayCalendar): + rules = [ + Holiday('NewYearsDay', 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) + ] + +def TradingDay(n): + return CustomBusinessDay(n, calendar=USTradingCalendar()) \ No newline at end of file diff --git a/test/test_trading_days.py b/test/test_trading_days.py new file mode 100644 index 0000000..dfe3d74 --- /dev/null +++ b/test/test_trading_days.py @@ -0,0 +1,17 @@ +from unittest import TestCase +from datetime import datetime + +from metrik.trading_days import TradingDay + + +class TradingDayTest(TestCase): + + def test_skip_july4(self): + start = datetime(2016, 7, 1) # Friday + end = start + TradingDay(1) + assert end == datetime(2016, 7, 5) + + def test_skip_july4_backwards(self): + end = datetime(2016, 7, 5) + start = end - TradingDay(1) + assert start == datetime(2016, 7, 1) \ No newline at end of file