0
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ I am far from a computer right now so cannot check to confirm but can you try passing the rng variable ("lowdiscrepancy") instead of 50000 as the trait variable? Trait usually refers to the type of random number generator in ql $\endgroup$
    – StackG
    Commented Sep 3, 2020 at 14:45
  • $\begingroup$ Tried this as well but resulted in the following error: File "C:\Users\nitin.kapai\Anaconda3\lib\site-packages\QuantLib\QuantLib.py", line 12049, in init _QuantLib.MCLDBarrierEngine_swiginit(self, _QuantLib.new_MCLDBarrierEngine(*args, **kwargs)) RuntimeError: no time steps provided $\endgroup$
    – Desi_Quant
    Commented Sep 3, 2020 at 14:48
  • $\begingroup$ Hahaha you enticed me to find a computer! $\endgroup$
    – StackG
    Commented Sep 3, 2020 at 14:52

1 Answer 1

1
$\begingroup$

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)
$\endgroup$
1
  • 1
    $\begingroup$ Works perfectly fine!! Yes the output is coming as 0.0. But when I used ATM option (underlying=100, strike =100), the output is 0.4274580677180991. I have cross checked it with Analytical engine and value is correct. Thanks again @StackG for the valuable feedback. $\endgroup$
    – Desi_Quant
    Commented Sep 3, 2020 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.