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.

realtime

Request Interactive Brokers real-time data

Syntax

tickerid = realtime(ib,ibContract,f)
tickerid = realtime(ib,ibContract,f,eventhandler)

Description

example

tickerid = realtime(ib,ibContract,f) requests Interactive Brokers® real-time data using IB Trader WorkstationSM connection ib, IB Trader Workstation IContract object ibContract, and Interactive Brokers fields f.

example

tickerid = realtime(ib,ibContract,f,eventhandler) requests Interactive Brokers real-time 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 real-time 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 default Interactive Brokers real-time data by setting the Interactive Brokers field f to an empty character vector .

Request real-time data using ib, ibContract, and f.

f = '';

tickerid = realtime(ib,ibContract,f)
tickerid =

     1

tickerid returns a number for tracking the real-time data request.

The realtime function returns real-time data in the MATLAB® workspace variable ibBuiltInRealtimeData.

Display this real-time data.

ibBuiltInRealtimeData
ibBuiltInRealtimeData = 

            id: 1
     BID_PRICE: 584.65
      BID_SIZE: 1
     ASK_PRICE: 585.80
      ASK_SIZE: 1
    LAST_PRICE: 585
     LAST_SIZE: 1
        VOLUME: 11611

The structure ibBuiltInRealtimeData contains these fields:

  • Real-time request identifier

  • Bid price

  • Bid size

  • Ask price

  • Ask size

  • Last price

  • Last size

  • Volume

The id field is a number that tracks the real-time data request for IB Trader Workstation IContract object ibContract. When you create multiple contracts, each real-time data display has a different value for the id field that corresponds to a specific contract.

Cancel the real-time market data request using tickerid.

ib.Handle.cancelMktData(tickerid)

Remove event handler assignments for each real-time event.

evtListeners = ib.Handle.eventlisteners; 
eventTypes = {'tickSize','tickPrice','tickSnapshotEnd', ...
    'tickOptionComputation','tickGeneric','tickString', ...
     'tickEFP','marketDataType'};
for j = 1:length(eventTypes) 
    i = strcmp(evtListeners(:,1),eventTypes{j}); 
    ib.Handle.unregisterevent([{evtListeners{i,1}}' {evtListeners{i,2}}']); 
end 

Close the IB Trader Workstation connection.

close(ib)

To request real-time 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 default Interactive Brokers real-time data by setting the Interactive Brokers field f to an empty character vector .

f = '';

Request real-time data using ib, ibContract, and f. Use the sample event handler ibExampleEventHandler to display the real-time data in the Command Window.

tickerid = realtime(ib,ibContract,f,...
                    @ibExampleEventHandler)
tickerid =

     1

    [1x1 COM.TWS_TwsCtrl_1]    [1]    [1]    [1]    [585.50]    [1]    [1x1 struct]    'tickPrice'

    [1x1 COM.TWS_TwsCtrl_1]    [2]    [1]    [0]    [1]    [1x1 struct]    'tickSize'

...

tickerid returns a number for tracking the real-time data request.

After the tickerid, ibExampleEventHandler streams real-time data to the Command Window. Each line is a type of tick. Here, there is a price tick and size tick.

For a price tick, the IB Trader Workstation returns:

  • Interactive Brokers ActiveX® object

  • Event identifier

  • Request identifier

  • Tick type

  • Price

  • Automatic execution flag

  • Structure that repeats the contents of the columns

  • Event type

For details about this data, see Interactive Brokers API Reference Guide.

Cancel the real-time market data request using tickerid.

ib.Handle.cancelMktData(tickerid)

Remove event handler assignments for each real-time event.

evtListeners = ib.Handle.eventlisteners; 
eventTypes = {'tickSize','tickPrice','tickSnapshotEnd', ...
    'tickOptionComputation','tickGeneric','tickString', ...
     'tickEFP','marketDataType'};
for j = 1:length(eventTypes) 
    i = strcmp(evtListeners(:,1),eventTypes{j}); 
    ib.Handle.unregisterevent([{evtListeners{i,1}}' {evtListeners{i,2}}']); 
end 

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 or a cell array for multiple IB Trader Workstation IContract objects. 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.

Data Types: cell

Interactive Brokers fields, specified as a character vector, string scalar, cell array of character vectors, or string array. These fields correspond to numeric identifiers that specify the Interactive Brokers generic market data tick types. For details, see Interactive Brokers API Reference Guide.

Data Types: char | cell | string

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

Interactive Brokers market request identifier, specified as a double for tracking and canceling the market data request. tickerid is a scalar for one Interactive Brokers contract and a vector of scalars for multiple contracts.

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?