Requesting live data through TWS API with C# .Net

Author: Joe
Tutorial Objective: Request live price data from Interactive Brokers TWS api using C# .Net
Souce 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 Connecting to Interactive Brokers TWS Using C# .Net 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 tick updates and display those updates in a ListBox control. Go ahead and open up your code from Connecting to Interactive Brokers TWS Using C# .Net. If you did not complete the tutorial, you can download the full source code at the end of its page.

The first thing we need to do is set up a way to see the output. Go ahead and place a ListBox control on the form. The next thing we need to do is tell C# which function we want to receive information about tick data. Add the following line inside the Form1_Load function at the end:

Tws1.tickPrice+=new AxTWSLib._DTwsEvents_tickPriceEventHandler(this.tickData);


This tells C# that we are going to later create a function called tickData that will receive the tick information.

Now, inside Form1_Load function, add the following line to the end:

Tws1.reqMktData(1, "MSFT", "STK", "", 0, "", "", "SMART", "", "USD","");

What this function call does is very simple. It basically tells TWS that we want to start receiving market data for a certain security.

  • 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: Special Tick Data


  • Now that we have told TWS we want to start receiving data, our next step is to specify a function to receive the incomming data. Add the following function:

    private void tickData(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
    {
    String msg = "Price Update: Id="+e.id+" Type: "+e.tickType+" Price: "+e.price;
    listBox1.Items.Add(msg);
    }


    What the above function does is receives all incomming market price data. For the purposes of this tutorial we are just sticking the data into a ListBox. You will notice that e has 3 important members. The first is e.id. This specifies the id that the data belongs to, this will match up with the id you sent with the request. The second important piece is e.price. This will contain the price data. However, the price may be last tick, bid price, ask price, high price, low price, or close price. The way to know what information is being sent is to look at the e.tickType variable. The following is what each value means:

  • tickType=1 means bid price


  • tickType=2 means ask price


  • tickType=4 means last price


  • tickType=6 means high price


  • tickType=7 means low price


  • tickType=9 means close price


  • You will notice that all we are receiving is price updates. What about volume updates? Ask size, bid size? For information on how to retrieve that information continue to Part 2

    Download the full source for this tutorial Here

    Similar tutorials

     

     

     

     

     

    Copyright© 2007 StockBotProgramming.com