diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/analytics/ir_swaption.py | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/python/analytics/ir_swaption.py b/python/analytics/ir_swaption.py index a83b6cd8..77485a31 100644 --- a/python/analytics/ir_swaption.py +++ b/python/analytics/ir_swaption.py @@ -5,7 +5,7 @@ from quantlib.time.api import Date, Period, Years, pydate_from_qldate from quantlib.instruments.api import MakeSwaption from quantlib.instruments.swap import SwapType from quantlib.pricingengines.api import BlackSwaptionEngine -from quantlib.settings import Settings +from scipy.optimize import brentq from yieldcurve import YC @@ -55,6 +55,21 @@ class IRSwaption: def pv(self): return self._direction * self._qloption.npv + @pv.setter + def pv(self, val): + def handle(x): + self.sigma = x + return self._direction * (self.pv - val) + + eta = 1.1 + a = 0.1 + b = a * eta + while True: + if handle(b) > 0: + break + b *= eta + self.sigma = brentq(handle, a, b) + @property def sigma(self): return self._sigma.value @@ -68,17 +83,18 @@ class IRSwaption: with conn.cursor() as c: c.execute("SELECT * from swaptions " "WHERE id = %s", (trade_id,)) rec = c.fetchone() - yc = YC(evaluation_date=rec["trade_date"], fixed=True) - p = Period(int(rec["security_id"].replace("USISDA", "")), Years) + yc = YC(evaluation_date=rec.trade_date, fixed=True, extrapolation=True) + p = Period(int(rec.security_id.replace("USISDA", "")), Years) swap_index = UsdLiborSwapIsdaFixAm(p, yc) instance = IRSwaption( swap_index, - Date.from_datetime(rec["expiration_date"]), - rec["strike"], - rec["option_type"], - rec["buysell"], - rec["notional"], + Date.from_datetime(rec.expiration_date), + rec.strike, + rec.option_type, + rec.buysell, + rec.notional, ) + instance.pv = rec.price / 100 * rec.notional * instance._direction return instance @property @@ -88,3 +104,7 @@ class IRSwaption: @value_date.setter def value_date(self, d): self.yc.link_to(YC(evaluation_date=d, fixed=True)) + + @property + def strike(self): + return self._qloption.underlying_swap().fixed_rate |
