diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/reto.py | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/python/reto.py b/python/reto.py index 2e2b9d7d..ca1a3cea 100644 --- a/python/reto.py +++ b/python/reto.py @@ -26,14 +26,15 @@ def parse_args(): return parser.parse_args() -def vol_surface(portf, try_days_back): - for source in ("BAML", "GS", "MS", "JPM"): - try: - vol_surface = generate_vol_surface(portf, try_days_back, source) - except IndexError: - pass - else: - return vol_surface +def build_vol_surface(portf): + for i in range(1, 6): + for source in ("BAML", "GS", "MS", "JPM"): + try: + vol_surface = generate_vol_surface(portf, i, source) + except IndexError: + pass + else: + return vol_surface def gen_spreads(shock_date, fund): @@ -51,7 +52,7 @@ def gen_spreads(shock_date, fund): ], ) portf, _ = build_portfolio(shock_date, shock_date, fund) - vol_surface = generate_vol_surface(portf, 10) + vol_surface = build_vol_surface(portf) portf.reset_pv() scens = run_portfolio_scenarios( portf, @@ -104,20 +105,19 @@ def gen_spreads(shock_date, fund): for key, item in strategies.items(): exist_columns = list(set(temp.columns).intersection(item)) temp[key] = temp[exist_columns].sum(axis=1) - temp.drop(exist_columns, axis=1, inplace=True) + temp = temp.drop(exist_columns, axis=1) temp["total"] = temp.sum(axis=1) results[i] = temp results = pd.concat(results) - results.index.set_levels( + results.index = results.index.set_levels( results.index.levels[results.index.names.index("spread_shock")] * Trade._ontr["HY"].spread, level="spread_shock", - inplace=True, ) return results -def process_dataframe(raw_df): +def process_shocks_dataframe(raw_df): """Clean and transform the input dataframe to insert into database.""" transformed_df = raw_df.reset_index() transformed_df = transformed_df.rename(columns={"level_0": "unit"}) @@ -134,20 +134,24 @@ def process_dataframe(raw_df): return transformed_df +def save_shocks(date, fund): + results = gen_spreads(args.date, fund) + with conn.cursor() as c: + c.execute( + "DELETE FROM shocks WHERE fund=%s AND date=%s", + ( + fund, + args.date, + ), + ) + conn.commit() + df = process_shocks_dataframe(results) + df["fund"] = fund + df.to_sql("shocks", dawn_engine, if_exists="append", index=False) + + if __name__ == "__main__": args = parse_args() conn = dbconn("dawndb") for fund in ("SERCGMAST", "BOWDST", "ISOSEL", "BRINKER"): - results = gen_spreads(args.date, fund) - with conn.cursor() as c: - c.execute( - "DELETE FROM shocks WHERE fund=%s AND date=%s", - ( - fund, - args.date, - ), - ) - conn.commit() - df = process_dataframe(results) - df["fund"] = fund - df.to_sql("shocks", dawn_engine, if_exists="append", index=False) + save_shocks(args.date, fund) |
