diff options
Diffstat (limited to 'python/analytics')
| -rw-r--r-- | python/analytics/portfolio.py | 34 | ||||
| -rw-r--r-- | python/analytics/tranche_basket.py | 24 |
2 files changed, 41 insertions, 17 deletions
diff --git a/python/analytics/portfolio.py b/python/analytics/portfolio.py index 3b9e1a9a..eb932034 100644 --- a/python/analytics/portfolio.py +++ b/python/analytics/portfolio.py @@ -1,5 +1,6 @@ from .index import CreditIndex from .option import BlackSwaption +from .tranche_basket import DualCorrTranche from warnings import warn import pandas as pd import numpy as np @@ -23,7 +24,9 @@ def portf_repr(method): 'Theta': thousands, 'Vega': thousands, 'Vol': percent, - 'Ref': thousands}, + 'Ref': thousands, + 'Attach Rho': percent, + 'Detach Rho': percent}, 'index': False} if method == 'string': kwargs['line_width'] = 100 @@ -38,6 +41,7 @@ class Portfolio: self.trade_ids = trade_ids self.indices = [t for t in trades if isinstance(t, CreditIndex)] self.swaptions = [t for t in trades if isinstance(t, BlackSwaption)] + self.tranches = [t for t in trades if isinstance(t, DualCorrTranche)] value_dates = set(t.value_date for t in self.trades) self._keys = set([(index.index_type, index.series, index.tenor) for index in self.indices]) for swaption in self.swaptions: @@ -46,6 +50,13 @@ class Portfolio: if len(value_dates) >= 1: warn(f"not all instruments have the same trade date, picking {self._value_date}") + def add_trades(self, trades, trade_ids): + self.trades.append(trades) + self.trade_ids.append(trade_ids) + self.indices = [t for t in self.trades if isinstance(t, CreditIndex)] + self.swaptions = [t for t in self.trades if isinstance(t, BlackSwaption)] + self.tranches = [t for t in self.trades if isinstance(t, DualCorrTranche)] + def __iter__(self): for t in self.trades: yield t @@ -155,18 +166,27 @@ class Portfolio: def _todf(self): headers = ["Product", "Index", "Notional", "Ref", "Strike", "Direction", "Type", "Expiry", "Vol", "PV", "Delta", "Gamma", "Theta", - "Vega"] + "Vega", "attach", "detach", "Attach Rho", "Detach Rho"] rec = [] for t in self.trades: if isinstance(t, CreditIndex): name = f"{t.index_type}{t.series} {t.tenor}" - r = ("Index", name, - t.notional, t.ref, "N/A", t.direction, "N/A", "N/A", None, t.pv, 1., 0., t.theta, 0.) + r = ("Index", name, t.notional, t.ref, "N/A", + t.direction, "N/A", "N/A", None, t.pv, + 1., 0., t.theta, 0., + None, None, None, None) elif isinstance(t, BlackSwaption): name = f"{t.index.index_type}{t.index.series} {t.index.tenor}" - r = ("Swaption", name, - t.notional, t.ref, t.strike, t.direction, t.option_type, t.forward_date, t.sigma, t.pv, - t.delta, t.gamma, t.theta, t.vega) + r = ("Swaption", name, t.notional, t.ref, t.strike, + t.direction, t.option_type, t.forward_date, t.sigma, t.pv, + t.delta, t.gamma, t.theta, t.vega, + None, None, None, None) + elif isinstance(t, DualCorrTranche): + name = f"{t.index_type}{t.series} {t.tenor}" + r = ("Tranche", name, t.notional, None, None, + t.direction, None, None, None, t.upfront, + None, None, None, None, + t.attach, t.detach, t.rho[0], t.rho[1]) else: raise TypeError rec.append(r) diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py index ff46c74e..f09cfedb 100644 --- a/python/analytics/tranche_basket.py +++ b/python/analytics/tranche_basket.py @@ -27,15 +27,14 @@ class DualCorrTranche(): value_date: pd.Timestamp=pd.Timestamp.today().normalize()): self._index = BasketIndex(index_type, series, [tenor], value_date=value_date) - # def get_quotes(self, spread): - # index = self. - - # maturity = self._index.maturities[0] - # return {maturity: - # self._snacpv(spread * 1e-4, self.coupon(maturity), self.recovery, - # maturity)} - # # monkey patch _get_quotes (don't try it at home) - # BasketIndex._get_quotes = get_quotes + def get_quotes(self, spread): + maturity = self.maturities[0] + return {maturity: + self._snacpv(spread * 1e-4, self.coupon(maturity), self.recovery, + maturity)} + # monkey patch _get_quotes (don't try it at home) + # but it works...: replaces _get_quotes with this simpler one + BasketIndex._get_quotes = get_quotes self.maturity = self._index.maturities[0] self.index_type = index_type self.series = series @@ -195,7 +194,7 @@ class DualCorrTranche(): return "\n".join(s) def shock(self, params=['pnl'], *, spread_shock, corr_shock, **kwargs): - orig_spread, orig_rho = self._index.spread()[0], self.rho + orig_rho = self.rho r = [] actual_params = [p for p in params if hasattr(self, p)] for ss in spread_shock: @@ -213,6 +212,11 @@ class DualCorrTranche(): index=pd.MultiIndex.from_product([spread_shock, corr_shock], names=['spread_shock', 'corr_shock'])) + def mark(self, **args): + #mark BasketIndex + #mark correlation + pass + class TrancheBasket(BasketIndex): def __init__(self, index_type: str, series: int, tenor: str, *, |
