{ "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": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "onTR.plot()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Constant Mean - GARCH Model Results
Dep. Variable: spread_return R-squared: -0.001
Mean Model: Constant Mean Adj. R-squared: -0.001
Vol Model: GARCH Log-Likelihood: 94.1921
Distribution: Normal AIC: -180.384
Method: Maximum Likelihood BIC: -161.947
No. Observations: 742
Date: Wed, Sep 11 2019 Df Residuals: 738
Time: 10:44:17 Df Model: 4
\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Mean Model
coef std err t P>|t| 95.0% Conf. Int.
mu -0.0178 7.408e-03 -2.400 1.641e-02 [-3.230e-02,-3.258e-03]
\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Volatility Model
coef std err t P>|t| 95.0% Conf. Int.
omega 3.6040e-03 1.414e-03 2.548 1.082e-02 [8.322e-04,6.376e-03]
alpha[1] 0.1348 4.345e-02 3.103 1.914e-03 [4.968e-02, 0.220]
beta[1] 0.7960 5.441e-02 14.631 1.774e-48 [ 0.689, 0.903]


Covariance estimator: robust" ], "text/plain": [ "\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\u001b[0m in \u001b[0;36m\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 }