aboutsummaryrefslogtreecommitdiffstats
path: root/python/ops/process_queue.py
blob: 2b1f82c29631787d3f447da7b6d66fb88663a29e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import blpapi
import logging
import psycopg
import pathlib
import re
import redis
import sys

from serenitas.analytics.api import CreditIndex

try:
    from serenitas.utils.env import DAILY_DIR
except KeyError:
    sys.exit("Please set path of daily directory in 'SERENITAS_DAILY_DIR'")

from collections import defaultdict
from pickle import dumps, loads
from serenitas.analytics.bbg_helpers import retrieve_data

from tabulate import tabulate
from .funds import Fund
from .trade_dataclasses import DealKind


def groupby(p: redis.client.Pipeline, key: str, trade_key: str):
    d = defaultdict(list)
    for buf in p.lrange(key, 0, -1):
        trade = loads(buf)
        d[trade[trade_key]].append(trade)
    return d


def get_trades(p: redis.client.Pipeline, key: str):
    for tradeid, trades in groupby(p, key, "id").items():
        if len(trades) == 1:
            yield trades[0]
        else:
            if trades[-1]["action"] == "CANCEL":
                continue
            if trades[0]["action"] == "NEW":
                trades[-1]["action"] = "NEW"
                yield trades[-1]
            if trades[-1]["action"] == "UPDATE":
                yield trades[-1]


def process_indicative(
    p: redis.client.Pipeline,
    trade_type: str,
    upload: bool,
    session: blpapi.session.Session,
    conn: psycopg.Connection,
) -> None:
    process_fun = globals().get(
        f"{trade_type}_trade_process", lambda conn, session, trade: trade
    )
    mtm = Fund["MTM"]
    for trade in get_trades(p, trade_type):
        process_fun(conn, session, trade)
        fund = trade["fund"]
        if trade.get("upload", True) and (
            fund in ("SERCGMAST", "BOWDST") or trade_type in ("cds", "swaption")
        ):
            p.rpush(f"{trade_type}_upload", dumps(trade))
            if trade.get("swap_type", None) in (
                "CD_INDEX_OPTION",
                "CD_INDEX_TRANCHE",
                "BESPOKE",
            ):
                mtm.stage(trade, trade_type=trade_type)
    if mtm.staging_queue:
        buf, dest = mtm.build_buffer(trade_type)
        mtm.upload(buf, dest.name)
        mtm.staging_queue.clear()
    p.delete(trade_type)


def process_upload(
    p: redis.client.Pipeline,
    trade_type: str,
    upload: bool,
) -> None:
    key = f"{trade_type}_upload"
    for fund_name, l in groupby(p, key, "fund").items():
        fund = Fund[fund_name]()
        for trade in l:
            fund.stage(trade, trade_type=trade_type, redis_pipeline=p)
        buf, dest = fund.build_buffer(trade_type)
        if upload:
            fund.upload(buf, dest.name)
        fund.staging_queue.clear()
    p.delete(key)


def terminate_list(
    p: redis.client.Pipeline,
    key: str,
    upload: bool,
    conn: psycopg.connection,
    base_dir: pathlib.Path = DAILY_DIR,
):
    trade_type, fund, _ = key.split("_")
    mtm = Fund["MTM"]
    f = Fund[fund]()
    for term in p.lrange(key, 0, -1):
        obj = DealKind["termination"].from_dict(**loads(term))
        mtm.staging_queue.append(obj.to_markit())
        f.staging_queue.append(obj.to_globeop())
    if mtm.staging_queue:
        buf, dest = mtm.build_buffer(trade_type)
        mtm.upload(buf, dest.name)
        mtm.staging_queue.clear()
    if upload and f.staging_queue:
        buf, dest = f.build_buffer((trade_type, "A"))
        f.upload(buf, dest.name)
    p.delete(key)


def get_bbg_data(
    conn,
    session,
    identifier,
    cusip=None,
    isin=None,
    settle_date=None,
    asset_class=None,
    **kwargs,
):
    fields = ["MTG_FACTOR_SET_DT", "INT_ACC", "ISSUER"]
    fields_dict = {
        "Mtge": ["MTG_FACE_AMT", "START_ACC_DT"],
        "Corp": ["AMT_ISSUED", "PREV_CPN_DT"],
    }
    with conn.cursor() as c:
        c.execute(
            "SELECT identifier FROM securities WHERE identifier=%s", (identifier,)
        )
        if not c.fetchone():
            fields += [
                "MATURITY",
                "CRNCY",
                "NAME",
                "FLOATER",
                "FLT_SPREAD",
                "CPN",
                "CPN_TYP",
                "CPN_FREQ",
                "FIRST_CPN_DT",
                "MTG_PAY_DELAY",
                "DAY_CNT_DES",
                "NOMINAL_PAYMENT_DAY",
                "ISSUE_DT",
                "RESET_IDX",
                "ID_BB_GLOBAL",
            ]

    cusip_or_isin = cusip or isin
    for bbg_type in ["Mtge", "Corp"]:
        bbg_id = cusip_or_isin + " " + bbg_type
        data = retrieve_data(
            session,
            [bbg_id],
            fields + fields_dict[bbg_type],
            overrides={"SETTLE_DT": settle_date} if settle_date else None,
        )
        if data[bbg_id]:
            break
    else:
        logging.error(f"{cusip_or_isin} not in bloomberg")
        return

    bbg_data = data[bbg_id]
    if bbg_data.get("MTG_FACTOR_SET_DT", 0) == 0:
        bbg_data["MTG_FACTOR_SET_DT"] = 1
        bbg_data["INT_ACC"] = 0
    if len(fields) > 3:  # we don't have the data in the securities table
        sql_fields = [
            "identifier",
            "cusip",
            "isin",
            "description",
            "face_amount",
            "maturity",
            "floater",
            "spread",
            "coupon",
            "frequency",
            "day_count",
            "first_coupon_date",
            "pay_delay",
            "currency",
            "bbg_type",
            "asset_class",
            "start_accrued_date",
            "issuer",
            "reset_index",
            "coupon_type",
            "payment_day",
            "issue_date",
            "figi",
        ]
        placeholders = ",".join(["%s"] * len(sql_fields))
        columns = ",".join(sql_fields)

        sqlstr = (
            f"INSERT INTO securities({columns}) VALUES({placeholders}) "
            "ON CONFLICT (identifier) DO NOTHING"
        )
        isfloater = bbg_data["FLOATER"] == "Y"
        pay_delay = bbg_data.get("MTG_PAY_DELAY", 0)
        day_count = bbg_data.get("DAY_CNT_DES")
        if m := re.match(r"[^(\s]+", day_count):
            day_count = m.group(0)
        if isinstance(pay_delay, str):
            pay_delay = int(pay_delay.split(" ")[0])
        with conn.cursor() as c:
            c.execute(
                sqlstr,
                (
                    identifier,
                    cusip,
                    isin,
                    bbg_data["NAME"],
                    bbg_data.get("MTG_FACE_AMT") or bbg_data.get("AMT_ISSUED"),
                    bbg_data.get("MATURITY"),
                    isfloater,
                    bbg_data.get("FLT_SPREAD") if isfloater else None,
                    bbg_data.get("CPN") if not isfloater else None,
                    bbg_data.get("CPN_FREQ"),
                    day_count,
                    bbg_data.get("FIRST_CPN_DT"),
                    pay_delay,
                    bbg_data.get("CRNCY"),
                    bbg_type,
                    asset_class,
                    bbg_data.get("START_ACC_DT") or bbg_data.get("PREV_CPN_DT"),
                    bbg_data["ISSUER"],
                    bbg_data.get("RESET_IDX"),
                    bbg_data["CPN_TYP"],
                    bbg_data["NOMINAL_PAYMENT_DAY"],
                    bbg_data["ISSUE_DT"],
                    bbg_data["ID_BB_GLOBAL"],
                ),
            )
        conn.commit()
    return bbg_data


def bond_trade_process(conn, session, trade):
    bbg_data = get_bbg_data(conn, session, **trade)
    currentface = trade["CurrentFace"] = (
        trade["faceamount"] * bbg_data["MTG_FACTOR_SET_DT"]
    )
    accrued_payment = trade["AccruedPayment"] = (
        bbg_data["INT_ACC"] * currentface / 100.0
    )
    principal_payment = trade["PrincipalPayment"] = currentface * trade["price"] / 100.0
    if trade["accrued"] is None:
        trade["accrued"] = bbg_data["INT_ACC"]
    else:
        if trade["accrued"] != bbg_data["INT_ACC"]:
            logging.error(
                f"{trade['accrued']} does not match bbg amount of {bbg_data['INT_ACC']}"
            )

    with conn.cursor() as c:
        c.execute(
            "UPDATE bonds SET principal_payment = %s, accrued_payment = %s, accrued=%s "
            "WHERE id = %s",
            (principal_payment, accrued_payment, trade["accrued"], int(trade["id"])),
        )
    # mark it at buy price
    if trade["buysell"]:
        sqlstr = "INSERT INTO marks VALUES(%s, %s, %s) ON CONFLICT DO NOTHING"
        with conn.cursor() as c:
            c.execute(
                sqlstr, (trade["trade_date"], trade["identifier"], trade["price"])
            )
    conn.commit()
    return trade


def is_tranche_trade(trade):
    return trade["swap_type"] in ("CD_INDEX_TRANCHE", "BESPOKE")


def swaption_trade_process(conn, session, trade):
    sqlstr = (
        "SELECT indexfactor/100 FROM index_version "
        "WHERE redindexcode=%(security_id)s"
    )
    try:
        with conn.cursor() as c:
            c.execute(sqlstr, trade)
            (factor,) = c.fetchone()
    except ValueError as e:
        logging.error(e)
        return trade
    except TypeError:
        # factor missing, probably IR swaption
        pass
    else:
        trade["factor"] = factor
    finally:
        if trade["option_type"] == "RECEIVER":
            trade["OptionType"] = "Call"
        elif trade["option_type"] == "PAYER":
            trade["OptionType"] = "Put"
    return trade


def cds_trade_process(conn, session, trade):
    sqlstr = (
        "SELECT indexfactor/100 FROM index_version "
        "WHERE redindexcode=%(security_id)s"
    )
    try:
        with conn.cursor() as c:
            c.execute(sqlstr, trade)
            (factor,) = c.fetchone()
    except ValueError:
        bbg_data = get_bbg_data(
            conn,
            session,
            trade["security_id"],
            isin=trade["security_id"],
            asset_class="Subprime",
        )

        factor = bbg_data["MTG_FACTOR_SET_DT"]
    if is_tranche_trade(trade):
        tranche_factor = (trade["attach"] - trade["detach"]) / (
            trade["orig_attach"] - trade["orig_detach"]
        )
        trade["curr_notional"] = trade["notional"] * tranche_factor
        trade["Factor"] = tranche_factor
    else:
        trade["curr_notional"] = trade["notional"] * factor
        trade["Factor"] = factor
    if trade["upfront"]:
        return trade
    index = CreditIndex(
        redcode=trade["security_id"],
        maturity=trade["maturity"],
        notional=trade["notional"],
        value_date=trade["trade_date"],
    )
    index.direction = trade["protection"]
    with conn.cursor() as c:
        if trade["traded_level"]:
            if not is_tranche_trade(trade):
                index.ref = float(trade["traded_level"])
                trade["upfront"] = -index.pv
            else:
                accrued = index._accrued * trade["fixed_rate"]
                match index.index_type:
                    case "HY":
                        dirty_price = float(trade["traded_level"]) + accrued
                        trade["upfront"] = (
                            -(100 - dirty_price)
                            * index.notional
                            * trade["Factor"]
                            * 0.01
                        )
                    case "EU" | "XO" if trade["orig_attach"] in (6, 12, 35):
                        if trade["orig_attach"] == 6:
                            index.recovery = 0.0
                        index.spread = float(trade["traded_level"])
                        trade["upfront"] = (
                            -index._pv * trade["notional"] * trade["Factor"]
                        )
                    case _:
                        dirty_protection = float(trade["traded_level"]) - accrued
                        trade["upfront"] = (
                            -dirty_protection * index.notional * trade["Factor"] * 0.01
                        )
            c.execute(
                "UPDATE cds SET upfront=%s WHERE dealid=%s",
                (trade["upfront"], trade["dealid"]),
            )

        else:
            index.pv = -trade["upfront"]
            trade["traded_level"] = index.ref
            c.execute(
                "UPDATE cds SET traded_level=%s WHERE dealid=%s",
                (trade["traded_level"], trade["dealid"]),
            )
    conn.commit()
    return trade


def print_trade(trade):
    d = trade.copy()
    d["buysell"] = "Buy" if d["buysell"] else "Sell"
    return tabulate((k, v) for k, v in d.items())