diff options
Diffstat (limited to 'python/tranche_functions.py')
| -rw-r--r-- | python/tranche_functions.py | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/python/tranche_functions.py b/python/tranche_functions.py index 28481130..a92044e3 100644 --- a/python/tranche_functions.py +++ b/python/tranche_functions.py @@ -1,8 +1,8 @@ import numpy as np from ctypes import * from numpy.ctypeslib import ndpointer -from quantlib.time.schedule import Schedule, CDS -from quantlib.time.api import Actual360, Period, UnitedStates, Following, today +from quantlib.time.schedule import Schedule, CDS2015 +from quantlib.time.api import Actual360, Period, WeekendsOnly, ModifiedFollowing, Unadjusted, today from quantlib.util.converter import qldate_to_pydate, pydate_to_qldate import pandas as pd from scipy.special import h_roots @@ -284,33 +284,32 @@ def tranche_pl(L, cs, K1, K2, scaled=False): def tranche_pv(L, R, cs, K1, K2): return tranche_pl(L, cs, K1, K2) + tranche_cl(L, R, cs, K2, K2) -def creditSchedule(tradedate, tenor, coupon, yc, enddate = None): +def credit_schedule(tradedate, tenor, coupon, yc, enddate=None): tradedate = pydate_to_qldate(tradedate) - start = tradedate - Period('4Mo') - enddate = pydate_to_qldate(enddate) if enddate is None: enddate = tradedate + Period(tenor) - cal = UnitedStates() + else: + enddate = pydate_to_qldate(enddate) + cal = WeekendsOnly() DC = Actual360() - sched = Schedule(start, enddate, Period('3Mo'), cal, date_generation_rule=CDS) - prevpaydate = sched.previous_date(tradedate) - sched = [d for d in sched if d>=prevpaydate] - df = [yc.discount(d) for d in sched if d > tradedate] - - dates = pd.to_datetime([str(d) for d in sched if d > tradedate], "%m/%d/%Y") - coupons = np.diff([DC.year_fraction(prevpaydate, d) * coupon for d in sched]) + sched = Schedule.from_rule(tradedate, enddate, Period('3M'), cal, + ModifiedFollowing, Unadjusted, CDS2015) + dates = sched.to_npdates() + pydates = dates.astype('O') + df = [yc.discount_factor(d) for d in pydates if d > tradedate] + coupons = [DC.year_fraction(d1, d2) * coupon for d1, d2 in zip(sched[:-2], sched[1:-1])] + coupons.append(Actual360(True).year_fraction(sched[-2], sched[-1]) * coupon) - return pd.DataFrame({"df":df, "coupons":coupons}, dates) + return pydates[0], pd.DataFrame({"df": df, "coupons": coupons}, dates[1:]) def cdsAccrued(tradedate, coupon): tradedate = pydate_to_qldate(tradedate) - start = tradedate - Period('3Mo') end = tradedate + Period('3Mo') start_protection = tradedate + 1 DC = Actual360() cal = UnitedStates() - sched = Schedule(start, end, Period('3Mo'), cal, date_generation_rule=CDS) + sched = Schedule(start, end, Period('3Mo'), cal, date_generation_rule=CDS2015) prevpaydate = sched.previous_date(tradedate) return DC.year_fraction(prevpaydate, start_protection) * coupon |
