diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/collateral_calc.py | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/python/collateral_calc.py b/python/collateral_calc.py index a5cba4e3..139e89a0 100644 --- a/python/collateral_calc.py +++ b/python/collateral_calc.py @@ -1,4 +1,5 @@ import os +import logging import pandas as pd from db import dbengine @@ -11,7 +12,13 @@ from pandas.tseries.offsets import BDay DAILY_DIR = Path(os.environ["DAILY_DIR"]) -def download_files(d=None): + +def download_files(d=None, + report_types=["OTC_CASH_ACTIVITY", "OTC_POSITIONS", + "OTC_MARGIN"], + retry_count=0): + if retry_count > 20: + return DATA_DIR = DAILY_DIR / "SG_reports" with FTP('ftp.newedgegroup.com') as ftp: ftp.login('SerenitasGamma@USA', "SSqrrLL99") @@ -19,25 +26,24 @@ def download_files(d=None): if d is None: for f in ftp.nlst(): if f.endswith("csv"): - if ("OTC_CASH_ACTIVITY" in f) or \ - ("OTC_POSITIONS" in f) or \ - ("OTC_MARGIN" in f): - with open(DATA_DIR / f, "wb") as fh: - ftp.retrbinary('RETR ' + f, fh.write) + for report_type in report_types: + if f.endswith(f"{report_type}.csv"): + with open(DATA_DIR / f, "wb") as fh: + ftp.retrbinary('RETR ' + f, fh.write) else: - i = 0 - while i <= 20: - i +=1 - file_list = ftp.nlst() - for report_type in ["OTC_CASH_ACTIVITY", "OTC_POSITIONS", "OTC_MARGIN"]: - f = f"{d:%Y%m%d}_{report_type}.csv" - if f not in file_list: - sleep(500) - break + file_list = ftp.nlst() + for report_type in report_types: + f = f"{d:%Y%m%d}_{report_type}.csv" + if f not in file_list: + logging.info("File not here yet, trying again in 500s...") + logging.info(f"Try count: {retry_count}") + sleep(500) + ftp.close() + download_files(d, report_types, retry_count + 1) + else: with open(DATA_DIR / f, "wb") as fh: ftp.retrbinary('RETR ' + f, fh.write) - else: - break + def download_emails(): emails = get_msgs(path=["NYops"], subject_filter="SERCX **Daily") @@ -49,6 +55,7 @@ def download_emails(): with open(DATA_DIR / fname, "wb") as fh: fh.write(attach.content) + def sg_collateral(d): df_activity = pd.read_csv(DAILY_DIR / "SG_reports" / f"{d:%Y%m%d}_OTC_CASH_ACTIVITY.csv") df_position = pd.read_csv(DAILY_DIR / "SG_reports" / f"{d:%Y%m%d}_OTC_POSITIONS.csv") @@ -73,7 +80,7 @@ def sg_collateral(d): ref_entity.index_type[ref_entity.index_type == "EUROPE"] = "EU" df = df.join(ref_entity) df = df.groupby(['index_type', 'series', 'tenor'])['Collateral'].sum() - positions = pd.read_sql_query("SELECT security_desc, folder, notional, currency " \ + positions = pd.read_sql_query("SELECT security_desc, folder, notional, currency " "FROM list_cds_positions_by_strat(%s)", dbengine("dawndb"), params=(d.date(),)) instruments = positions.security_desc.str.split(expand=True)[[1, 3, 4]] @@ -101,6 +108,7 @@ def sg_collateral(d): 'SER_IGCURVE': 'SER_IGCVECSH'}) return df + def ms_collateral(d): df = pd.read_excel(DAILY_DIR / "MS_reports" / f"{d:%Y%m%d}.xls" ) d = {'TRCDX': 'IGTCDSCSH', @@ -114,14 +122,16 @@ def ms_collateral(d): r.append(["M_CSH_CASH", -6_810_000 - acc, "USD"]) return pd.DataFrame.from_records(r, columns=['Strategy', 'Amount', 'Currency']) + def send_email(account, df_ms, df_sg): pd.set_option('display.float_format', '{:.2f}'.format) - content = HTMLBody('<html><body>' \ - '<h3>At Morgan Stanley:</h3>' \ - '{}' \ + content = HTMLBody('<html><body>' + '<h3>At Morgan Stanley:</h3>' + '{}' '<h3>At Societe Generale:</h3>' - '{}' \ - '</body><html>'.format(df_ms.to_html(index=False), df_sg.to_html(index=False))) + '{}' + '</body><html>'.format(df_ms.to_html(index=False), + df_sg.to_html(index=False))) m = Message( account=account, folder=account.sent, @@ -132,6 +142,7 @@ def send_email(account, df_ms, df_sg): ) m.send_and_save() + if __name__ == "__main__": download_emails() d = (pd.Timestamp.today() - BDay()).normalize() |
