diff options
Diffstat (limited to 'python/notebooks/PnL.ipynb')
| -rw-r--r-- | python/notebooks/PnL.ipynb | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/python/notebooks/PnL.ipynb b/python/notebooks/PnL.ipynb new file mode 100644 index 00000000..89ac2487 --- /dev/null +++ b/python/notebooks/PnL.ipynb @@ -0,0 +1,139 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "1d37c1d7-e332-4cb3-b228-0045c547ab93", + "metadata": {}, + "outputs": [], + "source": [ + "import pnl_explain as pl\n", + "import datetime\n", + "from itertools import chain\n", + "from serenitas.utils.db import dbconn\n", + "\n", + "dawndb = dbconn(\"dawndb\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f600c71b-59a9-4f10-beac-37718f6e4016", + "metadata": {}, + "outputs": [], + "source": [ + "today = datetime.date.today()\n", + "start_date = datetime.date(2022,7,1)\n", + "end_date = datetime.date(2022,11,1)\n", + "strats = {\n", + " \"swaption\": (\"IGOPTDEL\", \"HYOPTDEL\"),\n", + " \"macro_hedge\": (\"HEDGE_MAC\",),\n", + " \"tranche\": (\"IGINX\", \"HYINX\", \"XOINX\", \"EUINX\"),\n", + " \"curve\": (\"SER_ITRXCURVE\", \"SER_IGCURVE\", \"SER_HYCURVE\"),\n", + " \"rmbs_hedge\":(\"HEDGE_MBS\",),\n", + " \"clo_hedge\": (\"HEDGE_CLO\",),\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79fdf8bb-79a9-48e5-b97e-ad5d862078fd", + "metadata": {}, + "outputs": [], + "source": [ + "for fund in ['SERCGMAST', 'ISOSEL', 'BOWDST']:\n", + " pnl = {}\n", + " #bond PNL---------------\n", + " for ac in ['CRT', 'Subprime', 'CLO']:\n", + " df_instrument = pl.get_pv(conn=dawndb, \n", + " fund=fund, \n", + " pnl_type = 'bond', \n", + " asset_class = ac, \n", + " start_date = start_date, \n", + " end_date = end_date)\n", + " if not df_instrument.empty:\n", + " pnl[ac] = pl.get_pnl(df_instrument, 'bond')\n", + " #Tranches---------------\n", + " ac = 'tranche'\n", + " pv2=True\n", + " df_instrument = pl.get_pv(conn=dawndb, \n", + " fund=fund, \n", + " pnl_type = ac, \n", + " start_date = start_date, \n", + " end_date = end_date,\n", + " pv2=pv2)\n", + " pnl[ac] = pl.get_pnl(df_instrument, ac, pv2=pv2)\n", + " #swaptions--------------\n", + " ac = 'swaption'\n", + " df_instrument = pl.get_pv(conn=dawndb, \n", + " fund=fund, \n", + " pnl_type = ac, \n", + " start_date = start_date, \n", + " end_date = end_date, \n", + " source_list=['CITI', 'JPM'])\n", + " pnl[ac] = pl.get_pnl(df_instrument, ac)\n", + " #All the cleared indices--------\n", + " for ac in ['macro_hedge', 'curve', 'tranche', 'swaption', 'rmbs_hedge', 'clo_hedge']: \n", + " df_index = pl.get_index_pv(\n", + " start_date, end_date, fund, dawndb, strats[ac]\n", + " )\n", + " pnl[ac+'_index'] = df_index.pv.diff() + df_index[[\"upfront\", \"accrued\"]].sum(axis=1)\n", + " #FX PV-------------\n", + " ac = 'fx_forward'\n", + " df_instrument = pl.get_fx_pv(start_date = start_date,\n", + " end_date = end_date,\n", + " fund=fund)\n", + " pnl_inst = pl.get_pnl(df_instrument, ac)\n", + " pnl_inst.index = pd.to_datetime(pnl_inst.index)\n", + " pnl[ac] = pnl_inst\n", + " pnl_all = pd.concat(pnl, axis=1).fillna(0)\n", + " filename = '/home/serenitas/Daily/' + today.strftime(\"%Y-%m-%d\")+ \"/\" + fund + '_pnl.csv'\n", + " pnl_all.to_csv(filename)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e179cc99-edb1-48b9-b50e-7a5ae25c1e86", + "metadata": {}, + "outputs": [], + "source": [ + "#check if first day of NAV and if the upfront payment line up. if not, PNL will not work\n", + "#check trade date vs. that NULL NAV/upfront date. If it is termination it is okay\n", + "ac= 'tranche'\n", + "df_instrument = pl.get_pv(conn=dawndb, \n", + " fund=fund, \n", + " pnl_type = ac, \n", + " start_date = start_date, \n", + " end_date = end_date,\n", + " pv2=pv2)\n", + "check_trades = df_instrument.loc[df_instrument['clean_nav'].isna() &\n", + " df_instrument['principal'].notna()]\n", + "cds_trades = pd.read_sql_query(\"SELECT id, trade_date from cds\", dawndb,parse_dates=[\"trade_date\"], index_col=['id'])\n", + "check_trades = pd.merge(check_trades, cds_trades, left_index=True, right_index=True)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} |
