Take the 2-minute tour ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

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**
share|improve this question
add comment

4 Answers

In a while loop, the condition expression is evaluated and, if TRUE, the block is executed. The error tells you that the condition in the loop did not evaluate to TRUE or FALSE, but to NA. So check how you define/compute 'count' and 'err'.

share|improve this answer
add comment

I see two ways:

  1. Could you use EuropeanOptionImpliedVolatility which is contained in RQuantLib ?
  2. use the print command and look at the values for count and err in each iteration. For a simple example look at this c = 1; print(paste("Hallo:",c));
share|improve this answer
add comment

If sig==0, then BS returns NaN because of dividing by zero on this line:

d1 <- (log(S/K) + (r + sig^2/2)*T) / (sig*sqrt(T))

That then causes err to be NaN.

share|improve this answer
add comment

Try to put some parenthesis to your condition, i experienced many issues liked to this in the past.

+    while( (abs(err) > 0.00001) && (count < 1000) ){
share|improve this answer
    
This will not help. See my answer. –  Joshua Ulrich Dec 31 '13 at 13:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.