joshuaulrich / IBrokers

R API to Interactive Brokers Trader Workstation
65 stars 54 forks source link

placeOrder does not include/transmit algoParams #39

Open fmair opened 1 year ago

fmair commented 1 year ago

Description

In IBrokers 0.10-2 the function IBrokers::placeOrder creates an order with algoStrategy that is written to the connection, but algoParams is not, while it is an argument to twsOrder. Therefore, orders that were created including algoParams tend to be rejected by TWS.

In plain terms: We can tell TWS to create a market order with "algoStrategy" == "Adaptive", but not with an adaptivePriority, e.g. "Urgent".

Expected behavior

The function should include algoParams such that algo orders can be created using IBrokers

Minimal, reproducible example

library(IBrokers) # v 0.10-2

tws <- twsConnect(port = 7496, verbose = TRUE)

contract <- twsFuture(symbol = "ES",
                       exch = "CME",
                       expiry = "202306",
                       currency = "USD")

order <- IBrokers::twsOrder(action = "BUY",
                            totalQuantity = 1,
                            orderType = "MKT",
                            tif = "DAY",
                            transmit = FALSE,
                            algoStrategy = "Adaptive",
                            algoParams = c("1", "adaptivePriority", "Patient")

placeOrder(twsconn = tws,
           Contract = contract,
           Order = order)

Suggested fix

Include Order$algoParams, in line 65 in the placeOrder function. It works on my local version. I'll try to come up with a merge request.

Session Info

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] twsInstrument_1.4-10      FinancialInstrument_1.3.1 quantmod_0.4.20           TTR_0.24.3                IBrokers_0.10-2           xts_0.13.0               
[7] zoo_1.8-11               

loaded via a namespace (and not attached):
[1] compiler_4.2.1  tools_4.2.1     rstudioapi_0.14 curl_4.3.2      grid_4.2.1      lattice_0.20-45