May 23rd, 2015, 1:50 pm
This is the code in R that I use to script European options from QuantLib. You can do something like that with Monte-Carlo.## Generate random outputs for unit-testing. The script saves outputs into CSV files that can be# parsed by testing code.## Number of test casesN = 10library('RQuantLib')# European Option with the Black-Scholesf <- file("european.csv")o <- c()for (i in 1:N){ c = ifelse(runif(1,min=0,max=1.0) <= 0.5, 'call', 'put') s = runif(1,min=1,max=1000) k = runif(1,min=1,max=1000) r = runif(1,min=0,max=5.0) d = runif(1,min=0,max=5.0) t = runif(1,min=0,max=10.0) v = runif(1,min=0,max=5.0) e <- EuropeanOption(type=c, underlying=s, strike=k, dividendYield=d, riskFreeRate=r, maturity=t, volatility=v) o <- c(o, c(paste('<-Test', toString(i)), toString(c), toString(s), toString(k), toString(r), toString(d), toString(t), toString(v), toString(e$value), toString(e$delta), toString(e$gamma), toString(e$vega), toString(e$theta), toString(e$rho), toString(e$divRho), '\n'))}writeLines(o, f)close(f)