diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/analytics/index.py | 49 | ||||
| -rw-r--r-- | python/analytics/option.py | 18 |
2 files changed, 38 insertions, 29 deletions
diff --git a/python/analytics/index.py b/python/analytics/index.py index ddb77abf..3d929cd0 100644 --- a/python/analytics/index.py +++ b/python/analytics/index.py @@ -35,7 +35,7 @@ def g(index, spread, exercise_date, forward_yc = None): class Index(object): """ minimal class to represent a credit index """ def __init__(self, start_date, end_date, recovery, fixed_rate, - notional = 10e6): + notional = 10e6, quote_is_price=False): """ start_date : :class:`datetime.date` index start_date (Could be issue date, or last imm date) @@ -61,6 +61,7 @@ class Index(object): self._spread = None self._price = None self.name = None + self._quote_is_price = quote_is_price @property def start_date(self): @@ -155,6 +156,20 @@ class Index(object): self._update() @property + def ref(self): + if self._quote_is_price: + return self.price + else: + return self.spread + + @ref.setter + def ref(self, val): + if self._quote_is_price: + self.price = val + else: + self.spread = val + + @property def DV01(self): old_pv = self.pv self.spread += 1 @@ -229,8 +244,8 @@ class Index(object): self._update() @classmethod - def from_name(cls, index, series, tenor, trade_date = datetime.date.today(), - notional = 10e6): + def from_name(cls, index, series, tenor, trade_date=datetime.date.today(), + notional=10e6): try: r = engine.execute("SELECT maturity, coupon FROM index_maturity " \ "WHERE index=%s AND series=%s AND tenor = %s", @@ -242,7 +257,8 @@ class Index(object): raise ValueError("Index not found") else: recovery = 0.4 if index.lower() == "ig" else 0.3 - instance = cls(trade_date, maturity, recovery, coupon) + instance = cls(trade_date, maturity, recovery, coupon, notional, + index=="HY") instance.name = "MARKIT CDX.NA.{}.{} {:%m/%y} ".format( index.upper(), series, @@ -309,14 +325,19 @@ class Index(object): class ForwardIndex(object): - def __init__(self, index, forward_date, ref_is_price = False): + def __init__(self, index, forward_date): self.index = index self.forward_date = forward_date self.exercise_date_settle = (pd.Timestamp(forward_date) + 3* BDay()).date() self.df = index._yc.discount_factor(self.exercise_date_settle) - self._ref_is_price = ref_is_price self._update() + @classmethod + def from_name(cls, index_type, series, tenor, forward_date, + trade_date=datetime.date.today(), notional=10e6): + index = Index.from_name(index_type, series, tenor, trade_date, notional) + return cls(index, forward_date, indextype == "HY") + @property def forward_annuity(self): return self._forward_annuity @@ -331,22 +352,12 @@ class ForwardIndex(object): @property def ref(self): - if ref_is_price: - return self.index.price - else: - return self.index.spread + return self.index.ref @ref.setter def ref(self, val): - if self._ref_is_price: - if self.index.price is None or \ - math.fabs(self.index.price - val) > 1e-6: - self.index.price = val - self._update() - else: - if self.index.spread is None or val != self.index.spread: - self.index.spread = val - self._update() + self.index.ref = val + self._update() def _update(self): if self.index._sc is not None: diff --git a/python/analytics/option.py b/python/analytics/option.py index e931d626..4fcc5023 100644 --- a/python/analytics/option.py +++ b/python/analytics/option.py @@ -48,7 +48,7 @@ def memoize(f): return v return cached_f -def ATMstrike(index, exercise_date, price=False): +def ATMstrike(index, exercise_date): """computes the at-the-money strike. Parameters @@ -60,9 +60,9 @@ def ATMstrike(index, exercise_date, price=False): price : bool, defaults to False If price is true return a strike price, returns a spread otherwise. """ - fi = ForwardIndex(index, exercise_date, price) + fi = ForwardIndex(index, exercise_date) fp = fi.forward_pv - if price: + if index._quote_is_price: return 100 * (1 - fp) else: closure = lambda S: g(index, S, exercise_date) - fp @@ -77,13 +77,11 @@ def ATMstrike(index, exercise_date, price=False): class Swaption(ForwardIndex): """Swaption class""" - def __init__(self, index, exercise_date, strike, - option_type="payer", strike_is_price=False): - ForwardIndex.__init__(self, index, exercise_date, strike_is_price) + def __init__(self, index, exercise_date, strike, option_type="payer"): + ForwardIndex.__init__(self, index, exercise_date) self._exercise_date = exercise_date self._forward_yc = roll_yc(index._yc, exercise_date) self._T = None - self._strike_is_price = strike_is_price self.strike = strike self.option_type = option_type.lower() self._Z, self._w = GHquad(50) @@ -104,14 +102,14 @@ class Swaption(ForwardIndex): @property def strike(self): - if self._strike_is_price: + if self.index._quote_is_price: return 100 * (1 - self._G) else: return self._strike @strike.setter def strike(self, K): - if self._strike_is_price: + if self.index._quote_is_price: self._G = (100 - K) / 100 # we compute the corresponding spread to the strike price def handle(S, index, forward_date, forward_yc): @@ -294,7 +292,7 @@ class Swaption(ForwardIndex): @property def breakeven(self): pv = self.pv / self.notional - if self._strike_is_price: + if self.index._quote_is_price: if self.option_type == "payer": return 100-(self._G + pv)*100 else: |
