aboutsummaryrefslogtreecommitdiffstats
path: root/python/collateral
diff options
context:
space:
mode:
Diffstat (limited to 'python/collateral')
-rw-r--r--python/collateral/gs.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/python/collateral/gs.py b/python/collateral/gs.py
new file mode 100644
index 00000000..97c12b4b
--- /dev/null
+++ b/python/collateral/gs.py
@@ -0,0 +1,57 @@
+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"], count=count, subject__contains="Margin"
+ )
+ DATA_DIR = DAILY_DIR / "GS_reports"
+ for msg in emails:
+ for attach in msg.attachments:
+ fname = attach.name
+ if fname.endswith("xls"):
+ p = DATA_DIR / fname
+ if not p.exists():
+ p.write_bytes(attach.content)
+
+
+def load_file(d, pattern):
+ try:
+ fname = next(
+ (DAILY_DIR / "GS_reports").glob(f"{pattern}*{d.strftime('%d_%b_%Y')}*")
+ )
+ except StopIteration:
+ raise FileNotFoundError(f"GS {pattern} file not found for date {d}")
+ return pd.read_excel(fname, skiprows=9, skipfooter=77)
+
+
+def collateral(d, dawn_trades, *args):
+ df = load_file(d, "Collateral_Detail")
+ collateral = float(df.Quantity)
+ df = load_file(d, "Trade_Detail")
+ df = df[["Trade Id", "Transaction Type", "NPV (USD)", "Initial Margin Required"]]
+ 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[["folder", "NPV (USD)", "Initial Margin Required"]]
+ df = df.groupby("folder").sum()
+ df = df.sum(axis=1).to_frame(name="Amount")
+ df["Currency"] = "USD"
+ df = df.reset_index()
+ df.columns = ["Strategy", "Amount", "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")