diff options
Diffstat (limited to 'python/pnl_explain.py')
| -rw-r--r-- | python/pnl_explain.py | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/python/pnl_explain.py b/python/pnl_explain.py index b52c6b52..fa2fe22c 100644 --- a/python/pnl_explain.py +++ b/python/pnl_explain.py @@ -123,7 +123,10 @@ def get_tranche_pv( for d in dr: prev_day = (d - bus_day).date() if previous_twentieth(prev_day, roll=True) == prev_day: - amount = cds_accrued(prev_day, 1.0, True) + # we know prev_day is an accrued payment date + # we remove one business day so that previous_twentieth actually returns + # the previous twentieth + amount = (prev_day - previous_twentieth(prev_day - bus_day)).days / 360 accrued = 0.0 for t in portf.trades: accrued -= ( @@ -163,9 +166,18 @@ def get_tranche_pv( upfronts.append(nav) accrueds.append(accrued) df = pd.DataFrame( - {"pv": pv, "upfront": upfronts, "accrued": accrued}, index=pd.to_datetime(dates) + {"pv": pv, "upfront": upfronts, "accrued": accrueds}, + index=pd.to_datetime(dates), ) - return df + defaults = pd.DataFrame( + {"upfront": [963398.61 * 3, 738908.68 * 3, 990427.08 * 3]}, + index=[ + pd.Timestamp("2020-06-01"), + pd.Timestamp("2020-06-10"), + pd.Timestamp("2020-06-25"), + ], + ) + return df.sub(defaults, fill_value=0.0) def get_tranche_pv2( @@ -198,7 +210,7 @@ def get_tranche_pv2( "FROM terminations " "JOIN cds USING (dealid) " "LEFT JOIN fx ON termination_date=date " - "WHERE termination_date BETWEEN %s AND %s " + "WHERE termination_date > %s AND termination_date <=%s " "AND fund=%s", (start_date, end_date, fund), ) @@ -221,9 +233,19 @@ def get_tranche_pv2( for df_temp in (df_terminations, df_upfronts): df_temp.date = pd.to_datetime(df_temp.date) df_temp.set_index(["date", "id"], inplace=True) - daily = pd.concat([df_terminations, df_upfronts], axis=1).sum(axis=1) - daily.name = "daily" - return pd.concat([df, daily], axis=1).join(strategies).sort_index() + principal = pd.concat([df_terminations, df_upfronts], axis=1).sum(axis=1) + principal.name = "principal" + df_cashflows = pd.read_sql_query( + "SELECT * FROM tranche_cashflows " "WHERE date BETWEEN %s AND %s", + conn, + params=(start_date, end_date), + parse_dates=["date"], + index_col=["date", "tranche_id"], + ).fillna(0.0) + df_cashflows.index.names = ["date", "id"] + 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() def get_pv(**kwargs): @@ -382,3 +404,5 @@ if __name__ == "__main__": print(cumulative_from_daily(pnl.sum(axis=1))) elif args.pnl_type == "hedge": print(cumulative_from_daily(pnl_index)) + # HTZ: -738908.68 * 3 + # JCP 963398.61 * 3 |
