From d581ddf780173cdedae259c797dbc4c9d8f8a403 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 16 Aug 2016 16:06:59 -0400 Subject: [PATCH] Add a trading day timedelta --- metrik/trading_days.py | 21 +++++++++++++++++++++ test/test_trading_days.py | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 metrik/trading_days.py create mode 100644 test/test_trading_days.py 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