From a1554f98515d506e5b494b4d849b91306744163e Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Fri, 16 Mar 2018 16:59:44 -0400 Subject: Fix previous_twentieth --- pyisda/curve.pyx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index c966d03..7fbc151 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -2,7 +2,7 @@ from libc.math cimport log1p, log from date cimport (JpmcdsStringToDateInterval, pydate_to_TDate, dcc, TMonthDayYear, JpmcdsDateIntervalToFreq, JpmcdsDateFwdThenAdjust, TDate_to_pydate, JpmcdsDateFromBusDaysOffset, JpmcdsStringToDayCountConv, ACT_360, - JpmcdsDateToMDY, JpmcdsMDYToDate) + JpmcdsDateToMDY, JpmcdsMDYToDate, JpmcdsFormatDate) from date import dcc_tostring from cdsone cimport JpmcdsStringToStubMethod, TStubMethod from legs cimport (JpmcdsCdsContingentLegMake, JpmcdsCdsFeeLegMake, @@ -42,19 +42,26 @@ cdef inline void double_free(double* ptr) nogil: cdef TDate previous_twentieth(TDate d) nogil: cdef TMonthDayYear mdy - JpmcdsDateToMDY(d, &mdy) + if JpmcdsDateToMDY(d, &mdy) != SUCCESS: + return -1 if mdy.day < 20: - mdy.month = 12 if mdy.month == 1 else mdy.month - 1 + if mdy.month == 1: + mdy.month = 12 + mdy.year -= 1 + else: + mdy.month -= 1 mdy.day = 20 cdef int mod = mdy.month % 3 if mod != 0: mdy.month -= mod - if mdy.month < 0: + if mdy.month <= 0: mdy.month += 12 mdy.year -= 1 cdef TDate r - JpmcdsMDYToDate(&mdy, &r) - return r + if JpmcdsMDYToDate(&mdy, &r) != SUCCESS: + return -1 + else: + return r cdef class Curve(object): @@ -489,6 +496,8 @@ cdef class SpreadCurve(Curve): if start_date is None: start_date_c = previous_twentieth(today_c) + if start_date_c == -1: + raise ValueError("incorrect today's date: " + today) else: start_date_c = pydate_to_TDate(start_date) if step_in_date is None: -- cgit v1.2.3-70-g09d2