Requesting Snapshot Data using TWS API with C# .Net Part 1

Author: Joe
Tutorial Objective: Request Snapshot data using Interactive Brokers TWS api using C# .Net
Source Code:  Download the full source for this tutorial Here

Disclaimer

This tutorial is to be used at your own risk. By using this tutorial you agree not to hold the writers of this tutorial, StockBotProgramming.com, or anyone affiliated with StockBotProgrammign.com liable for any damages that may occur as a result of using this tutorial and/or provided source code.

This tutorial will assume you have a copy of Visual Studio 2005 and have Interactive Broker's TWS API installed. You can get a copy Visual Studio Here, and the TWS API is available for download Here.

It is highly recommended that you read Requesting live data through TWS API with C#.Net Part before attempting this tutorial. If you have any suggestions regarding this tutorial or if you notice any mistakes please suggest it here.

Today we are going to modify our code to receive snapshot data. Go ahead and open up your code from Requesting live data through TWS API with C# .Net . If you did not complete the tutorial, you can download the full source code at the end of its page.

In the previous tutorials we learned how to receive live quote data. Interactive brokers offers another way to get live data, 5 second snapshots. If you are building time bars bigger than 5 seconds, then this method of requesting data has several advantages. The primary advantage is that volume data for the 5 second snapshot will be accurate. When building time bars using the previously mentioned method for requesting live quote data it is challenging to build accurate bars. By using 5 second snapshots we can build any size timebar that is divisible by 5 seconds. Best of all they are more accurate.

First verify that you are using the latest version of the TWS lib from Interactive Brokers. Now we need to to replace the call to Tws1.reqMktData with the following code:

Tws1.reqRealTimeBars(1, "MSFT", "STK", "", 0, "", "", "SMART", "", "USD",0, 5, "TRADES", 0);

What this function does is tell TWS that we want to receive a snapshot every 5 seconds for MSFT.

  • Parameter 1: This is an ID value. Essentially anytime you make a request for market data for a new security, give the function a new id. When data for that security is returned it does not tell you what symbol the data belongs to. Instead it will return that ID back. You then match up your ID's to determine which symbol the market data coming in goes to.
  • Parameter 2: The symbol for the security
  • Parameter 3: Security type, valid values are "STK", "FUT","OPT","IND","FOP","CASH"
  • Parameter 4: Expiration, in the format YYYYMM, just send "" if the security your trading does not have an expiration date
  • Parameter 5: Put/Call send "PUT", or "CALL". If your not trading options send ""
  • Parameter 6: Option multiplier
  • Parameter 7: Exchange the security is traded on
  • Parameter 8: Primary Exchange, useful when a security trades on multiple exchanges
  • Parameter 9: Currency the security trades in
  • Parameter 10: IsExpired. Useful for when getting data about an expired contract. Put 0 if no, and 1 if yes.
  • Parameter 11: Bar size, at the moment you must put 5. Since TWS only supports 5 second snapshots.
  • Parameter 12: What to show. This can be either "TRADES" or "MIDPOINT".
  • Parameter 13: Put a 1 if you only want to receive data from the contracts open hours and not from extended trading.

Now TWS knows you want to receive snapshot data, but we have to tell it what function is to receive the incoming data. Add the following line inside your FormLoad function.

Tws1.realtimeBar += new AxTWSLib._DTwsEvents_realtimeBarEventHandler(this.realTimeBar);

Now we need to declare the function realTimeBar to receive the snapshot info. Add the following function:

private void realTimeBar(object sender, AxTWSLib._DTwsEvents_realtimeBarEvent e)
{
Console.WriteLine("5 Second Snapshot: " + e.close);
}

That is all you need to do. Now you will receive the 5 second snapshot data every 5 seconds. In the next tutorial we will show you how to aggregate your 5 second snapshots into 1 minute time bars.

Download the full source for this tutorial Here

Similar tutorials

 

 

 

 

 

 

 

Copyright© 2007 StockBotProgramming.com