Home > Net >  writing an if-else function which returns 2 values
writing an if-else function which returns 2 values

Time:11-22

I am working with the below function, basically will return with both delta and gamma value, may I know between the calldelta & calleval, so is putdelta & puteval, how may I use operator to connect the 2 return values together? Tried & with no luck. Many thanks for your attention.

library(optionstrat)
library(quantmod)


  
x=160
sigma=0.25
t=0.25
r=0.05
    delta<-function(option_type,stock_price)
    { 
      if (option_type=="c")
      calldelta(s,x,sigma,t,r)
      calleval(s,x,sigma,t,r)$Gamma
    else
      putdelta(s,x,sigma,t,r)
      puteval(s,x,sigma,t,r)$Gamma
    }

CodePudding user response:

Looks like calldelta and calleval respectively are functions and not sure what these functions are returning (values or dataframes). But, store the values in variable:

delta <- function(option_type,stock_price) { 
  if (option_type=="c") {
  v1 <- calldelta(s,x,sigma,t,r)
  v2 <- calleval(s,x,sigma,t,r)$Gamma
  print(paste0(v1, ' and ', v2), collapse=NULL)
} else {
 v3 <-  putdelta(s,x,sigma,t,r)
 v4 <-  puteval(s,x,sigma,t,r)$Gamma
 print(paste0(v3, ' and ', v4), collapse=NULL)
}

}

  • Related