![](/img/trans.png)
[英]Using R and IBrokers, what is the function to call to get the bid and ask prices of given stock symbol?
[英]How to get the latest option prices on SPX using IBrokers in R
我正在嘗試使用以下代碼獲取給定行使價(2700)和到期(15/11/2018)的SPX期權看跌期權價格:
require("IBrokers")
tws <- twsConnect()
opt=twsOPT("",symbol="SPX",right="P", strike="2700", expiry="20181115")
test1=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)
下面報告了兩個函數eWrapper.data.Last和snapShot。
我得到這個:
2 -1 2104 Connessione con il服務器設計和運行OK:使用
2 -1 2104 Connessione con il服務器詳細信息可以:eufarm
2 -1 2104 Connessione con il服務器詳細信息可以:使用
2 -1 2104 Connessione con il服務器設計商:OK:usfarm.us
2 -1 2106 Connessione con HMDS數據場èOK:euhmds
2 -1 2106 Connessione con HMDS數據場èOK:ushmds
2 1 10090無效的合伙企業。 剔除不合格的股票。SPXS&P 500股票指數/ TOP / ALL
連接似乎有效,但未創建任何對象。
eWrapper.data.Last <- function(n) {
eW <- eWrapper(NULL) # use basic template
eW$assign.Data("data",
rep(list(structure(.xts(matrix(rep(NA_real_,2),nc=2),0),
.Dimnames=list(NULL,c("LastSize","Last")))),n))
eW$tickPrice <- function(curMsg, msg, timestamp, file, ...)
{
tickType = msg[3]
msg <- as.numeric(msg)
id <- msg[2] #as.numeric(msg[2])
data <- eW$get.Data("data") #[[1]] # list position of symbol (by id ==
msg[2])
attr(data[[id]],"index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
if(tickType == .twsTickType$LAST) {
data[[id]][nr.data,2] <- msg[4]
}
eW$assign.Data("data", data)
c(curMsg, msg)
}
eW$tickSize <- function(curMsg, msg, timestamp, file, ...)
{
data <- eW$get.Data("data")
tickType = msg[3]
msg <- as.numeric(msg)
id <- as.numeric(msg[2])
attr(data[[id]],"index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
if(tickType == .twsTickType$LAST_SIZE) {
data[[id]][nr.data,1] <- msg[4]
}
eW$assign.Data("data", data)
c(curMsg, msg)
}
return(eW)
}
snapShot <- function (twsCon, eWrapper, timestamp, file, playback = 1, ...){
if (missing(eWrapper))
eWrapper <- eWrapper()
names(eWrapper$.Data$data) <- eWrapper$.Data$symbols
con <- twsCon[[1]]
if (inherits(twsCon, "twsPlayback")) {
sys.time <- NULL
while (TRUE) {
if (!is.null(timestamp)) {
last.time <- sys.time
sys.time <- as.POSIXct(strptime(paste(readBin(con,
character(), 2),
collapse = " "), timestamp))
if (!is.null(last.time)) {
Sys.sleep((sys.time - last.time) * playback)
}
curMsg <- .Internal(readBin(con, "character",
1L, NA_integer_, TRUE, FALSE))
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, format(sys.time,
timestamp), file, ...)
}
else {
curMsg <- readBin(con, character(), 1)
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
if (curMsg == .twsIncomingMSG$REAL_TIME_BARS)
Sys.sleep(5 * playback)
}
}
}
else {
while (TRUE) {
socketSelect(list(con), FALSE, NULL)
curMsg <- .Internal(readBin(con, "character", 1L,
NA_integer_, TRUE, FALSE))
if (!is.null(timestamp)) {
processMsg(curMsg, con, eWrapper, format(Sys.time(),
timestamp), file, ...)
}
else {
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
}
if (!any(sapply(eWrapper$.Data$data, is.na)))
return(do.call(rbind, lapply(eWrapper$.Data$data,
as.data.frame)))
}
}
}
如果我翻譯此消息:
2 1 10090無效的合伙企業。 剔除不合格的股票。SPXS&P 500股票指數/ TOP / ALL
我得到這個翻譯:
部分必需的市場數據尚未訂閱。 獨立的價格變動仍然活躍.SPX S&P 500股票指數/ TOP / ALL遞延市場數據不可用
這意味着您錯過了一些市場數據訂閱。 您需要檢查一下。 嘗試運行一些期權合約並獲取其詳細信息。
如果我運行代碼:
library(IBrokers)
tws <- twsConnect()
serverVersion(tws)
[1] "76"
opt <- twsOption(local = "", symbol = "SPX", expiry="20181115", strike="2700", right="P")
details <- reqContractDetails(tws, opt)
details[[1]]$validExchanges
[1] "SMART" "CBOE"
我看到CBOE是該合約的交易交易所。 您需要查看您是否具有正確的市場數據訂閱,才能查看CBOE上的期權合約。 我認為您需要訂閱OPRA的市場數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.