Connecting to Interactive Brokers API with Java


The hardest part of creating your own automated trading system (ATS) is starting. This article is the first step of creating ATS. API provides great flexibility in implementing your automated trading ideas, all of its functionality runs through TWS or IB Gateway. This means that you must have a TWS account with IB, and that you must have your TWS/IB Gateway running in order for the API to work.

Download and Install IB API client

This step takes you out to the IB download API website at

  1. Download twsapi_macunix.n.m.jar to your computer. I use version 9.71.
        (where n and m are the major and minor version numbers respectively.)
  2. Access the command line terminal.
        (On a MAC from the Applications menu. First select Utilities then select Terminal.)
  3. At the command line, extract the contents of the jar file by typing, where n.m is your version:
        jar xf twsapi_macunix.n.m.jar


    Fig.1: OSX Terminal window with unpacking command

  4. Open Netbeans and create new project
       (File-> New Project -> Java Application -> YourProjectName -> Finish)
  5. Add JavaClient source package folder
       (Right click your project -> Properties -> Source -> Add Folder -> ../IBJts/source/JavaClient  )

Fig.2: NetBeans Project Properties window


Connecting to API

Now when we are done with installation we can continue to actually connecting to our account. In our package we create two classes. and We creating, because its parameter in ApiController constructor method.

In logger we implement all abstract methods. In our case it’s just method log().

Now in our main class we implements IConnectionHandler Interface from com.ib.controller.ApiController. Again we have to implement all its abstract methods. For the sake of simplicity we leave them empty.


Now when we run our API (you have to be logged in IB Gateway) we should seen following output:


Fig.3: NetBeans console output after successful connection

And in IB Gateway we should seen our client connected:


Fig.4: IB Gateway window after successful client connection

As you notice we have public void connected() and public void disconnected() methods implemented but not used, so far. This two methods serving as hooks and are called after api is connected() and after api is disconnected(). So this is your starting point. Good luck!


Fig.5: Unlimited power you now have


[1] IB API Reference Guide

[2] IB API Download page

[3] [PDF] Java API Getting Started



Related Post

Right-Click to Translate (Look Up) with Google Tra... If you are not native english speaker, you may be frustrated with default "look up" options you get after selecting a text and right clicking. There a...
Right Click to Create New File on Mac OS X TL;DR; Windows has a lot of cons but it has also some pros. One of them is a smooth way to create empty file on right-click. Mac OS doesn't come with...
Free Better Battery Stats and Time Remaining for M... Better Battery Stats and Time Remaining for Mac OS TL;DR; Do you miss the time remaining indicator on your MacBook, MacBook Air, or MacBook Pro? He...
FAQ: Mac OS X Services HEIC to JPG on Right-Click Service Q: Where’d all my services go? I don’t see anything in the Services menu. A: If you don’t select anything before...
Convert HEIC to PNG on Right Click for Free Convert HEIC to PNG on Right-Click In iOS 11 Apple is replacing the PNG image format with the new HEIC alternative. But what to do when device or pr...
Translate Selected Text to English on Right Click ... TL;DR; Need instant translation right in context menu? You can accomplish it for free with one line of code in Automator, or you can proc...
Buy me a coffeeOut of coffee 😱, please help!