diff options
Diffstat (limited to 'python/risk/tranches.py')
| -rw-r--r-- | python/risk/tranches.py | 83 |
1 files changed, 59 insertions, 24 deletions
diff --git a/python/risk/tranches.py b/python/risk/tranches.py index 134baa24..01c08951 100644 --- a/python/risk/tranches.py +++ b/python/risk/tranches.py @@ -1,3 +1,4 @@ +from pyisda.date import cds_accrued from serenitas.analytics.api import Portfolio, DualCorrTranche from serenitas.analytics.dates import prev_business_day from serenitas.analytics.utils import get_fx @@ -55,37 +56,63 @@ def insert_tranche_pnl_explain(portf, conn): with conn.cursor(binary=True) as c: c.execute("SELECT * FROM tranche_risk WHERE date=%s", (prev_day,)) prev_day_risk = {rec.tranche_id: rec for rec in c} - c.execute("SELECT id, upfront FROM cds WHERE trade_date=%s", (value_date,)) + c.execute( + "SELECT cds.id, cds.upfront, cds_delta.upfront AS delta_upfront, " + "cds_delta.notional * (CASE WHEN cds_delta.protection='Buyer' THEN -1.0 ELSE 1.0 END) AS notional, " + "cds.currency::text FROM cds " + " LEFT JOIN cds AS cds_delta ON cds_delta.id=cds.delta_id " + "WHERE cds.trade_date=%s", + (value_date,), + ) daily_trades = {rec.id: rec for rec in c} c.execute( - "SELECT dealid, termination_amount, termination_fee " - "FROM terminations WHERE deal_type='CDS' AND termination_date=%s", + "SELECT terminations.dealid, termination_amount, termination_fee, terminations.currency::text, " + "cds.notional * delta_alloc * (CASE WHEN cds.protection='Buyer' THEN -1.0 ELSE 1.0 END) AS notional, " + "cds.upfront * delta_alloc AS delta_upfront " + "FROM terminations LEFT JOIN cds ON cds.id=terminations.delta_id " + "WHERE deal_type='CDS' AND termination_date=%s", (value_date,), ) terminations = {int(rec.dealid.removeprefix("SCCDS")): rec for rec in c} - today_trades = {trade_id: trade for (strat, trade_id), trade in portf.items()} - all_ids = today_trades.keys() | prev_day_risk.keys() + current_trades = {trade_id: trade for (strat, trade_id), trade in portf.items()} + all_ids = current_trades.keys() | prev_day_risk.keys() to_insert = [] for trade_id in all_ids: pnl = 0 + fx_pnl = 0 if trade_id in daily_trades: - pnl += daily_trades[trade_id].upfront * get_fx( - value_date, trade.currency - ) + trade = daily_trades[trade_id] + pnl = trade.upfront * get_fx(value_date, trade.currency) if trade_id in terminations: - pnl += terminations[trade_id].termination_fee * get_fx( - value_date, trade.currency + term = terminations[trade_id] + pnl += term.termination_fee * get_fx(value_date, term.currency) + fx_pnl += term.termination_fee * ( + get_fx(value_date, term.currency) - get_fx(prev_day, term.currency) ) - if trade_id not in today_trades: + if trade_id not in current_trades: previous_risk = prev_day_risk[trade_id] pnl = pnl - (previous_risk.clean_nav + previous_risk.accrued) + dirty_index_pv = ( + 1 + - previous_risk.index_refprice * 0.01 + - cds_accrued(prev_day, previous_risk.running * 1e-4) + ) + if ( + term.delta_upfront + ): # if None means either no delta or we didn't populate + delta_pnl = ( + term.delta_upfront + - term.notional * dirty_index_pv * previous_risk.index_factor + ) + else: + delta_pnl = 0.0 else: - trade = today_trades[trade_id] + trade = current_trades[trade_id] if trade_id in prev_day_risk: previous_risk = prev_day_risk[trade_id] - pnl = trade.pv * get_fx(value_date, trade.currency) - ( + pnl += trade.pv * get_fx(value_date, trade.currency) - ( previous_risk.clean_nav + previous_risk.accrued ) @@ -99,21 +126,31 @@ def insert_tranche_pnl_explain(portf, conn): * previous_risk.notional * ( float(trade._index.pv()) + * get_fx(value_date, trade.currency) - (1 - previous_risk.index_refprice * 0.01) + * get_fx(prev_day, trade.currency) ) - * get_fx(prev_day, trade.currency) ) else: - fx_pnl = trade.pv * ( - get_fx(value_date, trade.currency) - - get_fx(prev_day, trade.currency) - ) - delta_pnl = 0.0 - to_insert.append((value_date, trade_id, pnl, fx_pnl, delta_pnl)) + fx_pnl = 0.0 + day_trade = daily_trades[trade_id] + dirty_index_pv = float(trade._index.pv() - trade._index.accrued()) + if day_trade.notional: + delta_pnl = ( + day_trade.notional * dirty_index_pv * trade._index.factor + - day_trade.delta_upfront + ) + else: # if None means either no delta or we didn't populate + delta_pnl = 0 + pnl += trade.pv * get_fx(value_date, trade.currency) + unexplained = pnl - delta_pnl - fx_pnl + to_insert.append( + (value_date, trade_id, pnl, fx_pnl, delta_pnl, unexplained) + ) c.executemany( - "INSERT INTO tranche_pnl_explain(date, tranche_id, pnl, fx_pnl, delta_pnl) " - "VALUES (%s, %s, %s, %s, %s)", + "INSERT INTO tranche_pnl_explain(date, tranche_id, pnl, fx_pnl, delta_pnl, unexplained) " + "VALUES (%s, %s, %s, %s, %s, %s)", to_insert, ) conn.commit() @@ -151,8 +188,6 @@ def insert_tranche_risk(portf, conn): " ON CONFLICT (date, tranche_id) DO UPDATE " f"SET {update_str}" ) - value_date = portf.value_date - prev_day = prev_business_day(value_date) with conn.cursor(binary=True) as c: for (strat, trade_id), trade in portf.items(): logger.info(f"marking tranche {trade_id} in {strat}") |
