aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/option.py37
1 files changed, 22 insertions, 15 deletions
diff --git a/python/analytics/option.py b/python/analytics/option.py
index 13522a59..d7d9942f 100644
--- a/python/analytics/option.py
+++ b/python/analytics/option.py
@@ -370,10 +370,10 @@ class Swaption(BlackSwaption):
b *= eta
self.sigma = brentq(handle, a, b)
-def compute_vol(option, strike, mid, pv_type):
+def compute_vol(option, strike, mid):
option.strike = strike
try:
- setattr(option, pv_type, mid)
+ option.pv = mid
except ValueError as e:
return None
else:
@@ -385,7 +385,8 @@ class VolatilitySurface(ForwardIndex):
self._quotes = pd.read_sql_query(
"SELECT swaption_quotes.*, ref FROM swaption_quotes " \
"JOIN swaption_ref_quotes USING (quotedate, index, series, expiry)" \
- "WHERE quotedate::date = %s and index= %s and series = %s",
+ "WHERE quotedate::date = %s and index= %s and series = %s" \
+ "ORDER BY quotedate DESC",
engine,
parse_dates = ['quotedate', 'expiry'],
params=(trade_date, index_type, series))
@@ -396,16 +397,23 @@ class VolatilitySurface(ForwardIndex):
self._surfaces = {}
for k, g in self._quotes.groupby(['quotedate', 'quote_source']):
quotedate, source = k
- for surface_type in ["payer", "receiver", "payer_black", "receiver_black"]:
- self._surfaces[(quotedate, source, surface_type)] = None
+ for option_type in ["payer", "receiver"]:
+ for model in ["black", "precise"]:
+ self._surfaces[(quotedate, source, option_type, model)] = None
def vol(self, T, moneyness, surface_id):
"""computes the vol for a given moneyness and term."""
return self._surfaces[surface_id](T, moneyness)
- def list(self):
+ def list(self, source=None, option_type=None, model=None):
"""returns list of vol surfaces"""
- return list(self._surfaces.keys())
+ r = []
+ for k in self._surfaces.keys():
+ if (source is None or k[1] == source) and \
+ (option_type is None or k[2] == option_type) and \
+ (model is None or k[3] == model):
+ r.append(k)
+ return r
def __iter__(self):
return self._surfaces.items()
@@ -423,17 +431,15 @@ class VolatilitySurface(ForwardIndex):
def __getitem__(self, surface_id):
if self._surfaces[surface_id] is None:
- quotedate, source, surface_type = surface_id
+ quotedate, source, option_type, model = surface_id
quotes = self._quotes[(self._quotes.quotedate == quotedate) &
(self._quotes.quote_source == source)]
self._index.ref = quotes.ref.iat[0]
- if "_" in surface_type:
- option_type, _ = surface_type.split("_")
- pv_type = "pv_black"
+ if model == "black":
+ swaption_class = BlackSwaption
else:
- option_type = surface_type
- pv_type = "pv"
+ swaption_class = Swaption
moneyness, T, r = [], [], []
if option_type == "payer":
quotes = quotes.assign(mid = quotes[['pay_bid','pay_offer']].mean(1) * 1e-4)
@@ -441,11 +447,12 @@ class VolatilitySurface(ForwardIndex):
quotes = quotes.assign(mid = quotes[['rec_bid','rec_offer']].mean(1) * 1e-4)
for expiry, df in quotes.groupby(['expiry']):
atm_strike = ATMstrike(self._index, expiry.date())
- option = Swaption(self._index, expiry.date(), 100, option_type)
+ option = swaption_class(self._index, expiry.date(), 100, option_type)
+
T.append(option.T * np.ones(df.shape[0]))
moneyness.append(df.strike.values / atm_strike)
r.append(Parallel(n_jobs=4)(
- delayed(compute_vol)(option, strike, mid, pv_type) for strike, mid in
+ delayed(compute_vol)(option, strike, mid) for strike, mid in
df[['strike', 'mid']].values))
r = np.fromiter(chain(*r), np.float, quotes.shape[0])
f = SmoothBivariateSpline(np.hstack(T), np.hstack(moneyness), r)