Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

marketdepth

Request Interactive Brokers market depth data

Syntax

d = marketdepth(ib,ibContract,depth)
d = marketdepth(ib,ibContract,depth,eventhandler)

Description

example

d = marketdepth(ib,ibContract,depth) requests Interactive Brokers® market depth data using the IB Trader WorkstationSM connection ib, IB Trader Workstation IContract object ibContract, and price level depth.

example

d = marketdepth(ib,ibContract,depth,eventhandler) requests Interactive Brokers market depth data using an event handler function eventhandler. Use the sample event handler ibExampleEventHandler or write a custom event handler function.

Examples

collapse all

To request Interactive Brokers market depth data, set up the IB Trader Workstation connection ib using ibtws. Create an IB Trader Workstation IContract object ibContract as shown in Request Interactive Brokers Real-Time Data. An IContract object is an Interactive Brokers object for containing the data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request market depth data using ib and ibContract. Specify five price levels for the bid and offer sides for depth. This code assumes ibContract is an E-mini S&P 500 futures contract with an expiry of December 2014 that trades on the CME Globex exchange.

depth = 5;

d = marketdepth(ib,ibContract,depth)
d = 

      bid: [5x2 double]
    offer: [5x2 double]

d is a structure that contains the fields for bid and offer price levels.

Display the bid prices for five levels of market depth.

d.bid
ans = 

       1992.5                       495
      1992.25                      1479
         1992                      1950
      1991.75                      1763
       1991.5                      2117

The first column contains the bid price and the second column contains the bid size.

Close the IB Trader Workstation connection.

 close(ib) 

To request Interactive Brokers market depth data, set up the IB Trader Workstation connection ib using ibtws. Create an IB Trader Workstation IContract object ibContract as shown in Request Interactive Brokers Real-Time Data. An IContract object is an Interactive Brokers object for containing the data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request market depth data using ib and ibContract. Specify five price levels for the bid and offer sides for depth. This code assumes ibContract is an E-mini S&P 500 futures contract with an expiry of December 2014 that trades on the CME Globex exchange. Use the sample event handler function ibExampleEventHandler or write a custom event handler function.

depth = 5;

d = marketdepth(ib,ibContract,depth,@ibExampleEventHandler)
d =

        8147
        
     [1x1 COM.TWS_TwsCtrl_1]    [16.00]    [8147.00]    [0]    [0]    [1.00]    [1988.75]    [346.00]    [1x1 struct]    'updateMktDepth'
     ...

d is the request identifier.

After d, ibExampleEventHandler streams market depth data to the Command Window.

The columns are:

  • Interactive Brokers ActiveX® object

  • Event identifier

  • Request identifier

  • Position

  • Operation

  • Side

  • Price

  • Size

  • Structure that repeats the contents of the columns

  • Event type

Close the IB Trader Workstation connection.

 close(ib) 

Input Arguments

collapse all

IB Trader Workstation connection, specified as an IB Trader Workstation connection object created using ibtws.

IB Trader Workstation contract, specified as an IB Trader Workstation IContract object. This object is the instrument or security used in the order transaction. Create this object by calling the Interactive Brokers API function createContract. For details about createContract and the attributes that you can set, see Interactive Brokers API Reference Guide.

IB Trader Workstation market depth, specified as a scalar from one through 10. This number denotes the depth of the active book.

Data Types: double

Event handler, specified as a function handle, character vector, or string scalar to identify an event handler function that processes the returned data. Use the sample event handler or write a custom event handler function. For details, see Writing and Running Custom Event Handler Functions with Interactive Brokers.

Example: @eventhandler

Data Types: function_handle | char | string

Output Arguments

collapse all

IB Trader Workstation market depth data, returned as a structure containing the price level data for the bid and offer prices. Price level data consists of the price and size. When using an event handler function, d is a double denoting the request identifier.

Tips

  • ibBuiltInErrMsg appears in the MATLAB® workspace. Check the status of connection and function execution by displaying the contents of this variable. ibBuiltInErrMsg contains messages related to:

    • Connection

    • Information resulting from executing functions

    • Errors

Introduced in R2015a

Was this topic helpful?