aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/exploration/curve_trades.py47
-rw-r--r--python/notebooks/Curve Trades.ipynb35
2 files changed, 71 insertions, 11 deletions
diff --git a/python/exploration/curve_trades.py b/python/exploration/curve_trades.py
index c80a41d5..fbdbf7ec 100644
--- a/python/exploration/curve_trades.py
+++ b/python/exploration/curve_trades.py
@@ -31,13 +31,38 @@ def spreads_diff_table(spreads_diff):
pd.DataFrame(zscore(spreads_diff), index=spreads_diff.index, columns=spreads_diff.columns).plot()
return df
-def theta_matrix(index = 'IG', on_the_run = 28):
+def theta_matrix_by_series(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 theta_ratio_within_series(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'])
+ r = {}
+ for i,g in df.groupby(level=['date', 'series']):
+ five_yr = g.xs('5yr', level = 'tenor')['duration']
+ r[i] = g.duration/five_yr[0]
+ df1 = pd.concat(r)
+ dftemp= pd.DataFrame(df1.groupby(level=['date', 'tenor','series']).nth(-1).rename('ratio_to_5yr'))
+ df2 = df.groupby(level=['date', 'tenor','series']).nth(-1).merge(dftemp, left_index=True, right_index=True)
+ return df2.unstack(-2)
+
+def curve_3_5_10(theta_ratio_within_series):
+ df = theta_ratio_within_series
+ #buy 3y, sell 5y, buy 10y
+ df['3_5_10'] = - df.theta2['3yr'] / df.ratio_to_5yr['3yr'] \
+ + 2 * df.theta2['5yr'] \
+ - df.theta2['10yr'] / df.ratio_to_5yr['10yr']
+ df['3_5_10'].dropna().unstack(-1).plot()
+
+def curve_5_10(theta_ratio_within_series):
+ df = theta_ratio_within_series
+ #buy sell 5y, buy 10y
+ df['5_10'] = df.theta2['5yr'] - df.theta2['10yr'] / df.ratio_to_5yr['10yr']
+ df['5_10'].dropna().unstack(-1).plot()
+
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
@@ -61,6 +86,7 @@ def curve_returns(index = 'IG', on_the_run = 28):
for strat in strategy:
strategies_return_monthly[strat] = strategies_return[strat].groupby(pd.TimeGrouper(freq='M')).agg(lambda df:(1+df).prod()-1)
+ results = pd.DataFrame()
sharpe = {}
monthly_sharpe = {}
for strat in strategy:
@@ -69,22 +95,27 @@ def curve_returns(index = 'IG', on_the_run = 28):
worst_drawdown = {}
for strat in strategy:
- worst_drawdown[strat] = strategies_return[strat].nsmallest(10)
+ worst_drawdown[strat] = strategies_return[strat].nsmallest(10).mean()
+
+ results = results.append(sharpe, ignore_index=True)
+ results = results.append(monthly_sharpe, ignore_index=True)
+ results = results.append(worst_drawdown, ignore_index=True)
+ results['results'] = ['Sharpe','Monthly Sharpe','Mean Worst 10 Days Drawdown']
- return (sharpe, monthly_sharpe, worst_drawdown)
+ return results.set_index('results')
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)
+ ## look cross series - 3y to 5y
+ returns1 = df.xs(['5yr', index], 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]
+ for ind in list(range(on_the_run-2, on_the_run+1)):
+ price_diff[ind] = returns1[ind] - 1.6* 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()
+ plt.plot(monthly_returns_cross_series)
def forward_loss():
diff --git a/python/notebooks/Curve Trades.ipynb b/python/notebooks/Curve Trades.ipynb
index 7174706d..7b0ec3bb 100644
--- a/python/notebooks/Curve Trades.ipynb
+++ b/python/notebooks/Curve Trades.ipynb
@@ -6,14 +6,15 @@
"metadata": {},
"outputs": [],
"source": [
- "%matplotlib inline\n",
+ "#%matplotlib nbagg\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"
+ "import matplotlib.pyplot as plt\n",
+ "import pandas as pd"
]
},
{
@@ -54,7 +55,7 @@
"outputs": [],
"source": [
"#Theta per unit duration\n",
- "ct.theta_matrix(index, on_the_run)"
+ "ct.theta_matrix_by_series(index, on_the_run)"
]
},
{
@@ -81,6 +82,34 @@
"execution_count": null,
"metadata": {},
"outputs": [],
+ "source": [
+ "ct.cross_series_curve()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "theta_ratio_within_series = ct.theta_ratio_within_series()\n",
+ "ct.curve_3_5_10(theta_ratio_within_series)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "ct.curve_5_10(theta_ratio_within_series)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
"source": []
}
],