aboutsummaryrefslogtreecommitdiffstats
path: root/python/populate_tranche_cashflows.py
blob: b92430bc442663973cfaa10296b067b7930e0e3e (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
import datetime
from pyisda.date import previous_twentieth
from utils.db import serenitas_pool, dbconn
from risk.tranches import get_tranche_portfolio
from dates import bus_day


def accrual_days(event_date, auction_date):
    return (event_date - previous_twentieth(auction_date)).days + 1


dawndb = dbconn("dawndb")
conn = serenitas_pool.getconn()
with conn.cursor() as c:
    c.execute("SELECT * FROM defaulted ORDER BY auction_date")
    for r in c:
        if r.recovery is None:
            continue
        if r.auction_date < datetime.date(2020, 5, 1):
            continue
        acc = accrual_days(r.event_date, r.auction_date)
        severity = 1 - r.recovery
        principal = -1e6 * severity
        accrued = severity * acc * 0.05 / 360 * 1e6
        with dawndb.cursor() as c:
            for tranche_id in (1590, 1413, 1116):
                c.execute(
                    "INSERT INTO tranche_cashflows VALUES(%s, %s, %s, %s, %s)",
                    (r.auction_date + bus_day, tranche_id, principal, accrued, "USD"),
                )
        dawndb.commit()
serenitas_pool.putconn(conn)

portf = get_tranche_portfolio(datetime.date(2020, 6, 19), dawndb)
with dawndb.cursor() as c:
    for t in portf:
        c.execute(
            "INSERT INTO tranche_cashflows(date, tranche_id, accrued, currency) "
            "VALUES(%s, %s, %s, %s)",
            (
                datetime.date(2020, 6, 22),
                t.trade_id,
                -t.cs["coupons"][0] * t.tranche_running * t.notional * 1e-4,
                t.currency,
            ),
        )
dawndb.commit()