0

I need to calculate de Implied Volatility for Financial Options using the QuantLib package for R. I'm having trouble using iterations for the function "EuropeanOptionImpliedVolatility" because its output is a Object (called ImpliedVolatility).

largo = nrow(call26) #number of rows in my data set
impl_vol= vector("list",largo)
for(i in largo){
  impl_vol[[i]] = EuropeanOptionImpliedVolatility(type="call", value=valor_opcion[i],
      underlying=st[i], strike=strike[i], dividendYield=dividendo[i], 
      riskFreeRate=rf[i], maturity=maturity[i], volatility=0.4)
}

The result of this is:

list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, structure(list(impliedVol = 0.173643438965225, 
        parameters = structure(list(type = "call", value = 52.95, 
            underlying = 1497.66, strike = 1680, dividendYield = 0.01, 
            riskFreeRate = 0.04, maturity = 0.983561644, volatility = 0.4), .Names = c("type", 
        "value", "underlying", "strike", "dividendYield", "riskFreeRate", 
        "maturity", "volatility"))), .Names = c("impliedVol", 
    "parameters"), class = c("EuropeanOptionImpliedVolatility", 
    "ImpliedVolatility")))

And I need the implied volatilities... wich if I calculate for a single Financial Option i can acces to it with

valor$impliedVol

What can i do? Thanks!

rjara
  • 7
  • 1
  • 4

2 Answers2

0

There's an error in your loop, instead of:

for(i in largo){

it's :

for(i in 1:largo){

Because of this error, only the implied volatility of the last element (index 195) is computed (as you can clearly see in the output structure you have posted, the first 194 list slots are NULL and the last has a value).

Once you have fixed that typo, to access to the value just do:

impl_vol[[i]]$impliedVol
digEmAll
  • 56,430
  • 9
  • 115
  • 140
0

Quick ones:

  • You cannot store compond objects (like the one returned by EuropeanOptionImpliedVolatility() in a vector.

  • You can store them in a list

  • If you know the dimension you can fill them in a matrix.

  • Else you can grow data.frame objects (which is not efficient)

If all you want is just the implied volatility, extract that and store it in a vector. The RQuantLib package has examples of pretty much all uses.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Technically lists are vectors, just not atomic ones. The call: `impl_vol= vector("list",largo)` should have created a valid list. – IRTFM May 19 '14 at 02:39