我正在使用来自IBrokers包的代码查询TWS Interactive中悬而未决的订单列表。
函数reqOpenOrders(tws)中有一个错误,因为它挂起了如图所示的here,而且即使当它工作时输出也是混乱的,如所示的here。
我从不同的来源收集了一些代码片段,以创建一个清晰/合乎逻辑的函数,同时输出结果的data.frame。
该函数如下所示,它在第一次使用时运行良好。但是,当在TWS中进行更改时(例如取消挂起的订单,或发出跟踪订单),如果在R中运行下面的函数,则不会更新更改。
如果我关闭并再次打开到TWS的连接,则在运行下面的函数后得到一个空的data.frame。如果关闭所有连接并多次尝试该函数,它最终会返回更新的结果。
有人知道为什么调用函数不查询TWS中更新最多的更改吗?
我认为这与与TWS的R连接有关。该函数似乎在等待连接最终可用。如果我重新启动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)发布于 2022-03-14 20:39:54
这可能不是最好的解决方案,但我找到了从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
}https://stackoverflow.com/questions/71453840
复制相似问题