diff options
Diffstat (limited to 'python/exploration/curve_trades.py')
| -rw-r--r-- | python/exploration/curve_trades.py | 78 |
1 files changed, 60 insertions, 18 deletions
diff --git a/python/exploration/curve_trades.py b/python/exploration/curve_trades.py index 89149306..ce134b6f 100644 --- a/python/exploration/curve_trades.py +++ b/python/exploration/curve_trades.py @@ -1,4 +1,5 @@ from index_data import get_index_quotes, index_returns, _serenitas_engine +from analytics import Index, Portfolio import pandas as pd import math import statsmodels.formula.api as smf @@ -65,12 +66,13 @@ def curve_3_5_10(df): df['3_5_10'] = - df.theta2['3yr'] / df.duration_ratio_to_5yr['3yr'] \ + 2 * df.theta2['5yr'] \ - df.theta2['10yr'] / df.duration_ratio_to_5yr['10yr'] - df['3_5_10'].dropna().unstack(-1).plot() + df1['3_5_10'].dropna().unstack(-1).plot() def curve_5_10(df): #buy sell 5y, buy 10y - df['5_10'] = df.theta2['5yr'] - df.theta2['10yr'] / df.duration_ratio_to_5yr['10yr'] - df['5_10'].dropna().unstack(-1).plot() + df1 = pd.DataFrame() + df1['5_10'] = df.theta2['5yr'] - df.theta2['10yr'] / df.duration_ratio_to_5yr['10yr'] + df1['5_10'].dropna().unstack(-1).plot() def on_the_run_theta(index='IG', rolling=6): otr = on_the_run(index) @@ -144,28 +146,68 @@ def forward_loss(index='IG'): df['fwd_loss_rate'] = df.indexel.diff(2)/df.duration.diff(2) def read_IG_curve_pos(): - from analytics import Index, Portfolio trade_1 = Index.from_tradeid(886) trade_2 = Index.from_tradeid(885) - trade_3 = Index.from_tradeid(884) - portf = Portfolio([trade_1, trade_2, trade_3]) + trade_1.notional = 132_900_000 + trade_2.notional = 75_000_000 + return Portfolio([trade_1, trade_2]) -def curve_model(): +def curve_model(tenor_1='5yr', tenor_2='10yr'): #OLS model df = ratio_within_series(param='closespread') df = df.groupby(level='date').last() - - df = pd.concat([df.duration['5yr'], df.closespread['5yr'], df.closespread_ratio_to_5yr['10yr']], - axis=1, keys=['duration', 'closespread', 'ratio']) - results = smf.ols('np.log(ratio) ~ np.log(duration) + np.log(closespread)', data=df).fit() + df = pd.concat([df.duration[tenor_1],df.closespread[tenor_1], df.closespread_ratio_to_5yr[tenor_2]], axis = 1, keys=['duration', 'closespread', 'ratio']) + ols_model = smf.ols('np.log(ratio) ~ np.log(duration) + np.log(closespread)', data=df).fit() df['predicted'] = np.exp(results.predict()) - results.summary() - prstd_ols, iv_l, iv_u = wls_prediction_std(results) - df['up_2_stdev'] = np.exp(iv_u) - df['down_2_stdev'] = np.exp(iv_l) + return df, ols_model + +def curve_model_results(df, model): - #dr/dspread = exp(k) * spread_coeff * duration ^ dur_coeff * spread ^ (spread_coeff-1) - df['dr_dspread'] = np.exp(results.params[0]) * results.params[2] * df.duration ** results.params[1] * df.closespread ** (results.params[2] -1) - df['beta'] = df.dr_dspread * df.closespread + prstd_ols, df['down_2_stdev'], df['up_2_stdev'] = wls_prediction_std(model) + #dr/dspread = exp(k) + spread_coeff * duration ^ dur_coeff * spread ^ (spread_coeff-1) + df['down_2_stdev'] = np.exp(df['down_2_stdev']) + df['up_2_stdev'] = np.exp(df['up_2_stdev']) + df['predicted'] = np.exp(model.predict()) + df['dr_dspread'] = np.exp(model.params[0]) * model.params[2] * df.duration ** model.params[1] * df.closespread ** (model.params[2] -1) return df + +def curve_var(): + portf = read_IG_curve_pos() + df = curve_model_results(curve_model()[0], curve_model()[1]) + portf.trade_date = pd.datetime.today() - pd.tseries.offsets.BDay(1) + portf.mark() + portf_orig_pv = portf.pv + portf.indices[1].spread = portf.indices[0].spread * df.ratio.quantile(.05) + stress_1 = portf_orig_pv - portf.pv + portf.indices[1].spread = portf.indices[0].spread * df.predicted[-1] + stress_2 = portf_orig_pv - portf.pv + portf.indices[1].spread = portf.indices[0].spread * df.down_2_stdev[-1] + stress_3 = portf_orig_pv - portf.pv + + return stress_1, stress_2, stress_3 + +def spread_fin_crisis(index='IG'): + on_the_run = on_the_run(index) + ## look at spreads + df = get_index_quotes(index, list(range(8,on_the_run+1)), tenor=['3yr', '5yr', '7yr', '10yr'], years = 20) + spreads = df.groupby(level=['date', 'tenor']).nth(-1)['closespread'].unstack(-1) + spreads.columns = [int(col[:-2]) for col in spreads.columns] + spreads = spreads.sort_index(1) + spreads_diff = spreads.diff(axis=1) + to_plot = pd.DataFrame() + to_plot['spread'] = spreads[5] + to_plot['3 - 5 diff'] = spreads_diff[5] + to_plot['5 - 10 diff'] = spreads_diff[7] + spreads_diff[10] + + fig = plt.figure() + ax = fig.add_subplot(111) + ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax + + width = 0.4 + to_plot['spread'].plot(color='red', ax=ax) + to_plot['5 - 10 diff'].plot(color='blue', ax=ax2) + to_plot['3 - 5 diff'].plot(color='green', ax=ax2) + plt.legend(bbox_to_anchor=(.5, -.1), ncol = 2) + + plt.show() |
