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", } 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, ) if df.empty: raise ValueError(f"empty position statement for {d}") df.columns = [c.replace("\n", " ").strip() for c in df.columns] df = df[1:] 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 = get_collateral(next_business_day(d), fund) 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 = df.append( { "Strategy": "M_CSH_CASH", "Amount": -collateral - df.Amount.sum(), "Currency": "USD", }, ignore_index=True, ) df["date"] = d return df.set_index("Strategy")