Close Navigation
Learn more about IBKR accounts

Market Data Functions

Lesson 5 of 8
Duration 7:31
Level Intermediate
Close Navigation

In this lesson, we will cover the different types of Market Data, learn about Market data lines and show how to fetch streaming and historical market data from the Trader Workstation.

Contributed By: QuantInsti

Study Notes:

In the previous lesson, we learned about the IBrokers package and some of the basic API methods like establishing connection with TWS, getting account details, and creating contracts for use in API calls. In this lesson, we will cover the different types of Market Data, learn about Market data lines and show how to fetch streaming and historical market data from the Trader Workstation.

In this lesson, you would:

  • Different types of market data and market data lines
  • Learn to fetch streaming and historical market data

Types of Market Data

It is possible to fetch different kinds of market data from the TWS. A live market data subscription is required to have market data for the requested instruments in TWS.

Types of market data include:

  • Top Market Data (Level 1)
  • Market Depth (Level II)
  • Real Time Bars

Market Data Lines

Market data lines represent the active market data requests which a user has made. By default, every user has a maximum ticker Limit of 100 market data lines. Thus, the user can get real time market data of up to 100 instruments simultaneously. An error message is thrown if it has reached the maximum number of simultaneous requests.

Streaming Market Data Functions – The reqMktData function

Streaming level 1 market data can be obtained from the TWS via the reqMktData function.

The function takes arguments like:

conn – a valid twsConnection or twsPlayback connection

Contract – twsContract object(s) requested data for

tickGenerics – comma delimited string of generic tick types

snapshot – should snapshot data be returned

tickerId – ticker id to associate with the returned data

timeStamp – include R time stamps

file – passed to internal cat calls

eventWrapper – eWrapper object

CALLBACK – main receiver callback

Example:

The image below illustrates how the streaming data function works. We first define the contract for which we want to pull the streaming data on line 8.

On line 11, we use the function with the TWS connection, and the security objects as the arguments. Upon execution a continuous stream of market data is returned by the TWS.

By default, there are certain ‘default tick types’ that are returned. Additional data types are available that can be requested by specifying certain ‘generic tick types’ in the market data request.

Callbacks, via CALLBACK and eventWrapper are designed to allow for R level processing of the real-time data stream and provide for more control over the incoming results. We will see how to do this in the next lesson.

Streaming Market Data Functions – The reqMktDepth function

This function allows for streaming market depth i.e. order book data to be handled in R.

The function takes the following arguments:

conn – a valid twsConnection or twsPlayback connection

Contract – twsContract object(s) requested data for

tickerId – ticker id to associate with the returned data

timeStamp – include R time stamps

playback – playback speed adjustment

file – passed to internal cat calls

eventWrapper – eWrapper object

CALLBACK – main receiver callback

Example:

The image below illustrates how the streaming market depth function works. We first define the contract for which we want to pull the data on line 8. On line 11, we use the function with the TWS connection, and the security objects as the arguments.

The output can be seen in the console. We can see the market depth data up to 5 best bid and ask prices.

Streaming Market Data Functions – reqRealTimeBars Function

This function allows for streaming real-time bars to be handled in R.

The function takes the following arguments:

conn – a valid twsConnection or twsPlayback connection

Contract – twsContract object(s) requested data for

tickerId – ticker id to associate with the returned data

whatToShow – type of data to be extracted

bar size – bar size to retrieve

file – passed to internal cat calls

eventWrapper – eWrapper object

CALLBACK – main receiver callback

Example:

Let us now see how the real time bars function works. We first define the contract on line 8. We construct the function on line 11 with tws connection, security object, and the whatToShow as the arguments. Upon execution, a continuous stream of real time bars data is returned by the TWS.

Historical Market Data Function – reqHistoricalData Function

Historical market data is obtained from the TWS via the reqHistoricalData function. The data is delivered in the form of candlesticks.

The function takes the following arguments:

tickerId – a unique identifier which will serve to identify the incoming data

Contract – a twsContract you are interested in

endDateTime – request’s end date and time

duration – time span that the request will cover

barSize – bar size to retrieve

whatToShow – type of data to be extracted

Timeformat – format in which the incoming bar’s data should be presented

eventHistoricalData – callback function to process data

file – file to write data to

Valid Duration String Units – The duration string must be of the form ‘n unit’ where the unit string can be any of strings shown in the table below.

Valid Bar Sizes – The valid bar sizes must be specified exactly as shown in the below table. However, there is no guarantee from the API that all will work for all securities or durations.

Valid whatToShow values – The whatToShow values can be any one of the following type: Trades, Midpoint, Bid, Ask, and Bid_Ask

Example:

Let us now see how the historical data function works. We first define the contract for which we want to pull the historical data on line 8. On line 11, we use the function with the TWS connection, and the security objects as the arguments. Upon execution the output can be seen in the console.

In the next example, we pull the 1 minute data for the duration of 1 week by specifying the respective arguments. Upon execution the 1 minute data can be seen in the console.

Summary

In this lesson, we covered the types of market data, learnt about market data lines and saw the working of the various streaming and historical market data functions. In the next lesson, we will learn how to customize these market data functions.

Note – The IBrokers package provides native R access to Interactive Brokers Trader Workstation API

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

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.

IBKR Campus Newsletters

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.