Connecting to Interactive Brokers TWS Using Visual Basic .Net

Author: Joe
Tutorial Objective: Connect to TWS using Visual Basic
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 of Visual Studio Here, and the TWS API is available for download Here.

The goal of this tutorial is to teach you how to use the TWS API with Visual Basic, not to teach Visual Basic. If you are unfamiliar with using Visual Basic, read through the tutorials found Here. Although we are not trying to teach Visual Basic in this tutorial, we will try to make things as simple as possible. The code generated here will not be the "best" or most "optimized" ways of doing things. That is not the point of the tutorial. We want to provide a simple and workable foundation on which to build a bot. If you have any suggestions regarding this tutorial or if you notice any mistakes suggest it here.

First we will need to create a new project. Open up Visual Studio 2005. Now select File->New->Project. Now chose Visual Basic->Windows->Windows Application. Name your project "VbTutorial1" and click ok.

Now your new Visual Basic project should be created. We now need to tell Visual Basic that we will be using the TWS API. We will need to add references for AxInterop.TWSlib and Interop.TWSlib. To do so, click on Project->Tutorial1 Properties. In this screen click on the "References" tab, now click Add Reference. You will need to chose the browse tab and browse to the directory where you have your TWS API installed. Once at your TWS API folder go to TWSAPI_VBSample.NET\obj\Debug. Here you will see the two files AxInterop.TWSlib and Interop.TWSlib. Go ahead and select both of them and click ok.

Now Visual Basic knows we are trying to use the TWS API. We must do one more thing before Visual Basic can fully use the API. Click to view source on form1. Inside of class Form1 add the following line:

Public WithEvents Tws1 As AxTWSLib.AxTws

To keep things simple lets edit the form1 load event. If you do not already see this event in your form1 code, double click on the form from inside the designer window. Now you should be editing the load event section. We will make the application connect to TWS as soon as it starts. Add the following lines of code to the load section:

Tws1 = New AxTWSLib.AxTws
Tws1.BeginInit()
Tws1.Enabled = True
Tws1.Location = New System.Drawing.Point(32, 664)
Tws1.Name = "Tws1"
Controls.Add(Tws1)
Tws1.EndInit()



Now we are able to use our Tws1 ActiveX control. To connect to TWS add the following line after the above code in your form load event:

Call Tws1.connect("127.0.0.1", 7496, 0)

This call has 3 parameters. The first is the ip address where TWS is running. In our example we used "127.0.0.1" which is your local box that you are on. The second parameter 7496 is the socket port number that TWS is listening on. By default this is the port, however you can change this from within TWs. The last parameter is the client id. This tells the TWS who you are. We chose 0 in this case. If you wanted to run a second bot connected to this same TWS make its client id 1, or another number besides 0. Before we can test this code you need to make sure that your TWS is set to accept bot connections. To do this turn on TWS if not already on and go to: Configure->Api and make sure there Is a check next to Enable ActiveX and socket clients.

Lets do one more thing before testing this code. We need a way to know that our connection has succeeded. After the connect line in the form load event add the following lines:

Dim msg As String
msg = "Connected to TWS server version " & Tws1.serverVersion() & _
" at " & Tws1.TwsConnectionTime()
MessageBox.Show(msg)



Now run this code. If all worked correctly you should see a message box popup saying what time you connected. Congratulations, you now can connect to the TWS using the API. Your well on your way towards building a fully functional Bot. Next tutorial: Requesting historical data through TWS API with Visual Basic .Net

 Download the full source for this project Here

Similar tutorials

 

 

 

 

 

 

Copyright© 2007 StockBotProgramming.com