aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/PnL.ipynb
blob: 89ac24877b8b8de8fcc14d5c345c2dee42aa4e33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
}