aboutsummaryrefslogtreecommitdiffstats
path: root/python/risk/bonds.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/risk/bonds.py')
-rw-r--r--python/risk/bonds.py43
1 files changed, 34 insertions, 9 deletions
diff --git a/python/risk/bonds.py b/python/risk/bonds.py
index b15ed2bf..5b104091 100644
--- a/python/risk/bonds.py
+++ b/python/risk/bonds.py
@@ -155,6 +155,7 @@ def subprime_risk(pos_date, conn, engine, model_date=None, fund="SERCGMAST"):
),
date=pd.Timestamp(pos_date),
)
+ df_calc = df_calc[(df_calc.usd_market_value != 0)]
df_calc.bond_yield += (
np.log(df_calc.pv1 * df_calc.curr_ntl / df_calc.local_market_value)
/ df_calc.modDur
@@ -232,6 +233,7 @@ def get_portfolio(date, conn, asset_class: AssetClass, fund="SERCGMAST"):
def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
analytics.init_ontr(date)
+ yc = YC(evaluation_date=date)
df = get_portfolio(date, dawn_conn, AssetClass.CRT, fund)
scen = {
datetime.date(2019, 5, 31): "base",
@@ -251,7 +253,9 @@ def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
)
with crt_engine.connect() as c:
r = c.execute(sql_string, (scen_type, date - datetime.timedelta(days=15), date))
- (model_date,) = r.fetchone()
+ model_date = r.fetchone()
+ if model_date:
+ model_date = model_date[0]
df_model = pd.read_sql_query(
"SELECT * from priced_at_market WHERE timestamp = %s",
crt_engine,
@@ -261,13 +265,22 @@ def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
if any(~df_model["delta.ir"].isna()):
df_model = df_model[~df_model["delta.ir"].isna()]
df = df.join(df_model)
- df["curr_ntl"] = df.notional * df.factor
- df["hy_equiv"] = (
- df.duration_FW
- / analytics._ontr["HY"].risky_annuity
- * analytics._beta["CRT"]
- * df.curr_ntl
+ df.rename(columns={"duration_FW": "modDur"}, inplace=True)
+ df = df[(df.notional != 0)]
+ df = df.assign(
+ bond_yield=df.modDur.apply(
+ lambda x: x if np.isnan(x) else float(yc.zero_rate(x))
+ ),
+ curr_ntl=df.notional * df.factor,
+ hy_equiv=(
+ df.modDur
+ / analytics._ontr["HY"].risky_annuity
+ * analytics._beta["CRT"]
+ * df.notional
+ * df.factor
+ ),
)
+ df.bond_yield += df.DM / 10000
delta = pd.read_sql_query(
"SELECT distinct on (strategy) "
"strategy, beta_crt from beta_crt WHERE "
@@ -281,6 +294,7 @@ def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
def clo_risk(date, dawn_conn, et_conn, fund="SERCGMAST"):
+ yc = YC(evaluation_date=date)
df = get_portfolio(date, dawn_conn, AssetClass.CLO, fund)
if df.empty:
return None
@@ -291,6 +305,17 @@ def clo_risk(date, dawn_conn, et_conn, fund="SERCGMAST"):
)
model.index = df.index
df = df.join(model, lsuffix="mark")
- df["curr_ntl"] = df["notional"] * df["factor"]
- df["hy_equiv"] = df["curr_ntl"] * df["delta"]
+ df.rename(columns={"duration": "modDur"}, inplace=True)
+ df = df.assign(
+ bond_yield=df.modDur.apply(
+ lambda x: x if np.isnan(x) else float(yc.zero_rate(x))
+ ),
+ curr_ntl=df.notional * df.factor,
+ hy_equiv=(df.delta * df.notional * df.factor),
+ )
+ df.bond_yield += (
+ np.log(df.curr_ntl / df.local_market_value) / df.modDur
+ + df.curr_cpn / 100
+ - float(yc.zero_rate(0.25))
+ ).clip(upper=1.0)
return df