Search code examples
ribrokerstws

IBrokers: queueing up an order using R


I'm trying to do a simple order through the Interactive Brokers API using R.

For example, I'm trying to buy 1 share of IBM and short 1 share of MSFT.

I cannot find any documentation on how to do this step in R. Does anyone have familiarity working in R with the TWS API?

Thank you!


Solution

  • There is a pretty good project on cran called IBrokers, which wraps the C++ API for IB.

    You can find it on cran: http://cran.r-project.org/web/packages/IBrokers/index.html

    Have a look at the vignettes for a good reference on how to get data and set orders:

    About the general setup and receiving data: http://cran.r-project.org/web/packages/IBrokers/vignettes/IBrokers.pdf

    Also, I can really recommend the cheat sheet: http://cran.r-project.org/web/packages/IBrokers/vignettes/IBrokersREFCARD.pdf

    --

    So, to set an order, use the placeOrder Object, which you supply with the connection details (those are described in the general setup I linked) :

    placeOrder(twsconn=tws,Contract=twsSTK("IBM"),Order=twsOrder(reqIds(tws),"BUY",1,"MKT"))
    placeOrder(twsconn=tws,Contract=twsSTK("MSFT"),Order=twsOrder(reqIds(tws),"SELL",1,"MKT"))
    

    Here, both are market orders.

    I hope that gives you a starting point.