diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/Option Trades.ipynb | 28 | ||||
| -rw-r--r-- | python/notebooks/Realized Vol.ipynb | 24 | ||||
| -rw-r--r-- | python/notebooks/Risk Management.ipynb | 119 | ||||
| -rw-r--r-- | python/notebooks/Valuation Backtest.ipynb | 36 |
4 files changed, 183 insertions, 24 deletions
diff --git a/python/notebooks/Option Trades.ipynb b/python/notebooks/Option Trades.ipynb index 939d4cce..b72a4d85 100644 --- a/python/notebooks/Option Trades.ipynb +++ b/python/notebooks/Option Trades.ipynb @@ -31,21 +31,25 @@ "source": [ "#Ad hoc\n", "option_delta = Index.from_name('HY', 29, '5yr')\n", - "option_delta.price = 107.375\n", - "option1 = BlackSwaption(option_delta, datetime.date(2017, 12, 20), 104, option_type=\"payer\")\n", - "option2 = BlackSwaption(option_delta, datetime.date(2017, 12, 20), 100, option_type=\"payer\")\n", - "option1.sigma = .415\n", - "option2.sigma = .563\n", - "option1.notional = 25_000_000\n", - "option2.notional = 25_000_000\n", - "option1.direction = 'Long'\n", - "option2.direction = 'Short'\n", + "option_delta.price = 107.5\n", + "option1 = BlackSwaption(option_delta, datetime.date(2017, 10, 18), 105, option_type=\"payer\")\n", + "option2 = BlackSwaption(option_delta, datetime.date(2017, 11, 15), 105, option_type=\"payer\")\n", + "option3 = BlackSwaption(option_delta, datetime.date(2017, 12, 20), 105, option_type=\"payer\")\n", + "option1.sigma = .459\n", + "option2.sigma = .378\n", + "option3.sigma = .377\n", + "option1.notional = 35_000_000\n", + "option2.notional = 15_000_000\n", + "option3.notional = 15_000_000\n", + "option1.direction = 'Short'\n", + "option2.direction = 'Long'\n", + "option3.direction = 'Long'\n", "#option_delta.notional = 1\n", - "option_delta.notional = option1.notional * option1.delta + option2.notional * option2.delta\n", + "option_delta.notional = option1.notional * option1.delta + option2.notional * option2.delta + option3.notional * option3.delta\n", "option_delta.direction = 'Seller' if option_delta.notional > 0 else 'Buyer'\n", "option_delta.notional = abs(option_delta.notional)\n", - "portf = Portfolio([option1, option2, option_delta])\n", - "spread.plot_trade_scenarios(portf, -.15, .8)" + "portf = Portfolio([option1, option2, option3, option_delta])\n", + "spread.plot_trade_scenarios(portf, -.15, .3)" ] }, { diff --git a/python/notebooks/Realized Vol.ipynb b/python/notebooks/Realized Vol.ipynb index 4fa97ede..65d70d5f 100644 --- a/python/notebooks/Realized Vol.ipynb +++ b/python/notebooks/Realized Vol.ipynb @@ -8,7 +8,7 @@ "source": [ "from option_trades import realized_vol\n", "from matplotlib import pyplot as plt\n", - "plt.style.use('ggplot')" + "#plt.style.use('ggplot')" ] }, { @@ -18,7 +18,7 @@ "outputs": [], "source": [ "ig28, model_ig = realized_vol(\"IG\", 28, \"5yr\", years=3)\n", - "hy27, model_hy = realized_vol(\"HY\", 27, \"5yr\", years=3)" + "hy28, model_hy = realized_vol(\"HY\", 28, \"5yr\", years=3)" ] }, { @@ -36,7 +36,7 @@ "metadata": {}, "outputs": [], "source": [ - "hy27.plot()" + "hy28.plot()" ] }, { @@ -53,6 +53,24 @@ "execution_count": null, "metadata": {}, "outputs": [], + "source": [ + "ig28" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hy28" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [] } ], diff --git a/python/notebooks/Risk Management.ipynb b/python/notebooks/Risk Management.ipynb new file mode 100644 index 00000000..a650b1c3 --- /dev/null +++ b/python/notebooks/Risk Management.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import portfolio_var as port\n", + "from analytics import Swaption, BlackSwaption, Index, VolatilitySurface, Portfolio\n", + "from analytics.scenarios import run_swaption_scenarios, run_index_scenarios, run_portfolio_scenarios\n", + "import datetime\n", + "import pandas as pd\n", + "\n", + "#import exploration.swaption_calendar_spread as spread\n", + "import exploration.swaption_calendar_spread as spread" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df, spread, dur = port.rel_spread_diff()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#The 95%tile \n", + "stress = pd.DataFrame(index = ['widen', 'tighten'], columns=['pts'])\n", + "stress.loc['widen'] = df.quantile(.975) \n", + "stress.loc['tighten'] = df.quantile(.025)\n", + "stress = -stress * spread * dur/100" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#August ME Bond HY Equiv\n", + "bond_HY_equiv = .1652\n", + "stress['nav_impact'] = bond_HY_equiv * stress" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Swaptions\n", + "#Aug 2018: Buy Sept HY payer spread\n", + "option_delta = Index.from_tradeid(891)\n", + "option1 = BlackSwaption.from_tradeid(10, option_delta)\n", + "option2 = BlackSwaption.from_tradeid(11, option_delta)\n", + "portf = Portfolio([option1, option2, option_delta])\n", + "portf.trade_date = datetime.date(2017, 8, 31)\n", + "portf.mark()\n", + "orig_pv = portf.pv\n", + "orig_ref = portf.ref" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for x, y in stress.pts.iteritems():\n", + " portf.ref = orig_ref + y\n", + " stress[x] = portf.pv - orig_pv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stress" + ] + }, + { + "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.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/python/notebooks/Valuation Backtest.ipynb b/python/notebooks/Valuation Backtest.ipynb index 6326ec98..6a81dcd5 100644 --- a/python/notebooks/Valuation Backtest.ipynb +++ b/python/notebooks/Valuation Backtest.ipynb @@ -55,7 +55,7 @@ "outputs": [], "source": [ "#Regression Intercept\n", - "results[0]" + "round(results[0],1)" ] }, { @@ -65,7 +65,7 @@ "outputs": [], "source": [ "#Sale Difference\n", - "results[1]" + "round(results[1],2)" ] }, { @@ -97,10 +97,17 @@ "#difference by source\n", "nav = ops.get_net_navs()['endbooknav']\n", "difference = mark.diff_by_source_percentage(df)\n", - "#difference.to_clipboard()\n", - "\n", + "#difference.to_clipboard()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "#plot\n", - "ax = difference.plot(kind = 'bar', legend = True)\n", + "ax = difference.plot(kind = 'bar', legend = True, figsize = [10, 3.5])\n", "\n", "visible = ax.xaxis.get_ticklabels()[::6]\n", "for label in ax.xaxis.get_ticklabels():\n", @@ -110,7 +117,11 @@ "ax.xaxis.set_major_formatter(plt.FixedFormatter(difference.index.to_series().dt.strftime(\"%b %Y\")))\n", "ax.set_ylabel('NAV Impact vs. Fund Policy (%)')\n", "vals = ax.get_yticks()\n", - "ax.set_yticklabels(['{:3.0f}%'.format(x*100) for x in vals])" + "ax.set_yticklabels(['{:3.0f}%'.format(x*100) for x in vals])\n", + "ax.set_xlabel('')\n", + "ax.grid(False, which='major',axis = 'x')\n", + "lgd = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -.3), shadow=True, ncol=5)\n", + "ax.figure.savefig(\"/home/serenitas/edwin/PythonGraphs/Valuation_2.png\", bbox_extra_artists=(lgd,), bbox_inches='tight')" ] }, { @@ -164,7 +175,7 @@ "metadata": {}, "outputs": [], "source": [ - "mark.annual_performance(results[1])" + "round(mark.annual_performance(results[1])*100,2)" ] }, { @@ -174,7 +185,7 @@ "outputs": [], "source": [ "#A positive impact % means the alternative methodology results in a higher NAV than the fund's valuation policy.\n", - "pd.DataFrame(mark.alt_nav_impact())" + "round(pd.DataFrame(mark.alt_nav_impact())*100,2)" ] }, { @@ -213,7 +224,14 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "df = ops.trade_performance()\n", + "df = df.set_index('trade_date')\n", + "df.days_held = df.days_held.dt.days\n", + "winner = df[df.percent_gain > 0]\n", + "df[df.days_held.notnull()].groupby(pd.TimeGrouper(freq='A')).mean()\n", + "df" + ] } ], "metadata": { |
