diff options
| -rw-r--r-- | python/collateral/__main__.py | 81 | ||||
| -rw-r--r-- | python/collateral/baml_fcm.py | 2 | ||||
| -rw-r--r-- | python/collateral/bnp.py | 7 | ||||
| -rw-r--r-- | python/collateral/common.py | 14 | ||||
| -rw-r--r-- | sql/dawn.sql | 2 |
5 files changed, 67 insertions, 39 deletions
diff --git a/python/collateral/__main__.py b/python/collateral/__main__.py index 6f82ed9f..5dc74661 100644 --- a/python/collateral/__main__.py +++ b/python/collateral/__main__.py @@ -5,7 +5,7 @@ from importlib import import_module from utils import SerenitasFileHandler from utils.db import dawn_engine, dbconn -from .common import get_dawn_trades, send_email +from .common import get_bilateral_trades, send_email from pandas.tseries.offsets import BDay import argparse @@ -39,44 +39,69 @@ if args.download: for fund in ("Serenitas", "Brinker", "BowdSt"): cp_mod.download_files(em, fund=fund) -dawn_trades = get_dawn_trades(args.workdate, dawn_engine) - -df = {} -mapping = {"baml_fcm": "BAML", "wells": "WF"} args.workdate -= BDay() -for cp in counterparties: + +cp_dict = { + "Serenitas": { + "fcms": ("baml_fcm", "wells"), + "isda_cps": ("citi", "baml_isda", "ms", "gs", "bnp", "cs"), + }, + "Brinker": {"fcms": (), "isda_cps": ("ms", "gs")}, + "BowdSt": {"fcms": (), "isda_cps": ("ms", "bnp")}, +} + + +def run_collateral(cp, fund, positions, workdate, engine): cp_mod = import_module("." + cp, "collateral") - if cp in ["baml_fcm", "wells"]: + lookback = 0 + while lookback < 2: + try: + return cp_mod.collateral( + workdate - BDay(lookback), positions, engine=engine, fund=fund + ) + except FileNotFoundError as e: + logger.info(e) + lookback += 1 + except ValueError as e: + logger.error(e) + break + else: + break + + +df = {} +fcm_mapping = {"baml_fcm": "BAML", "wells": "WF"} +fund_mapping = {"Serenitas": "SERCGMAST", "Brinker": "BRINKER", "BowdSt": "BOWDST"} + +for fund in ("Serenitas", "Brinker", "BowdSt"): + bilat_positions = get_bilateral_trades( + args.workdate, fund_mapping[fund], dawn_engine + ) + for fcm in cp_dict[fund]["fcms"]: positions = pd.read_sql_query( "SELECT security_id, security_desc, maturity, " "folder, notional, currency " "FROM list_cds_positions_by_strat_fcm(%s, %s)", dawn_engine, - params=(args.workdate.date(), mapping[cp]), + params=(args.workdate.date(), fcm_mapping[fcm]), index_col=["security_id", "maturity"], ) - else: - positions = dawn_trades - for fund in ("Serenitas", "Brinker", "BowdSt"): - lookback = 0 - while lookback < 2: - try: - df[(fund, cp.upper())] = cp_mod.collateral( - args.workdate - BDay(lookback), positions, engine=dawn_engine, - fund=fund - ) - except FileNotFoundError as e: - logger.info(e) - lookback += 1 - except ValueError as e: - logger.error(e) - break - else: - break + df[(fund, fcm.upper())] = run_collateral( + fcm, fund, positions, args.workdate, dawn_engine + ) + for cp in cp_dict[fund]["isda_cps"]: + df[(fund, cp.upper())] = run_collateral( + cp, fund, bilat_positions, args.workdate, dawn_engine + ) + df = pd.concat(df, names=["fund", "broker", "strategy"]).reset_index() df.strategy = df.strategy.str.replace("^(M_|SER_)?", "", 1) -df = df[["date", "broker", "strategy", "Amount", "Currency"]] +df["fund"] = df.fund.map( + {"Serenitas": "SERCGMAST", "Brinker": "BRINKER", "BowdSt": "BOWDST"} +) + +df = df[["date", "broker", "strategy", "Amount", "Currency", "fund"]] conn = dbconn("dawndb") sql_str = ( "INSERT INTO strategy_im VALUES(%s, %s, %s, %s, %s, %s) " @@ -90,4 +115,4 @@ conn.commit() conn.close() if args.send_email: - send_email(args.workdate, df) + send_email(args.workdate, df[df.fund == "Serenitas"].drop("fund", axis=1)) diff --git a/python/collateral/baml_fcm.py b/python/collateral/baml_fcm.py index 1f6150b2..ff80f480 100644 --- a/python/collateral/baml_fcm.py +++ b/python/collateral/baml_fcm.py @@ -9,7 +9,7 @@ def download_files(*args, **kwargs): sftp.download_files("outgoing", DAILY_DIR / "BAML_reports") -def collateral(d, positions, *, engine): +def collateral(d, positions, *, engine, **kwargs): df = pd.read_csv( DAILY_DIR / "BAML_reports" diff --git a/python/collateral/bnp.py b/python/collateral/bnp.py index a89dc19c..39f7e72a 100644 --- a/python/collateral/bnp.py +++ b/python/collateral/bnp.py @@ -23,9 +23,12 @@ def download_files(em, count: int = 20, *, fund="Serenitas", **kwargs): def load_file(d: datetime.date, report_type: str, fund: str): + fund_mapping = { + "Serenitas": "SERENITAS CREDIT GAMMA MASTER FUND, LP", + "BowdSt": "BOSTON PATRIOT BOWDOIN ST LLC", + } fname = ( - f"{report_type} - BNP PARIBAS - SERENITAS CREDIT GAMMA " - f"MASTER FUND, LP - COB {d:%Y%m%d}.XLS" + f"{report_type} - BNP PARIBAS - {fund_mapping[fund]} " f"- COB {d:%Y%m%d}.XLS" ) return pd.read_excel(DAILY_DIR / fund / "BNP_reports" / fname, skiprows=7) diff --git a/python/collateral/common.py b/python/collateral/common.py index 3d07796b..6a7afdfe 100644 --- a/python/collateral/common.py +++ b/python/collateral/common.py @@ -52,29 +52,29 @@ def compare_notionals(df: pd.DataFrame, positions: pd.DataFrame, fcm: str) -> No ) -def get_dawn_trades(d: datetime.date, engine: Engine) -> pd.DataFrame: +def get_bilateral_trades(d: datetime.date, fund: str, engine: Engine) -> pd.DataFrame: df_cds = pd.read_sql_query( "SELECT cpty_id, folder, initial_margin_percentage * abs(notional) / 100 as IA " - "FROM list_cds(%s::date) " + "FROM list_cds(%s::date, %s) " "WHERE cpty_id IS NOT NULL", engine, - params=(d,), + params=(d, fund), ) df_swaptions = pd.read_sql_query( "SELECT cpty_id, folder, initial_margin_percentage * notional / 100 AS IA " "FROM swaptions " "WHERE cpty_id IS NOT NULL " - "AND trade_date <= %s", + "AND trade_date <= %s AND fund=%s", engine, - params=(d,), + params=(d, fund), ) df_caps = pd.read_sql_query( "SELECT cpty_id, folder, initial_margin_percentage * amount / 100 AS IA " "FROM capfloors " "WHERE cpty_id IS NOT NULL " - "AND trade_date <= %s", + "AND trade_date <= %s AND fund=%s", engine, - params=(d,), + params=(d, fund), ) df = pd.concat([df_cds, df_swaptions, df_caps]) df = df.replace({"folder": STRATEGY_CASH_MAPPING}) diff --git a/sql/dawn.sql b/sql/dawn.sql index 40e75e36..c6f45400 100644 --- a/sql/dawn.sql +++ b/sql/dawn.sql @@ -1627,7 +1627,7 @@ CREATE TYPE strategy AS ENUM( -- OPTIONS portfolio 'IGOPTDEL', 'IGPAYER', 'IGREC', 'HYOPTDEL', 'HYPAYER', 'HYREC', 'IGCDSCSH', 'HYCDSCSH', 'COCSH' -- IR portfolio -'IR', 'IRDEVCSH', 'STEEP', 'FLAT', 'DELTAONE', +'IR', 'IRDEVCSH', 'STEEP', 'FLAT', 'DELTAONE', 'DV01', -- STRUCTURED portfolio 'STR_MAV', 'STR_MEZZ', 'HEDGE_CSO', 'CSO_TRANCH', 'CSOCDSCSH', -- MORTGAGES portfolio |
