Connect to Interactive Brokers API

Build a C# form app to connect to Interactive Brokers API.

If you read Enable Interactive Brokers API to Start Algorithmic Trading you have enabled the Trader Workstation (TWS) API to allow you to connect to Interactive Brokers API from the localhost.
We are now going to create a C# form application that will:

  • Connect to the API to receive events
  • Receive Group change events to keep your app in sync with symbol changes in the TWS platform
  • Submit a order into the system

Create a new Windows Forms App (.NET Framework) C#

Download Interactive Brokers API Libraries

  • Go download the stable http://interactivebrokers.github.io/ version of the API libraries we are going to use in our project. Get the Latest version to have access to the latest features f the softwar.

    Interactive Brokers API Library Download
  • After installing the app navigate to this path “C:\TWS API\samples\CSharp\IBSampleApp\bin\Release”. This is assuming you selected the default path for installation. Now copy the CSharpAPI.dll file out to “C:\TWS API”. This just makes it easier to reference in the visual studio project created in the earlier step.

Setting up Interactive Brokers base objects in Visual Studio

  • Add the CSharpAPI.dll as a reference to the project. You can see everything in that project if you explore the CSharpClient project in the source directory under TWS API you installed.

    Visual Studio CSharpAPI dll reference

  • Create a new empty C# class and call it “IBClient.cs”. This is going to give you all of the methods to handle the events you will be recieving from TWS.
  • Implement the EWrapper interface in the new class created. This will add all of the methods required by the interface to the class.

    The methods updated to support connection, group and order events in the IBClient.cs file are displayGroupUpdated, contractDetails, managedAccounts, nextValidId, openOrder, orderStatus, openOrderEnd, connectionClosed and error methods.

  • You can open download the full sample application here.

Add Controls Event Handlers to the MainForm

  • You will need to add a 3 text box fields, 2 combo boxes and 2 buttons to the form.
  • Here are the names of the items added to the form respectively: textBoxSymbol, textBoxBuy, textBoxQuantity, comboBoxGroup, comboBoxOrderType, buttonLong, buttonShort.
  • Here are the names of the events that get attached to the controls you created: buttonLong_Click, buttonShort_Click, comboBoxGroup_SelectedIndexChanged and MainForm_Load

    Sample Forms Application with Controls

Interactive Brokers Event Messaging

The design pattern from the documentation and samples from Interactive Brokers uses the Synchronization Context class for asynchronous methods to post messages back to the User Interface (UI) thread. It helps you simplify that process of getting data from the API to show in the main form and not have to invoke delegates.

Message Folder Sample Content

MainForm.cs code behind file contents

I added many supporting methods and processes in this MainForm.cs file to make all the previous steps work. These 3 variables are important for connection because they would have been setup during enabling the Interactive Brokers API through the TWS desktop application.

private int clientId = 1;
private string host = "127.0.0.1";
private int port = 7497;
 

Please download the sample code to view the full example.
zip_icon

Related Posts
Enable Interactive Brokers API to Start Algorithmic Trading
DotNet Phoenix Default Post Image

Algorithmic trading allows you to take a defined trading plan and write a piece of software to execute that plan Read more