Study Notes:
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.
In this lesson, you would:
- Learn how to construct different types of orders
- How to place or cancel orders to TWS
Order Function Types
The orders functions covered in the lesson include:
- reqIds function (helper function)
- twsOrder function
- placeOrder function
- cancelOrder function
reqIds Function
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:
twsOrder Function
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:
placeOrder and cancelOrder Functions
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:
Summary
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
Disclosure: Interactive Brokers
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.
Disclosure: R API Disclosure
This software is in no way affiliated, endorsed, or approved by Interactive Brokers or any of its affiliates. It comes with absolutely no warranty and should not be used in actual trading unless the user can read and understand the source. IBrokers is a pure R implementation of the TWS API.