summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pyisda/credit_index.pxd2
-rw-r--r--pyisda/credit_index.pyx59
2 files changed, 51 insertions, 10 deletions
diff --git a/pyisda/credit_index.pxd b/pyisda/credit_index.pxd
index 24ccf6f..cb7c944 100644
--- a/pyisda/credit_index.pxd
+++ b/pyisda/credit_index.pxd
@@ -8,6 +8,8 @@ cdef class CurveList:
cdef int _len_curves
cdef double* _T
cdef int _len_T
+ cdef list _tickers
+ cdef dict _tickersdict
cdef class CreditIndex(CurveList):
cdef TDate* _maturities
diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx
index 0ea469c..c7cd4e9 100644
--- a/pyisda/credit_index.pyx
+++ b/pyisda/credit_index.pyx
@@ -3,7 +3,7 @@ cimport cython
from legs cimport (JpmcdsCdsContingentLegMake, JpmcdsCdsFeeLegMake,
JpmcdsContingentLegPV, JpmcdsFeeLegPV, FeeLegAI, JpmcdsFeeLegFree)
from curve cimport (SpreadCurve, JpmcdsCopyCurve, tweak_curve, YieldCurve,
- JpmcdsFreeTCurve, JpmcdsNewTCurve)
+ JpmcdsFreeTCurve)
from date cimport pydate_to_TDate, TDate_to_pydate, ACT_365F
from cdsone cimport JpmcdsStringToStubMethod, TStubMethod
from date cimport ACT_365F
@@ -11,8 +11,15 @@ cimport numpy as np
np.import_array()
cdef class CurveList:
- def __init__(self, curves):
- cdef SpreadCurve sc = <SpreadCurve?>curves[0]
+ def __init__(self, curves, tickers=None):
+ cdef SpreadCurve sc
+ if isinstance(curves[0], tuple):
+ sc = <SpreadCurve?>(curves[0][1])
+ elif isinstance(curves[0], SpreadCurve):
+ sc = <SpreadCurve>curves[0]
+ else:
+ raise TypeError("curves need to be a list of SpreadCurve "\
+ "or a list of tuple (SpreadCurve, ticker)")
self._len_T = sc._thisptr.fNumItems
self._base_date = sc._thisptr.fBaseDate
self._T = <double*>malloc(sizeof(double) * self._len_T)
@@ -21,8 +28,24 @@ cdef class CurveList:
self._T[i] = (sc._thisptr.fArray[i].fDate - self._base_date) / 365.
self._len_curves = len(curves)
self._curves = <TCurve**>malloc(sizeof(TCurve*) * self._len_curves)
- for i, c in enumerate(curves):
- self._curves[i] = JpmcdsCopyCurve((<SpreadCurve?>c)._thisptr)
+
+ self._tickers = []
+ self._tickersdict = {}
+ if isinstance(curves[0], tuple):
+ for i, (t, c) in enumerate(curves):
+ self._curves[i] = JpmcdsCopyCurve((<SpreadCurve?>c)._thisptr)
+ self._tickers.append(t)
+ self._tickersdict[t] = i
+ else:
+ for i, c in enumerate(curves):
+ self._curves[i] = JpmcdsCopyCurve((<SpreadCurve?>c)._thisptr)
+
+ if tickers and not self._tickers:
+ if len(tickers) != self._len_curves:
+ raise ValueError("tickers must have the same length as curves")
+ else:
+ self._tickers = tickers
+ self._tickersdict = {t: i for i, t in enumerate(tickers)}
def __dealloc__(self):
if self._T is not NULL:
@@ -32,6 +55,24 @@ cdef class CurveList:
JpmcdsFreeTCurve(self._curves[i])
free(self._curves)
+ def __getitem__(self, str ticker):
+ ## would need to use a shared pointer to avoid a copy
+ cdef SpreadCurve sc
+ if ticker in self._tickers:
+ sc = SpreadCurve.__new__(SpreadCurve)
+ sc._thisptr = JpmcdsCopyCurve(self._curves[self._tickersdict[ticker]])
+ return sc
+ else:
+ raise KeyError(ticker)
+
+ def iteritems(self):
+ ## would need to use a shared pointer to avoid a copy
+ cdef SpreadCurve sc
+ for i in range(self._len_curves):
+ sc = SpreadCurve.__new__(SpreadCurve)
+ sc._thisptr = self._curves[i]
+ yield (self._tickers[i], sc)
+
@property
def curves(self):
"""returns the list of curves inside the porfolio.
@@ -166,13 +207,10 @@ cdef class CreditIndex(CurveList):
if mask == NULL:
raise ValueError("maturity is not correct")
h = <double*>malloc(sizeof(double) * self._len_T)
- tweaked_curve = JpmcdsNewTCurve(self._base_date,
- self._len_T,
- 5000.,
- ACT_365F)
+ tweaked_curve = JpmcdsCopyCurve(self._curves[0])
cdef:
- double fl_pv, cl_pv, r=0
+ double fl_pv, cl_pv, r = 0
for i in range(self._len_curves):
if epsilon != 0:
@@ -207,6 +245,7 @@ cdef class CreditIndex(CurveList):
cdef list r = []
for i in range(self._len_maturities):
r.append(TDate_to_pydate(self._maturities[i]))
+ return r
def tweak_portfolio(self, double epsilon, maturity, bint inplace=True):
cdef TDate maturity_c = pydate_to_TDate(maturity)