mirror of
https://github.com/bspeice/metrik
synced 2024-11-04 22:48:11 -05:00
Add a trading day timedelta
This commit is contained in:
parent
cd7009729e
commit
d581ddf780
21
metrik/trading_days.py
Normal file
21
metrik/trading_days.py
Normal 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
17
test/test_trading_days.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user