Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open order status Ibroker with R

Tags:

r

This question is rather more about IBroker R package and not so about programming.

I have found couple of entries around 'open orders' non of them is sufficient however.

The problem lies in reliability and its output structure. First I would like to have a reqOpenOrders function that allows me to assign the output so I remove the while() part.

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

Everytime I run the function I get different data structure(results)!

conn <- ibgConnect(); reqOpenOrders(conn) 

So in order to catch the many outputs (every time you run the reqOpenOrders) I wrote small loop.

x <- list()
for(i in 1:5){
x[[i]] <- reqOpenOrders(conn)
}

There are various variations of output here they are: (I cannot really associate the output and the meaning of all of them)

[[1]]
[1] "5"                        "22"                       "4"                         "46189223"                 "NZD"                      "CASH"                    
[7] ""                         "0"                        "?"                        "IDEALPRO"                 "CAD"                      "NZD.CAD"                 
[13] "SELL"                     "5000"                     "LMT"                      

or

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

or

   [[3]]
[1] "53" "1" 

Any help is greatly appreciated.

like image 484
Maximilian Avatar asked Oct 31 '16 18:10

Maximilian


Video Answer


1 Answers

The first element represents the type of information. Check '.twsIncomingMSG' for details. In the case above, your function will revoke three types of information

# 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)
}
like image 92
qed Avatar answered Oct 01 '22 21:10

qed