import datetime from yieldcurve import YC, rate_helpers from quantlib.indexes.swap.usd_libor_swap import UsdLiborSwapIsdaFixAm from quantlib.instruments.make_swaption import MakeSwaption from quantlib.pricingengines.api import BlackSwaptionEngine from quantlib.instruments.swap import Receiver from quantlib.time.api import Period, Years, Days from quantlib.settings import Settings from quantlib.experimental.risk.sensitivityanalysis import parallel_analysis helpers = rate_helpers("USD", evaluation_date=datetime.date.today()) yc = YC(helpers) index1 = UsdLiborSwapIsdaFixAm(Period(2, Years), yc) index2 = UsdLiborSwapIsdaFixAm(Period(10, Years), yc) swaption1 = (MakeSwaption(index1, Period(2, Years), strike=0.020805). with_underlying_type(Receiver). with_nominal(10_000_000)()) swaption2 = (MakeSwaption(index2, Period(10, Years), strike=0.0212). with_underlying_type(Receiver). with_nominal(10_000_000)()) pe1 = BlackSwaptionEngine(yc, 0.2815) pe2 = BlackSwaptionEngine(yc, 0.2830) swaption1.set_pricing_engine(pe1) swaption2.set_pricing_engine(pe2) def theta(swaption): old_pv = swaption.npv with Settings() as settings: settings.evaluation_date += Period(1, Days) theta = swaption.npv - old_pv return theta def dv01(swaption, helpers): old_pv = swaption.npv old_quotes = [rh.quote.value for rh in helpers] for rh in helpers: rh.quote.value += 1e-4 dv01 = old_pv - swaption.npv for rh, q in zip(helpers, old_quotes): rh.quote.value = q return dv01 delta, gamma = parallel_analysis([h.quote for h in helpers], [swaption1], []) print("NPV: ", swaption1.npv) print("DV01: ", dv01(swaption1, helpers)) print("theta: ", theta(swaption1)) print("vega: ", swaption1.vega / 100)