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