diff options
| -rw-r--r-- | python/pnl_explain.py | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/python/pnl_explain.py b/python/pnl_explain.py index 2bb3df82..83407f35 100644 --- a/python/pnl_explain.py +++ b/python/pnl_explain.py @@ -248,7 +248,8 @@ def get_tranche_pv2( ) df_cashflows = principal.to_frame().add(df_cashflows, fill_value=0.0) df_cashflows = df_cashflows.rename(columns={"accrued": "realized_accrued"}) - return pd.concat([df, df_cashflows], axis=1).join(strategies).sort_index() + df = pd.concat([df, df_cashflows], axis=1).join(strategies).sort_index() + return df.rename(columns={"clean_nav": "pv"}) def get_pv(**kwargs): @@ -256,7 +257,7 @@ def get_pv(**kwargs): if pnl_type == "swaption": return get_swaption_pv(**kwargs) elif pnl_type == "tranche": - return get_tranche_pv(**kwargs) + return get_tranche_pv2(**kwargs) else: return get_bond_pv(**kwargs) @@ -326,9 +327,10 @@ def get_pnl(df_instrument, asset_class: Literal["bond", "tranche", "swaption"]): if asset_class == "bond": return df_instrument.drop("notional", axis=1).groupby("date").sum().sum(axis=1) elif asset_class == "tranche": - return df_instrument.pv.diff() + df_instrument[["upfront", "accrued"]].sum( - axis=1 - ) + df_pnl = df_instrument.groupby(level="id")[["pv", "accrued"]].diff().sum(axis=1) + df_pnl += df_instrument.realized_accrued.fillna(0) + cashflows = df_instrument.dropna(subset=["principal"]).fillna(0).sum(axis=1) + return pd.concat([df_pnl, cashflows]).groupby("date").sum() elif asset_class == "swaption": return df_instrument.pv.diff() + df_instrument.daily |
