aboutsummaryrefslogtreecommitdiffstats
path: root/python/risk/tranches.py
blob: 4b13dc22a54190fdf81d566fd46cd3226072e08e (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
from analytics import Portfolio, DualCorrTranche
import logging

logger = logging.getLogger(__name__)

def get_tranche_portfolio(date, conn, by_strat=False):
    if by_strat:
        sql_string = "SELECT * from list_tranche_positions_by_strat(%s)"
    else:
        sql_string = "SELECT dealid from list_tranche_positions(%s)"
    with conn.cursor() as c:
        c.execute(sql_string, (date,))
        if by_strat:
            trade_ids = list(c)
        else:
            trade_ids = [dealid for dealid, in c]
    if by_strat:
        portf = Portfolio([DualCorrTranche(redcode=d['security_id'],
                                maturity=d['maturity'],
                                notional=d['notional'],
                                tranche_running=d['fixed_rate']*100,
                                attach=d['orig_attach'],
                                detach=d['orig_detach'],
                                corr_attach=None, corr_detach=None) for d in trade_ids])
        portf.trade_ids = [(d["folder"],
                             f"{t.index_type} {t.series} {t.tenor} {t.attach}-{t.detach}")
                            for d, t in zip(trade_ids, portf.trades)]
    else:
        portf = Portfolio([DualCorrTranche.from_tradeid(dealid) for dealid in trade_ids],
                          trade_ids)
    portf.value_date = date
    portf.mark()
    return portf