Choosing Python Arguments for QuantLib’s Monte Carlo Barrier Engine
Summary
The document describes a Python argument error when setting up QuantLib’s Monte Carlo barrier option engine. The immediate cause is that an integer is passed as the engine’s second positional argument, where the binding expects a string identifying the simulation traits. The suggested correction passes the low-discrepancy setting as `traits` and supplies the time steps and required sample count by name. It also recommends using Python’s help function to inspect the expected arguments and advises against wildcard imports, which can make it harder to identify where names come from.
The example is a down-and-in European call under a Black–Scholes–Merton process. Although the corrected code runs, the answer reports a zero value and does not diagnose whether that result is appropriate. The discussion therefore helps with the API error and argument mapping, but it does not establish a sound price, explain the Monte Carlo estimator, or assess convergence. The example’s parameter choices and unexplained output should not be treated as pricing guidance.
Key ideas
- The engine call fails because an integer is supplied where the Python binding expects a traits string.
- Pass simulation settings by named arguments to make their roles clear.
- Python help can reveal the arguments expected by a QuantLib binding.
- Wildcard imports can make it harder to trace where imported names originate.
- A corrected call running successfully does not validate the resulting option price.
Tags
Full text
# Issue in Pricing Barrier Options using MCBarrierEngine in QuantLib Python
# Issue in Pricing Barrier Options using MCBarrierEngine in QuantLib Python
Extremely sorry for bugging the community again, but I am struggling with finding proper documentation of QuantLib Python. I am trying to price Barrier Option using MC Simulation. Here is the code:
```
from QuantLib import *
import matplotlib.pyplot as plt
barrier, barrierType, optionType, rebate = (80.0, Barrier.DownIn, Option.Call, 0.0)
underlying, strike, rf, sigma, maturity, divYield = (100, 105, .05, 0.2, 12, 0.0)
Grids = (5, 10, 25, 50, 100, 1000, 50000)
maxG = Grids[-1]
today = Settings.instance().evaluationDate
maturity_date = today + int(maturity)
process = BlackScholesMertonProcess(QuoteHandle(SimpleQuote(underlying)),
YieldTermStructureHandle(FlatForward(today, divYield, Thirty360())),
YieldTermStructureHandle(FlatForward(today, rf, Thirty360())),
BlackVolTermStructureHandle(BlackConstantVol(
today, NullCalendar(), sigma, Thirty360())))
option = BarrierOption(barrierType, barrier, rebate,PlainVanillaPayoff(optionType, strike),
EuropeanExercise(maturity_date))
steps = 2
rng = "lowdiscrepancy"
numPaths = 500000
traits=50000
engine = MCBarrierEngine(process, traits)
option.setPricingEngine(engine)
trueValue = option.NPV()
print(trueValue)
```
Here is the output:
```
runfile('C:/Users/nitin.kapai/Documents/Exam_v1/QuantLib Code/Barrier_DownIn_MC_Simulation_QuantLib.py', wdir='C:/Users/nitin.kapai/Documents/Exam_v1/QuantLib Code')
Traceback (most recent call last):
File "C:\Users\nitin.kapai\Documents\Exam_v1\QuantLib Code\Barrier_DownIn_MC_Simulation_QuantLib.py", line 35, in <module>
engine = MCBarrierEngine(process, traits)
File "C:\Users\nitin.kapai\Anaconda3\lib\site-packages\QuantLib\QuantLib.py", line 12067, in MCBarrierEngine
traits = traits.lower()
AttributeError: 'int' object has no attribute 'lower'
```
Any suggestion/feedback would be greatly appreciated
## Answer by StackG (score 1, accepted)
https://quant.stackexchange.com/a/57768
A few things... firstly, I've attached a correction to your code at the bottom. It runs, but gives a solution of 0.0. Not sure why that is, but the code runs at least, can you work out the pricing problem yourself?
You've used the `import *` pattern here. It seems easy now, but it will cause you trouble in future, because when you come to work out where each import has come from, you won't know where... this is really best avoided! (trust me, I've done it and regretted).
You also had some trouble with `ql` arguments. We're working on documenting them all over at ReadTheDocs but are a bit slow due to day jobs, in the meantime I recommend the following commands (one of the two should help): `help(ql.MCBarrierEngine)` or `help(ql.MCBarrierEngine())`, should make sense of the arguments that Python QuantLib is expecting.
Here is the solution to your problem above:
```
from QuantLib import *
import matplotlib.pyplot as plt
barrier, barrierType, optionType, rebate = (80.0, Barrier.DownIn, Option.Call, 0.0)
underlying, strike, rf, sigma, maturity, divYield = (100, 105, .05, 0.2, 12, 0.0)
Grids = (5, 10, 25, 50, 100, 1000, 50000)
maxG = Grids[-1]
today = Settings.instance().evaluationDate
maturity_date = today + int(maturity)
process = BlackScholesMertonProcess(QuoteHandle(SimpleQuote(underlying)),
YieldTermStructureHandle(FlatForward(today, divYield, Thirty360())),
YieldTermStructureHandle(FlatForward(today, rf, Thirty360())),
BlackVolTermStructureHandle(BlackConstantVol(
today, NullCalendar(), sigma, Thirty360())))
option = BarrierOption(barrierType, barrier, rebate,PlainVanillaPayoff(optionType, strike),
EuropeanExercise(maturity_date))
steps = 2
rng = "lowdiscrepancy"
numPaths = 500000
engine = MCBarrierEngine(process, traits=rng, timeSteps=steps, requiredSamples=numPaths)
option.setPricingEngine(engine)
trueValue = option.NPV()
print(trueValue)
```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.