{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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", "from analytics.scenarios import run_curve_scenarios\n", "from db import dbengine" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w = widgets.Dropdown(\n", " options=['IG', 'EU'],\n", " value='IG',\n", " description='Index:',\n", " disabled=False,\n", ")\n", "w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "index = w.value\n", "report_date = (pd.datetime.today() - pd.offsets.BDay(2)).normalize()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#On the run spread differences\n", "spreads_diff = ct.curve_spread_diff(index, 6)\n", "spreads_diff.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Table of Spread Differences, and Z-score of current spread differences\n", "ct.spreads_diff_table(spreads_diff)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Theta per unit duration\n", "ct.theta_matrix_by_series(index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#on the run theta\n", "ct.on_the_run_theta(index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Curve Trade returns\n", "ct.curve_returns()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ct.cross_series_curve(index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Theta with 3-5-10 Strategy\n", "df = ct.ratio_within_series(param='duration')\n", "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()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#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()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Relative Spread Difference\n", "spread_ratio = ct.ratio_within_series(param = 'closespread')\n", "spread_ratio.groupby(level = ['date']).last()['closespread_ratio_to_5yr'].plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = ct.curve_model('5yr', '10yr')\n", "model_results = ct.curve_model_results(model[0], model[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_plot = df[df.curve_per == curve_per[5]]\n", "g.plot_time_color_map(df_plot, spread_range, attr='pnl')" ] }, { "cell_type": "code", "execution_count": null, "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_positions.pv - origpv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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.iloc[-1].endbooknav" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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.iloc[-1].endbooknav *100\n", "scen_table.pivot(index='tighter', columns='wider')" ] }, { "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 }