Requesting historical data through TWS API with C# .Net Part 2

Author: Joe
Tutorial Objective: Requesting historical quote data from TWS and then storing it in a comma seperated file for backtesting.
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 Requesting historical data through TWS API with 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 build off of the source code from Requesting historical data through TWS API with C# .Net and add code to request and save the historical data in a comma separated format for use with backtesting. The first thing we must do is go to the form designer window. We will have to make a lot of changes to our current form. Start off by deleting the ListBox control.

Now we need to add a ton of labels to our form, add the following labels: Symbol, Security Type, Expiration, Strike Price, Call/Put, Multiplier, Exchange, Currency, End Date, Duration, Bar Size, What to Show, Real Time Hours, Date Format, File Name:

Now add the following text boxes to your form next to the appropriate label: txtSymbol, txtExpiration, txtStrike, txtCallPut, txtMultiplier, txtExchange, txtCurrency, txtEndDate, txtDuration, txtFileName

Add the following combo boxes: comboSecurity, comboBarSize, comboShow, comboRealTime, comboDateFormat

Add a button called btnRun. We now have all of our form controls in place. We have to add some default values to these controls. For txtSymbol put "MSFT". Put 0 in txtStrike. Set txtExchange to "SMART", txtCurrency "USD", txtEndDate "20070424 11:01:00", txtDuration "3600 S", and txtFileName "c:\csharpdata.txt"

Our next step is to put informatino into our combo boxes. Inside the form_load event add the following code:

comboSecurity.Items.Add("STK");
comboSecurity.Items.Add("FUT");
comboSecurity.Items.Add("OPT");
comboSecurity.Items.Add("IND");
comboSecurity.Items.Add("FOP");
comboSecurity.Items.Add("CASH");
comboSecurity.SelectedIndex = 0;

comboBarSize.Items.Add("1 secs");
comboBarSize.Items.Add("5 secs");
comboBarSize.Items.Add("15 secs");
comboBarSize.Items.Add("30 secs");
comboBarSize.Items.Add("1 min");
comboBarSize.Items.Add("2 mins");
comboBarSize.Items.Add("5 mins");
comboBarSize.Items.Add("15 mins");
comboBarSize.Items.Add("30 mins");
comboBarSize.Items.Add("1 hour");
comboBarSize.Items.Add("1 day");
comboBarSize.Items.Add("1 week");
comboBarSize.Items.Add("1 month");
comboBarSize.Items.Add("3 months");
comboBarSize.SelectedIndex = 4;

comboShow.Items.Add("TRADES");
comboShow.Items.Add("MIDPOINT");
comboShow.Items.Add("BID");
comboShow.Items.Add("ASK");
comboShow.Items.Add("BID/ASK");
comboShow.SelectedIndex = 0;

comboRealTime.Items.Add("0");
comboRealTime.Items.Add("1");
comboRealTime.SelectedIndex = 1;

comboDateFormat.Items.Add("1");
comboDateFormat.Items.Add("2");
comboDateFormat.SelectedIndex = 0;


Now we need to add a few global variables. At the top of the class declaration add the following lines:

int intId = 0;
String globalSymbol = "";
System.IO.TextWriter oWrite;


In those lines we have our id so that we can give each new data request a unique id. We also have our variables for our file we are going to be writing to.

Inside the button run click event we are going to need to add the following code:

String symbol = txtSymbol.Text;
String type = (String)comboSecurity.SelectedItem;
String expiration = txtExpiration.Text;
String strike = txtStrike.Text;
String callput = txtCallPut.Text;
String multiplier = txtMultiplier.Text;
String exchange = txtExchange.Text;
String currency = txtCurrency.Text;
String enddate = txtEndDate.Text;
String duration = txtDuration.Text;
String barsize = (String)comboBarSize.SelectedItem;
String show = (String)comboShow.SelectedItem;
String realtime = (String)comboRealTime.SelectedItem;
String dateformat = (String)comboDateFormat.SelectedItem;
String filename = txtFileName.Text;

intId = intId + 1;
globalSymbol = symbol;
oWrite = new System.IO.StreamWriter(filename);
Tws1.reqHistoricalData(intId, symbol, type, expiration, Convert.ToDouble(strike), callput,
multiplier, exchange, currency,0, enddate,
duration,barsize.ToString(),
show, Convert.ToInt32(realtime), Convert.ToInt32(dateformat));


The above code does a few important things. First it grabs all the data we entered into text boxes and combo boxes. Then we increment our id by 1 so that it will always be a unique request. We then set our globalSymbol variable = symbol. We do this because in a minute we will use that so we know the symbol when saving it to our comma separated file. We then open the file we will be saving to and make the request for historical data. We are almost done, the last thing we need to do is modify our historicalData function. Change the function to look like this:

private void historicalData(object sender, AxTWSLib._DTwsEvents_historicalDataEvent e)
{
String strData = "";

if(e.date.Equals("finished"))
{
oWrite.Close();
MessageBox.Show("Done");
}
Else
{
strData = globalSymbol + "," + e.date + "," + e.open + "," + e.high + "," + e.low + "," + e.close + "," + e.volume;
oWrite.WriteLine(strData);
}
}


The above piece of code simply checks if it is the end of the historical data stream. If so it closes the file, if not it writes the information to the file. Now you should be able to use this code, as a building block for any tools you may need for gathering historical data.

Download the full source for this project Here

Similar tutorials

 

 

 

 

 

 

Copyright© 2007 StockBotProgramming.com