簡體   English   中英

R IBrokers reqopenorders 未更新

[英]R IBrokers reqopenorders not updating

我正在使用來自 IBrokers package 的代碼查詢 TWS Interactive Brokers 中的掛單列表。

function reqOpenOrders(tws) 有一個錯誤,它掛起,如此處所示,即使它工作,輸出也是混亂的,如此處所示

我將來自不同來源的一些代碼片段放在一起,以創建一個 function ,它在輸出結果的數據幀時清晰/合乎邏輯。

下面是function,第一次用就很好用。 但是,當在 TWS 中進行更改時(如取消掛單或下跟蹤訂單),如果在 R 中運行下面的 function,則更改不會更新。

如果我關閉並再次打開與 TWS 的連接,我會在運行下面的 function 后得到一個空的 data.frame。 如果我關閉所有連接並多次嘗試 function,它最終會返回更新的結果。

有誰知道為什么撥打function查詢不到TWS的最新變化?

我認為這與 R 與 TWS 的連接有關。 似乎 function 等待連接最終可用。 如果我重新啟動 R,第一次運行 function,結果會更新,提示如果所有連接都恢復,function 工作正常。 對這個問題有什么建議嗎?

請問sockets和function退出后是否需要關閉所有連接? 我嘗試了這個解決方案的不同版本但沒有成功。

任何幫助是極大的贊賞。

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 獲取有關未結訂單的最新數據。 上面的原始 function 似乎只有在使用正確的連接時才有效。 在兩次嘗試之間,它返回一個空的 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM