diff options
Diffstat (limited to 'python/dates.py')
| -rw-r--r-- | python/dates.py | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/python/dates.py b/python/dates.py index acfafe12..49ba10d5 100644 --- a/python/dates.py +++ b/python/dates.py @@ -1,23 +1,14 @@ +import datetime import pandas as pd from pandas.tseries.offsets import CustomBusinessDay, Day, QuarterBegin from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday +from analytics.utils import previous_twentieth import unittest fed_cal = get_calendar('USFederalHolidayCalendar') bond_cal = HolidayCalendarFactory('BondCalendar', fed_cal, GoodFriday) bus_day = CustomBusinessDay(calendar=bond_cal()) -def prev_immdate(valdate): - dates = [pd.datetime(valdate.year-1, 12, 20)] + \ - [pd.datetime(valdate.year, m, 20) for m in [3, 6, 9, 12]] - dates = [bus_day.rollforward(d) for d in dates] - for i, d in enumerate(dates[1:], 1): - if d >= valdate: - break - else: - return dates[-1] - return d if d==valdate else dates[i-1] - def imm_dates(start_date, end_date): start_date = bus_day.rollback(start_date)-19*Day() start_date = QuarterBegin(startingMonth=3).rollback(start_date) @@ -27,8 +18,13 @@ def imm_dates(start_date, end_date): shift(0, bus_day)) def days_accrued(tradedate): - start_protection = pd.Timestamp(tradedate) + Day() - delta = start_protection-prev_immdate(start_protection) + tradedate = pd.Timestamp(tradedate) + start_protection = tradedate + Day() + # we want the the largest twentieth imm date rolled forward such that + # it's less than start_protection + effective_date = pd.Timestamp(previous_twentieth(bus_day.rollback(start_protection).date())) + effective_date = bus_day.rollforward(effective_date) + delta = start_protection - effective_date return delta.days def isleapyear(date): |
