Requesting historical data through TWS API with C# .Net
Author: Joe
Tutorial Objective: Request historical
quote data through TWS and display it in a ListBox
Souce Code: Download
the full source for this project 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 read historical data from
MSFT and put it into a table. 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.
From within the code, add the following lines to the end of the
Form_Load event.
Tws1.reqHistoricalData(1, "MSFT",
"STK", "", 0, "",
"", "smart", "usd",0, "20070424
11:01:00",
"3600 S", "5 mins",
"MIDPOINT", 1, 1);
This is the function call that will tell TWS that you want to receive
some historical data.
Parameter 1: 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 2: TICKER Symbol
Parameter 3: Security Type, This is the type of security you are
requesting data for. Valid values "STK","FUT","OPT","IND","FOP","CASH". The
two most commonly used ones are "STK" for stock, and "FUT" for futures.
Parameter 4: Expiration Date in the format YYYYMM, as you can see in my
above example, you can just put "" if you are requesting data for a security
type that does not have expiration dates.
Parameter 5: Strike Price, this is another field you can just put 0 in
if dealing with a security type that does not use it.
Parameter 6: This field is for "PUT" or "CALL" when dealing with
options, other times just put ""
Parameter 7: Multiplier, used when multiple contracts exist such as with
futures. A valid value is to put ""
Parameter 8: Exchange, for most things you can just put "SMART",
however some securities such as futures you will have to use other values.
For example "ECBOT"
Parameter 9: Currency, selects which currency the security trades in.
Normally just enter "USD"
Parameter 10: isExpired
Parameter 11: 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. As you can see in my
above example I am requesting historical data up until April 24, 200 at
11:01. This date has to be within the past 6 months.
Parameter 12: 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 13: Bar Size, this is what size each data bar sent will be in. Valid
values are 1 secs, 5 secs, 10 secs, 15 secs, 30 secs, 1 min, 2 mins,
5 mins, 10 mins, 15 mins, 30 mins,1 hour, 1 day, 1 week, 1 month,
3 months, 1 year
Parameter 14: What to show. This field specifies what data to show,
such as "TRADES", "MIDPOINT", "BID","ASK","BID/ASK"
Parameter 15: 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 16: 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 we need to register the event handler for historical data, so also add
the following line to the form_load event.
Tws1.historicalData+=new AxTWSLib._DTwsEvents_historicalDataEventHandler(this.historicalData);
This line tells C# that we will be using a function called historicalData
for all historicalData events. Now add this function:
private void historicalData(object sender, AxTWSLib._DTwsEvents_historicalDataEvent
e)
{
String msg = "Id="+e.reqId+" Date: "+e.date+" open: "+e.open+" high:
"+e.high+" low: "+e.low+" close: "+e.close+" volume: "+e.volume;
listBox1.Items.Add(msg);
}
That function will receive the historical data events and put the
information into a list box. We have not yet created this list box, so go to
to the form view and place a list box on the form. You may want to make it
wide so that the data fits correctly. We are now ready to run the code, it
may take a minute for your list box to populate with data. If all worked
correctly, you are now one step closer to having a stock bot.
Download the full source for this project Here
Similar tutorials
|