aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/analytics/basket_index.py3
-rw-r--r--python/thetas-durations.py118
2 files changed, 61 insertions, 60 deletions
diff --git a/python/analytics/basket_index.py b/python/analytics/basket_index.py
index ac65b25b..fd245a2a 100644
--- a/python/analytics/basket_index.py
+++ b/python/analytics/basket_index.py
@@ -407,4 +407,5 @@ if __name__ == "__main__":
WeekendsOnly(),
date_generation_rule=Rule.CDS2015,
)
- sp = ig28.survival_matrix(cds_schedule.to_npdates().view("int") + 134774)
+
+ sp = ig28.survival_matrix()
diff --git a/python/thetas-durations.py b/python/thetas-durations.py
index 3959354f..bf7d41a7 100644
--- a/python/thetas-durations.py
+++ b/python/thetas-durations.py
@@ -36,9 +36,8 @@ def get_legs(index, series, tenors):
contingent_legs[tenor] = (ContingentLeg(issue_date, maturity, True),
ContingentLeg(issue_date, maturity_short, True))
# number of seconds since epoch
- df.maturity = df.maturity.view(int) // int(86400 * 1e9)
# number of days between 1900-1-1 and epoch
- df.maturity += 134774
+ df.maturity = (df.maturity.values.view("M8[D]").astype(int) + 134774)
return fee_legs, contingent_legs, df
def index_pv(fl, cl, value_date, step_in_date, cash_settle_date, yc, sc, recovery):
@@ -46,63 +45,64 @@ def index_pv(fl, cl, value_date, step_in_date, cash_settle_date, yc, sc, recover
cl_pv = fl.pv(value_date, step_in_date, cash_settle_date, yc, sc, True)
return dl_pv - cl_pv
-conn = serenitas_engine.raw_connection()
-for index in ["IG", "HY", "EU", "XO"]:
- if index in ["HY", "XO"]:
- recoveries = np.full(len(tenors[index]), 0.3)
- else:
- recoveries = np.full(len(tenors[index]), 0.4)
- for series in range(18, 33):
- if index in ["EU", "XO"] and series == 32:
- continue
- fee_legs, contingent_legs, df = \
- get_legs(index, series, tenors[index])
+if __name__ == "__main__":
+ conn = serenitas_engine.raw_connection()
+ for index in ["IG", "HY", "EU", "XO"]:
+ if index in ["HY", "XO"]:
+ recoveries = np.full(len(tenors[index]), 0.3)
+ else:
+ recoveries = np.full(len(tenors[index]), 0.4)
+ for series in range(18, 33):
+ if index in ["EU", "XO"] and series == 32:
+ continue
+ fee_legs, contingent_legs, df = \
+ get_legs(index, series, tenors[index])
- index_quotes = pd.read_sql_query(
- "SELECT distinct on (date, tenor) id, date, tenor, version, close_price "
- "FROM index_quotes_pre "
- "LEFT JOIN index_risk USING (id) "
- "WHERE index=%s AND series=%s "
- "AND source='MKIT' AND duration is NULL AND tenor IN %s "
- "ORDER BY date, tenor, version desc",
- serenitas_engine,
- params=(index, series, tenors[index]),
- parse_dates=['date'],
- index_col='id')
- if index_quotes.empty:
- continue
- index_quotes.tenor = index_quotes.tenor.astype(tenor_t)
- index_quotes = index_quotes.sort_values('tenor')
- index_quotes['close_price'] = 1. - index_quotes['close_price'] / 100
- with conn.cursor() as c:
- for value_date, data in index_quotes.groupby('date'):
- yc = get_curve(value_date, "USD" if index in ["IG", "HY"] else "EUR")
- # right_index?? is it a bug?
- data = data.merge(df, on='tenor', right_index=True)
- step_in_date = value_date + datetime.timedelta(days=1)
- cash_settle_date = value_date + 3 * BDay()
- start_date = previous_twentieth(value_date)
- sc = SpreadCurve(value_date, yc, start_date,
- step_in_date, cash_settle_date,
- data.maturity.values, data.coupon.values,
- data.close_price.values,
- recoveries)
- for r in data[['coupon', 'tenor', 'close_price']].itertuples():
- fl, fl_short = fee_legs[r.tenor]
- cl, cl_short = contingent_legs[r.tenor]
+ index_quotes = pd.read_sql_query(
+ "SELECT distinct on (date, tenor) id, date, tenor, version, close_price "
+ "FROM index_quotes_pre "
+ "LEFT JOIN index_risk USING (id) "
+ "WHERE index=%s AND series=%s "
+ "AND source='MKIT' AND duration is NULL AND tenor IN %s "
+ "ORDER BY date, tenor, version desc",
+ serenitas_engine,
+ params=(index, series, tenors[index]),
+ parse_dates=['date'],
+ index_col='id')
+ if index_quotes.empty:
+ continue
+ index_quotes.tenor = index_quotes.tenor.astype(tenor_t)
+ index_quotes = index_quotes.sort_values('tenor')
+ index_quotes['close_price'] = 1. - index_quotes['close_price'] / 100
+ with conn.cursor() as c:
+ for value_date, data in index_quotes.groupby('date'):
+ yc = get_curve(value_date, "USD" if index in ["IG", "HY"] else "EUR")
+ # right_index?? is it a bug?
+ data = data.merge(df, on='tenor', right_index=True)
+ step_in_date = value_date + datetime.timedelta(days=1)
+ cash_settle_date = value_date + 3 * BDay()
+ start_date = previous_twentieth(value_date)
+ sc = SpreadCurve(value_date, yc, start_date,
+ step_in_date, cash_settle_date,
+ data.maturity.values, data.coupon.values,
+ data.close_price.values,
+ recoveries)
+ for r in data[['coupon', 'tenor', 'close_price']].itertuples():
+ fl, fl_short = fee_legs[r.tenor]
+ cl, cl_short = contingent_legs[r.tenor]
- duration = fl.pv(value_date, step_in_date,
- cash_settle_date, yc, sc,
- True)
- if cl_short.end_date <= value_date.date():
- theta = None
- else:
- pv = index_pv(fl_short, cl_short,
- value_date, step_in_date, cash_settle_date, yc, sc,
- recoveries[0])
- theta = r.close_price - pv + r.coupon
+ duration = fl.pv(value_date, step_in_date,
+ cash_settle_date, yc, sc,
+ True)
+ if cl_short.end_date <= value_date.date():
+ theta = None
+ else:
+ pv = index_pv(fl_short, cl_short,
+ value_date, step_in_date, cash_settle_date, yc, sc,
+ recoveries[0])
+ theta = r.close_price - pv + r.coupon
- c.execute("INSERT INTO index_risk VALUES(%s, %s, %s)",
- (r.Index, theta, duration))
- conn.commit()
-conn.close()
+ c.execute("INSERT INTO index_risk VALUES(%s, %s, %s)",
+ (r.Index, theta, duration))
+ conn.commit()
+ conn.close()