Requesting live data through TWS API with Visual Basic .Net
Author: Joe
Tutorial Objective: Request live
price data from Interactive Brokers TWS api using Visual Basic .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 Visual
Basic .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 Visual
Basic .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. Now, inside Form1_Load function,
add the following line to the end:
Call 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 Sub Tws1_LiveData(ByVal eventSender
As System.Object, ByVal eventArgs As AxTWSLib._DTwsEvents_tickPriceEvent)
Handles Tws1.tickPrice
Dim strData As String = ""
strData = "id=" & eventArgs.id & " Type" & eventArgs.tickType & "
Price=" & eventArgs.price
ListBox1.Items.Add(strData)
End Sub
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 eventArgs has 3 important members. The first
is eventArgs.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 eventArgs.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
eventArgs.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
|