aboutsummaryrefslogtreecommitdiffstats
path: root/python/monthend_recon_bowdst.py
blob: a4d1919f2efcd9b1aa3d2b0e94158c2c0806ab22 (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
import pandas as pd
from serenitas.utils.db import dbconn
import argparse
from serenitas.utils.exchange import ExchangeMessage
from exchangelib import FileAttachment
from io import StringIO
from serenitas.utils.env import DAILY_DIR
from serenitas.utils.db import dbconn, dawn_engine
import datetime
from pandas.tseries.offsets import BDay
import numpy as np
from dateutil.relativedelta import relativedelta
from serenitas.analytics.dates import prev_business_day


def get_dir(date):
    p = DAILY_DIR / "BOWD_recon" / f"{date:%Y_%m}"
    return p


def clear_date(date, conn):
    with conn.cursor() as c:
        c.execute("DELETE FROM bowdst_val bv WHERE as_of_date = %s;", (date,))
    conn.commit()


def load_val_report(date, conn):
    p = get_dir(date) / f"Asset Detail.csv"
    df = pd.read_csv(
        p, thousands=",", parse_dates=["As Of Date", "Maturity Date", "Report Run Date"]
    )
    df = df.drop(
        [
            "Reporting Account Number",
            "Reporting Account Name",
            "Source Account Name",
            "Xref Security ID",
            "Country Name",
            "Country Code",
            "Local Currency Name",
            "Acct Base Currency Name",
            "Acct Base Currency Code",
            "CINS",
            "Issuer ID",
            "SEDOL",
            "Valoren",
            "Sicovam",
            "WPK",
            "Quick",
            "Underlying Sec ID",
            "Loan ID",
            "Manager",
            "Book Yield Value",
            "Counterparty",
            "Ticker with Exchange Code",
            "Ticker with Yellow Key",
            "Accounting Status",
            "Primary GSP Account",
            "Extended GSP Account Number",
            "Percent Of Total",
        ],
        axis=1,
    )
    if "Acctg Status Update (EDT)" in df:
        del df["Acctg Status Update (EDT)"]
    elif "Acctg Status Update (EST)" in df:
        del df["Acctg Status Update (EST)"]
    df["Source Account Number"] = df["Source Account Number"].str[-4:].astype("int")
    df.columns = df.columns.str.replace(" ", "_").str.lower()
    df = df.rename(
        columns={
            "shares/par": "current_notional",
            "local_unrealized_gain/loss": "local_unrealized_pnl",
            "base_unrealized_gain/loss": "base_unrealized_pnl",
        }
    )
    for col in [
        "current_notional",
        "local_price",
        "base_price",
        "local_cost",
        "base_cost",
        "local_market_value",
        "base_market_value",
        "local_unrealized_pnl",
        "base_unrealized_pnl",
        "local_notional_cost",
        "base_notional_cost",
        "local_notional_value",
        "base_notional_value",
    ]:
        if df[col].dtype != "float64":
            df[col] = df[col].apply(lambda s: "-" + s[1:-1] if s.startswith("(") else s)
            df[col] = pd.to_numeric(df[col].str.replace(",", ""))
    df["row"] = df.index
    clear_date(date, conn)
    df.to_sql("bowdst_val", dawn_engine, if_exists="append", index=False)


def difference(df):
    if ("db_mv" in df.columns) and ("db_notional" in df.columns):
        df["mv_difference"] = df["db_mv"] - df["admin_mv"]
        df["notional_difference"] = df["db_notional"] - df["admin_notional"]
    elif "db_mv" in df.columns:
        df["mv_difference"] = df["db_mv"] - df["admin_mv"]
    elif "db_notional" in df.columns:
        df["notional_difference"] = df["db_notional"] - df["admin_notional"]
    return df


def sums(df):
    if ("db_mv" in df.columns) and ("db_notional" in df.columns):
        return df[["db_mv", "admin_mv", "db_notional", "admin_notional"]].sum()
    elif "db_mv" in df.columns:
        return df[["db_mv", "admin_mv"]].sum()
    elif "db_notional" in df.columns:
        return df[["db_notional", "admin_notional"]].sum()


def recon(date, conn):
    df = pd.read_sql_query(
        "SELECT * FROM bowdst_val where as_of_date=%s", conn, params=(date,)
    )
    bowd_bond_trades = df[df["cusip"].notnull()]
    bond_asset_classes = ["Subprime", "CRT", "CLO"]
    bond_trades_combined = []
    for asset in bond_asset_classes:
        db_bond_trades = pd.read_sql_query(
            f"select * from risk_positions(%s, %s, 'BOWDST')",
            conn,
            params=(date, asset),
        )
        bond_trades = bowd_bond_trades.merge(
            db_bond_trades,
            left_on="mellon_security_id",
            right_on="identifier",
            how="right",
        )
        bond_trades = bond_trades[
            [
                "description",
                "identifier",
                "notional",
                "factor",
                "current_notional",
                "base_market_value",
                "usd_market_value",
                "identifier",
            ]
        ]
        bond_trades["db_notional"] = bond_trades["notional"] * bond_trades["factor"]
        bond_trades.rename(
            columns={
                "usd_market_value": "db_mv",
                "current_notional": "admin_notional",
                "base_market_value": "admin_mv",
            },
            inplace=True,
        )
        bond_trades_combined.append(bond_trades)
    bond_trades_combined = pd.concat(bond_trades_combined)

    tranche_trades = pd.read_sql_query(
        f"select security_desc, maturity, orig_attach, orig_detach, sum(notional * tranche_factor) as db_notional, sum(admin_notional) as admin_notional, sum(serenitas_clean_nav) as db_mv, sum(admin_clean_nav) as admin_mv from tranche_risk_bowdst where date=%s group by security_desc, maturity, orig_attach, orig_detach ;",
        conn,
        params=(date,),
    )

    cdx_trades = pd.read_sql_query(
        f"select security_id, security_desc, index, series, version, maturity, globeop_notional as admin_notional, notional * factor as db_notional, clean_nav as db_mv, globeop_nav as admin_mv from list_cds_marks_legacy(%s, null, 'BOWDST')",
        conn,
        params=(date,),
    )

    cdx_swaption_trades = pd.read_sql_query(
        f"select security_id, option_type, strike, expiration_date, sum(serenitas_nav) as db_mv, sum(globeop_nav) as admin_mv from list_swaption_positions_and_risks(%s, 'BOWDST') group by security_id, option_type, strike, expiration_date;",
        conn,
        params=(date,),
    )

    ir_swaption_trades = pd.read_sql_query(
        "SELECT deal_id, option_type, strike, SECURITY_Id, expiration_date, notional AS db_notional, current_notional AS admin_notional, nav AS db_mv, base_market_value AS admin_mv FROM list_ir_swaption_positions(%s, 'BOWDST') LEFT JOIN bowdst_val ON deal_id=link_ref WHERE as_of_date=%s;",
        conn,
        params=(prev_business_day(date), date),
    )

    kinds = [
        bond_trades_combined,
        tranche_trades,
        cdx_trades,
        cdx_swaption_trades,
        ir_swaption_trades,
    ]
    names = [
        "bond_trades",
        "tranche_trades",
        "cdx_trades",
        "cdx_swaption_trades",
        "ir_swaption_trades",
    ]
    overview = []
    em = ExchangeMessage()
    attachments = []
    for kind, name in zip(kinds, names):
        buf = StringIO()
        difference(kind.round(decimals=0).fillna(0)).to_csv(buf)
        attachments.append(
            FileAttachment(name=f"{name}_{date}.csv", content=buf.getvalue().encode())
        )
        pd.set_option("display.float_format", lambda x: "%.2f" % x)
        df = pd.DataFrame(sums(kind), columns=["sums"])
        df["name"] = name
        df.set_index("name")
        overview.append(df)
    buf = StringIO()
    pd.concat(overview).round(decimals=0).to_csv(buf)
    attachments.append(
        FileAttachment(name=f"overview.csv", content=buf.getvalue().encode())
    )
    em.send_email(
        subject=f"Notional Totals {date}",
        body="See attached",
        to_recipients=("fyu@lmcg.com",),
        attach=attachments,
    )


def download_reports(date: datetime.date):
    target_directory = get_dir(date)

    if not target_directory.exists():
        target_directory.mkdir()

    em = ExchangeMessage()

    for msg in em.get_msgs(path=["Month End Recon", "BNY"]):
        # We are getting reports the month after
        from dateutil import relativedelta

        if (
            msg.datetime_received.month
            == (date + relativedelta.relativedelta(months=1)).month
        ):
            for attachment in msg.attachments:
                (target_directory / attachment.name).write_bytes(attachment.content)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("end_date", type=datetime.date.fromisoformat)
    args = parser.parse_args()
    # download_reports(args.end_date)
    dawndb = dbconn("dawndb")
    # load_val_report(args.end_date, dawndb)
    recon(args.end_date, dawndb)