Subscribing to Real-Time Market Data With Interactive Brokers Using the SumZero Trading API

Connecting to Interactive Brokers to receive streaming real-time market data is easy with the SumZero Trading API. This example will illustrate how to connect to an Interactive Brokers TraderWorkstation (TWS) or IB Gateway instance in order to obtain quotes for Amazon.

The first step is to create a new InteractiveBrokersClient object passing in the hostname that TWS or IB Gateway is running on, the port that is listening on, as well as a client ID for the connection. All connections to Interactive Brokers require a client ID which must be unique for each application that connects via the API.

Once the connection is established a new StockTicker object is created which will be used to subscribe to market data for Amazon (AMZN). In order to subscribe to market data the subscribeLevel1() method needs to be called on the client object and passed a Level1Listener object. In this case a lambda expression is passed in which will check the type of Level1Quote that was received, and if the quote was a ‘Last’ price, as opposed to a bid or ask, then print the value of that price to the console.

For this example I’ve connected to the special Interactive Brokers “edemo” account which is free to use, but provides fictitious data for its data feed. It is a good account to test with to make sure that an application is connecting and receiving data as expected.

The output of the example application which was running within NetBeans IDE appears below.