diff options
Diffstat (limited to 'python/risk/portfolio.py')
| -rw-r--r-- | python/risk/portfolio.py | 50 |
1 files changed, 31 insertions, 19 deletions
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 |
