aboutsummaryrefslogtreecommitdiffstats
path: root/python/citco_ce_tranche.py
blob: be6ca3739765e533c903721feaf6fedca1a1e11d (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
import datetime

from serenitas.utils.db import dbconn
from serenitas.ops.trade_dataclasses import CDSDeal
from serenitas.ops.funds import Service

from globeop_ce_tranche import calculate_attach_detach


def upload_citco_products(index, affected_series, fund, event_date, conn, event_name):
    NEW_TRADES_QUERY = (
        "SELECT trc.*, b.security_id, cds.currency, cds.fixed_rate, cds.cp_code as custodian_cpty, cds.cp_code, cds.protection, cds.account_code, cds.frequency, "
        "cds.dealid, cds.portfolio, cds.folder, cds.swap_type, cds.clearing_facility, a.custodian, a.cash_account "
        "FROM tranche_risk_citco trc "
        "LEFT JOIN LATERAL (SELECT redindexcode as security_id FROM index_desc WHERE index_desc.lastdate >%s AND index_desc.index=trc.index AND index_desc.series=trc.series LIMIT 1) b ON TRUE "
        "LEFT JOIN cds ON trc.trade_id=cds.id "
        "LEFT JOIN accounts a ON cds.account_code=a.code "
        "WHERE date=%s AND trc.fund=%s AND index=%s AND series in %s"
    )

    with conn.cursor() as c:
        c.execute(
            NEW_TRADES_QUERY,
            (
                event_date,
                event_date,
                fund,
                index,
                affected_series,
            ),
        )
        service = Service["ISOSEL-Product"]
        for row in c:
            d = row._asdict()
            d["notional"] = abs(d["notional"])
            d["traded_level"] = None
            d["upfront"] = (d["serenitas_clean_nav"] + d["serenitas_accrued"]) * -1
            d["trade_date"] = event_date
            d["dealid"] = f'{d["dealid"]}_{event_name}'
            d["attach"], d["detach"] = calculate_attach_detach(
                d["security_id"], d["orig_attach"], d["orig_detach"], conn
            )
            trade = CDSDeal.from_dict(**d)
            prod = trade.product
            service.staging_queue.append(prod.to_citco())

        service.build_buffer(trade_type="cds")


if __name__ == "__main__":
    index = "HY"
    affected_series = (
        34,
        35,
        36,
        37,
        38,
        39,
    )
    fund = "ISOSEL"
    event_date = datetime.date(2023, 4, 13)
    settlement_date = datetime.date(2023, 4, 18)
    event_name = "DIAMOND"

    conn = dbconn("dawndb")
    upload_citco_products(index, affected_series, fund, event_date, conn, event_name)
    # terminate_old_trades(index, affected_series, fund, event_date, )