aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/notebooks/swaption_scenarios.ipynb52
1 files changed, 38 insertions, 14 deletions
diff --git a/python/notebooks/swaption_scenarios.ipynb b/python/notebooks/swaption_scenarios.ipynb
index 13639918..e632d922 100644
--- a/python/notebooks/swaption_scenarios.ipynb
+++ b/python/notebooks/swaption_scenarios.ipynb
@@ -34,42 +34,48 @@
"source": [
"portf = get_swaption_portfolio(datetime.date.today() - pd.offsets.BDay(), conn, source_list=['GS'])\n",
"\n",
- "hedges = pd.read_sql_query(\"SELECT security_desc, notional FROM list_cds_positions_by_strat(%s) \"\n",
+ "hedges = pd.read_sql_query(\"SELECT security_id as redcode, maturity, notional, folder FROM list_cds_positions_by_strat(%s) \"\n",
" \"WHERE folder in ('IGOPTDEL', 'HYOPTDEL')\",\n",
" conn, params=(datetime.date.today(),))\n",
"\n",
"for i, r in hedges.iterrows():\n",
- " portf.add_trade(CreditIndex(r['security_desc'].split(\" \")[1],\n",
- " r['security_desc'].split(\" \")[3][1:],\n",
- " '5yr', value_date=datetime.date.today() - pd.offsets.BDay(),\n",
- " notional = r['notional']), ('delta', i))\n",
+ " strategy = r.pop(\"folder\")\n",
+ " trade_index = CreditIndex(**r, value_date=datetime.date.today() - pd.offsets.BDay())\n",
+ " trade_index.mark()\n",
+ " portf.add_trade(trade_index, (strategy, i))\n",
"\n",
"vol_surface = {}\n",
"for trade in portf.swaptions:\n",
- " vs = BlackSwaptionVolSurface(trade.index.index_type, trade.index.series, \n",
- " value_date=datetime.date.today(), interp_method = \"bivariate_linear\")\n",
- " vol_surface[(trade.index.index_type, trade.index.series, trade.option_type)] = vs[vs.list(source='GS', option_type=trade.option_type)[-1]]\n",
+ " k = (trade.index.index_type, trade.index.series, trade.option_type)\n",
+ " if k not in vol_surface:\n",
+ " vs = BlackSwaptionVolSurface(trade.index.index_type, trade.index.series, \n",
+ " value_date=datetime.date.today(), interp_method = \"bivariate_linear\")\n",
+ " vol_surface[k] = vs[vs.list('GS', option_type=trade.option_type)[-1]]\n",
"\n",
"#Set original_pv as of yesterday's EOD levels, don't reset PV after this time\n",
- "portf.mark(interp_method=\"bivariate_linear\", source_list=['GS'])\n",
"portf.reset_pv()\n",
"\n",
"#set ref to today's levels\n",
"portf.value_date = datetime.date.today()\n",
- "portf.mark(interp_method=\"bivariate_linear\", source_list=['GS'])\n",
- "\n",
+ "portf.mark(interp_method=\"bivariate_linear\", source_list=['GS'])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
"spread_shock = np.round(np.arange(-.1, .1, .01), 4)\n",
"scens = run_portfolio_scenarios(portf, [datetime.datetime.now()], params=['pnl', 'hy_equiv', 'sigma'],\n",
" spread_shock=spread_shock,\n",
" vol_shock=[0],\n",
- " corr_shock=[0],\n",
" vol_surface=vol_surface)\n",
"pnl = scens.xs('pnl', level = 2, axis=1).sum(axis=1)\n",
"hy_equiv = scens.xs('hy_equiv', level = 2, axis=1).sum(axis=1)\n",
"\n",
"ig = CreditIndex('IG', 32, '5yr')\n",
"ig.mark()\n",
- "\n",
"pnl.index = pnl.index.set_levels((1+pnl.index.get_level_values('spread_shock')) * ig.spread, level = 'spread_shock')\n",
"hy_equiv.index = pnl.index"
]
@@ -80,6 +86,24 @@
"metadata": {},
"outputs": [],
"source": [
+ "pnl.reset_index([\"date\", \"vol_shock\"], drop=True).to_frame(\"pnl\").plot()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "hy_equiv.reset_index([\"date\", \"vol_shock\"], drop=True).to_frame(\"hy_equiv\").plot()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
"pnl, hy_equiv"
]
},
@@ -424,7 +448,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.7.3"
+ "version": "3.7.4"
}
},
"nbformat": 4,