diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/collateral/gs_fcm.py | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/python/collateral/gs_fcm.py b/python/collateral/gs_fcm.py index 87ab09a2..0ea49917 100644 --- a/python/collateral/gs_fcm.py +++ b/python/collateral/gs_fcm.py @@ -1,15 +1,26 @@ +import datetime import pandas as pd from . import DAILY_DIR, SftpClient -from .common import compare_notionals, STRATEGY_CASH_MAPPING +from .common import compare_notionals, STRATEGY_CASH_MAPPING, next_business_day -def collateral(d, positions, *, engine, **kwargs): - df = pd.read_csv( - DAILY_DIR - / "BowdSt" - / "GS_fcm_reports" - / f"Open _Trades_Report_LMCG_51341_{d:%Y%m%d}.csv", +def download_files(*args, **kwargs): + sftp = SftpClient.from_creds("gs") + sftp.download_files("outgoing", DAILY_DIR / "BowdSt" / "GS_fcm_reports") + + +def load_file(d: datetime.date): + try: + fname = next( + (DAILY_DIR / "BowdSt" / "GS_fcm_reports").glob( + f"GS-OTCSDIReporting-Open_Trades_Redcode-*-{d:%Y%m%d}-*.csv" + ) + ) + except StopIteration: + raise FileNotFoundError("GS fcm file not found for date {d}") + return pd.read_csv( + fname, usecols=[ "Notional", "Direction", @@ -22,8 +33,13 @@ def collateral(d, positions, *, engine, **kwargs): index_col=["Red Code", "Maturity Date"], thousands=",", ) + + +def collateral(d: datetime.date, positions, *, engine, **kwargs): + df = load_file(next_business_day(d)) df.Notional = df.Notional.where(df.Direction == "Buy", -df.Notional) df.index.names = ["security_id", "maturity"] + cob_date = df["COB Date"][0] df = df.groupby(level=["security_id", "maturity"])[ ["Notional", "NPV (local)"] ].sum() @@ -53,7 +69,7 @@ def collateral(d, positions, *, engine, **kwargs): parse_dates=["COB Date"], thousands=",", ) - + df["date"] = cob_date df_margin = df_margin.rename( columns={ "Opening Balance (local)": "beginning_balance", @@ -83,5 +99,4 @@ def collateral(d, positions, *, engine, **kwargs): f"INSERT INTO fcm_moneyline({','.join(cols)}) VALUES({place_holders})", list(df_margin[cols].itertuples(index=False)), ) - df["date"] = d return df.set_index("Strategy") |
