Skip to content
All library documents

Reusing a QuantLib Pricing Engine to Plot Option Value Across Spot Prices

Article Quant Q&A · Author: roller

Summary

The question asks how to plot a European option’s value across many underlying prices without rebuilding the QuantLib example for each value. The accepted response illustrates setting up a vanilla option with an analytic European pricing engine, attaching its underlying to a mutable quote, and then changing that quote inside a loop. Each updated spot produces a new net present value that can be collected and plotted.

This approach reuses the option, market handles, and pricing engine while varying the underlying input. The example uses Python bindings and a plotting library, and the author suggests that this is fast enough for the task. However, the thread gives no timing comparison or benchmark, and its sample inputs are illustrative rather than a general performance result. The method is specific to repricing the same option under different spot values; it does not discuss broader parameter sweeps, calibration, or production performance constraints.

Key ideas

  • A mutable underlying quote can be changed to reprice the same option repeatedly.
  • The example attaches an analytic European engine once, then records each net present value as spot changes.
  • The resulting price series can be plotted against the range of underlying prices.
  • The response claims practical speed but provides no benchmark or comparative timing evidence.

Tags

Full text
# Option price quantlib


# Option price quantlib












I am lookin at https://github.com/lballabio/QuantLib/blob/master/Examples/EquityOption/EquityOption.cpp . I want plot a graph of the option price for different underlying prices. Other than changing the `Real underlying = 36;` for each of the different underlying prices I want to calculate , is there any way to decrease the time this calculation would take. I want to plot a graph of the option price for different underlyin prices.

## Answer by David Duarte (score 1, accepted)

https://quant.stackexchange.com/a/57103

Doing it in python is fast enough so not sure why it would take long in c++.

```
import QuantLib as ql
import matplotlib.pyplot as plt

today = ql.Date().todaysDate()
strike = 100.0
maturity= ql.Date(15,6,2021)
option_type = ql.Option.Call
payoff = ql.PlainVanillaPayoff(option_type, strike)

europeanExercise = ql.EuropeanExercise(maturity)
europeanOption = ql.VanillaOption(payoff, europeanExercise)

spot = ql.SimpleQuote(100)
riskFreeTS = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.01, ql.Actual365Fixed()))
volTS = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today, ql.NullCalendar(), 0.2, ql.Actual365Fixed()))
process = ql.BlackScholesProcess(ql.QuoteHandle(spot), riskFreeTS, volTS)
engine = ql.AnalyticEuropeanEngine(process)
europeanOption.setPricingEngine(engine)
europeanOption.NPV()

prices = []
for n in range(50,200):
    spot.setValue(n)
    prices.append(europeanOption.NPV())
    
plt.plot(range(50,200), prices);
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.