aboutsummaryrefslogtreecommitdiffstats
path: root/python/collateral/ms.py
blob: c8bdbc8a2af2b655eec067a97ca80778c622459a (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
import pandas as pd
from . import DAILY_DIR


def download_files(count=20):
    from exchange import ExchangeMessage

    em = ExchangeMessage()
    emails = em.get_msgs(
        path=["NYops", "Margin calls MS"],
        count=count,
        subject__contains="SERCX **Daily",
    )
    DATA_DIR = DAILY_DIR / "MS_reports"
    for msg in emails:
        for attach in msg.attachments:
            if "NETSwaps" in attach.name:
                fname = "Trade_Detail_" + attach.name.split("_")[1]
            elif "NET_Collateral" in attach.name:
                fname = "Collateral_Detail_" + attach.name.rsplit("_", 1)[1]
            else:
                continue
            p = DATA_DIR / fname
            if not p.exists():
                p.write_bytes(attach.content)


def collateral(d, dawn_trades, *args):
    df = pd.read_excel(DAILY_DIR / "MS_reports" / f"Collateral_Detail_{d:%Y%m%d}.xls")
    collat = df.loc[1, "coll_val_ccy"].replace(",", "")
    if "(" in collat:
        collat = collat[1:-1]
        collat = -float(collat)
    else:
        collat = float(collat)
    df = pd.read_excel(DAILY_DIR / "MS_reports" / f"Trade_Detail_{d:%Y%m%d}.xls")
    df = df.dropna(subset=["trade_ccy"])
    df = df.merge(dawn_trades, how="left", left_on="trade_id", right_on="cpty_id")
    missing_ids = df.loc[df.cpty_id.isnull(), "trade_id"]
    if not missing_ids.empty:
        raise ValueError(f"{missing_ids.tolist()} not in the database")
    df = df.groupby("folder")[["collat_req_in_agr_ccy"]].sum()
    df["Currency"] = "USD"
    df = df.reset_index()
    df.columns = ["Strategy", "Amount", "Currency"]
    df = df.append(
        {
            "Strategy": "M_CSH_CASH",
            "Amount": -collat - df.Amount.sum(),
            "Currency": "USD",
        },
        ignore_index=True,
    )
    df["date"] = d
    return df.set_index("Strategy")