- Solve real problems with our hands-on interface
- Progress from basic puts and calls to advanced strategies
Lesson 7 of 8
In the previous lesson, we learned how to customize market data functions which allows us to pull the market data from the TWS in the required format. In this lesson, we cover the various order functions available in the IBrokers package.
The orders functions covered in the lesson include:
This is a helper function which is used to obtain the next valid order ID for use with the TWS. The function takes a valid tws connection object as its argument. The function returns a character representation of the next numeric ID.
The syntax of the function is given as:
reqIds(conn)
Where
conn – a valid twsConnection of class twsconn.
Example:

The function creates a twsOrder object which is used in the placeOrder API call. This function gives the flexibility to construct different types of orders as per the requirement of the strategy.
The function takes arguments like:
orderId – the id for the order
action – identifies the side (BUY,SELL,SSHORT)
totalQuantity – order quantity
orderType – order type (MKT, LMT, PEGMKT, STP, STPLMT etc.)
lmtPrice – the LIMIT price for LMT, STPLMT, and REL order type.
transmit – specify whether the order is transmitted to the TWS
trailStopPrice – For TRAILLIMIT orders only
Example:

These functions are used to place or cancel an order to the TWS.
The syntax of the function is given as:
placeOrder(twsconn , Contract, Order)
cancelOrder(twsconn , orderId)
The function takes the following arguments:
twsconn – a twsConnection object
Contract – a twsContract object
Order – a twsOrder object
orderId – a valid order ID
Example:

In this lesson, we covered the various order functions available in the IBrokers package and provided a simple explanation with example. Knowledge of these order functions is essential to build an automated trading system.
Note – The IBrokers package provides native R access to Interactive Brokers Trader Workstation API
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Join The Conversation
For specific platform feedback and suggestions, please submit it directly to our team using these instructions.
If you have an account-specific question or concern, please reach out to Client Services.
We encourage you to look through our FAQs before posting. Your question may already be covered!