aboutsummaryrefslogtreecommitdiffstats
path: root/python/book_bbg.py
blob: 671bf1c01ff995f0e2e613ba94081a94b529b35e (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
from serenitas.utils.env import DAILY_DIR
from serenitas.utils.remote import SftpClient
from zoneinfo import ZoneInfo
import datetime
import csv
from trade_dataclasses import CDSDeal, BondDeal
from decimal import Decimal
from serenitas.utils.db import dbconn
from lru import LRU
from stat import S_ISREG
import pandas as pd
from sqlalchemy.exc import SQLAlchemyError
import time

_funds = {"SERENITAS_CGMF": "SERCGMAST", "BOWDOINST": "BOWDST"}
_fcms = {"Bank of America, N.A.": "BAML", "Goldman Sachs": "GS"}
_cdx_cp = {
    "MSDU": "MSCSNY",
    "GSMX": "GOLDNY",
    "JPGP": "JPCBNY",
    "JFF": "JEFF",
    "BMLE": "BAMSNY",
    "BARX": "BARCNY",
    "CSDA": "CSFBBO",
    "EBNP": "BNPBNY",
    "WFCD": "WELFEI",
    "BSEF": "BSEONY",
    "JPOS": "JPCBNY",
    "CGCI": "CITINY",
}
_bond_cp = {
    "CG": "CITINY",
    "WFBS": "WELFEI",
    "MZZ": "MIZUNY",
    "BABS": "BAML",
    "PTRU": "PERFCH",
    "BARC": "BARCNY",
    "MS": "MORGNY",
    "BA": "BAML",
    "FB": "CSUINY",
    "INTC": "STONEX",
    "SOCG": "SGSANY",
    "NOM": "NOMINY",
    "JP": "JPCBNY",
    "BTIG": "BTIG",
}


def get_indic_data(conn, redcode, tenor):
    sql_str = (
        "SELECT maturity, coupon "
        "FROM index_desc "
        "WHERE tenor=%s AND redindexcode=%s "
    )
    with conn.cursor() as c:
        c.execute(sql_str, (redcode, tenor))
        return c.fetchone()


def cdx_booking_process(path, conn):
    with open(path) as fh:
        reader = csv.DictReader(fh)
        for line in reader:
            tenor = line["Security"].rsplit(" ", 1)[-1].lower() + "r"
            maturity, coupon = get_indic_data(conn, tenor, line["Red Code"])
            trade = CDSDeal(
                fund=_funds[line["Account"]],
                folder="*",
                portfolio="UNALLOCATED",
                security_id=line["Red Code"],
                security_desc=line["Security"].removesuffix(" PRC"),
                traded_level=Decimal(line["Price (Dec)"]),
                notional=line["Quantity"],
                fixed_rate=coupon * 0.01,
                trade_date=datetime.datetime.strptime(
                    line["Trade Dt"], "%m/%d/%Y"
                ).date(),
                maturity=maturity,
                currency=line["Curncy"],
                protection="Buyer" if line["Side"] == "B" else "Seller",
                upfront=line["Principal"],
                cp_code=_cdx_cp[line["Brkr"]],
                account_code=_fcms[line["Client FCM"]],
            )
            trade.stage()
        CDSDeal.commit()


def bond_booking_process(path, conn, fname, _cache):
    df = pd.read_csv(path)
    for _, line in df.iterrows():
        trade = BondDeal(
            faceamount=Decimal(line["Quantity"]),
            price=Decimal(line["Price (Dec)"]),
            cp_code=_bond_cp[line["Brkr"]],
            cusip=line["Cusip"],
            identifier=line["Cusip"],
            trade_date=datetime.datetime.strptime(line["Trade Dt"], "%m/%d/%Y"),
            settle_date=datetime.datetime.strptime(line["SetDt"], "%m/%d/%Y"),
            portfolio="UNALLOCATED",
            asset_class=None,
            description=line["Security"].removesuffix(" Mtge"),
            buysell=line["Side"] == "B",
            bbg_ticket_id=fname,
        )
        trade.stage()
    df["bbg_ticket_id"] = [fname]
    try:
        df.to_sql("bond_tickets", dawn_engine, if_exists="append", index=False)
    except SQLAlchemyError as e:
        error = str(e.__dict__["orig"])
        BondDeal._insert_queue.clear()
        print(error)
    else:
        _cache[fname] = None
    finally:
        BondDeal.commit()


def get_bbg_id(name):
    return name.split("_", 1)[1]


if __name__ == "__main__":
    from serenitas.utils.db import serenitas_pool, dawn_engine

    conn = serenitas_pool.getconn()
    _cache = LRU(128)
    while True:
        d = datetime.date.today()
        sftp = SftpClient.from_creds("bbg")
        filters = [lambda f: S_ISREG(f.st_mode)]
        for f in filter(
            lambda f: all(filt(f) for filt in filters), sftp.client.listdir_iter("/")
        ):
            if ("CDX" in f.filename) or ("BOND" in f.filename):
                if get_bbg_id(f.filename) in _cache.keys():
                    continue
                else:
                    if "CDX" in f.filename:
                        # cds_booking_process(sftp.client.open(f"/{f.filename}"), conn, get_bbg_id(f.filename), _cache)
                        pass
                    elif "BOND" in f.filename:
                        bond_booking_process(
                            sftp.client.open(f"/{f.filename}"),
                            conn,
                            get_bbg_id(f.filename),
                            _cache,
                        )
        time.sleep(60)