R IBrokers reqopenorders not updating



我正在使用IBrokers包中的代码查询TWS Interactive Brokers中的挂起订单列表。

函数reqOpenOrders(tws(中有一个错误,因为它如这里所示挂起,即使它工作时,输出也很混乱,如这里所述。

我把来自不同来源的一些代码放在一起,创建了一个清晰/合乎逻辑的函数,同时输出结果的data.frame。

函数如下,第一次使用时效果良好。然而,当在TWS中进行更改(如取消待定订单或下跟踪订单(时,如果在R中运行以下功能,则不会更新更改。

如果我关闭并再次打开与TWS的连接,在运行下面的函数后,我会得到一个空的data.frame。如果我关闭所有连接并尝试该函数几次,最终它会返回更新的结果。

有人知道为什么调用函数不能查询TWS中最新的更改吗?

我认为这与R连接TWS有关。该函数似乎在等待连接最终可用。如果我在第一次运行该函数时重新启动R,结果会更新,这表明如果恢复了所有连接,该函数运行良好。对这个问题有什么建议吗?

我想知道退出功能后是否需要关闭所有插座和连接?我尝试了不同版本的解决方案,但没有成功。

非常感谢您的帮助。

library(IBrokers)
Get.Open.Orders<- function(tws)
{
library(dplyr)
Open.Orders <- function(tws)
{
con <- tws[[1]] #connector used to communicate with TWS
VERSION <- "1"
writeBin(c(.twsOutgoingMSG$REQ_OPEN_ORDERS, VERSION), con) #send request for open orders
eW  <- eWrapper()                         #Creates a custom function to get the data that was requested
socketSelect(list(con), FALSE, NULL)      #Wait for first socket connection to become available
curMsg <- readBin(con, character(), 1L)   #read the response received from IB            
processMsg(curMsg, con, eW)               #Process message 
}
#orginize the data recieved into a data.frame, selects only  set of vaiables received.
open <- data.frame()
i <- 0
while(i < 2)
{
x <- Open.Orders(tws)
if(!is.null(x) && x[1] == 53) # OPEN_ORDER_END
{i = i + 1 } else if(!is.null(x) && x[1] == 5) # For Open Orders
{
x <- data.frame(t(x), stringsAsFactors = FALSE)
open <- bind_rows(open, x)
}
rm(x)
}
if(nrow(open) > 0)
{
open <- open %>% distinct() %>%
rename(conId = X4, symbol = X5, sectype = X6, strike = X10,
currency = X11, action = X13, totalQuantity = X14,
orderType = X15, lmtPrice = X16, auxPrice = X17,
tif = X18, outsideRTH = X19, account = X20, orderId = X25, Status = X77
) %>%
select(account, orderId, conId, symbol, sectype, strike, currency,
action, totalQuantity, orderType, lmtPrice, auxPrice, tif, Status) %>%
mutate(orderId = as.integer(orderId)
, totalQuantity = as.numeric(totalQuantity)
, lmtPrice = as.numeric(lmtPrice)
, auxPrice = as.numeric(auxPrice) )
} else
{
open <- data.frame(account = character()
, orderId = integer()
, conId = character()
, symbol = character()
, sectype = character()
, strike = character()
, currency = character()
, action = character()
, totalQuantity = numeric()
, orderType = character()
, lmtPrice = numeric()
, auxPrice = numeric()
, tif = character()
, Status = character()
, stringsAsFactors = FALSE)
}
assign("IB.open.orders", open, envir = .GlobalEnv)
rm(i, Open.Orders, open)
return(data.frame(IB.open.orders))
}

#now connect to IB and get active orders; it works fine first time
tws = twsConnect()
Get.Open.Orders (tws)
# now go to TWS and delete any given pending order for the sake of an example. Then in R run function again to request pending orders
Get.Open.Orders (tws)
#The change in TWS is now reflected in the output, so I disconnect TWS, and connect again
twsDisconnect(tws)  #disconned TWS
tws = twsConnect()  #connect to TWS
Get.Open.Orders (tws)
#the result is an empty dataframe. If I run the three lines of code above...say 10 times, eventually the updated results are returned.
showConnections(all = TRUE)
closeAllConnections()
tws = twsConnect()
Get.Open.Orders (tws)

这可能不是最好的解决方案,但我找到了一种方法,可以从TWS获得最新的未结订单数据。上面的原始功能似乎只有在使用正确的连接时才能工作。在两次尝试之间,它返回一个空的data.frame。所以我所做的是创建一个while循环,将查询发送到TWS,直到使用正确的连接。代码如下。它可以工作,并在几次尝试后返回正确的数据。我想我共享代码是作为一个临时解决方案。

tws = twsConnect()
#Current pending orders
OpenOrders=Get.Open.Orders (tws)
showConnections(all = TRUE)    
nOP=nrow(OpenOrders)
while (nOP==0){
tws = twsConnect()
OpenOrders=Get.Open.Orders (tws)  
nOP=nrow(OpenOrders)
quiet = TRUE
}

相关内容

  • 没有找到相关文章

最新更新