Wednesday, March 26, 2014

Let's continue our journey of capturing data from InteractiveBrokers. In this blog, we discuss how to capture real time bars. A bar here means a summary of prices over the last 5-second interval. Currently you can choose one of the following 4 types of prices that occurred in the interval to generate bars:

TRADES: actual trade prices;

BID: bid prices;

ASK: ask prices;

MIDPOINT: mid prices.

For the type of prices you chose, the information in a bar includes OHLC (open-high-low-close), volume, WAP (weighted average price), and count (the number of trades that occurred, which is applicable to TRADES only). Compared to ad hoc data like tick prices or market depth, real time bars are sent over to you on a regular basis -- you receive an update every 5 seconds.
The code example here is to capture real time bars on TRADES. As in the previous two blog posts, the first thing we need to do is to specify the stock we are interested in. Again, here I chose Infosys listed in Mumbai.

Tuesday, March 4, 2014

In the last blog post, we discussed how to capture real-time tick data from InteractiveBrokers. Now let's see how we can capture real-time market depth data from InteractiveBrokers. The example code in this post is very similar to the one in the previous post. As far as the code is concerned, the main difference is just that now we subscribe to and process market depth events instead of tick price events. However, to interpret market depth events is slightly more complicated than to interpret tick data events, since an order book is basically a table with multiple entries.

Before we talk about how to process market depth events to maintain an order book, let's first take a look at an example below, which is a snapshot of the order book for the stock "Infosys" listed in Mumbai. The snapshot was taken at some time on 24th Feb 2014. You can see that the depth of this order book is 5, i.e., it shows the best 5 entries for both bid side and ask side, so there are 10 entries in total. Moreover, all entries are sorted by price, so that the entries for bid are in descending order and those for ask are in ascending order. By observing the market depth data, it's not difficult to see that the first bid/ask entry of an order book should be always the same as the tick price data. As such, subscribing to tick price events is equivalent to subscribing to market depth events with depth 1.

bid

ask

position

price

size

price

size

1

3748.70

14

3749.00

71

2

3748.50

290

3749.40

10

3

3748.45

20

3749.45

1

4

3748.15

25

3749.85

50

5

3748.10

125

3749.90

15

So, how do we subscribe to market depth events? It's almost as easy as to subscribe to tick price events. The following code snippet creates an contract object specifying the stock we are interested in. In this case, it's Infosys listed in Mumbai.

Please note that though one can specify an arbitrarily large number for market depth, the effective market depth will still be subject to the exchange and your data subscription with InteractiveBrokers. More often than not, market depth data are not free, but there are some exceptions, like NSE in Mumbai. Once we have invoked the reqMktDepthEx method, we immediately receive the initial batch of market depth events, which are meant to initialize a complete snapshot of the order book. After the initial batch, we will receive subsequent market depth events on an ad hoc basis. These subsequent events represent increment changes to the order book. Basically, a market depth event consists of the following properties:

id: ticker ID, which is specified by us via the reqMktDepthEx method;

side: 0 = ask, 1 = bid;

operation: 0 = insert, 1 = update, 2 = delete;

position: indicate which position in the order book to update;

price: bid/ask price;

size: bid/ask size.

For example, the snapshot example above was constructed by the following initial batch of events:

event sequence

id

side

operation

position

price

size

1

1

1

0

0

3748.7

14

2

1

1

0

1

3748.5

290

3

1

1

0

2

3748.45

20

4

1

1

0

3

3748.15

25

5

1

1

0

4

3748.1

125

6

1

0

0

0

3749

71

7

1

0

0

1

3749.4

10

8

1

0

0

2

3749.45

1

9

1

0

0

3

3749.85

50

10

1

0

0

4

3749.9

15

From the 11th event onward, we receive increment changes. For example, the 11th event was as follows:

event sequence

id

side

operation

position

price

size

11

1

1

1

0

3748.7

17

Based on this event, we should update the first bid entry of the order book with price 3748.7 and size 17. So I hope this is clear enough on how to interpret market depth events.

As for to unsubscribe, we just need invoke the cancelMktDepth method with the ticker ID for which we want to cancel the subscription.

tws1.cancelMktDepth(1)

Finally, below is a simple demo program, which captures market depth events for 10 minutes and simply prints them out. This program does not present an order book visually.