joshuaulrich / IBrokers

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

fix #39 algoParams #40

Open fmair opened 1 year ago

fmair commented 1 year ago

By adding algoParams to placeOrder isssue #39 is resolved.

I tested the new function with an order for ES DEC 2023 with the following code and it worked as expected. Note that you'll need a running TWS version and trading access to CME futures:

library(IBrokers)

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

contract <- twsFuture(symbol = "ES",
                       exch = "CME",
                       expiry = "202312",
                       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)

Result in TWS:

image

junyzhou10 commented 1 year ago

I also met the same issue when trying to incorporate algoParams. So I am really happy to see this post. But is the code in #40 the same as that in #39? I failed to see how to add algoParams in placeOrder()... Any hint?

fmair commented 1 year ago

You add the algoParams to the order object as above. The placeOrder function needs to be changed, see: https://github.com/joshuaulrich/IBrokers/pull/40/files. It is just one line.

junyzhou10 commented 1 year ago

Cool!! Brilliant!! Problem solved!!

junyzhou10 commented 1 year ago

I am exploring other IB algoStrategy in R, and got stuck. For example, for VWAP order, I read the IB API document, and trying to implement that in R with this package.

It has the following parameters which I believe should be fill in the algoParams

image

I succeed in code like

algoStrategy = "Vwap",
algoParams = c("1", "maxPctVol", "0.2")

but get no luck when trying to incorporate more parameters, for example,

algoStrategy = "Vwap",
algoParams = c("1", "maxPctVol", "0.2", 
               "2", "startTime", "09:00:00 US/Eastern")

Do you have any suggestion? I guess I might have trouble to understand what the "1" means in algoParams. Thanks!

junyzhou10 commented 1 year ago

I got it... Key is to change the number in c("1", "maxPctVol", "0.2"). "1" means one argument specified.

For detailed example, I put it here: https://github.com/junyzhou10/IBrokers/tree/master