Calculating EuropeanOptionImpliedVolatility in Quantile Python

I have R code that uses the RQuantlib library. To run it from python, I use RPy2. I know that python has its own bindings for quantlib (quantlib-python). I would like to completely switch from R to python.

Please let me know how I can run the following using the quantile-python method

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 

Run Example:

 $ python vol.py Loading required package: Rcpp Implied Volatility for EuropeanOptionImpliedVolatility is 0.381 
+6
python r quantlib rpy2
source share
1 answer

You will need a little customization. For convenience, and if you do not come across names, you are better off importing everything:

 from QuantLib import * 

then create an option that requires exercise and gain:

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

(Note that you will need a due date, not a time to maturity.)

Now, whether you want to evaluate it or get implied volatility, you have to set up the Black-Scholes process. There are several mechanisms involved, because you canโ€™t just pass the cost of, say, a risk-free rate: you need a full curve, so you create a flat one and wrap it in a pen. The same goes for dividend yield and volume; the base value is given in the quote. (I am not explaining that all objects are: commentary 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) 

(Volatility will not actually be used to calculate the implied volume, but you still need it.)

Now, for implied volatility, you will call:

 option.impliedVolatility(11.10, process) 

and to evaluate:

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

You can use other functions (transfer speed in quote so you can change them later, etc.), but this should help you get started.

+19
source share

All Articles