summaryrefslogtreecommitdiffstats
path: root/zerocurve.pyx
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@serenitascapital.com>2016-06-28 15:30:16 -0400
committerGuillaume Horel <guillaume.horel@serenitascapital.com>2016-06-28 15:30:16 -0400
commit60e49a606ae270fb4d455da1b565e0ea656ffb3c (patch)
treeada0b2e8bfa6b5bb4dc8180f7d83ca57572378e5 /zerocurve.pyx
downloadpyisda-60e49a606ae270fb4d455da1b565e0ea656ffb3c.tar.gz
initial import
Diffstat (limited to 'zerocurve.pyx')
-rw-r--r--zerocurve.pyx68
1 files changed, 68 insertions, 0 deletions
diff --git a/zerocurve.pyx b/zerocurve.pyx
new file mode 100644
index 0000000..2918bae
--- /dev/null
+++ b/zerocurve.pyx
@@ -0,0 +1,68 @@
+from cpython cimport datetime
+from libc.stdlib cimport malloc, free
+from pyisda.zerocurve cimport JpmcdsBuildIRZeroCurve
+from pyisda.yearfrac cimport dcc
+from pyisda.convert cimport JpmcdsStringToDateInterval, JpmcdsDateIntervalToFreq, JpmcdsDate
+
+cdef int SUCCESS = 0
+
+cdef class Tcurve:
+ cdef TCurve* _thisptr
+ cdef TDate* _dates
+
+ def __cinit__(self, date, str types,
+ list dates, double[:] rates,
+ str mm_dcc, str fixed_swap_period, str float_swap_period,
+ str fixed_swap_dcc, str float_swap_dcc, char bad_day_conv,
+ char* holidayFile):
+
+ cdef:
+ double fixed_freq
+ double float_freq
+ TDateInterval ivl
+ char* routine = 'zerocurve'
+ TDate value_date
+
+ if isinstance(date, datetime.date):
+ value_date = JpmcdsDate(date.year, date.month, date.day)
+ else:
+ raise ValueError
+
+ cdef TDate* _dates = <TDate*>malloc(len(dates) * sizeof(TDate))
+
+ for i, d in enumerate(date):
+ _dates[i] = JpmcdsDate(d.year, d.month, d.day)
+
+ fixed_bytes = fixed_swap_period.encode('utf-8')
+ float_bytes = float_swap_period.encode('utf-8')
+ types_bytes = types.encode('utf-8')
+
+ if JpmcdsStringToDateInterval(fixed_bytes, routine, &ivl) != SUCCESS:
+ raise ValueError
+ if JpmcdsDateIntervalToFreq(&ivl, &fixed_freq) != SUCCESS:
+ raise ValueError
+ if JpmcdsStringToDateInterval(float_bytes, routine, &ivl) != SUCCESS:
+ raise ValueError
+ if JpmcdsDateIntervalToFreq(&ivl, &float_freq) != SUCCESS:
+ raise ValueError
+ self._thisptr = JpmcdsBuildIRZeroCurve(
+ value_date, types_bytes, _dates,
+ &rates[0], len(dates), dcc(mm_dcc), <long> fixed_freq,
+ <long> float_freq,
+ dcc(fixed_swap_dcc), dcc(fixed_swap_dcc), <long>bad_day_conv, "None"
+ )
+
+ def __dealloc__(self):
+ if self._thisptr is not NULL:
+ JpmcdsFreeTCurve(self._thisptr)
+ if self._dates is not NULL:
+ free(self._dates)
+
+ def discount_factor(self, date):
+ cdef TDate discount_date
+ if isinstance(date, datetime.date):
+ discount_date = JpmcdsDate(date.year, date.month, date.day)
+ else:
+ raise ValueError
+
+ return JpmcdsZeroPrice(self._thisptr, discount_date)