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
|
import datetime
import pandas as pd
from . import DAILY_DIR
from .common import load_pdf, next_business_day
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",
}
for collat_type in ("RVM", "IM"):
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 += float(value.strip().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=29,
)
df.columns = [c.replace("\n", " ").strip() for c in df.columns]
df = df[1:]
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 = get_collateral(next_business_day(d), fund)
df = load_cs_positions(d, fund)
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 = df.append(
{
"Strategy": "M_CSH_CASH",
"Amount": -collateral - df.Amount.sum(),
"Currency": "USD",
},
ignore_index=True,
)
df["date"] = d
return df.set_index("Strategy")
|