1
$\begingroup$

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**
$\endgroup$
1
$\begingroup$

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'.

$\endgroup$
1
$\begingroup$

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) ){
$\endgroup$
  • $\begingroup$ This will not help. See my answer. $\endgroup$ – Joshua Ulrich Dec 31 '13 at 13:44
0
$\begingroup$

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));
$\endgroup$
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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