简体   繁体   English

在quantlib-python中计算EuropeanOptionImpliedVolatility

[英]Calculating EuropeanOptionImpliedVolatility in quantlib-python

I have R code that uses RQuantlib library. 我有使用RQuantlib库的R代码。 In order to run it from python I am using RPy2. 为了从python运行它我使用RPy2。 I know python has its own bindings for quantlib (quantlib-python). 我知道python有自己的quantlib绑定(quantlib-python)。 I'd like to switch from R to python completely. 我想完全从R切换到python。

Please let me know how I can run the following using quantlib-python 请告诉我如何使用quantlib-python运行以下命令

import rpy2.robjects as robjects

robjects.r('library(RQuantLib)')
x = robjects.r('x<-EuropeanOptionImpliedVolatility(type="call", value=11.10, underlying=100,strike=100, dividendYield=0.01, riskFreeRate=0.03,maturity=0.5, volatility=0.4)')
print x

Sample run: 样品运行:

$ python vol.py 
Loading required package: Rcpp
Implied Volatility for EuropeanOptionImpliedVolatility is 0.381

You'll need a bit of setup. 你需要一些设置。 For convenience, and unless you get name clashes, you better import everything: 为方便起见,除非您发生名称冲突,否则最好导入所有内容:

from QuantLib import *

then, create the option, which needs an exercise and a payoff: 然后,创建选项,这需要练习和支付:

exercise = EuropeanExercise(Date(3,August,2011))
payoff = PlainVanillaPayoff(Option.Call, 100.0)
option = EuropeanOption(payoff,exercise)

(note that you'll need an exercise date, not a time to maturity.) (请注意,您需要一个锻炼日期,而不是成熟的时间。)

Now, whether you want to price it or get its implied volatility, you'll have to setup a Black-Scholes process. 现在,无论您想要定价还是获得其隐含波动率,您都必须设置Black-Scholes流程。 There's a bit of machinery involved, since you can't just pass a value, say, of the risk-free rate: you'll need a full curve, so you'll create a flat one and wrap it in a handle. 有一些机器涉及,因为你不能只传递一个无风险率的值:你需要一个完整的曲线,所以你将创建一个扁平的,并将其包裹在一个手柄中。 Ditto for dividend yield and vol; 股息收益率和收益率同样如此; the underlying value goes in a quote. 底层价值在报价中。 (I'm not explaining what all the objects are; comment if you need it.) (我不解释所有对象是什么;如果你需要,请评论。)

S = QuoteHandle(SimpleQuote(100.0))
r = YieldTermStructureHandle(FlatForward(0, TARGET(), 0.03, Actual360()))
q = YieldTermStructureHandle(FlatForward(0, TARGET(), 0.01, Actual360()))
sigma = BlackVolTermStructureHandle(BlackConstantVol(0, TARGET(), 0.20, Actual360()))
process = BlackScholesMertonProcess(S,q,r,sigma)

(the volatility won't actually be used for implied-vol calculation, but you need one anyway.) (波动率实际上不会用于隐含卷计算,但无论如何你需要一个。)

Now, for implied volatility you'll call: 现在,对于隐含波动率,您将调用:

option.impliedVolatility(11.10, process)

and for pricing: 和定价:

engine = AnalyticEuropeanEngine(process)
option.setPricingEngine(engine)
option.NPV()

You might use other features (wrap rates in a quote so you can change them later, etc.) but this should get you started. 您可以使用其他功能(报价中的汇率以便您可以在以后更改它们等)但这应该可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM