import analytics import numpy as np from analytics import on_the_run, CreditIndex, BlackSwaptionVolSurface from copy import deepcopy from .tranches import get_tranche_portfolio from .swaptions import get_swaption_portfolio from .bonds import subprime_risk, clo_risk, crt_risk from .indices import get_index_portfolio from utils.db import dbconn, dbengine from pandas.tseries.offsets import BDay def hy_equiv_trade(value_date, notional): return CreditIndex("HY", on_the_run("HY", value_date), "5yr", value_date, notional) def build_portfolio(position_date, value_date=None, fund="SERCGMAST"): """ Output two portfolios: 1) All synthetic + curve with just delta-proxy + dummy index as cash bonds proxy (portf) 2) All synthetic (portf_syn) """ analytics._local = False if value_date is None: value_date = position_date analytics.init_ontr(value_date) conn = dbconn("dawndb") portf = get_tranche_portfolio(position_date, conn, False, fund) swaption_portf = get_swaption_portfolio(position_date, conn, fund) portf += swaption_portf syn_portf = deepcopy(portf) curve_portf = get_index_portfolio( position_date, conn, fund, include_strategies="%CURVE" ) nocurve_portf = get_index_portfolio( position_date, conn, fund, exclude_strategies="%CURVE" ) portf += nocurve_portf curve_portf.value_date = value_date curve_portf.mark() portf.add_trade( hy_equiv_trade(value_date, curve_portf.hy_equiv), ("curve_trades", "curve_trades"), ) syn_portf += curve_portf + nocurve_portf # get bond risks: rmbs_pos = subprime_risk(position_date, conn, dbengine("rmbs_model"), fund=fund) crt_pos = crt_risk(position_date, conn, dbengine("crt"), fund=fund) portf.add_trade( hy_equiv_trade(value_date, -rmbs_pos.get("hy_equiv", np.zeros(1)).sum()), ("rmbs_bonds", "rmbs_bonds"), ) portf.add_trade( hy_equiv_trade(value_date, -crt_pos.get("hy_equiv", np.zeros(1)).sum()), ("crt_bonds", "crt_bonds"), ) with dbconn("etdb") as etconn: clo_pos = clo_risk(position_date, conn, etconn, fund=fund) if clo_pos is not None: portf.add_trade( hy_equiv_trade(value_date, -clo_pos["hy_equiv"].sum()), ("clo_bonds", "clo_bonds"), ) for p in [portf, syn_portf]: p.value_date = value_date p.mark(interp_method="bivariate_linear") p.reset_pv() return portf, syn_portf def generate_vol_surface(portf, try_days_back=5): vol_surface = {} for trade in portf.swaptions: try: vs = BlackSwaptionVolSurface( trade.index.index_type, trade.index.series, value_date=portf.value_date, interp_method="bivariate_linear", ) except: vs = BlackSwaptionVolSurface( trade.index.index_type, trade.index.series, value_date=portf.value_date - BDay(try_days_back), interp_method="bivariate_linear", ) vol_surface[ (trade.index.index_type, trade.index.series, trade.option_type) ] = vs[vs.list(source="MS", option_type=trade.option_type)[-1]] return vol_surface