diff options
| -rw-r--r-- | curve.pyx | 34 | ||||
| -rw-r--r-- | date.pxd | 8 | ||||
| -rw-r--r-- | date.pyx | 14 | ||||
| -rw-r--r-- | legs.pyx | 10 |
4 files changed, 39 insertions, 27 deletions
@@ -1,6 +1,5 @@ -from cpython cimport datetime from libc.stdlib cimport malloc, free -from curve cimport JpmcdsBuildIRZeroCurve, JpmcdsZeroPrice, +from curve cimport JpmcdsBuildIRZeroCurve, JpmcdsZeroPrice from date cimport (JpmcdsStringToDateInterval, pydate_to_TDate, dcc, JpmcdsDateIntervalToFreq, JpmcdsDateFwdThenAdjust, TDate_to_pydate, JpmcdsDateFromBusDaysOffset) @@ -14,9 +13,12 @@ cpdef public enum BadDay: MODIFIED = <long>'M' cdef class Curve: - def __dealloc__(self): - if self._thisptr is not NULL: - JpmcdsFreeTCurve(self._thisptr) + def __dealloc__(self): + if self._thisptr is not NULL: + JpmcdsFreeTCurve(self._thisptr) + + def inspect(self): + return {'base_date': TDate_to_pydate(self._thiptr.fBaseDate)} cdef class ZeroCurve(Curve): @@ -87,20 +89,20 @@ cdef class ZeroCurve(Curve): cdef class SpreadCurve(Curve): - def __init__(self, today, ZeroCurve zc, start_date, step_in_date, cash_settle_date, - list end_dates, double[:] coupon_rates, double recovery_rate, - TBoolean pay_accrued_on_default): + def __init__(self, today, ZeroCurve zc, start_date, step_in_date, + cash_settle_date, list end_dates, double[:] coupon_rates, + double recovery_rate, int pay_accrued_on_default): - cdef today_c = pydate_to_TDate(today) - cdef step_in_date_c = pydate_to_TDate(step_in_date) - cdef cash_settle_date_c = pydate_to_TDate(cash_settle_date) - cdef start_date_c = pydate_to_TDate(start_date) + cdef TDate today_c = pydate_to_TDate(today) + cdef TDate step_in_date_c = pydate_to_TDate(step_in_date) + cdef TDate cash_settle_date_c = pydate_to_TDate(cash_settle_date) + cdef TDate start_date_c = pydate_to_TDate(start_date) cdef TDate* end_dates_c = <TDate*>malloc(len(end_dates) * sizeof(TDate)) - + self._thisptr = NULL cdef size_t i for i, d in enumerate(end_dates): - end_dates_c[i] = pydate_to_TDate(end_dates) + end_dates_c[i] = pydate_to_TDate(d) cdef TStubMethod stub_type if JpmcdsStringToStubMethod(b"f/s", &stub_type) != 0: @@ -110,7 +112,7 @@ cdef class SpreadCurve(Curve): zc._thisptr, start_date_c, step_in_date_c, - cash_settle_date, + cash_settle_date_c, len(end_dates), end_dates_c, &coupon_rates[0], @@ -121,4 +123,4 @@ cdef class SpreadCurve(Curve): dcc('ACT/360'), &stub_type, <long>'M', - NULL) + b'NONE') @@ -1,7 +1,9 @@ +from cpython cimport datetime as c_datetime + cdef extern from "isda/yearfrac.h": int JpmcdsStringToDayCountConv(char* day_count, long* type) -cdef long dcc(str day_count) +cdef long dcc(str day_count) except -1 cdef extern from "isda/cdate.h": ctypedef struct TDateInterval: @@ -37,6 +39,6 @@ cdef extern from "isda/busday.h": long offset, # (I) number of business days char *holidayFile, # (I) holiday file specification TDate *result); -cdef TDate pydate_to_TDate(d) +cdef TDate pydate_to_TDate(c_datetime.date d) -cpdef object TDate_to_pydate(TDate d) +cpdef c_datetime.date TDate_to_pydate(TDate d) @@ -1,16 +1,18 @@ import datetime +from cpython cimport datetime as c_datetime from date cimport JpmcdsDate, JpmcdsStringToDayCountConv -cdef TDate pydate_to_TDate(d): - assert isinstance(d, datetime.date) - return JpmcdsDate(d.year, d.month, d.day) +c_datetime.import_datetime() -cpdef object TDate_to_pydate(TDate d): +cdef TDate pydate_to_TDate(c_datetime.date d): + return JpmcdsDate(<long>d.year, <long>d.month, <long>d.day) + +cpdef c_datetime.date TDate_to_pydate(TDate d): cdef TMonthDayYear date if JpmcdsDateToMDY(d, &date) == 0: - return datetime.date(date.year, date.month, date.day) + return c_datetime.date_new(date.year, date.month, date.day) -cdef long dcc(str day_count): +cdef long dcc(str day_count) except -1: cdef long r dc_bytes = day_count.encode('utf-8') cdef char* dc = dc_bytes @@ -14,6 +14,9 @@ cdef class ContingentLeg: pydate_to_TDate(end_date), notional, protect_start) + if self._thisptr is NULL: + raise ValueError + def __dealloc__(self): if self._thisptr is not NULL: free(self._thisptr) @@ -38,14 +41,17 @@ cdef class FeeLeg: coupon_rate, dcc(payment_dcc), <long>'M', - NULL, + b'NONE', protect_start) + if self._thisptr is NULL: + raise ValueError + @property def cashflows(self): cdef TCashFlowList* cfl = JpmcdsFeeLegFlows(self._thisptr) cdef TCashFlow cf result = [] - for i in range(cf.fNumItems): + for i in range(cfl.fNumItems): cf = cfl.fArray[i] result.append((TDate_to_pydate(cf.fDate), cf.fAmount)) return result |
