Duration: 5:22
Level: Intermediate

In this lesson, we will cover the structure of the real-time market data functions and learn to customize these functions.

Contributed By: QuantInsti

Study Notes:

In the previous lesson, we learned about various streaming and historical market data functions which allow pulling market data from the TWS. In this lesson, we will cover the structure of the real-time market data functions and learn to customize these functions.

In this lesson, you would:

  • Learn the structure of real-time market data functions
  • Learn to customize the functions

Arguments to the Market Data Functions

The data functions like reqMktData, reqMktDepth, and reqRealTimeBars all have a special CALLBACK and EventWrapper arguments. By default, the CALLBACK argument calls the twsCALLBACK function from the IBrokers package.

The arguments of the functions can be seen in the image below.

twsCALLBACK Function

The twsCALLBACK function receives the header of each incoming message from the TWS. The header is then passed to the processMsg function along with the eWrapper object.

The eWrapper object has functions to manage all the incoming message types from the TWS. Once the process Message call returns, another cycle of the infinite loop occurs.

This can be understood from the internal code of the twsCALLBACK function shown below.

In this internal code, we can see that the incoming message gets stored in the curMsg variable which is passed to the processMsg function. This function is placed within the infinite while loop. Let us understand the processMsg function.

processMsg Function

The processMsg function internally is a series of if-else statements that branch according to a known incoming message type. Each message received will invoke the appropriately named eWrapper callback. This can be understood from the internal code of the processMsg function shown below.

In this internal code, we can see a series of if-else statements based on incoming message type and has a corresponding eWrapper function.

eWrapper Function

eWrapper is an R closure that contains a list of functions to manage all incoming message type. Each message has a corresponding function in eWrapper to handle the particular details of each incoming message.

The eWrapper function creates an instance of eWrapper closure. By creating an instance of eWrapper we can then modify any or all the particular methods embedded in the eWrapper object.

This image below shows the list of eWrapper methods. The listed methods can be modified for the rapid customization of incoming messages. The data environment is .Data, with accessor methods get.Data, assign.Data, and remove.Data.

Customizing Data Function

Let us take an example to show how customization of data function works.

In this example, the codes shown in the green boxes modify the tws callback function. The code in the first green box assigns symbol names to the eWrapper data. The code in the second box, binds the data in a dataframe for easy readability.

This modified twsCALLBACK function is named as “snapShot” and is used as an argument to the request Market data function.

Let us now compare the data streams using the normal request market data function versus the modified function. On line 11, we run the normal request market data function. As can be seen the streaming data is not easily readable.

Let us now look at the modified request market data function. On line 14, we first source the snapshot R file that contains the modified twsCALLBACK function named as snapshot.

We run the request market data function with the snapshot function as the input to the CALLBACK argument of the request market data function.

As can be seen, the snapshot data output is more convenient to read than the normal output.

Similar to this example, you can try different ways to customize the real-time market data functions based on your requirements.

Summary

In this lesson, we learnt the structure of real-time market data functions and saw how to customize the functions. You can try different ways to customize the data functions based on your requirements.

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


Join the Discussion

Thank you for engaging with IBKR Campus. 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.

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.