diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/exploration/curve_trades.py | 108 | ||||
| -rw-r--r-- | python/notebooks/Curve Trades.ipynb | 108 |
2 files changed, 187 insertions, 29 deletions
diff --git a/python/exploration/curve_trades.py b/python/exploration/curve_trades.py index fa545b21..c80a41d5 100644 --- a/python/exploration/curve_trades.py +++ b/python/exploration/curve_trades.py @@ -1,41 +1,91 @@ from index_data import get_index_quotes, index_returns import pandas as pd import math +from scipy.stats.mstats import zscore -## look at spreads -df = get_index_quotes("IG", [23, 24, 25, 26, 27], tenor=['3yr', '5yr', '7yr', '10yr']) -spreads = df.groupby(level=['date', 'tenor']).nth(-1)['closespread'].unstack(-1) -# remove 'yr' -spreads.columns = [int(col[:-2]) for col in spreads.columns] -spreads = spreads.sort_index(1) -spreads_diff = spreads.diff(axis=1) -spreads_diff = spreads_diff.filter([5, 7, 10]) -spreads_diff.columns = ['3-5', '5-7', '7-10'] -spreads_diff.plot() +import matplotlib.pyplot as plt -## look at returns -df = index_returns(index='IG', series=[24, 25, 26, 27, 28], tenor=['3yr', '5yr', '7yr', '10yr']) -## on-the-run returns -returns = df.price_return.unstack(-1).dropna().groupby(level='date').nth(-1) -strategy510 = 1.78 * returns['5yr'] - returns['10yr'] -strategy710 = 1.33 * returns['7yr'] - returns['10yr'] -strategy3510 = -2 * returns['3yr']+ 3 * returns['5yr'] - 1 * returns['10yr'] +index = 'IG' +on_the_run = 28 -monthly_returns510 = strategy510.groupby(pd.TimeGrouper(freq='M')).agg(lambda df:(1+df).prod()-1) -monthly_returns710 = strategy710.groupby(pd.TimeGrouper(freq='M')).agg(lambda df:(1+df).prod()-1) -monthly_returns3510 = strategy3510.groupby(pd.TimeGrouper(freq='M')).agg(lambda df:(1+df).prod()-1) +def curve_spread_diff(index = 'IG', on_the_run = 28): + ## look at spreads + df = get_index_quotes(index, list(range(on_the_run-6,on_the_run+1)), tenor=['3yr', '5yr', '7yr', '10yr']) + spreads = df.groupby(level=['date', 'tenor']).nth(-1)['closespread'].unstack(-1) + # remove 'yr' + spreads.columns = [int(col[:-2]) for col in spreads.columns] + spreads = spreads.sort_index(1) + spreads_diff = spreads.diff(axis=1) + spreads_diff = spreads_diff.filter([5, 7, 10]) + spreads_diff['5-10'] = spreads_diff[7] + spreads_diff[10] + spreads_diff.columns = ['3-5', '5-7', '7-10', '5-10'] + return spreads_diff -sharpe510 = strategy510.mean()/strategy510.std()*math.sqrt(252) -sharpe710 = strategy710.mean()/strategy710.std()*math.sqrt(252) -sharpe3510 = strategy3510.mean()/strategy3510.std()*math.sqrt(252) +def spreads_diff_table(spreads_diff): + df = pd.DataFrame() + df['min'] = spreads_diff.min() + df['max'] = spreads_diff.max() + df['average'] = spreads_diff.mean() + df['current'] = spreads_diff.iloc[-1] + df['zscore'] = pd.Series(zscore(spreads_diff)[-1], index = df.index) + pd.DataFrame(zscore(spreads_diff), index=spreads_diff.index, columns=spreads_diff.columns).plot() + return df -monthly_sharpe510 = monthly_returns510.mean()/monthly_returns510.std()*math.sqrt(12) -monthly_sharpe710 = monthly_returns710.mean()/monthly_returns710.std()*math.sqrt(12) -monthly_sharpe3510 = monthly_returns3510.mean()/monthly_returns3510.std()*math.sqrt(12) +def theta_matrix(index = 'IG', on_the_run = 28): + df = get_index_quotes(index, list(range(on_the_run-6,on_the_run+1)), tenor=['3yr', '5yr', '7yr', '10yr']) + df['theta_per_dur'] = df.theta2/df.duration2 + theta_matrix = df.groupby(level=['date', 'tenor','series']).nth(-1)['theta_per_dur'] + theta_matrix_1 = theta_matrix.xs(theta_matrix.index.max()[0], level = 0).unstack(0) + return theta_matrix_1[['3yr', '5yr', '7yr', '10yr']] + +def on_the_run_theta(index = 'IG', on_the_run = 28): + df = get_index_quotes(index, list(range(on_the_run-6,on_the_run+1)), tenor=['3yr', '5yr', '7yr', '10yr']) + df['theta_per_dur'] = df.theta2/df.duration2 + theta_matrix = df.groupby(level=['date', 'tenor']).nth(-1)['theta_per_dur'] + theta_matrix.unstack(-1).plot() + +def curve_returns(index = 'IG', on_the_run = 28): + ## look at returns + df = index_returns(index= index, series=list(range(on_the_run-6,on_the_run+1)), tenor=['3yr', '5yr', '7yr', '10yr']) + ## on-the-run returns + returns = df.price_return.unstack(-1).dropna().groupby(level='date').nth(-1) + + strategy = ['510', '710', '3510'] + + strategies_return = pd.DataFrame() + strategies_return[strategy[0]] = 1.78 * returns['5yr'] - returns['10yr'] + strategies_return[strategy[1]] = 1.33 * returns['7yr'] - returns['10yr'] + strategies_return[strategy[2]] = -2 * returns['3yr']+ 3 * returns['5yr'] - 1 * returns['10yr'] + + strategies_return_monthly = pd.DataFrame() + for strat in strategy: + strategies_return_monthly[strat] = strategies_return[strat].groupby(pd.TimeGrouper(freq='M')).agg(lambda df:(1+df).prod()-1) + + sharpe = {} + monthly_sharpe = {} + for strat in strategy: + sharpe[strat] = strategies_return[strat].mean()/strategies_return[strat].std()*math.sqrt(252) + monthly_sharpe[strat] = strategies_return_monthly[strat].mean()/strategies_return_monthly[strat].std()*math.sqrt(12) + + worst_drawdown = {} + for strat in strategy: + worst_drawdown[strat] = strategies_return[strat].nsmallest(10) + + return (sharpe, monthly_sharpe, worst_drawdown) + +def cross_series_curve(index = 'IG', on_the_run = 28): + + df = index_returns(index= index, series=list(range(on_the_run-6,on_the_run+1)), tenor=['3yr', '5yr', '7yr', '10yr']) + ## look cross series + returns1 = df.xs(['5yr','IG'], level = ['tenor','index']).price_return.unstack(-1) + price_diff = pd.DataFrame() + for ind in [28,27,26]: + price_diff[ind] = returns1[ind] - 1.55* returns1[ind - 4] + + price_diff = price_diff.stack().groupby(level = 'date').nth(-1) + monthly_returns_cross_series = price_diff.groupby(pd.TimeGrouper(freq='M')).agg(lambda df:(1+df).prod()-1) + monthly_returns_cross_series.plot() -worst_drawdown510 = strategy510.nsmallest(10) -worst_drawdown710 = strategy710.nsmallest(10) -worst_drawdown3510 = strategy3510.nsmallest(10) def forward_loss(): from db import dbengine, dbconn diff --git a/python/notebooks/Curve Trades.ipynb b/python/notebooks/Curve Trades.ipynb new file mode 100644 index 00000000..7174706d --- /dev/null +++ b/python/notebooks/Curve Trades.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "\n", + "import os\n", + "import sys\n", + "sys.path.append(os.path.join(os.environ['CODE_DIR'], 'python', 'exploration'))\n", + "sys.path.append(os.path.join(os.environ['CODE_DIR'], 'python'))\n", + "import curve_trades as ct\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "index = 'IG'\n", + "on_the_run = 28" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#On the run spread differences\n", + "spreads_diff = ct.curve_spread_diff(index, on_the_run)\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(index, on_the_run)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ct.on_the_run_theta(index, on_the_run)" + ] + }, + { + "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": [] + } + ], + "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 +} |
