2

quantmod と呼ばれる関数を使用して、オプション チェーンを引き込んでいますgetOptionsChain。GOOG、AAPL などの株式で提供​​されるミニ オプション契約があるため、コードにバグが発生します。シンボルの後の数字をストライプ化して、ミニ契約が GOOG7 ですべてのデータを通過するようになりました。何か案は?

library(quantmod)
underlying <- 'GOOG'
# set what your volatility forcast or assumption is
volforcast <- .25
# Get symbols current price
yqf <- "Last Trade (Price Only)"
underlying.price <- getQuote(underlying,what=yahooQF(yqf))$Last

OC <- getOptionChain(underlying, NULL)
#check data
head(OC)
lputs <- lapply(OC, FUN = function(x) x$puts)
head(lputs) #check for NA values, yahoo returns all NA values sometimes
puts <- do.call('rbind', lputs )
#check data
head(puts,150)

symbols <- as.vector(unlist(lapply(lputs, rownames)))
expiries <- unlist(lapply(symbols, function(x) {
  regmatches(x=x, regexpr('[0-9]{6}', x)) } ))
puts$maturity <- as.numeric((as.Date(expiries, "%y%m%d") - Sys.Date())/365)
GetIV <- function(type, value,
              underlying, strike,dividendYield, riskFreeRate, maturity, volatility,
              timeSteps=150, gridPoints=151) {


  AmericanOptionImpliedVolatility(type, value,
                                  underlying, strike,dividendYield, riskFreeRate, maturity,  volatility, timeSteps=150, gridPoints=151)$impliedVol
}
#this is the part that throws the error due to NA values in puts$maturity
puts$IV <- mapply(GetIV, value = puts$Ask, strike = puts$Strike, maturity = puts$maturity,
                  MoreArgs= list(type='put', underlying= underlying.price,
                  dividendYield=0, riskFreeRate = 0.01,  
                  volatility = volforcast), SIMPLIFY=TRUE)
#this is the error Error: Date's serial number (-2147442285) outside allowed range [367-109574],      i.e. [January 1st, 1901-December 31st, 2199]

puts$maturityisの行を追加することは避けたいですNA

4

2 に答える 2