Requesting historical data through TWS API with Visual Basic .Net Part 2

Author: Joe
Tutorial Objective: Request historical quote data from TWS and then save the data in a comma seperated file to be used for backtesting.
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 Requesting historical data through TWS API with 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 build off of the source code from Requesting historical data through TWS API with Visual Basic .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:\vbdata.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:

Dim intId As Integer = 0
Dim globalSymbol As String = ""
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter


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:

Dim symbol As String = txtSymbol.Text
Dim type As String = comboSecurity.SelectedItem
Dim expiration As String = txtExpiration.Text
Dim strike As String = txtStrike.Text
Dim callput As String = txtCallPut.Text
Dim multiplier As String = txtMultiplier.Text
Dim exchange As String = txtExchange.Text
Dim currency As String = txtCurrency.Text
Dim enddate As String = txtEndDate.Text
Dim duration As String = txtDuration.Text
Dim barsize As String = comboBarSize.SelectedItem
Dim show As String = comboShow.SelectedItem
Dim realtime As String = comboRealTime.SelectedItem
Dim dateformat As String = comboDateFormat.SelectedItem
Dim filename As String = txtFileName.Text
intId = intId + 1
globalSymbol = symbol
oWrite = oFile.CreateText(filename)
Call Tws1.reqHistoricalData(intId, symbol, type, expiration, CDbl(strike), callput, _
multiplier, exchange, currency, enddate, _
duration, CStr(barsize), _
show, realtime, 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 seperated 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 modifiy our Tws1_intradayData function. Change the function to look like this:

Private Sub Tws1_intradayData(ByVal eventSender As System.Object, ByVal eventArgs As AxTWSLib._DTwsEvents_historicalDataEvent) Handles Tws1.historicalData
Dim strData As String = ""

If (eventArgs.date.Equals("finished")) Then
oWrite.Close()
MessageBox.Show("Done")
Else
strData = globalSymbol & "," & eventArgs.date & "," & eventArgs.open & "," & eventArgs.high & "," & eventArgs.low & "," & eventArgs.close & "," & eventArgs.volume
oWrite.WriteLine(strData)
End If
End Sub


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