summaryrefslogtreecommitdiffstats
path: root/legs.pyx
blob: 5db39d339d481293789bd1f27962ba72d9922ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from legs cimport (JpmcdsCdsContingentLegMake,
                   JpmcdsCdsFeeLegMake, JpmcdsFeeLegFlows, TBoolean)
from libc.stdlib cimport free
from date cimport pydate_to_TDate, TDate_to_pydate, dcc
from cdsone cimport JpmcdsStringToStubMethod, TStubMethod
from curve cimport ZeroCurve, SpreadCurve

cdef class ContingentLeg:

    def __cinit__(self, start_date, end_date, double notional,
                  TBoolean protect_start = True):

        self._thisptr = JpmcdsCdsContingentLegMake(pydate_to_TDate(start_date),
                                                   pydate_to_TDate(end_date),
                                                   notional,
                                                   protect_start)
    def __dealloc__(self):
        if self._thisptr is not NULL:
            free(self._thisptr)

cdef class FeeLeg:

    def __cinit__(self, start_date, end_date, TBoolean pay_accrued_on_default,
                  double notional, double coupon_rate, str payment_dcc = 'ACT/360',
                  TBoolean protect_start = True):

        cdef TStubMethod stub_type

        if JpmcdsStringToStubMethod(b"f/s", &stub_type) != 0:
            raise ValueError("can't convert stub")

        self._thisptr = JpmcdsCdsFeeLegMake(pydate_to_TDate(start_date),
                                            pydate_to_TDate(end_date),
                                            pay_accrued_on_default,
                                            NULL,
                                            &stub_type,
                                            notional,
                                            coupon_rate,
                                            dcc(payment_dcc),
                                            <long>'M',
                                            NULL,
                                            protect_start)
    @property
    def cashflows(self):
        cdef TCashFlowList* cfl = JpmcdsFeeLegFlows(self._thisptr)
        cdef TCashFlow cf
        result = []
        for i in range(cf.fNumItems):
            cf = cfl.fArray[i]
            result.append((TDate_to_pydate(cf.fDate), cf.fAmount))
        return result

    def pv(self, today, step_in_date, value_date, ZeroCurve zc, SpreadCurve sc,
           TBoolean pay_accrued_at_start):
        cdef today_c = pydate_to_TDate(today)
        cdef step_in_date_c = pydate_to_TDate(step_in_date)
        cdef value_date_c = pydate_to_TDate(value_date)
        cdef double pv
        if JpmcdsFeeLegPV(self._thisptr, today, step_in_date, value_date,
                          zc._thisptr, sc._thisptr, pay_accrued_at_start, &pv) == 0:
            return pv
        else:
            raise ValueError

    def __dealloc__(self):
        if self._thisptr is not NULL:
            JpmcdsFeeLegFree(self._thisptr)