aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/option.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics/option.py')
-rw-r--r--python/analytics/option.py44
1 files changed, 29 insertions, 15 deletions
diff --git a/python/analytics/option.py b/python/analytics/option.py
index 7be140a9..64b2187a 100644
--- a/python/analytics/option.py
+++ b/python/analytics/option.py
@@ -73,19 +73,20 @@ def ATMstrike(index, exercise_date):
class BlackSwaption(ForwardIndex):
"""Swaption class"""
+ __slots__ = ['_forward_yc', '_T', '_G', '_strike', 'option_type',
+ 'notional', 'sigma', '_original_pv', '_direction']
+
def __init__(self, index, exercise_date, strike, option_type="payer",
direction="Long"):
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 = strike
self.option_type = option_type.lower()
- self._Z, self._w = GHquad(50)
self.notional = 1
self.sigma = None
- self._cache = {}
- self._direction = 1
+ self._original_pv = None
+ self.direction = direction
@classmethod
def from_tradeid(cls, trade_id):
@@ -96,22 +97,23 @@ class BlackSwaption(ForwardIndex):
return ValueError("trade_id doesn't exist")
index = Index.from_name(redcode=rec.security_id, maturity=rec.maturity, trade_date=rec.trade_date)
index.spread = 62
- instance = cls(index, rec.expiration_date, rec.strike, rec.swaption_type.lower())
+ instance = cls(index, rec.expiration_date, rec.strike, rec.swaption_type.lower(),
+ direction="Long" if rec.buysell else "Short")
instance.notional = rec.notional
instance.pv = rec.price * 1e-2 * rec.notional
- instance.direction = "Long" if rec.buysell else "Short"
+ instance._original_pv = rec.price * 1e-2 * rec.notional
return instance
@property
def exercise_date(self):
- return self._exercise_date
+ return self.forward_date
@exercise_date.setter
def exercise_date(self, d):
- self._exercise_date = d
+ self.forward_date = d
ForwardIndex.__init__(self, self.index, d)
self._forward_yc = roll_yc(self.index._yc, d)
- self._G = g(self.index, self.strike, self.exercise_date, self._forward_yc)
+ self._G = g(self.index, self.strike, d, self._forward_yc)
@property
def strike(self):
@@ -163,11 +165,11 @@ class BlackSwaption(ForwardIndex):
def intrinsic_value(self):
V = self.df * (self.forward_pv - self._G)
intrinsic = max(V, 0) if self.option_type == "payer" else max(-V, 0)
- return self._direction * intrinsic
+ return self._direction * intrinsic * self.notional
def __hash__(self):
- return hash(dumps([v for k, v in self.__dict__.items() if k not in
- ['_cache', '_Z', '_w']], protocol=pickle.HIGHEST_PROTOCOL))
+ return hash((super().__hash__(),
+ tuple(getattr(self, k) for k in BlackSwaption.__slots__[:-1])))
@property
def pv(self):
@@ -328,6 +330,16 @@ class BlackSwaption(ForwardIndex):
return "\n".join(s)
class Swaption(BlackSwaption):
+ __slots__ = ["_cache", "_Z", "_w"]
+ def __init__(self, index, exercise_date, strike, option_type="payer",
+ direction="Long"):
+ super().__init__(index, exercise_date, strike, option_type, direction)
+ self._Z, self._w = GHquad(50)
+ self._cache = {}
+
+ def __hash__(self):
+ return super().__hash__()
+
@property
@memoize
def pv(self):
@@ -386,8 +398,10 @@ class Swaption(BlackSwaption):
def __setpv_black(self, val):
black_self = BlackSwaption.__new__(BlackSwaption)
- black_self.__dict__ = vars(self).copy()
- black_self._cache = {}
+ for k in super().__slots__:
+ setattr(black_self, k, getattr(self, k))
+ for k in ForwardIndex.__slots__:
+ setattr(black_self, k, getattr(self, k))
black_self.pv = val
self.sigma = black_self.sigma
@@ -413,7 +427,7 @@ class VolatilitySurface(ForwardIndex):
"ORDER BY quotedate DESC",
engine,
parse_dates = ['quotedate', 'expiry'],
- params=(trade_date, index_type, series))
+ params=(trade_date, index_type.upper(), series))
self._quotes['quotedate'] = (self._quotes['quotedate'].
dt.tz_convert('America/New_York').
dt.tz_localize(None))