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 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.