aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/exploration/curve_trades.py57
-rw-r--r--python/notebooks/Curve Trades.ipynb34
2 files changed, 87 insertions, 4 deletions
diff --git a/python/exploration/curve_trades.py b/python/exploration/curve_trades.py
index 310cc9a6..154d2595 100644
--- a/python/exploration/curve_trades.py
+++ b/python/exploration/curve_trades.py
@@ -1,15 +1,19 @@
-from analytics.index_data import get_index_quotes, index_returns, _serenitas_engine
+from analytics.index_data import get_index_quotes, index_returns
+from db import dbengine
from analytics import Index, Portfolio
import pandas as pd
import math
import statsmodels.formula.api as smf
+import analytics.basket_index as bkt
import numpy as np
+import matplotlib.pyplot as plt
+
from statsmodels.sandbox.regression.predstd import wls_prediction_std
-import matplotlib.pyplot as plt
+_engine = dbengine('serenitasdb')
def on_the_run(index):
- r = _serenitas_engine.execute("SELECT max(series) FROM index_version WHERE index=%s",
+ r = _engine.execute("SELECT max(series) FROM index_version WHERE index=%s",
(index,))
series, = r.fetchone()
return series
@@ -196,3 +200,50 @@ def spread_fin_crisis(index='IG'):
plt.legend(bbox_to_anchor=(.5, -.1), ncol = 2)
plt.show()
+
+def forward_spread(index = 'IG', series = None):
+
+ if series is None:
+ series = on_the_run(index = index)
+ tenors = ['3yr', '5yr', '7yr', '10yr']
+ b_index = bkt.MarkitBasketIndex(index, series, tenors)
+ b_index.tweak()
+
+ f_spread = {}
+ date_range = pd.bdate_range(pd.datetime.today(), max(b_index.maturities), freq='M')
+ for d in date_range.date:
+ b_index.trade_date = d
+ f_spread[d] = b_index.spread()
+
+ return pd.DataFrame(f_spread).transpose()
+
+def spot_forward(index = 'IG', series = None):
+
+ if series is None:
+ series = on_the_run(index = index)
+ tenors = ['3yr', '5yr', '7yr', '10yr']
+ b_index = bkt.MarkitBasketIndex(index, series, tenors)
+ b_index.tweak()
+
+ spread = b_index.spread()
+ new_maturity = [(d - pd.DateOffset(years=1)).date() for d in b_index.maturities]
+ new_duration = {}
+ for i, m in enumerate(new_maturity):
+ new_duration[b_index.index_desc.tenor[i]] = b_index.duration(m)
+ dur_df = pd.Series(new_duration)
+
+ one_yr = (b_index.trade_date + pd.DateOffset(years=1)).date()
+ one_yr_pv = b_index.pv(one_yr, coupon = b_index.index_desc.coupon[0])
+ one_yr_dur = b_index.duration((b_index.trade_date + pd.DateOffset(years=1)).date())
+ new_spread = ((b_index.pv() - b_index.theta() + b_index.index_desc.coupon[0])/ dur_df + b_index.index_desc.coupon[0])* 1e4
+
+ df = pd.DataFrame(dict(today_spread=spread, spread_in_one_year=new_spread))
+ df.index.name = 'tenor'
+ df = df.reset_index().merge(b_index.index_desc.tenor.reset_index(), on='tenor')
+ df.loc[-2] = ['0yr', 0, 0, b_index.trade_date]
+ df.loc[-1] = ['1yr', 0, (one_yr_pv/one_yr_dur + b_index.index_desc.coupon[0])*1e4, one_yr]
+
+ df['maturity'] = pd.to_datetime(df['maturity'])
+ df = df.sort_values('maturity')
+
+ return df.set_index('maturity') \ No newline at end of file
diff --git a/python/notebooks/Curve Trades.ipynb b/python/notebooks/Curve Trades.ipynb
index 20912cb1..f0027e40 100644
--- a/python/notebooks/Curve Trades.ipynb
+++ b/python/notebooks/Curve Trades.ipynb
@@ -164,6 +164,15 @@
"metadata": {},
"outputs": [],
"source": [
+ "model_results"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
"results\n",
"#first number: at 5% flatness\n",
"#second number: at predicted curve shape\n",
@@ -175,6 +184,29 @@
"execution_count": null,
"metadata": {},
"outputs": [],
+ "source": [
+ "df = ct.forward_spread(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.plot()\n",
+ "plt.ylabel('spread')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
"source": []
}
],
@@ -194,7 +226,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.3"
+ "version": "3.6.4"
}
},
"nbformat": 4,