diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/tranche and swaption portfolio strategy.ipynb | 105 |
1 files changed, 90 insertions, 15 deletions
diff --git a/python/notebooks/tranche and swaption portfolio strategy.ipynb b/python/notebooks/tranche and swaption portfolio strategy.ipynb index 8e73f973..3bef916a 100644 --- a/python/notebooks/tranche and swaption portfolio strategy.ipynb +++ b/python/notebooks/tranche and swaption portfolio strategy.ipynb @@ -11,22 +11,15 @@ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", - "from analytics.scenarios import run_tranche_scenarios, run_portfolio_scenarios\n", + "from analytics.scenarios import run_tranche_scenarios, run_portfolio_scenarios, run_tranche_scenarios_rolldown\n", "from analytics import Swaption, BlackSwaption, Index, BlackSwaptionVolSurface, Portfolio, ProbSurface\n", "from db import dbengine\n", "from datetime import date\n", "from graphics import plot_time_color_map\n", "\n", - "dawnengine = dbengine('dawndb')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "value_date = (pd.datetime.today() - pd.offsets.BDay(1)).date()" + "dawnengine = dbengine('dawndb')\n", + "\n", + "value_date = (pd.datetime.today() - pd.offsets.BDay(2)).date()" ] }, { @@ -64,11 +57,12 @@ "#Run Swaption sensitivities\n", "#Set Shock range\n", "shock_min = -.3\n", - "shock_max = 1.5\n", + "shock_max = 1.25\n", "spread_shock = np.arange(shock_min, shock_max, 0.05)\n", "#Set Date range\n", "earliest_expiry = min(portf.swaptions, key=lambda x: x.exercise_date).exercise_date\n", "date_range = pd.bdate_range(value_date, earliest_expiry - pd.offsets.BDay(), freq='20B')\n", + "#date_range = [earliest_expiry - pd.offsets.BDay()]\n", "#Setup Vol Surface\n", "vs = BlackSwaptionVolSurface(index,series, value_date=value_date)\n", "ps = ProbSurface(index,series, value_date=value_date)\n", @@ -98,7 +92,7 @@ "#Run tranche scenarios\n", "temp = []\n", "for i, r in pos.iterrows():\n", - " df = run_tranche_scenarios(r.basket, spread_range, date_range, corr_map=False)\n", + " df = run_tranche_scenarios_rolldown(r.basket, spread_range, date_range, corr_map=False)\n", " temp.append(r.notional*df.xs(str(r.attach) + \"-\" + str(r.detach), axis=1, level=1))\n", "tranches_scens = sum(temp)" ] @@ -109,6 +103,7 @@ "metadata": {}, "outputs": [], "source": [ + "#Create snapshot of the the first scenario date\n", "total_scens = swaption_scens.reset_index().merge(tranches_scens.reset_index(), \n", " left_on=['date', 'spread'], \n", " right_on=['date', 'spread_range'], \n", @@ -125,8 +120,8 @@ "metadata": {}, "outputs": [], "source": [ - "#tranche positions delta at different spreads \n", - "ax = total_scens_single_date.delta_t.plot()\n", + "#tranche positions delta at different spreads\n", + "ax = total_scens_single_date.delta_t.plot(title = 'delta vs. spread levels')\n", "ax.ticklabel_format(style='plain')\n", "plt.tight_layout()" ] @@ -178,6 +173,86 @@ "execution_count": null, "metadata": {}, "outputs": [], + "source": [ + "#PNL of just the swaptions\n", + "plot_time_color_map(swaption_scens, spread_range, attr=\"pnl\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Construct levered Super senior hedged with swaption\n", + "index = 'IG'\n", + "series = 30\n", + "option_delta = Index.from_name(index, series, '5yr')\n", + "option_delta.spread = 62\n", + "option_delta.notional = 1\n", + "option1 = BlackSwaption(option_delta, date(2018, 7, 19), 75, option_type=\"payer\")\n", + "option1.sigma = .52\n", + "option1.direction = 'Long'\n", + "option1.notional = 2_000_000_000\n", + "\n", + "#If we have two options instead of just one\n", + "option2 = BlackSwaption(option_delta, date(2018, 7, 19), 90, option_type=\"payer\")\n", + "option2.sigma = .68\n", + "option2.direction = 'Long'\n", + "option2.notional = 6_000_000_000\n", + "\n", + "option3 = BlackSwaption(option_delta, date(2018, 12, 19), 55, option_type=\"receiver\")\n", + "option3.sigma = .373\n", + "option3.direction = 'Short'\n", + "option3.notional = 5_000_000_000\n", + "\n", + "#portf = Portfolio([option1, option_delta])\n", + "portf = Portfolio([option1, option2, option3, option_delta])\n", + "portf.value_date = value_date\n", + "portf.reset_pv()\n", + "#Run Swaption sensitivities\n", + "#Set Shock range\n", + "shock_min = -.5\n", + "shock_max = 1.25\n", + "spread_shock = np.arange(shock_min, shock_max, 0.05)\n", + "#Set Date range\n", + "earliest_expiry = min(portf.swaptions, key=lambda x: x.exercise_date).exercise_date\n", + "date_range = pd.bdate_range(value_date, earliest_expiry - pd.offsets.BDay(), freq='10B')\n", + "#Setup Vol Surface\n", + "vs = BlackSwaptionVolSurface(index,series, value_date=value_date)\n", + "ps = ProbSurface(index,series, value_date=value_date)\n", + "vol_surface = vs[vs.list(option_type='payer')[-1]]\n", + "swaption_scens = run_portfolio_scenarios(portf, date_range, spread_shock, np.array([0]),\n", + " vol_surface, params=[\"pnl\", \"delta\"])\n", + "#swaption delta is in protection terms: switch to risk terms\n", + "swaption_scens.delta = -swaption_scens.delta\n", + "\n", + "notional = 30_000_000_000\n", + "t = bkt.TrancheBasket('IG', '29', '3yr')\n", + "t.build_skew()\n", + "#get back to 17bps, .36 delta\n", + "port_spread = 67\n", + "#t.rho = np.array([np.nan, 0.39691196, 0.48904597, 0.8, np.nan])\n", + "t.tweak(port_spread)\n", + "spread_range = (1+ spread_shock) * port_spread\n", + "tranches_scens = run_tranche_scenarios_rolldown(t, spread_range, date_range, corr_map=False)\n", + "tranches_scens = notional*tranches_scens.xs('15-100', axis=1, level=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dawnengine.dispose()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [] } ], |
