diff options
Diffstat (limited to 'python/pnl_explain.py')
| -rw-r--r-- | python/pnl_explain.py | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/python/pnl_explain.py b/python/pnl_explain.py index 7103e36b..45704538 100644 --- a/python/pnl_explain.py +++ b/python/pnl_explain.py @@ -7,7 +7,7 @@ from psycopg2.extensions import connection from risk.swaptions import get_swaption_portfolio from risk.indices import get_index_portfolio from risk.tranches import get_tranche_portfolio -from pyisda.date import previous_twentieth +from pyisda.date import previous_twentieth, cds_accrued from typing import Tuple, Union @@ -28,7 +28,7 @@ def get_index_pv( accrued = 0.0 for t in portf.trades: _, amount = t._fee_leg.cashflows[0] - amount *= get_fx(d, t.currency) + amount *= get_fx(prev_day, t.currency) accrued -= amount * t.notional * t.factor * t.fixed_rate * 1e-4 else: accrued = 0.0 @@ -45,7 +45,7 @@ def get_index_pv( conn.reset() raise e for (fee, curr) in c: - nav += fee * get_fx(d, curr) + nav += fee * get_fx(prev_day, curr) daily.append(nav + accrued) pvs.append(portf.pv) dates.append(prev_day) @@ -100,31 +100,44 @@ def get_tranche_pv( dates = [] for d in dr: prev_day = (d - bus_day).date() + if previous_twentieth(d, roll=True) == d.date(): + amount = cds_accrued(prev_day, 1.0, True) + for t in portf.trades: + accrued -= ( + amount + * get_fx(prev_day, t._index.currency) + * t.notional + * t.tranche_factor + * t.tranche_running + * 1e-4 + ) + else: + accrued = 0.0 portf = get_tranche_portfolio(prev_day, conn, **kwargs) nav = 0.0 # add terminations with conn.cursor() as c: c.execute( - "SELECT termination_fee " + "SELECT termination_fee, currency " "FROM terminations JOIN cds USING (dealid) " "WHERE termination_date=%s AND dealid LIKE 'SCCDS%%' ", (prev_day,), ) - for (fee,) in c: - nav += fee + for (fee, currency) in c: + nav += fee * get_fx(prev_day, currency) # add new trades with conn.cursor() as c: c.execute( - "SELECT upfront " + "SELECT upfront, currency " "FROM cds WHERE trade_date=%s AND swap_type='CD_INDEX_TRANCHE' " "AND fund='SERCGMAST'", (prev_day,), ) - for (fee,) in c: - nav += fee + for (fee, currency) in c: + nav += fee * get_fx(prev_day, currency) dates.append(prev_day) pv.append(portf.pv) - daily.append(nav) + daily.append(nav + accrued) df = pd.DataFrame({"pv": pv, "daily": daily}, index=pd.to_datetime(dates)) return df |
