|
|
Requesting historical data through TWS API with C++
Author: Joe
Tutorial Objective: Request historical
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 read historical data from
MSFT and print it to the screen. Go ahead and open up your code from
Connecting to Interactive Brokers TWS Using C++t.
If you did not complete the tutorial, you can download the full source
code at the end of its page.
The first thing we will want to do is add the following line into the function event_error:
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 historical
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_historical_data(tws, 1, &contract, "20070429
16:26:44", "3600 S", 5, "TRADES", 1, 1);
Parameter 1: This is a pointer to our tws connection.
Parameter 2: This is your request ID. This ID needs to be unique. Used
later so you know which request the incomming data belongs to and used if
you need to cancel the request. In our example we just used 1. However, if
you went to make a second request in the same program you need to change the
ID to something new each time.
Parameter 3: The security contract that we defined a few steps before.
Parameter 4: End Date, this specifies the date at which you want the
historical data query to end. The format for the date is: yyyymmdd HH:mm:ss
zzz The "zzz" section is optional and is for timezone.
Parameter 5: Duration, in my example I am specifying 3600 seconds which
is 60 minutes. The value is entered by an integer value followed by either
"S" for seconds, "D" for days, or "W" for week
Parameter 6: Bar Size, this is what size each data bar sent will be in.
Valid values are 1 for 1 second, 2 for 5 seconds, 3 for 15 seconds, 4 for 30
seconds, 5 for 1 minute, 6 for 2 minutes,7 for 5 minutes,8 for15 minutes, 9
for 30 minutes, 10 for 1 hour, 11 for 1 day.
Parameter 7: What to show. This field specifies what data to show,
such as "TRADES", "MIDPOINT", "BID","ASK","BID/ASK"
Parameter 8: Use real time hours. Set this value to "1" if you want
data only from the real time hours. If you don't care if the data comes from
pre or after market use "0"
Parameter 9: Date format. You have 2 choices in how the data is
returned for each data bar. "1" means the date will be in the format
yyyymmdd{space}{space}hh:mm:dd and "2" means the date will be a timestamp.
Now our historical request has been made. Our next step is to make sure our program
does not terminate before our data comes in. Add the following lines after the request:
while(1==1)
{
}
We are almost done. The last thing we need to do is add the following line to our event_historical_data function.
printf("\nData: Date: %s Open: %f High: %f Low:
%f Close: %f Volume: %d",date,open,high,low,close,volume);
Now make sure TWS is running and run this program. You should see quotes printed to the screen.
Download the full source for this project Here
|
|
|