diff options
Diffstat (limited to 'python/analytics/tranche_basket.py')
| -rw-r--r-- | python/analytics/tranche_basket.py | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py index bf80a8e8..66f2657b 100644 --- a/python/analytics/tranche_basket.py +++ b/python/analytics/tranche_basket.py @@ -1,7 +1,8 @@ from .basket_index import BasketIndex from .tranche_functions import ( credit_schedule, adjust_attachments, GHquad, BCloss_recov_dist, - BCloss_recov_trunc, tranche_cl, tranche_pl, tranche_pl_trunc, tranche_cl_trunc) + BCloss_recov_trunc, tranche_cl, tranche_pl, tranche_pl_trunc, + tranche_cl_trunc) from .index_data import get_tranche_quotes from .utils import memoize, build_table from collections import namedtuple @@ -27,13 +28,14 @@ class DualCorrTranche(): notional: float=10_000_000, value_date: pd.Timestamp=pd.Timestamp.today().normalize(), use_trunc=False): - self._index = BasketIndex(index_type, series, [tenor], value_date=value_date) + self._index = BasketIndex(index_type, series, [tenor], + value_date=value_date) def get_quotes(self, spread): maturity = self.maturities[0] return {maturity: - self._snacpv(spread * 1e-4, self.coupon(maturity), self.recovery, - 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 @@ -210,7 +212,7 @@ class DualCorrTranche(): # TODO: handle factor change days_accrued = (self.value_date - self._trade_date).days / 360 return self.notional * self._direction * (-self.pv + self._original_pv + - self.tranche_running/10000 * days_accrued) + self.tranche_running * 1e-4 * days_accrued) def __repr__(self): s = ["{}{} {} Tranche".format(self.index_type, self.series, self.tenor), @@ -254,8 +256,8 @@ class DualCorrTranche(): "WHERE a.detach = %s OR a.attach = %s ORDER BY a.attach") with dbconn('serenitasdb') as conn: with conn.cursor() as c: - c.execute(sql_string, (self.index_type, self.series, self.tenor, - self.value_date, + c.execute(sql_string, (self.index_type, self.series, + self.tenor, self.value_date, self.attach, self.attach)) if self.attach == 0: self.rho[1], = next(c) @@ -266,24 +268,25 @@ class DualCorrTranche(): @property def tranche_factor(self): - return (self.K[1] - self.K[0]) / (self.K_orig[1] - - self.K_orig[0]) * self._index.factor + return (self.K[1] - self.K[0]) / (self.K_orig[1] - self.K_orig[0]) * \ + self._index.factor @property def delta(self): calc = self._greek_calc() factor = self.tranche_factor / self._index.factor - return (calc['bp'][1] - calc['bp'][2]) / (calc['indexbp'][1] - - calc['indexbp'][2]) * factor * -self._direction + return (calc['bp'][1] - calc['bp'][2]) / \ + (calc['indexbp'][1] - calc['indexbp'][2]) * \ + factor * -self._direction @property def gamma(self): calc = self._greek_calc() factor = self.tranche_factor / self._index.factor - deltaplus = (calc['bp'][3] - calc['bp'][0]) / (calc['indexbp'][3] - - calc['indexbp'][0]) * factor - delta = (calc['bp'][1] - calc['bp'][2]) / (calc['indexbp'][1] - - calc['indexbp'][2]) * factor + deltaplus = (calc['bp'][3] - calc['bp'][0]) / \ + (calc['indexbp'][3] - calc['indexbp'][0]) * factor + delta = (calc['bp'][1] - calc['bp'][2]) / \ + (calc['indexbp'][1] - calc['indexbp'][2]) * factor return -(deltaplus - delta) / (calc['indexbp'][1] - calc['indexbp'][0]) / 100 def _greek_calc(self): @@ -384,8 +387,8 @@ class TrancheBasket(BasketIndex): return {self.maturity: 1 - refprice / 100} if refspread is not None: return {self.maturity: - self._snacpv(refspread * 1e-4, self.coupon(self.maturity), self.recovery, - self.maturity)} + self._snacpv(refspread * 1e-4, self.coupon(self.maturity), + self.recovery, self.maturity)} raise ValueError("ref is missing") @property |
