Modern Java solution for TWS API

“Ib-client” is a Java wrapper around native TWS API provided by Interactive Brokers company, the leader between electronic trading platforms.

The native API

Used as a base of 20+ projects.

The native API is powerful but is hard and inconvenient to use. Before any business logic can be written, a developer has to write a large amount of boilerplate code, resolve concurrency issues, handle reconnects, filter out order statuses duplicates, check incoming messages, etc.

order   ❯ 

1. Use only 2 lines

IbClient client = new IbClient();
client.connect("127.0.0.1", 7497, 0").timeout(10, TimeUnit.SECONDS). blockingAwait();

2. Automatic reconnects to TWS

client.connectionStatus().subscribe(connected -> {
if (connected) {
log.info("Connection to IB is restored");
} else {
log.warm("Connection lost");
}
})

3. Use power of reactive programming

Single<IbOrder> order1Obsevable = client.placeOrder(contract, order1);
Single<IbOrder> order2Obsevable = client.placeOrder(contract, order2);
Single<IbOrder> order3Obsevable = client.placeOrder(contract, order3);

Single.merge(order1Obsevable, order2Obsevable, order3Obsevable)
.toObservable()
.observeOn(Schedulers.newThread())
.doOnNext(order -> log.info("Order {} is submited", order.getOrderId()))
.flatMap(order -> client.subscribeOnOrderNewStatus())
.filter (status -> status.isFilled())
.subscribe(status -> log.info("Order {} is filled", status.getOrderId()),
e -> log.error("Orders placing error: {}", e));

4. Build-in and verbose logging

2019-03-29 12:26:24,948 | DEBUG | IbClient: Connecting to 127.0.0.1:7497, id=1 ...
2019-03-29 12:26:25,042 | DEBUG | ConnectionMonitor: Status change: UNKNOWN => DISCONNECTED
2019-03-29 12:26:25,042 | DEBUG | ConnectionMonitor: Command: CONNECT
2019-03-29 12:26:25,143 | DEBUG | ConnectionMonitor: Status change: DISCONNECTED => CONNECTING
2019-03-29 12:26:25,168 | INFO | Wrapper: Connection is opened. version: 148
2019-03-29 12:26:25,171 | DEBUG | ConnectionMonitor: Command: CONFIRM_CONNECT
2019-03-29 12:26:25,171 | DEBUG | Wrapper: Managed accounts are: AA12345
2019-03-29 12:26:25,188 | DEBUG | Wrapper: New request ID: 85
2019-03-29 12:26:25,271 | DEBUG | ConnectionMonitor: Status change: CONNECTING => SLEEP
2019-03-29 12:26:26,279 | DEBUG | ConnectionMonitor: Status change: SLEEP => CONNECTED
2019-03-29 12:26:26,411 | INFO | RequestRepository: Register to {type=EVENT_ORDER_STATUS}
2019-03-29 12:26:26,432 | INFO  | RequestRepository: Register to {id=100000000, type=REQ_CONTRACT_DETAIL}...