1

Here is my code for downloading spot prices and calculating realized volatilities for a bunch of indices.

library(quantmod)
library(PerformanceAnalytics)
library(RQuantLib)

tickers.index = c("^RUT","^STOXX50E","^HSI")
myEnv <- new.env()
getSymbols(tickers.index, src='yahoo', from = "2004-03-26", to = "2012-10-10", env = myEnv, adjust=TRUE)
index <- do.call(merge, c(eapply(myEnv, Ad), all=TRUE))
index <-na.locf(index)

#Calculate daily returns for all indices and convert to arithmetic returns
index.ret <- exp(CalculateReturns(index,method="compound")) - 1
index.ret[1,] <- 0

#Calculate realized vol for all the indices
index.realized <- xts(apply(index.ret,2,runSD,n=20), index(index.ret))*sqrt(252)
index.realized[1:19,] <- 1

What I would like to do now is to calculate a series of Put prices with the function EuropeanOption for every index, every day with the following parameters:

  • Underlying Price - Today's close from the index XTS
  • Strike Price - Yesterday's close from the index XTS
  • Implied Vol - Yesterday's realized vol from the index.realized XTS
  • All other parameters will just be constants

I have tried to implement this with various attempts using apply and etc but couldn't get it to work. I don't have to use the RQuantLib - if other functions to calculate the price of an European option can make this easier, I am fine with it. Would greatly appreciate any help.

Thank you.

mchangun
  • 9,814
  • 18
  • 71
  • 101
  • Why are you using PerformanceAnalytics? Just to calculate returns? Haven't we [been through this before](http://stackoverflow.com/questions/12823445/faster-way-of-calculating-rolling-realized-volatility-in-r) – GSee Oct 17 '12 at 16:28
  • Yes yes - just haven't gotten around to changing that bit yet. – mchangun Oct 17 '12 at 16:31
  • @GSee OK I got it working `puts.unwind <- mapply(EuropeanOption,"put",index,na.locf(lag(index,1),fromLast=TRUE),0,0,29/365,index.realized + 0.03) puts.unwind <- xts(matrix(as.numeric(puts.unwind[1,]),nrow(index),ncol(index)),index(index))` First line calculates the puts and the second line extracts only the prices and reformats into an XTS. – mchangun Oct 20 '12 at 04:33

1 Answers1

1

OK I got it working

puts.unwind <- mapply(EuropeanOption,"put",index,na.locf(lag(index,1),fromLast=TRUE),0,0,29/365‌​,index.realized) 
puts.unwind <- xts(matrix(as.numeric(puts.unwind[1,]),nrow(index),ncol(index)),index(index)) 

First line calculates the puts and the second line extracts only the prices and reformats into an XTS.

mchangun
  • 9,814
  • 18
  • 71
  • 101