aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/reto.py13
-rw-r--r--python/risk/portfolio.py50
2 files changed, 32 insertions, 31 deletions
diff --git a/python/reto.py b/python/reto.py
index ca1a3cea..6d9f163c 100644
--- a/python/reto.py
+++ b/python/reto.py
@@ -26,17 +26,6 @@ def parse_args():
return parser.parse_args()
-def build_vol_surface(portf):
- for i in range(1, 6):
- for source in ("BAML", "GS", "MS", "JPM"):
- try:
- vol_surface = generate_vol_surface(portf, i, source)
- except IndexError:
- pass
- else:
- return vol_surface
-
-
def gen_spreads(shock_date, fund):
Trade.init_ontr(shock_date)
ana._local = False
@@ -52,7 +41,7 @@ def gen_spreads(shock_date, fund):
],
)
portf, _ = build_portfolio(shock_date, shock_date, fund)
- vol_surface = build_vol_surface(portf)
+ vol_surface = generate_vol_surface(portf, try_days_back=10, source="BAML")
portf.reset_pv()
scens = run_portfolio_scenarios(
portf,
diff --git a/python/risk/portfolio.py b/python/risk/portfolio.py
index 88e10f26..ad3665d3 100644
--- a/python/risk/portfolio.py
+++ b/python/risk/portfolio.py
@@ -1,9 +1,11 @@
import serenitas.analytics
import numpy as np
+import logging
from serenitas.analytics.base import Trade
from serenitas.analytics.index_data import on_the_run
from serenitas.analytics.api import CreditIndex, BlackSwaptionVolSurface
+from serenitas.analytics.exceptions import MissingDataError
from copy import deepcopy
from .tranches import get_tranche_portfolio
from .swaptions import get_swaption_portfolio
@@ -14,6 +16,8 @@ from serenitas.utils.db2 import dbconn
from serenitas.utils.pool import dawn_pool
from pandas.tseries.offsets import BDay
+logger = logging.getLogger(__name__)
+
def hy_equiv_trade(value_date, notional):
return CreditIndex(
@@ -100,25 +104,33 @@ def build_portfolio(position_date, value_date=None, fund="SERCGMAST"):
def generate_vol_surface(portf, try_days_back=5, source="MS"):
-
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=source, option_type=trade.option_type)[-1]]
+ unique = {
+ (trade.index.index_type, trade.index.series, trade.option_type)
+ for trade in portf.swaptions
+ }
+
+ for (
+ index_type,
+ series,
+ option_type,
+ ) in unique:
+ i = 0
+ while i < try_days_back:
+ try:
+ vs = BlackSwaptionVolSurface(
+ index_type,
+ series,
+ value_date=portf.value_date - BDay(i),
+ interp_method="bivariate_linear",
+ )
+ except MissingDataError as e:
+ i += 1
+ logger.info(f"Trying {portf.value_date - BDay(i)}")
+ else:
+ break
+ vol_surface[(index_type, series, option_type)] = vs[
+ vs.list(source=source, option_type=option_type)[-1]
+ ]
return vol_surface