Connecting to Interactive Brokers API with Java
Introduction
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 interactivebrokers.github.io
- 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.) - Access the command line terminal.
(On a MAC from the Applications menu. First select Utilities then select Terminal.) - 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 - Open Netbeans and create new project
(File-> New Project -> Java Application -> YourProjectName -> Finish) - Add JavaClient source package folder
(Right click your project -> Properties -> Source -> Add Folder -> ../IBJts/source/JavaClient )
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. Logger.java and API.java. We creating Logger.java, because its parameter in ApiController constructor method.
In logger we implement all abstract methods. In our case it’s just method log().
package api; | |
import com.ib.controller.ApiConnection.ILogger; | |
public class Logger implements ILogger { | |
@Override | |
public void log(String valueOf) { | |
System.out.print(valueOf); | |
} | |
} |
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.
package api; | |
import com.ib.controller.ApiController; | |
import java.util.ArrayList; | |
public class API implements ApiController.IConnectionHandler { | |
static API INSTANCE = new API(); | |
Logger m_inLogger = new Logger(); | |
Logger m_outLogger = new Logger(); | |
ApiController m_controller = new ApiController( this, m_inLogger, m_outLogger); | |
public static void main(String[] args) { | |
INSTANCE.run(); | |
} | |
void run() { | |
// make initial connection to local host, port 7496, client id 0 | |
m_controller.connect( "127.0.0.1", 7496, 0); | |
// Your implementation | |
} | |
// Abstract methods from IConnectionHandler implementation | |
@Override | |
public void connected() { } | |
@Override | |
public void disconnected() { } | |
@Override | |
public void accountList(ArrayList<String> list) { } | |
@Override public void error(Exception e) { } | |
@Override public void message(int id, int errorCode, String errorMsg) { } | |
@Override | |
public void show(String string) { } | |
} |
Now when we run our API (you have to be logged in IB Gateway) we should seen following output:
And in IB Gateway we should seen our client connected:
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!
Resources
[3] [PDF] Java API Getting Started