Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating EuropeanOptionImpliedVolatility in quantlib-python

I have R code that uses RQuantlib library. In order to run it from python I am using RPy2. I know python has its own bindings for quantlib (quantlib-python). I'd like to switch from R to python completely.

Please let me know how I can run the following using 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
like image 316
AnalyticsBuilder Avatar asked Feb 03 '11 20:02

AnalyticsBuilder


People also ask

Is it possible to calculate implied volatility in Python?

You have to realize that the implied volatility calculation is computationally expensive and if you want realtime numbers maybe python is not the best solution.

What is the europeanoptionimpliedvolatility function?

The EuropeanOptionImpliedVolatility function solves for the (unobservable) implied volatility, given an option price as well as the other required parameters to value an option. The well-known closed-form solution derived by Black, Scholes and Merton is used for valuation.

Is it possible to use QuantLib with Python?

I know python has its own bindings for quantlib (quantlib-python). I'd like to switch from R to python completely. Show activity on this post. You'll need a bit of setup. For convenience, and unless you get name clashes, you better import everything: (note that you'll need an exercise date, not a time to maturity.)

Can I use rquantlib with rpy2?

I have R code that uses RQuantlib library. In order to run it from python I am using RPy2. I know python has its own bindings for quantlib (quantlib-python). I'd like to switch from R to python completely. Show activity on this post. You'll need a bit of setup. For convenience, and unless you get name clashes, you better import everything:


1 Answers

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. 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.

like image 83
Luigi Ballabio Avatar answered Oct 19 '22 09:10

Luigi Ballabio