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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import analytics.tranche_basket as bkt\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\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')\n",
"\n",
"value_date = (pd.datetime.today() - pd.offsets.BDay(2)).date()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Construct IG Swaption Portfolio\n",
"index = 'IG'\n",
"series = 30\n",
"option_delta = Index.from_name(index, series, '5yr')\n",
"option_delta.spread = 65\n",
"option1 = BlackSwaption(option_delta, date(2018, 10, 17), 60, option_type=\"payer\")\n",
"option1.sigma = .398\n",
"option1.direction = 'Long'\n",
"option2 = BlackSwaption(option_delta, date(2018, 10, 17), 100, option_type=\"payer\")\n",
"option2.sigma = .609\n",
"option2.direction = 'Short'\n",
"option1.notional = 100_000_000\n",
"option2.notional = 300_000_000\n",
"option_delta.notional = option1.notional * option1.delta + option2.notional * option2.delta\n",
"\n",
"portf = Portfolio([option1, option2, option_delta])\n",
"portf.value_date = value_date\n",
"portf.reset_pv()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Run Swaption sensitivities\n",
"#Set Shock range\n",
"shock_min = -.3\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",
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Get current Tranche positions\n",
"sql_string = \"select * from list_tranche_marks(%s)\"\n",
"pos = pd.read_sql_query(sql_string, dawnengine, params=(value_date,), parse_dates=['maturity'])\n",
"tranche_port = []\n",
"for i, r in pos.iterrows():\n",
" tranche_port.append(bkt.TrancheBasket(r.p_index, r.p_series, '5yr'))\n",
" tranche_port[i].build_skew()\n",
"pos['basket'] = tranche_port\n",
"#Set Shock Range\n",
"spread_range = (1+ spread_shock) * option_delta.spread\n",
"#Run tranche scenarios\n",
"temp = []\n",
"for i, r in pos.iterrows():\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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
" suffixes=['_s', '_t'])\n",
"total_scens['pnl'] = total_scens['pnl_s'] + total_scens['pnl_t']\n",
"total_scens['delta'] = total_scens['delta_s'] + total_scens['delta_t']\n",
"total_scens_single_date = total_scens.set_index('date').xs(date_range[0])\n",
"total_scens_single_date = total_scens_single_date.set_index('spread', drop=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#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()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Tranche + Swaptions positions delta at different spreads\n",
"ax1 = total_scens_single_date.delta.plot()\n",
"ax1.ticklabel_format(style='plain')\n",
"plt.tight_layout()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"total_scens_single_date[['delta', 'delta_t', 'delta_s']].plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"total_scens_single_date[['pnl', 'pnl_t', 'pnl_s']].plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"total_scens.date = pd.to_datetime(total_scens.date)\n",
"total_scens = total_scens.set_index(['date'])\n",
"plot_time_color_map(total_scens, spread_range, attr=\"pnl\")\n",
"#plot_time_color_map(df, shock_range, attr=\"final_delta\", color_map= 'rainbow', centered = False)"
]
},
{
"cell_type": "code",
"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": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|