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
281
282
283
284
285
286
287
288
289
290
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import exploration.option_trades as rvol\n",
"import datetime\n",
"import pandas as pd\n",
"\n",
"from analytics import on_the_run\n",
"from scipy.interpolate import interp1d\n",
"from matplotlib import pyplot as plt\n",
"from ipywidgets import widgets"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b7808ffade83485bba8f589bedb10f68",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Dropdown(description='Index:', options=('IG', 'HY'), value='IG')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w = widgets.Dropdown(\n",
" options=['IG', 'HY'],\n",
" value='IG',\n",
" description='Index:',\n",
" disabled=False,\n",
")\n",
"w"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"index = w.value\n",
"start_date=datetime.date(2014, 6, 11)\n",
"onTR, model = rvol.realized_vol(index, tenor=\"5yr\", years=3)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4dfbcdf09a294dc781ad1a5b4c382c96",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7fe3eb2ad390>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"onTR.plot()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table class=\"simpletable\">\n",
"<caption>Constant Mean - GARCH Model Results</caption>\n",
"<tr>\n",
" <th>Dep. Variable:</th> <td>spread_return</td> <th> R-squared: </th> <td> -0.001</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Mean Model:</th> <td>Constant Mean</td> <th> Adj. R-squared: </th> <td> -0.001</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Vol Model:</th> <td>GARCH</td> <th> Log-Likelihood: </th> <td> 94.1921</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Distribution:</th> <td>Normal</td> <th> AIC: </th> <td> -180.384</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Method:</th> <td>Maximum Likelihood</td> <th> BIC: </th> <td> -161.947</td>\n",
"</tr>\n",
"<tr>\n",
" <th></th> <td></td> <th> No. Observations: </th> <td>742</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Date:</th> <td>Wed, Sep 11 2019</td> <th> Df Residuals: </th> <td>738</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Time:</th> <td>10:44:17</td> <th> Df Model: </th> <td>4</td> \n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<caption>Mean Model</caption>\n",
"<tr>\n",
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>95.0% Conf. Int.</th> \n",
"</tr>\n",
"<tr>\n",
" <th>mu</th> <td> -0.0178</td> <td>7.408e-03</td> <td> -2.400</td> <td>1.641e-02</td> <td>[-3.230e-02,-3.258e-03]</td>\n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<caption>Volatility Model</caption>\n",
"<tr>\n",
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>95.0% Conf. Int.</th> \n",
"</tr>\n",
"<tr>\n",
" <th>omega</th> <td>3.6040e-03</td> <td>1.414e-03</td> <td> 2.548</td> <td>1.082e-02</td> <td>[8.322e-04,6.376e-03]</td>\n",
"</tr>\n",
"<tr>\n",
" <th>alpha[1]</th> <td> 0.1348</td> <td>4.345e-02</td> <td> 3.103</td> <td>1.914e-03</td> <td>[4.968e-02, 0.220]</td> \n",
"</tr>\n",
"<tr>\n",
" <th>beta[1]</th> <td> 0.7960</td> <td>5.441e-02</td> <td> 14.631</td> <td>1.774e-48</td> <td>[ 0.689, 0.903]</td> \n",
"</tr>\n",
"</table><br/><br/>Covariance estimator: robust"
],
"text/plain": [
"<class 'statsmodels.iolib.summary.Summary'>\n",
"\"\"\"\n",
" Constant Mean - GARCH Model Results \n",
"==============================================================================\n",
"Dep. Variable: spread_return R-squared: -0.001\n",
"Mean Model: Constant Mean Adj. R-squared: -0.001\n",
"Vol Model: GARCH Log-Likelihood: 94.1921\n",
"Distribution: Normal AIC: -180.384\n",
"Method: Maximum Likelihood BIC: -161.947\n",
" No. Observations: 742\n",
"Date: Wed, Sep 11 2019 Df Residuals: 738\n",
"Time: 10:44:17 Df Model: 4\n",
" Mean Model \n",
"==============================================================================\n",
" coef std err t P>|t| 95.0% Conf. Int.\n",
"------------------------------------------------------------------------------\n",
"mu -0.0178 7.408e-03 -2.400 1.641e-02 [-3.230e-02,-3.258e-03]\n",
" Volatility Model \n",
"============================================================================\n",
" coef std err t P>|t| 95.0% Conf. Int.\n",
"----------------------------------------------------------------------------\n",
"omega 3.6040e-03 1.414e-03 2.548 1.082e-02 [8.322e-04,6.376e-03]\n",
"alpha[1] 0.1348 4.345e-02 3.103 1.914e-03 [4.968e-02, 0.220]\n",
"beta[1] 0.7960 5.441e-02 14.631 1.774e-48 [ 0.689, 0.903]\n",
"============================================================================\n",
"\n",
"Covariance estimator: robust\n",
"\"\"\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'df' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m-----------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-bc681e92175e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#compute lo and hi percentiles of atm volatility daily change (vol of vol)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mrvol\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvol_var\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/projects/code/python/exploration/option_trades.py\u001b[0m in \u001b[0;36mvol_var\u001b[0;34m(percentile, index, start_date)\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mwe\u001b[0m \u001b[0mshould\u001b[0m \u001b[0mgroup\u001b[0m \u001b[0mit\u001b[0m \u001b[0mby\u001b[0m \u001b[0mseries\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \"\"\"\n\u001b[0;32m--> 113\u001b[0;31m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0matm_vol\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstart_date\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 114\u001b[0m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrolling_vol\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mterm\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/projects/code/python/exploration/option_trades.py\u001b[0m in \u001b[0;36matm_vol\u001b[0;34m(index, date, series, moneyness)\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0matm_vol\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseries\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmoneyness\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 89\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0matm_vol_calc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmoneyness\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 90\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 91\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrolling_vol\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcol\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'atm_vol'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mterm\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'df' is not defined"
]
}
],
"source": [
"#compute lo and hi percentiles of atm volatility daily change (vol of vol)\n",
"rvol.vol_var()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = rvol.atm_vol(index, start_date, moneyness = .2)\n",
"df['steepness'] = df.otm_vol - df.atm_vol\n",
"df1 = df.reset_index()\n",
"df1['date'] = df1.quotedate.dt.date\n",
"df1 = df1.groupby(['date','expiry']).last()\n",
"#Need to do: the vol looks jumpy, is it because of quote source issue? yes, need to first try to get the same quote source..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = []\n",
"time = [1/12, 2/12, 3/12, 4/12, 5/12]\n",
"for t in time:\n",
" for date, g in df1.groupby(level='date'):\n",
" f = interp1d(g['T'].values, g['steepness'].values, fill_value='extrapolate')\n",
" r.append((date, t, f(t)))\n",
"steepness = pd.DataFrame(r, columns=['date', 'T', 'steepness'])\n",
"steepness = steepness.set_index(['date','T']).unstack().astype('float')\n",
"steepness.columns = steepness.columns.droplevel()\n",
"steepness.ewm(span = 3).mean().plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(steepness.iloc[-1] - steepness.mean()) / steepness.std()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Need to do: look at steepness not on moneyness but on delta range (60 delta vs 20 delta)"
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|