I am unsuccessfully trying to find the Implied Volatilities for the SPX on a given date using information of the CBOE, as well as Open Interest, but as I run the code I am getting and error message that I cannot resolve.
> ## library(RQuantLib)
> library(RQuantLib)
Loading required package: Rcpp
> ## library(Rcpp)
> library(Rcpp)
> ## Black-Scholes Function
> BS <-
+ function(S, K, T, r, sig, type="C"){
+ d1 <- (log(S/K) + (r + sig^2/2)*T) / (sig*sqrt(T))
+ d2 <- d1 - sig*sqrt(T)
+ if(type=="C"){
+ value <- S*pnorm(d1) - K*exp(-r*T)*pnorm(d2)
+ }
+ if(type=="P"){
+ value <- K*exp(-r*T)*pnorm(-d2) - S*pnorm(-d1)
+ }
+ return(value)
+ }
> ## Function to find BS Implied Vol using Bisection Method
> implied.vol <-
+ function(S, K, T, r, market, type){
+ sig <- 0.20
+ sig.up <- 1
+ sig.down <- 0.001
+ count <- 0
+ err <- BS(S, K, T, r, sig, type) - market
+ ## repeat until error is sufficiently small or counter hits 1000
+ while(abs(err) > 0.00001 && count<1000){
+ if(err < 0){
+ sig.down <- sig
+ sig <- (sig.up + sig)/2
+ }else{
+ sig.up <- sig
+ sig <- (sig.down + sig)/2
+ }
+ err <- BS(S, K, T, r, sig, type) - market
+ count <- count + 1
+ }
+ ## return NA if counter hit 1000
+ if(count==1000){
+ return(NA)
+ }else{
+ return(sig)
+ }
+ }
> ## read in data
> dat <- read.csv('C:/Users/Edgar Martinez/Downloads/SPX_data.csv')
> ## read in data
> dat <- read.csv('C:/Users/Edgar Martinez/Downloads/SPX_data.csv')
> ## calculate implied vol for Call
> S <- 1841.36
> T <- 20/365
> r <- 0.01
> n <- dim(dat)[1]
> c.vol.Ask <- rep(0,n)
> c.vol.Bid <- rep(0,n)
> p.vol.Ask <- rep(0,n)
> p.vol.Bid <- rep(0,n)
> for(i in 1:n){
+ c.vol.Ask[i] <- implied.vol(S, dat$K[i], T, r, dat$C.Ask[i], "C")
+ c.vol.Bid[i] <- implied.vol(S, dat$K[i], T, r, dat$C.Bid[i], "C")
+ p.vol.Ask[i] <- implied.vol(S, dat$K[i], T, r, dat$P.Ask[i], "P")
+ p.vol.Bid[i] <- implied.vol(S, dat$K[i], T, r, dat$P.Bid[i], "P")
+ }
**Error in while (abs(err) > 1e-05 && count < 1000) { :
missing value where TRUE/FALSE needed**