diff options
Diffstat (limited to 'python/risk/ir_swap.py')
| -rw-r--r-- | python/risk/ir_swap.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/python/risk/ir_swap.py b/python/risk/ir_swap.py new file mode 100644 index 00000000..72691fb7 --- /dev/null +++ b/python/risk/ir_swap.py @@ -0,0 +1,45 @@ +import logging +from psycopg import sql + +logger = logging.getLogger(__name__) + + +def insert_ir_swap_portfolio(portf, conn): + cols = [ + "date", + "swp_id", + "notional", + "pv", + "DV01", + "IRGamma1bp", + ] + sql_str = sql.SQL( + "INSERT INTO ir_swap_risk({columns}) " + "VALUES({placeholders}) " + " ON CONFLICT (date, swp_id) DO UPDATE " + "SET {update_str}" + ).format( + columns=sql.SQL(",").join([sql.Identifier(c) for c in cols]), + placeholders=sql.SQL(",").join([sql.Placeholder()] * len(cols)), + update_str=sql.SQL(",").join( + [ + sql.SQL("{c} = EXCLUDED.{c}").format(c=sql.Identifier(c)) + for c in cols[2:] + ] + ), + ) + with conn.cursor() as c: + for trade_id, trade in portf.items(): + logger.info(f"marking IR swap {trade_id}") + c.execute( + sql_str, + ( + trade.value_date, + trade_id.split("_")[1], + trade.notional, + trade.pv, + trade.DV01, + trade.gamma, + ), + ) + conn.commit() |
