aboutsummaryrefslogtreecommitdiffstats
path: root/python/collateral/cs.py
blob: c7a5cc86f484360f304fec9dc6044c1eb8350315 (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
import datetime
import logging
import pandas as pd
from . import DAILY_DIR
from .common import load_pdf, next_business_day

logger = logging.getLogger(__name__)

paths = {
    "Serenitas": ["NYops", "Margin Calls CS"],
    "BowdSt": ["BowdoinOps", "Margin CS"],
}


def download_files(em, count=20, *, fund="Serenitas", **kwargs):
    if fund not in paths:
        return
    DATA_DIR = DAILY_DIR / fund / "CS_reports"
    emails = em.get_msgs(path=paths[fund], count=count, subject__contains="DERV048829")
    for msg in emails:
        for attach in msg.attachments:
            fname = attach.name
            if fname.endswith("xlsx"):
                p = DATA_DIR / fname
                if not p.exists():
                    p.write_bytes(attach.content)

    emails = em.get_msgs(
        path=paths[fund],
        count=count,
        sender="americas.collateralmgt@credit-suisse.com",
    )

    for msg in emails:
        for attach in msg.attachments:
            fname = attach.name
            if "Serenitas CGMF" in fname:
                p = DATA_DIR / fname
                p = p.parent / f"{msg.datetime_sent:%Y-%m-%d} {p.stem}{p.suffix}"
                if not p.exists():
                    p.write_bytes(attach.content)
            else:
                p = DATA_DIR / fname
                if not p.exists():
                    p.write_bytes(attach.content)


def get_collateral(d: datetime.date, fund):
    DATA_DIR = DAILY_DIR / fund / "CS_reports"
    collat = 0
    full_name = {
        "Serenitas": "SerenitasCGMF",
        "BowdSt": "BostonBPStLLC",
    }
    collat = {}
    for collat_type in ("RVM", "IM", "FXIM"):
        pdf_file = (
            DATA_DIR
            / f"CollateralCptyStatement161{full_name[fund]}{collat_type}_{d:%m%d%Y}.pdf"
        )
        g = iter(load_pdf(pdf_file))
        for e in g:
            if e.text == "Cash  USD   (US Dollar)":
                next(g)
                value = next(g).text
                collat[collat_type] = float(
                    value.strip().replace(",", "").replace("(", "-").replace(")", "")
                )
                break
    return collat


def load_cs_positions(d: datetime.date, fund: str, legacy=False):
    if not legacy:
        d = next_business_day(d)
    DATA_DIR = DAILY_DIR / fund / "CS_reports"
    full_name = {
        "Serenitas": "SerenitasCGMF",
        "BowdSt": "BostonBPStLLC",
    }
    if legacy and fund == "Serenitas":
        df = pd.read_excel(
            DATA_DIR / f"DERV048829_{d:%b%d%Y}.xlsx",
            header=9,
            skipfooter=50,
        )

        df = df[["Order No", "Mid Price", "Notional Currency"]]
        df["Mid Price"] = (
            df["Mid Price"]
            .str.replace(",", "")
            .apply(lambda s: -float(s[1:-1]) if s.startswith("(") else float(s))
        )
        df["Order No"] = df["Order No"].astype("str")
        df = df.rename(
            columns={
                "Mid Price": "PV (USD)",
                "Notional Currency": "Currency",
                "Order No": "Structure ID",
            }
        )
    else:
        df = pd.read_excel(
            DATA_DIR / f"CollateralCptyStatement161{full_name[fund]}RVM_{d:%m%d%Y}.xls",
            header=5,
            skipfooter=28,
        )

        df.columns = [c.replace("\n", " ").strip() for c in df.columns]
        df = df[1:]
        if df.empty:
            raise ValueError(f"empty position statement for {d}")
        for i, e in df["Structure ID"].items():
            try:
                int(e)
            except ValueError:
                df = df[: (i - 1)]
                break
        df["Trade ID"] = df["Trade ID"].astype("int").astype("str")
        df = df.rename(columns={"Notional1 CCY": "Currency"})
    return df


def collateral(d, dawn_trades, *, fund="Serenitas", **kwargs):
    collateral = sum(get_collateral(next_business_day(d), fund).values())
    try:
        df = load_cs_positions(d, fund)
    except ValueError as e:
        logger.warning(e)
        return pd.DataFrame(
            {
                "Strategy": ["M_CSH_CASH"],
                "Amount": [-collateral],
                "Currency": ["USD"],
                "date": [d],
            }
        ).set_index("Strategy")
    df = df.merge(dawn_trades, how="left", left_on="Structure ID", right_on="cpty_id")
    missing_ids = df.loc[df.cpty_id.isnull(), "Structure ID"]
    if not missing_ids.empty:
        raise ValueError(f"{missing_ids.tolist()} not in the database")
    df.ia = df.ia.fillna(0.0)
    df["Amount"] = df.ia + df["PV (USD)"]
    df = df[["folder", "Amount", "Currency"]]
    df = df.groupby(["folder", "Currency"], as_index=False).sum()
    df = df.rename(columns={"folder": "Strategy"})
    df.Amount *= -1
    df.loc[len(df.index)] = ["M_CSH_CASH", "USD", -collateral - df.Amount.sum()]
    df["date"] = d
    return df.set_index("Strategy")