aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/tranche_basket.py
blob: 67327e0033b3a9e7d6d75eab6a0cb1d337654722 (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
from .basket_index import BasketIndex
from .db import _engine
from .tranche_functions import credit_schedule
from index_data import get_singlenames_curves, get_tranche_quotes
from pyisda.cdsone import upfront_charge
from pandas.tseries.offsets import BDay

import pandas as pd

class TrancheBasket(BasketIndex):
    def __init__(self, index_type: str, series: int, tenor: str, *args,
                 trade_date: pd.Timestamp=pd.Timestamp.today().normalize() - BDay()):
        super().__init__(index_type, series, [tenor], *args, trade_date)
        self.tranche_quotes = get_tranche_quotes(index_type, series, tenor, trade_date.date())
        index_desc = self.index_desc.reset_index('maturity').set_index('tenor')
        self.maturity = index_desc.loc[tenor].maturity
        self.start_date, self.cs = credit_schedule(trade_date, tenor[:-1], 1, self.yc)
        self.K_orig = [0] + [q['detach'] for q in self.tranche_quotes]
        #self.K = adjust_attachments(self.K_orig, self

    def _get_quotes(self):
        refprice = self.tranche_quotes[0]['indexrefprice']
        refspread = self.tranche_quotes[0]['indexrefspread']
        if refprice is not None:
            return {self.maturity: 1 - refprice / 100}
        if refspread is not None:
            return {self.maturity:
                    upfront_charge(self.trade_date, self.value_date, self.start_date,
                                   self.step_in_date, self.start_date, self.maturity,
                                   self.coupon(self.maturity), self.yc,
                                   refspread * 1e-4, self.recovery)}
            raise ValueError("ref is missing")

    @property
    def survival_matrix(self):
        return super().survival_matrix(self.cs.index.values.astype('M8[D]').view('int') + 134774)