1
0
mirror of https://github.com/bspeice/metrik synced 2024-11-23 07:38:09 -05:00

Add a trading day timedelta

This commit is contained in:
Bradlee Speice 2016-08-16 16:06:59 -04:00
parent cd7009729e
commit d581ddf780
2 changed files with 38 additions and 0 deletions

21
metrik/trading_days.py Normal file
View File

@ -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())

17
test/test_trading_days.py Normal file
View File

@ -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)