import pandas as pd from . import DAILY_DIR from pandas.tseries.offsets import BDay from .common import load_pdf def download_files(em, count=20, **kwargs): DATA_DIR = DAILY_DIR / "CS_reports" emails = em.get_msgs( path=["NYops", "Margin Calls CS"], 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=["NYops", "Margin Calls CS"], 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): DATA_DIR = DAILY_DIR / "CS_reports" collat = 0 for collat_type in ("RVM", "IM"): pdf_file = ( DATA_DIR / f"CollateralCptyStatement161SerenitasCGMF{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 collateral(d, dawn_trades, **kwargs): collateral = get_collateral(d + BDay()) df = pd.read_excel( f"/home/serenitas/Daily/CS_reports/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.merge(dawn_trades, how="left", left_on="Order No", right_on="cpty_id") missing_ids = df.loc[df.cpty_id.isnull(), "Order No"] 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["Mid Price"] df = df[["folder", "Amount", "Notional Currency"]] df = df.groupby(["folder", "Notional Currency"], as_index=False).sum() df = df.rename(columns={"folder": "Strategy", "Notional Currency": "Currency"}) 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")