Я подключал цепочки опций с помощью функции в 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$maturity
равно NA
.
names(OC)
, вы можете изменить слишком общее регулярное выражение[0-9]{6}
на что-то более точное, например1304[0-9]{2}
. - person Vincent Zoonekynd   schedule 30.03.2013