This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

Duration: 5:04

Contributor: QuantInsti

Level: Intermediate

In this lesson, we cover the various order functions available in the IBrokers package.

Read More

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

The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. 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.

Supporting documentation for any claims and statistical information will be provided upon request.

Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

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.

trading top