aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/risk/tranches.py48
1 files changed, 40 insertions, 8 deletions
diff --git a/python/risk/tranches.py b/python/risk/tranches.py
index 4b13dc22..76284a36 100644
--- a/python/risk/tranches.py
+++ b/python/risk/tranches.py
@@ -6,22 +6,28 @@ logger = logging.getLogger(__name__)
def get_tranche_portfolio(date, conn, by_strat=False):
if by_strat:
sql_string = "SELECT * from list_tranche_positions_by_strat(%s)"
+ params = (date,)
else:
- sql_string = "SELECT dealid from list_tranche_positions(%s)"
+ sql_string = ("SELECT id from cds "
+ "WHERE orig_attach IS NOT NULL "
+ "AND (termination_date IS NULL OR termination_date > %s) "
+ "AND maturity > %s ORDER BY trade_date")
+ params = (date, date)
with conn.cursor() as c:
- c.execute(sql_string, (date,))
+ c.execute(sql_string, params)
if by_strat:
trade_ids = list(c)
else:
trade_ids = [dealid for dealid, in c]
if by_strat:
portf = Portfolio([DualCorrTranche(redcode=d['security_id'],
- maturity=d['maturity'],
- notional=d['notional'],
- tranche_running=d['fixed_rate']*100,
- attach=d['orig_attach'],
- detach=d['orig_detach'],
- corr_attach=None, corr_detach=None) for d in trade_ids])
+ maturity=d['maturity'],
+ notional=d['notional'],
+ tranche_running=d['fixed_rate']*100,
+ attach=d['orig_attach'],
+ detach=d['orig_detach'],
+ corr_attach=None, corr_detach=None)
+ for d in trade_ids])
portf.trade_ids = [(d["folder"],
f"{t.index_type} {t.series} {t.tenor} {t.attach}-{t.detach}")
for d, t in zip(trade_ids, portf.trades)]
@@ -31,3 +37,29 @@ def get_tranche_portfolio(date, conn, by_strat=False):
portf.value_date = date
portf.mark()
return portf
+
+def insert_tranche_marks(portf, conn):
+ cols = ["clean_nav", "accrued", "theta", "duration", "delta",
+ "upfront", "running", "index_refprice", "index_refspread",
+ "index_duration"]
+ update_str = ",".join(f"{c} = EXCLUDED.{c}" for c in cols)
+ sql_str = (f"INSERT INTO tranche_risk VALUES({['%s'] * 12}) "
+ " ON CONFLICT (date, tranche_id) DO UPDATE "
+ f"SET {update_str}")
+ print(sql_str)
+ with conn.cursor() as c:
+ for trade_id, trade in portf.items():
+ c.execute(sql_str, (d,
+ trade_id,
+ trade.clean_pv,
+ trace._accrued,
+ None,
+ trade.duration,
+ trade.upfront,
+ trade.tranche_upfront,
+ trade.tranche_running,
+ trade._indexpv().bond_price * 100,
+ trade._index.spread(),
+ trade._index.duration())
+ )
+ conn.commit()