diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/Curve Trades.ipynb | 120 |
1 files changed, 98 insertions, 22 deletions
diff --git a/python/notebooks/Curve Trades.ipynb b/python/notebooks/Curve Trades.ipynb index 5637beaf..01669aae 100644 --- a/python/notebooks/Curve Trades.ipynb +++ b/python/notebooks/Curve Trades.ipynb @@ -6,11 +6,17 @@ "metadata": {}, "outputs": [], "source": [ - "import curve_trades as ct\n", + "import analytics.curve_trades as ct\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", + "import pandas as pd\n", + "import numpy as np\n", + "import graphics as g\n", + "import globeop_reports as go\n", + "\n", "from ipywidgets import widgets\n", - "import pandas as pd" + "from analytics.scenarios import run_curve_scenarios\n", + "from db import dbengine" ] }, { @@ -34,7 +40,8 @@ "metadata": {}, "outputs": [], "source": [ - "index = w.value" + "index = w.value\n", + "report_date = (pd.datetime.today() - pd.offsets.BDay(2)).normalize()" ] }, { @@ -105,7 +112,10 @@ "source": [ "#Theta with 3-5-10 Strategy\n", "df = ct.ratio_within_series(param='duration')\n", - "ct.curve_3_5_10(df)" + "s = - df.theta2['3yr'] / df.duration_ratio_to_5yr['3yr'] \\\n", + " + 2 * df.theta2['5yr'] \\\n", + " - df.theta2['10yr'] / df.duration_ratio_to_5yr['10yr']\n", + "s.dropna().unstack(-1).plot()" ] }, { @@ -114,9 +124,9 @@ "metadata": {}, "outputs": [], "source": [ - "#Theta with 5-10 Strategy\n", - "df = ct.ratio_within_series(param='duration')\n", - "ct.curve_5_10(df)" + "#Theta with 5-10 Strategy: buy sell 5y, buy 10y\n", + "s = df.theta2['5yr'] - df.theta2['10yr'] / df.duration_ratio_to_5yr['10yr']\n", + "s.dropna().unstack(-1).plot()" ] }, { @@ -146,10 +156,44 @@ "metadata": {}, "outputs": [], "source": [ - "#Scenario Anslysis on current position\n", - "report_date = (pd.datetime.today() - pd.offsets.BDay(1)).normalize()\n", - "curve_pos = ct.curve_pos(report_date)\n", - "origpv = curve_pos.pv" + "df = ct.forward_spread(report_date, index)\n", + "df.plot()\n", + "plt.ylabel('spread')\n", + "plt.xlabel('forward spread start date')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df = ct.spot_forward(index)\n", + "df = df.rename(columns={'1yr': 'Spot Spread - 1 Year Forward', 'current': 'Spot Spread - Today'})\n", + "ax = df.plot(title = 'Credit Curve Roll Down')\n", + "plt.ylabel('spread (bps)')\n", + "ax.figure.savefig(\"/home/serenitas/edwin/PythonGraphs/curve_trades_roll_down.png\", bbox_inches='tight')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "portf = ct.curve_pos(report_date, index)\n", + "shock_min = -.5\n", + "shock_max = .8\n", + "spread_shock = np.arange(shock_min, shock_max, 0.05)\n", + "sql_string = \"SELECT closespread FROM index_quotes where index = %s and series = %s and tenor = %s and date = %s\"\n", + "spread_df = pd.read_sql_query(sql_string, dbengine('serenitasdb'),\n", + " params=[index, ct.on_the_run(index), '5yr', report_date])\n", + "spread_range = (1+ spread_shock) * spread_df.iloc[0][0]\n", + "#need to max it at the closest maturity date\n", + "date_range = pd.bdate_range(report_date, report_date + 180* pd.offsets.DateOffset(), freq='5B')\n", + "curve_per = np.arange(.01, .99, .1)\n", + "\n", + "df = run_curve_scenarios(portf, spread_range, date_range, curve_per)" ] }, { @@ -158,9 +202,8 @@ "metadata": {}, "outputs": [], "source": [ - "flat_curve = ct.curve_shape(report_date, percentile = .05)\n", - "for ind in curve_pos.indices:\n", - " ind.spread = flat_curve((pd.to_datetime(ind.end_date) - trade_date).days/365)" + "df_plot = df[df.curve_per == curve_per[5]]\n", + "g.plot_time_color_map(df_plot, spread_range, attr='pnl')" ] }, { @@ -169,8 +212,14 @@ "metadata": {}, "outputs": [], "source": [ + "#Scenario Anslysis on current position\n", + "#curve_positions = ct.curve_pos(report_date, index)\n", + "#origpv = curve_positions.pv\n", + "#flat_curve = ct.curve_shape(report_date, index, percentile = .05)\n", + "#for ind in curve_positions.indices:\n", + "# ind.spread = flat_curve((pd.to_datetime(ind.end_date) - report_date).days/365)\n", "#PNL in flattening to a 5% case\n", - "curve_pos.pv - origpv" + "#curve_positions.pv - origpv" ] }, { @@ -179,10 +228,10 @@ "metadata": {}, "outputs": [], "source": [ - "df = ct.forward_spread(index)\n", - "df.plot()\n", - "plt.ylabel('spread')\n", - "plt.xlabel('forward spread start date')" + "curve_positions = ct.curve_pos(report_date, index)\n", + "df = ct.pos_pnl_abs(curve_positions, report_date)\n", + "navs = go.get_net_navs()\n", + "df_plot = df.pnl/navs.loc['2018-03-31'].endbooknav" ] }, { @@ -191,9 +240,36 @@ "metadata": {}, "outputs": [], "source": [ - "df = ct.spot_forward(index)\n", - "df.plot()\n", - "plt.ylabel('spread')" + "fig, ax = plt.subplots()\n", + "ax.plot(df_plot.index, df_plot.values)\n", + "ax.set(xlabel='date', ylabel='% of NAV',\n", + " title='PNL impact from spread curve scenario')\n", + "plt.xticks(rotation=90)\n", + "y_ticks = ax.get_yticks()\n", + "ax.set_yticklabels(['{:.2f}%'.format(y*100) for y in y_ticks])\n", + "plt.tight_layout()\n", + "#ax.figure.savefig(\"/home/serenitas/edwin/PythonGraphs/curve_trades.png\", bbox_inches='tight')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Historical PNL in a 5% case\n", + "df.pnl.quantile(.05)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scen_table = ct.curve_scen_table(curve_positions)\n", + "scen_table.pnl = scen_table.pnl/navs.loc['2018-03-31'].endbooknav *100\n", + "scen_table.pivot(index='tighter', columns='wider')" ] }, { |
