diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2017-02-27 17:18:14 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2017-02-27 17:50:46 -0500 |
| commit | 14fecfbcbfcc3a022b6cd74f86d8bb37d2fc1db0 (patch) | |
| tree | 46d8077844abcc4399b32d7eec96d037e5095ec3 | |
| parent | 4a7f3a3977dcac815dcfbd9dc068ba6f791f8a7e (diff) | |
| download | pyisda-14fecfbcbfcc3a022b6cd74f86d8bb37d2fc1db0.tar.gz | |
add theta method
| -rw-r--r-- | pyisda/credit_index.pxd | 1 | ||||
| -rw-r--r-- | pyisda/credit_index.pyx | 63 |
2 files changed, 60 insertions, 4 deletions
diff --git a/pyisda/credit_index.pxd b/pyisda/credit_index.pxd index 787cd15..bb9f7ba 100644 --- a/pyisda/credit_index.pxd +++ b/pyisda/credit_index.pxd @@ -12,6 +12,7 @@ cdef class CurveList: cdef dict _tickersdict cdef class CreditIndex(CurveList): + cdef _start_date cdef vector[TDate] _maturities cdef TContingentLeg** _contingent_legs cdef TFeeLeg** _fee_legs diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx index 2fa88bf..ad49a58 100644 --- a/pyisda/credit_index.pyx +++ b/pyisda/credit_index.pyx @@ -98,7 +98,7 @@ cdef class CurveList: cdef class CreditIndex(CurveList): def __init__(self, start_date, maturities, curves, double[:] weights=None): CurveList.__init__(self, curves, weights) - cdef TDate start_date_c = pydate_to_TDate(start_date) + self._start_date = pydate_to_TDate(start_date) for d in maturities: self._maturities.push_back(pydate_to_TDate(d)) @@ -111,12 +111,12 @@ cdef class CreditIndex(CurveList): raise ValueError("can't convert stub") cdef size_t i for i in range(self._maturities.size()): - self._contingent_legs[i] = JpmcdsCdsContingentLegMake(start_date_c, + self._contingent_legs[i] = JpmcdsCdsContingentLegMake(self._start_date, self._maturities[i], 1., True) - self._fee_legs[i] = JpmcdsCdsFeeLegMake(start_date_c, + self._fee_legs[i] = JpmcdsCdsFeeLegMake(self._start_date, self._maturities[i], True, NULL, @@ -240,8 +240,63 @@ cdef class CreditIndex(CurveList): free(mask) return r + def theta(self, step_in_date, value_date, maturity, YieldCurve yc not None, + double recovery_rate, double fixed_rate): + cdef: + TDate step_in_date_c = pydate_to_TDate(step_in_date) + TDate value_date_c = pydate_to_TDate(value_date) + TDate maturity_c = pydate_to_TDate(maturity) + TStubMethod stub_type + double accrued + + FeeLegAI(self._fee_legs[0], self._base_date, &accrued) + if JpmcdsStringToStubMethod(b"f/s", &stub_type) != 0: + raise ValueError("can't convert stub") + cdef: + TContingentLeg* cl = JpmcdsCdsContingentLegMake(self._start_date, + maturity_c, + 1., + True) + + TFeeLeg* fl = JpmcdsCdsFeeLegMake(self._start_date, + maturity_c, + True, + NULL, + &stub_type, + 1., + 1.0, + 3, #ACT_360 + <long>'M', # MODIFIED + b'NONE', + True) + + vector[shared_ptr[TCurve]].iterator it = self._curves.begin() + vector[double].iterator w = self._weights.begin() + double fl_pv, cl_pv, r = 0 + + while it != self._curves.end(): + JpmcdsContingentLegPV(cl, + self._base_date, + value_date_c, + step_in_date_c, + yc._thisptr.get(), + deref(it).get(), + recovery_rate, + &cl_pv) + JpmcdsFeeLegPV(fl, + self._base_date, + step_in_date_c, + value_date_c, + yc._thisptr.get(), + deref(it).get(), + True, + &fl_pv) + r += deref(w) * (cl_pv - (fl_pv - accrued) * fixed_rate) + preinc(it) + preinc(w) + return r + @cython.boundscheck(False) - @cython.cdivision(True) def duration(self, step_in_date, value_date, maturity, YieldCurve yc not None): cdef: |
