Skip to content

Instantly share code, notes, and snippets.

@josephdunn
Created June 23, 2012 00:15
Show Gist options
  • Save josephdunn/2975914 to your computer and use it in GitHub Desktop.
Save josephdunn/2975914 to your computer and use it in GitHub Desktop.
library(RQuantLib)
goog <- list(spot = 570.225,
yield = 0,
rate = 0.05,
maturity = as.numeric(as.Date('2012-06-29') - Sys.Date()) / 365,
volatility = 0.3038834, # annualizedVol()
chain = matrix(c(550.00, 21.95,
555.00, 17.40,
560.00, 13.30,
565.00, 9.85,
570.00, 6.70,
575.00, 4.15,
580.00, 2.375,
585.00, 1.25,
590.00, 0.65,
595.00, 0.35,
600.00, 0.20,
605.00, 0.125,
610.00, 0.10,
615.00, 0.075),
ncol = 2, byrow=TRUE)
)
toPlot = matrix()
for (i in 1:nrow(goog$chain))
{
res <- AmericanOptionImpliedVolatility('call',
goog$chain[i,2], # price
goog$spot,
goog$chain[i,1], # strike
goog$yield,
goog$rate,
goog$maturity,
goog$volatility)
message(goog$chain[i,1], ',', res$impliedVol)
toBind <- matrix(c(goog$chain[i,1], res$impliedVol), ncol=2)
if (ncol(toPlot) == 1)
toPlot <- toBind
else
toPlot <- rbind(toPlot, toBind)
}
plot(toPlot[,1], toPlot[,2], xlab='Strike Price', ylab='IV', type='l') # http://i.imgur.com/Ur8zz.png
annualizedVol <- function()
{
library(quantmod)
GOOG <- getSymbols('GOOG', auto.assign=FALSE, from=Sys.Date()-366)
returns <- na.omit(diff(log(GOOG$GOOG.Adjusted)))
return(apply(returns, 2, sd) / sqrt(1/252))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment