diff options
| -rw-r--r-- | python/analytics/index.py | 33 | ||||
| -rw-r--r-- | python/analytics/option.py | 6 |
2 files changed, 28 insertions, 11 deletions
diff --git a/python/analytics/index.py b/python/analytics/index.py index 44c0102f..6a0b0ad5 100644 --- a/python/analytics/index.py +++ b/python/analytics/index.py @@ -16,6 +16,8 @@ from pyisda.curve import SpreadCurve from .utils import previous_twentieth from yieldcurve import YC, ql_to_jp, roll_yc, rate_helpers +from blinker import signal + engine = dbengine('serenitasdb') def g(index, spread, exercise_date, forward_yc=None, pv=0.): @@ -148,6 +150,7 @@ class Index(object): if self.spread is None or s != self.spread: self._spread = s * 1e-4 self._update() + signal(self.name).send("spread was updated") @property def flat_hazard(self): @@ -161,7 +164,7 @@ class Index(object): @pv.setter def pv(self, val): - self._pv = val / (self.notional * self.factor) + self._pv = val / (self.notional * self.factor) * self._direction self._clean_pv = self._pv + self._accrued * self.fixed_rate * 1e-4 self.price = 100 * (1 - self._clean_pv) @@ -202,6 +205,7 @@ class Index(object): self._spread = self._clean_pv / (self._risky_annuity - self._accrued) \ + self.fixed_rate * 1e-4 self._price = val + signal(self.name).send("price was updated") @property def ref(self): @@ -292,6 +296,7 @@ class Index(object): self._value_date = pd.Timestamp(self._trade_date) + 3* BDay() if self._spread is not None: self._update() + signal(self.name).send("trade_date was updated") def set_original_pv(self): self._original_clean_pv = self._clean_pv @@ -318,7 +323,7 @@ class Index(object): params = (index.upper(), series, tenor) elif all([redcode, maturity]): sql_str = "SELECT index, series, indexfactor, lastdate, maturity, " \ - "coupon, issue_date " \ + "coupon, issue_date, tenor " \ "FROM index_desc WHERE redindexcode=%s AND maturity=%s" params = (redcode, maturity) else: @@ -329,6 +334,8 @@ class Index(object): params=params) maturity = df.maturity[0] coupon = df.coupon[0] + if tenor is None: + tenor = df.tenor[0] index_type = index.upper() if index else df.loc[0,'index'] series = series if series else df.series[0] df.loc[df.lastdate.isnull(),'lastdate'] = maturity @@ -343,10 +350,12 @@ class Index(object): index_type=="HY", df.issue_date[0]) instance.factor = factor instance.direction = "Buyer" - instance.name = "MARKIT CDX.NA.{}.{} {:%m/%y} ".format( - index_type, - series, - maturity) + tenor = tenor.upper() + if tenor.endswith("R"): + tenor = tenor[:-1] + instance.name = "CDX {} CDSI S{} {}".format(index_type, + series, + tenor) if index_type in ["IG", "HY"]: instance.currency = "USD" else: @@ -434,12 +443,15 @@ class Index(object): class ForwardIndex(object): __slots__ = ['index', 'forward_date', 'exercise_date_settle', 'df', - '_forward_annuity', '_forward_pv', '_forward_spread'] + '_forward_annuity', '_forward_pv', '_forward_spread', + '_debug', '__weakref__'] def __init__(self, index, forward_date): self.index = index self.forward_date = forward_date self.exercise_date_settle = pd.Timestamp(forward_date) + 3* BDay() self.df = index._yc.discount_factor(self.exercise_date_settle) + signal(index.name).connect(self._update) + self._debug = False self._update() @classmethod @@ -467,12 +479,13 @@ class ForwardIndex(object): @ref.setter def ref(self, val): self.index.ref = val - self._update() def __hash__(self): - return hash(tuple(getattr(self, k) for k in ForwardIndex.__slots__)) + return hash(tuple(getattr(self, k) for k in ForwardIndex.__slots__ if k != '__weakref__')) - def _update(self): + def _update(self, *args): + if self._debug and args is not None: + print(args[0]) if self.index._sc is not None: step_in_date = self.forward_date + datetime.timedelta(days=1) a = self.index._fee_leg.pv(self.index.trade_date, step_in_date, diff --git a/python/analytics/option.py b/python/analytics/option.py index ef2c43bb..4cf52140 100644 --- a/python/analytics/option.py +++ b/python/analytics/option.py @@ -342,6 +342,9 @@ class BlackSwaption(ForwardIndex): ] return "\n".join(s) + def __str__(self): + return "{} at 0x{:02x}".format(type(self), id(self)) + class Swaption(BlackSwaption): __slots__ = ["_cache", "_Z", "_w"] def __init__(self, index, exercise_date, strike, option_type="payer", @@ -414,7 +417,8 @@ class Swaption(BlackSwaption): for k in super().__slots__: setattr(black_self, k, getattr(self, k)) for k in ForwardIndex.__slots__: - setattr(black_self, k, getattr(self, k)) + if k != '__weakref__': + setattr(black_self, k, getattr(self, k)) black_self.pv = val self.sigma = black_self.sigma |
