aboutsummaryrefslogtreecommitdiffstats
path: root/python/risk/indices.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/risk/indices.py')
-rw-r--r--python/risk/indices.py32
1 files changed, 18 insertions, 14 deletions
diff --git a/python/risk/indices.py b/python/risk/indices.py
index 3c074df7..183414ba 100644
--- a/python/risk/indices.py
+++ b/python/risk/indices.py
@@ -16,14 +16,17 @@ def get_index_portfolio(
**kwargs
):
sql_str = (
- "SELECT security_id AS redcode, notional, maturity "
+ "SELECT security_id AS redcode, sum(notional) AS notional, maturity "
"FROM list_cds_positions_by_strat(%s) "
)
params = (d,)
if strategies is not None:
- sql_str += "WHERE folder in %s"
+ if isinstance(strategies, tuple):
+ sql_str += "WHERE folder in %s"
+ else:
+ sql_str += "WHERE folder = %s"
params += (strategies,)
-
+ sql_str += "GROUP BY security_id, maturity"
with conn.cursor() as c:
c.execute(sql_str, params)
trades = [
@@ -36,15 +39,18 @@ def get_index_portfolio(
for rec in c
if rec.redcode not in exclude_redcode
]
- portf = Portfolio(trades)
- portf.mark()
+ portf = Portfolio(trades)
+ portf.mark()
return portf
def VaR(portf: Portfolio, quantile=0.05, years: int = 5, period="monthly"):
index_types = tuple(set(t.index_type for t in portf))
df = index_returns(
- index=index_types, years=years, tenor=["3yr", "5yr", "7yr", "10yr"]
+ index=index_types,
+ years=years,
+ end_date=portf.value_date,
+ tenor=["3yr", "5yr", "7yr", "10yr"],
)
df = df.reorder_levels(["date", "index", "series", "tenor"])
returns = df.spread_return.dropna().reset_index("series")
@@ -74,7 +80,7 @@ def VaR(portf: Portfolio, quantile=0.05, years: int = 5, period="monthly"):
shocks = g.reset_index("date", drop=True).stack(["tenor"])
shocks.name = "shocks"
portf.spread = spreads.spread * (1 + spreads.join(shocks).shocks).values
- r.append((k, portf.local_pnl))
+ r.append((k, portf.pnl))
pnl = pd.DataFrame.from_records(r, columns=["date", "pnl"], index=["date"])
if period == "daily":
return float(pnl.quantile(quantile))
@@ -90,14 +96,12 @@ def insert_curve_risk(d: datetime.date, conn: connection, strategies=("SER_IGCUR
"ON CONFLICT (date, strategy) DO UPDATE SET "
'"VaR"=excluded."VaR", currency=excluded.currency'
)
+ # add a portfolio with all strategies
+ strategies = (*strategies, strategies)
with conn.cursor() as c:
for strat in strategies:
- portf = get_index_portfolio(
- d, conn, (strat,), exclude_redcode=["2I65BYDU6"]
- )
+ portf = get_index_portfolio(d, conn, strat, exclude_redcode=["2I65BYDU6"])
var = VaR(portf, period="daily")
- c.execute(sql_str, (d, strat, var, "USD"))
- portf = get_index_portfolio(d, conn, strategies, exclude_redcode=["2I65BYDU6"])
- var = VaR(portf, period="daily")
- c.execute(sql_str, (d, "*", var, "USD"))
+ strat_name = "*" if len(strat) > 1 else strat
+ c.execute(sql_str, (d, strat_name, var, "USD"))
conn.commit()