aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/risk/bonds.py29
1 files changed, 12 insertions, 17 deletions
diff --git a/python/risk/bonds.py b/python/risk/bonds.py
index 2a8b28c5..28d823cc 100644
--- a/python/risk/bonds.py
+++ b/python/risk/bonds.py
@@ -6,7 +6,7 @@ import datetime
from enum import Enum, auto
from serenitas.analytics.yieldcurve import YC
from serenitas.analytics.index import CreditIndex
-from serenitas.analytics.index_data import on_the_run
+from serenitas.analytics.base import Trade
class AssetClass(Enum):
@@ -228,8 +228,7 @@ def get_portfolio(date, conn, asset_class: AssetClass, fund="SERCGMAST"):
def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
- hy_ontr = CreditIndex("HY", on_the_run("HY", date), "5yr", value_date=date)
- hy_ontr.mark()
+ Trade.init_ontr(date)
yc = YC(evaluation_date=date)
df = get_portfolio(date, dawn_conn, AssetClass.CRT, fund)
scen = {
@@ -244,18 +243,23 @@ def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
if scen_type is None and date < d:
scen_type = s
sql_string = (
- "SELECT a.* FROM crt.priced_at_market a "
+ "SELECT a.*, be.* FROM crt.priced_at_market a "
"JOIN (SELECT cusip, MAX(timestamp) timestamp "
"FROM crt.priced_at_market where timestamp between %s "
"AND date_add(%s, INTERVAL 1 DAY) "
"and model_des = %s GROUP BY cusip) b "
- "ON a.cusip= b.cusip AND a.timestamp = b.timestamp;"
+ "using (cusip, timestamp) "
+ "LEFT JOIN map_cusip using (cusip) "
+ "LEFT JOIN bond_types USING (bond) "
+ "LEFT JOIN (select bond_type, value from crt.beta_estimates "
+ "where date = (SELECT MAX(date) FROM crt.beta_estimates where date <= %s)) be "
+ "using (bond_type)"
)
df_model = pd.read_sql_query(
sql_string,
crt_engine,
"cusip",
- params=(date - datetime.timedelta(days=15), date, scen_type),
+ params=(date - datetime.timedelta(days=15), date, scen_type, date),
)
if any(~df_model["delta.ir"].isna()):
df_model = df_model[~df_model["delta.ir"].isna()]
@@ -269,22 +273,13 @@ def crt_risk(date, dawn_conn, crt_engine, fund="SERCGMAST"):
curr_ntl=df.notional * df.factor,
hy_equiv=(
df.modDur
- / hy_ontr.risky_annuity
- * Trade._beta["CRT"]
+ / Trade._ontr["HY"].risky_annuity
+ * df.value
* df.notional
* df.factor
),
)
df["bond_yield"] = df.swap_rate + df.DM / 10000
- delta = pd.read_sql_query(
- "SELECT distinct on (strategy) "
- "strategy, beta_crt from beta_crt WHERE "
- "date <= %s ORDER BY strategy, date desc",
- dawn_conn,
- index_col="strategy",
- params=(date,),
- )
- df.hy_equiv *= df["strategy"].replace(delta.beta_crt.to_dict())
return df