10

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
AnalyticsBuilder
  • 4,111
  • 4
  • 24
  • 36
  • Have you tried something like `from quantlib import EuropeanOptionImpliedVolatility`, and then calling it with the same arguments. See http://quantlib.referata.com/wiki/Python_QuantLib_tutorial (seems to be the sum total of their documentation) – Thomas K Feb 03 '11 at 22:54
  • @Thomas K: I can do this: `from QuantLib import EuropeanOption` I was hoping for an explanation on how to set up a pricing engine for a given method of calculating vol. R takes a facade approach, python follows the original cpp Quantlib path of power and complexity, therefore my question. – AnalyticsBuilder Feb 04 '11 at 04:28

1 Answers1

28

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.

Luigi Ballabio
  • 4,128
  • 21
  • 29
  • You are Godsend Luigi Ballabio! It looks like above code is for as of now implied volatility. Is it possible to do it for some point in time in future/past? – user1700890 Jan 28 '16 at 21:13
  • 1
    You can set the global evaluation date to any date, if that's what you're asking. The code above will calculate the implied vol as of that date. Use `Settings.instance().evaluationDate = date` to set it. – Luigi Ballabio Jan 28 '16 at 21:46
  • Thank you again Luigi! I am reading python example I found in QuantLib-SWIG-1.7 and noticed minor differences from what you wrote. For example, you have `FlatForward(0, TARGET(), 0.03, Actual360())` and SWIG has `FlatForward(settlementDate, 0.05, Actual365Fixed())`. Could you help to understand the difference? Where can I read more about it? – user1700890 Jan 28 '16 at 21:53
  • I think this might be better asked in a new question, so that the information can be found more easily by others, too. – Luigi Ballabio Jan 28 '16 at 22:01
  • I am reading your book, Luigi, I am getting dangerously close to finding out the answer. I will post an extra question, if I fail. Thank you again! – user1700890 Jan 28 '16 at 22:19
  • @LuigiBallabio I know I am a quite late to asking this, but what is the purpose of the `sigma` (vol) passed to the `BlackScholesMertonProcess` here. Is it used somehow for the computation of `impliedVolatility`, if yes how does one arrive on that number as implied vol is something we're trying to compute? – Anand Singh Kunwar Mar 16 '23 at 10:59
  • As I mentioned in the answer, the volatility won't actually be used for implied-vol calculation. The process constructor needs it, though. – Luigi Ballabio Mar 16 '23 at 14:23