aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/curve_trades.py22
-rw-r--r--python/notebooks/Curve Trades.ipynb4
2 files changed, 8 insertions, 18 deletions
diff --git a/python/analytics/curve_trades.py b/python/analytics/curve_trades.py
index b6c17436..98361cad 100644
--- a/python/analytics/curve_trades.py
+++ b/python/analytics/curve_trades.py
@@ -306,12 +306,7 @@ def pos_pnl_abs(portf, value_date, index='IG', rolling=6, years=3):
lookup_table = pd.read_sql_query(sql_string, serenitasdb, parse_dates=['maturity'], params=[index, series])
lookup_table['year_frac'] = (lookup_table.maturity - pd.to_datetime(value_date)).dt.days/365
- indices = []
- for i, ind in enumerate(portf.indices):
- indices.append(deepcopy(ind))
- portf_copy = Portfolio(indices)
- portf_copy.reset_pv()
-
+ portf_copy = deepcopy(portf)
portf_copy.reset_pv()
r = []
@@ -328,19 +323,14 @@ def curve_scen_table(portf):
'''
Runs PNL scenario on portf by shocking different points on the curve - off-the-runs shocks are duration linearly interpolated'''
- value_date = portf.value_date
-
- indices = []
- for i, ind in enumerate(portf.indices):
- indices.append(deepcopy(ind))
- portf_copy = Portfolio(indices)
+ portf_copy = deepcopy(portf)
portf_copy.reset_pv()
index = portf_copy.indices[0].index_type
series = on_the_run(index)
sql_string = "SELECT tenor, maturity FROM index_maturity where index = %s and series = %s"
lookup_table = pd.read_sql_query(sql_string, serenitasdb, parse_dates=['maturity'], params=[index, series])
- lookup_table['year_frac'] = (lookup_table.maturity - pd.to_datetime(value_date)).dt.days/365
+ lookup_table['year_frac'] = (lookup_table.maturity - pd.to_datetime(portf_copy.value_date)).dt.days/365
lookup_table = lookup_table.iloc[[0,1,3]]
year_frac = lookup_table.year_frac.tolist()
@@ -348,17 +338,17 @@ def curve_scen_table(portf):
shock = 10 #shocks in bps
r = []
- tenor_shock = pd.DataFrame(0, index=lookup_table['year_frac'], columns=['shock'])
for t_frac, t_ten in zip(year_frac, tenors):
+ tenor_shock = pd.DataFrame(0, index=lookup_table['year_frac'], columns=['shock'])
tenor_shock.loc[t_frac] = -shock
for w_frac, w_ten in zip(year_frac, tenors):
tenor_shock.loc[w_frac] = 0 if t_ten == w_ten else shock
#f is the shock amount interpolated based on tenor
f = interp1d(np.hstack([0, year_frac]), np.hstack([tenor_shock.shock.iloc[0], tenor_shock.shock]))
for i, ind in enumerate(portf_copy.indices):
- ind.spread = max(0, portf.indices[i].spread + f((ind.end_date - value_date).days/365))
+ ind.spread = max(0, portf.indices[i].spread + f((ind.end_date - portf_copy.value_date).days/365))
r.append([t_ten, w_ten] + [portf_copy.pv - portf.pv])
- tenor_shock.loc[w_frac] = 0
+ tenor_shock.loc[w_frac] = -shock if t_ten == w_ten else 0
tenor_shock.loc[t_frac] = 0
return pd.DataFrame.from_records(r, columns=['tighter', 'wider', 'pnl'])
diff --git a/python/notebooks/Curve Trades.ipynb b/python/notebooks/Curve Trades.ipynb
index 01669aae..a6ae0b7e 100644
--- a/python/notebooks/Curve Trades.ipynb
+++ b/python/notebooks/Curve Trades.ipynb
@@ -231,7 +231,7 @@
"curve_positions = ct.curve_pos(report_date, index)\n",
"df = ct.pos_pnl_abs(curve_positions, report_date)\n",
"navs = go.get_net_navs()\n",
- "df_plot = df.pnl/navs.loc['2018-03-31'].endbooknav"
+ "df_plot = df.pnl/navs.iloc[-1].endbooknav"
]
},
{
@@ -268,7 +268,7 @@
"outputs": [],
"source": [
"scen_table = ct.curve_scen_table(curve_positions)\n",
- "scen_table.pnl = scen_table.pnl/navs.loc['2018-03-31'].endbooknav *100\n",
+ "scen_table.pnl = scen_table.pnl/navs.iloc[-1].endbooknav *100\n",
"scen_table.pivot(index='tighter', columns='wider')"
]
},