aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/tranches numbers.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'python/notebooks/tranches numbers.ipynb')
-rw-r--r--python/notebooks/tranches numbers.ipynb72
1 files changed, 70 insertions, 2 deletions
diff --git a/python/notebooks/tranches numbers.ipynb b/python/notebooks/tranches numbers.ipynb
index fc936b7a..d655f8ba 100644
--- a/python/notebooks/tranches numbers.ipynb
+++ b/python/notebooks/tranches numbers.ipynb
@@ -54,7 +54,7 @@
"outputs": [],
"source": [
"sql_string = '''SELECT a.*, b.trancheupfrontmid, b.indexrefspread, b.tranchedelta, b.trancherunningmid\n",
- " FROM risk_numbers_new a \n",
+ " FROM risk_numbers a \n",
" join tranche_quotes b on a.tranche_id = b.id\n",
" where a.index <> 'EU'\n",
" '''\n",
@@ -435,6 +435,74 @@
"execution_count": null,
"metadata": {},
"outputs": [],
+ "source": [
+ "#Back test tranche pair trades \n",
+ "sql_str = \"select * from markit_tranche_quotes a left join \" \\\n",
+ " \"(select index, series, version, basketid from index_version) b \" \\\n",
+ " \"using (basketid) order by quotedate asc\"\n",
+ "index_columns=['index', 'series', 'version', 'tenor', 'attach', 'detach']\n",
+ "df = pd.read_sql_query(sql_str, engine, parse_dates=['quotedate'], index_col=index_columns)\n",
+ "df['day_frac'] = (df.groupby(index_columns)['quotedate'].\n",
+ " transform(lambda s: s.\n",
+ " diff().astype('timedelta64[D]') / 360))\n",
+ "df['close_price'] = 1-df['upfront_mid']\n",
+ "df = df.set_index('quotedate', append=True)\n",
+ "df['price_return'] = df['close_price'].groupby(level=index_columns).diff()\n",
+ "df['price_return'] += df.day_frac * df.tranche_spread/10000\n",
+ "df = df.drop(['basketid', 'upfront_bid', 'upfront_ask', 'upfront_mid', 'index_price', 'day_frac', 'tranche_spread', 'close_price'], axis=1)\n",
+ "df = df.dropna()\n",
+ "#Focus on IG\n",
+ "ig_tranches = df.xs(('IG', '5yr', 23), level = ['index', 'tenor', 'series'])\n",
+ "ig_tranches = ig_tranches.reset_index(['version', 'detach'], drop=True)\n",
+ "ig_tranches = ig_tranches.unstack(level='attach')\n",
+ "ig_tranches.columns = ig_tranches.columns.droplevel()\n",
+ "\n",
+ "#carry strat = long 15-100 and short 7-15: 4.6 by 1, 50bps IA\n",
+ "carrystrat = (4.6 * ig_tranches[15] - ig_tranches[7])/.05\n",
+ "cum_return = (carrystrat+1).cumprod()\n",
+ "cum_return.plot()\n",
+ "#equity gamma strat = long 0-3 and short 7-15: 1 by 6, 12% IA\n",
+ "equitygammastrat = (1 * ig_tranches[0] - 6 * ig_tranches[7])/.12\n",
+ "cum_return = (equitygammastrat+1).cumprod()\n",
+ "cum_return.plot()\n",
+ "#mezz gamma strat = long 3-7 and short 7-15: 1 by 2.75, 2.5% IA\n",
+ "mezzgammastrat =(1 * ig_tranches[3] - 2.75 * ig_tranches[7])/.025\n",
+ "cum_return = (mezzgammastrat+1).cumprod()\n",
+ "cum_return.plot()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "r=[]\n",
+ "r.append(carrystrat.mean()/carrystrat.std() * math.sqrt(252))\n",
+ "r.append(equitygammastrat.mean()/equitygammastrat.std() * math.sqrt(252))\n",
+ "r.append(mezzgammastrat.mean()/mezzgammastrat.std() * math.sqrt(252))\n",
+ "r"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
"source": []
}
],
@@ -473,7 +541,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.6"
+ "version": "3.7.1"
}
},
"nbformat": 4,