summaryrefslogtreecommitdiffstats
path: root/legs.pyx
blob: 2df83e399fde807fe73ed2658a29ba17c2d5ca91 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 YieldCurve, 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)
        if self._thisptr is NULL:
            raise ValueError

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

    def pv(self, today, step_in_date, value_date, YieldCurve yc, SpreadCurve sc,
           double recovery_rate):
        cdef TDate today_c = pydate_to_TDate(today)
        cdef TDate step_in_date_c = pydate_to_TDate(step_in_date)
        cdef TDate value_date_c = pydate_to_TDate(value_date)
        cdef double pv
        if JpmcdsContingentLegPV(self._thisptr, today_c, value_date_c, step_in_date_c,
                                 yc._thisptr, sc._thisptr, recovery_rate, &pv) == 0:
            return pv
        else:
            raise ValueError

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',
                                            b'NONE',
                                            protect_start)
        if self._thisptr is NULL:
            raise ValueError

    def inspect(self):
        cdef list acc_start_dates = []
        cdef list acc_end_dates = []
        cdef list pay_dates = []
        cdef size_t i
        for i in range(self._thisptr.nbDates):
            acc_start_dates.append(TDate_to_pydate(self._thisptr.accStartDates[i]))
            acc_end_dates.append(TDate_to_pydate(self._thisptr.accEndDates[i]))
            pay_dates.append(TDate_to_pydate(self._thisptr.payDates[i]))
        return {'acc_start_dates': acc_start_dates,
                'acc_end_dates': acc_end_dates,
                'pay_dates': pay_dates}

    @property
    def cashflows(self):
        cdef TCashFlowList* cfl = JpmcdsFeeLegFlows(self._thisptr)
        cdef TCashFlow cf
        result = []
        for i in range(cfl.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, YieldCurve yc, SpreadCurve sc,
           TBoolean pay_accrued_at_start):
        cdef TDate today_c = pydate_to_TDate(today)
        cdef TDate step_in_date_c = pydate_to_TDate(step_in_date)
        cdef TDate value_date_c = pydate_to_TDate(value_date)
        cdef double pv
        if JpmcdsFeeLegPV(self._thisptr, today_c, step_in_date_c, value_date_c,
                          yc._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)