考虑迷你期权合约将数据拉入R的一种变通方法
我一直在使用quantmod中名为考虑迷你期权合约将数据拉入R的一种变通方法,r,quantmod,R,Quantmod,我一直在使用quantmod中名为getoptionscain的函数拉选项链。现在有了针对GOOG、AAPL等股票的迷你期权合约,这给我的代码带来了一个bug。我把符号后面的数字分条出来,现在迷你合约用GOOG7贯穿所有数据。有什么想法吗 library(quantmod) underlying <- 'GOOG' # set what your volatility forcast or assumption is volforcast <- .25 # Get symbols c
getoptionscain
的函数拉选项链。现在有了针对GOOG、AAPL等股票的迷你期权合约,这给我的代码带来了一个bug。我把符号后面的数字分条出来,现在迷你合约用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]
库(quantmod)
底层您只需要放置$duration
不是NA
的行?这将实现这一目标:
puts <- puts[!is.na(puts$maturity), ]
对于删除“GOOG7”或任何后跟“7”的基础安全符号的简洁筛选器,请尝试以下操作:
symbols <- grep(paste("^",underlying,"[0-6,8-9]", sep="", collapse = NULL),
symbols, value = TRUE)
符号您至少应该告诉我们错误是什么。错误消息?回答错了吗?计算机着火了?既然您从names(OC)
中知道了过期的年份和月份,那么您可以将过于通用的正则表达式[0-9]{6}
更改为更精确的表达式,例如1304[0-9]{2}
.1304?如果您跟踪(看跌期权,10),您将看到GOOG15017P01260000,即2015年1月17日到期。。我不明白你说的reg表达式。。但我认为你假设1304是常数。。那是一年又一天。。这是GOOG7130420P00780000,其中GOOG7代表与GOOG7签订的迷你合同,该合同抛出了Na'sCould,还可以查找“GOOG7”和invert
,以仅保留非“GOOG7”的内容:grep(粘贴(“^”,底层,“7”,sep=”“),符号,value=TRUE,invert=TRUE)
。此代码应一直有效,直到2070年到期的选项被列出。另一种获取股票代码后面没有7的所有内容的方法:grep(粘贴(“^”,基础,[^7]”,sep=“”),符号,value=TRUE)
。在括号([]
)内,^
表示“不”,但这是好的。。代码中还有其他依赖项导致它停止运行。。理想情况下,列表“OC”需要有GOOG7的子集……对不起@cdcaveman,我认为这很清楚。试试这个lputs我真的认为最好立即将'OC'变量中的数据子集化。。这是一张名单。。我不知道怎么做。。我确信我可以将grep作为一个参数应用于某个函数,该函数返回修改后的listsinteresting。。我让它与lputs一起工作
OC <- lapply(OC, function(x) {
list(calls=x$calls[grep("[A-Z]\\d{6}[CP]\\d{8}$", rownames(x$calls)), ],
puts=x$puts[grep("[A-Z]\\d{6}[CP]\\d{8}$", rownames(x$puts)), ],
symbol=x$symbol)
})
symbols <- grep(paste("^",underlying,"[0-6,8-9]", sep="", collapse = NULL),
symbols, value = TRUE)