这个问题更多地是关于IBroker R软件包,而不是关于编程。

我发现围绕“未结订单”输入了几个条目,但没有一个就足够了。

问题在于可靠性及其输出结构。首先,我想拥有一个reqOpenOrders函数,该函数可让我分配输出,因此我删除了while()部分。

reqOpenOrders <- function(twsconn) {
    .reqAllOpenOrders(twsconn)
     con <- twsconn[[1]]
      eW  <- eWrapper()
    socketSelect(list(con), FALSE, NULL)
   curMsg <- readBin(con, character(), 1L)
processMsg(curMsg, con, eW) }

每次运行该函数时,都会得到不同的数据结构(结果)!
conn <- ibgConnect(); reqOpenOrders(conn)

因此,为了捕获许多输出(每次运行reqOpenOrders),我编写了一个小循环。
x <- list()
for(i in 1:5){
x[[i]] <- reqOpenOrders(conn)
}

它们的输出有多种变化:(我无法真正将输出与所有输出的含义相关联)
[[1]]
[1] "5"                        "22"                       "4"                         "46189223"                 "NZD"                      "CASH"
[7] ""                         "0"                        "?"                        "IDEALPRO"                 "CAD"                      "NZD.CAD"
[13] "SELL"                     "5000"                     "LMT"

或者
 [[2]]
[1] "3"         "6"         "4"         "Submitted" "0"         "5000"      "0"         "9257XXXXX" "0"         "0"         "3"         ""

或者
   [[3]]
[1] "53" "1"

任何帮助是极大的赞赏。

最佳答案

第一个元素代表信息的类型。有关详细信息,请检查“.twsIncomingMSG”。在上述情况下,您的函数将撤消三种类型的信息

# ORDER_STATUS (3)
# OPEN_ORDER (5)
# OPEN_ORDER_END (53)
Get.Open.Orders <- function(tws)
{
  Open.Orders <- function(tws)
                  {
                    .reqOpenOrders(tws)
                    con <- tws[[1]]
                    eW  <- eWrapper()
                    socketSelect(list(con), FALSE, NULL)
                    curMsg <- readBin(con, character(), 1L)
                    processMsg(curMsg, con, eW)
                  }

  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
                   ) %>%
            select(account, orderId, conId, symbol, sectype, strike, currency,
                   action, totalQuantity, orderType, lmtPrice, auxPrice, tif) %>%
            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()
                       , stringsAsFactors = FALSE)
  }

  assign("IB.open.orders", open, envir = .GlobalEnv)
  rm(i, Open.Orders, open)
}

关于r - 带有R的未结订单状态Ibroker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40347935/

10-09 17:08