Connecting to Interactive Brokers TWS Using C# .Net

Author: Joe
Tutorial Objective: Connect to TWS using C#
Source 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.

The goal of this tutorial is to teach you how to use the TWS API with C#, not to teach C#. If you are unfamiliar with using C# read through the tutorials found Here. Although we are not trying to teach C# 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 C#->Windows->Windows Application. Name your project "C#Tutorial1" and click ok.

Now your new CSharp project should be created. We now need to tell C# that we will be using the TWS API. We will need to add references for AxInterop.TWSlib and Interop.TWSlib. To do so, in the solution explorer window right click on references and chose 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

The next thing we need to do is view the source code for form1. Here we need to add our TWS object. Add this line of code Inside your form1 class declaration:

private AxTWSLib.AxTws Tws1;

Next we need to initialize this object so that we can use it. For the purpose of this tutorial we will initialize it inside of the form_load event. Inside of the form load event add the following lines:

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 C# is completely ready to use the TWS api. After those lines in the form_load event add this line:

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:

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 C# .Net

 Download the full source for this project Here

Similar tutorials

 

 

 

 

 

 

Copyright© 2007 StockBotProgramming.com