程式

在 QuantLib Python 中使用 MCBarrierEngine 定價障礙選項的問題

  • September 3, 2020

非常抱歉再次打擾社區,但我正在努力尋找 QuantLib Python 的正確文件。我正在嘗試使用 MC Simulation 為障礙期權定價。這是程式碼:

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)

這是輸出:

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'

任何建議/回饋將不勝感激

有幾件事……首先,我在底部對您的程式碼進行了更正。它執行,但給出的解決方案為 0.0。不知道為什麼會這樣,但程式碼至少可以執行,你能自己解決定價問題嗎?

您在import *這裡使用了該模式。現在看起來很容易,但將來會給您帶來麻煩,因為當您計算出每個導入的來源時,您將不知道……這真的最好避免!(相信我,我已經做到了並且後悔了)。

ql你在爭論上也遇到了一些麻煩。我們正在ReadTheDocs上全面記錄它們,但由於日常工作有點慢,同時我建議使用以下命令(這兩個命令之一應該有幫助):help(ql.MCBarrierEngine)或者help(ql.MCBarrierEngine()),應該理解 Python QuantLib 的論點是期待。

這是您上述問題的解決方案:

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)

引用自:https://quant.stackexchange.com/questions/57767