Market Depth
Introduction
Market depth data, also known as Depth of Market or Level 2 data, represents an instrument’s order book and may be retrieved with EClient.reqMarketDepth(). To verify if market depth may be received via API, make a request with EClient.ReqMktDepthExchanges.
Unlike Top Market Data (Level I), market depth data is sent without sampling or filtering, however we cannot guarantee that every price quoted for a particular security will be displayed. In particular, odd lot orders are not included.
It is possible to Smart-route requests to receive aggregated data from all available exchanges.
An integral part of processing the incoming data is monitoring EWrapper.error for message 317 “Market depth data has been RESET. Please empty deep book contents before applying any new entries.” and handling it appropriately, otherwise the update process would be corrupted.
Request Market Depth
EClient.reqMarketDepth(tickerId, contract, numRows, isSmartDepth, mktDepthOptions)
Requests the contract’s market depth (order book).
Request identifier used to track data.
The Contract for which the depth is being requested.
The number of rows on each side of the order book.
Flag indicates that this is a Smart-routed market depth request. Supplying True will return data identical to the TWS Book Trader while False returns direct routed data similar to the TWS Market Depth tool. isSmartDepth should only be set to True if the Contract Object uses “SMART” in the exchange field.
Internal use only. Leave an empty array or None type.
Python
Java
C++
C#
VB.NET
Receive Market Depth
Market depth information returns data differently depending on the isSmartDepth field.
- If
isSmartDepth=True, see EWrapper.updateMktDepth() - If
isSmartDepth=False, see EWrapper.updateMktDepthL2()
EWrapper.updateMktDepth
EWrapper.updateMktDepth(tickerId, position, operation, side, price, size)
Returns the order book. Used for direct routed requests only.
Request identifier used to track data.
The order book’s row being updated
Indicates a change in the row’s value. 0 = insert (insert new price into the row), 1 = update (update the existing order in the row), 2 = delete (delete the existing order at the row).
0 for ask, 1 for bid
The order’s price
The order’s size
Python
Java
C++
C#
VB.NET
EWrapper.updateMktDepthL2
EWrapper.updateMktDepthL2(tickerId, position, marketMaker, operation, side, price, size, isSmartDepth)
Returns the order book.
Request identifier used to track data.
The order book’s row being updated.
The exchange holding the order if isSmartDepth is True, otherwise the MPID of the market maker.
Indicates a change in the row’s value. 0 = insert (insert new price into the row), 1 = update (update the existing order in the row), 2 = delete (delete the existing order at the row).
0 for ask, 1 for bid
The order’s price
The order’s size
Flag indicating if this is smart depth response (True) or the MPID of the market maker.
Python
Java
C++
C#
VB.NET
EWrapper.rerouteMktDepthReq
EWrapper.rerouteMktDepthReq(reqId, conId, exchange)
Returns the conId and exchange for an underlying contract when a request is made for level 2 data for the underlying instrument. IB does not provide market data for stock and forex CFDs. When level 2 data is requested for a CFD, a callback is made to EWrapper.rerouteMktDepthReq respectively with details about the underlying instrument in IB’s database which does have market data.
Request identifier used to track data.
Contract identifier of the underlying instrument which has market data.
Primary exchange of the underlying.
Python
Java
C++
C#
VB.NET
Cancel Market Depth
EClient.cancelMarketDepth(tickerId, isSmartDepth)
Cancel market depth request.
Request identifier used to track data.
Flag indicates that this is smart depth request.
Python
Java
C++
C#
VB.NET
Requesting Market Depth Exchanges
EClient.reqMktDepthExchanges()
Requests venues for which market depth data is available.
Python
Java
C++
C#
VB.NET
Receive Market Depth Exchanges
EWrapper.mktDepthExchanges(depthMktDataDescriptions)
Called when receives Depth Market Data Descriptions.
A list containing DepthMarketDataDescription objects. Each object contains information about what security types are availble at a given exchange, and indicates if the exchange may only provide direct-routed Level 2 (Deep2) data, or if smart market depth is supported.

