Requesting live quote data using TWS API and C++
Author: Joe
Tutorial Objective: Request live quote
data from TWS and print the data to the screen..
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++
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 request live quote data
for MSFT and print it to the screen. Go ahead and open up your code
from Connecting to Interactive Brokers TWS Using C++.
If you did not complete the tutorial, you can download the full
source code at the end of its page.
If you have already completed the c++ requesting historical quote
data tutorial then this one will seem very familiar. The first thing
we will want to do is add the following line into the function event_error()
if you have not already done so:
printf("\nError %s",error_string);
We do this so that we will know if any errors happen while attempting
to request our data. Now go to the main function. We will need to
specify a security that we wish to get data for. Add the following
lines:
tr_contract_t contract;
memset(&contract, 0, sizeof contract);
contract.c_symbol = "MSFT";
contract.c_sectype = "STK";
contract.c_expiry = "";
contract.c_right = "";
contract.c_multiplier = "";
contract.c_exchange = "SMART";
contract.c_primary_exch = "";
contract.c_currency = "USD";
contract.c_local_symbol = "";
The above code specifies which security we want to retrieve live
data for. The most commonly changed attributes are:
- c_symbol: This is the symbol for
the security.
- c_sectype: This is the type of
security, valid values are: "STK","FUT","OPT","IND","FOP","CASH"
- c_exchange: Here you specify what
exchange the security trades on.
- c_currency: What currency the security
trades in.
Now we have the security defined. The next step is to simply make
the request. To do so add the line:
tws_req_mkt_data(tws,0,&contract);
This function call has 2very important parameters.
- Parameter 2: 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 3: The contract structure that specifies which product
we wish to request data for.
Now that the request has been made we need to ensure that the program
will not exit before the quotes come in. Add the following lines:
while(1==1)
{
}
When live quote data comes in, the data will come in through one
of two functions. If the data deals with a price such as close,
last, bid, ask.... then the event_tick_price() function is called.
If the data deals with a size such as volume, bid size, ask size...
then the event_tick_size() function will be called. Knowing this
add the following lines to the event_tick_price() function:
printf("\nPrice Update: Ticker Id: %d
Type: %d Price: %f",ticker_id,field,price);
In the above print statement you see a variable called "field".
This value specifies which type of price data is being sent:
- field=9 means close price
Now that we can capture price data updates we need to add the following
line to the event_tick_size() function:
printf("\nVolume Update: Ticker Id:
%d Type: %d Size: %d",ticker_id,field,size);
In this function the "field" value has the following
meanings:
- field=5 means last trade size
Now you should be able to make TWS send you all the live price/volume
information you need to make your trading decisions.
Download the full source for this tutorial Here
|