Request Interactive Brokers Real-Time Data

This example shows how to connect to the IB Trader WorkstationSM, create IB Trader Workstation IContract objects, and request real-time data. For details about the IContract object, see Interactive Brokers API Reference Guide.

This example uses the sample event handler function ibExampleRealtimeEventHandler to handle events associated with requesting real-time data. Use this event handler or write a custom event handler function. For details, see Writing and Running Custom Event Handler Functions with Interactive Brokers.

Here, AAA, BBB, and DDDD are sample symbol names. EX is a sample primary exchange name. To create orders for your securities, substitute symbol names in ibContract.symbol and primary exchange names in ibContract.primaryExchange.

To access the code for this example, enter edit IBStreamingDataWorkflow.m.

Connect to the IB Trader Workstation and Create the Real-Time Data Display Figure

Connect to the IB Trader Workstation and create connection ib using the local host and port number 7496.

 ib = ibtws('',7496); 

MATLAB® returns ib as the connection to the IB Trader Workstation with the Interactive Brokers® ActiveX® object, the local host, and the port number that you choose.

To display real-time data, create an example figure.

This MATLAB code creates a MATLAB figure to contain the Interactive Brokers real-time data.

f = findobj('Tag','IBStreamingDataWorkflow');
if isempty(f)
    f = figure('Tag','IBStreamingDataWorkflow', ...
    pos = f.Position;
    f.Position = [pos(1) pos(2) pos(3)+37 109];
    colnames = {'Trade','Size','Bid','BidSize', ...
        'Ask','AskSize','Total Volume'};
    rownames = {'AAA','BBB','DDDD'};
    data = cell(3,6);
    uitable(f,'Data',data,'RowName',rownames, ...
        'ColumnName',colnames,'Position',[10 30 582 76], ...
    uicontrol('Style','text','Position',[10 5 497 20], ...
    uicontrol('Style','pushbutton','String','Close', ...
        'Callback', ...
        'evalin(''base'',''close(ib);close(findobj(''''Tag'''',''''IBStreamingDataWorkflow''''));'')', ...
        'Position',[512 5 80 20])

MATLAB displays the empty figure.

Create IB Trader Workstation IContract Objects

Create the IB Trader Workstation IContract object for the first security. Here, this object describes a security with these property values:

  • AAA symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

ibContract1 = ib.Handle.createContract;
ibContract1.symbol = 'AAA';
ibContract1.secType = 'STK'; = 'SMART';
ibContract1.primaryExchange = 'EX';
ibContract1.currency = 'USD';

Create the IB Trader Workstation IContract object for the second security symbol BBB.

ibContract2 = ib.Handle.createContract;
ibContract2.symbol = 'BBB';
ibContract2.secType = 'STK'; = 'SMART';
ibContract2.primaryExchange = 'EX';
ibContract2.currency = 'USD';

Create the IB Trader Workstation IContract object for the third security symbol DDDD.

ibContract3 = ib.Handle.createContract;
ibContract3.symbol = 'DDDD';
ibContract3.secType = 'STK'; = 'SMART';
ibContract3.primaryExchange = 'EX';
ibContract3.currency = 'USD';

Display the data in the symbol property of ibContract1.

ans =

Request real-time data for the three securities. Set f to 100 to retrieve the Option Volume tick type. For details about other generic market data tick types, see Interactive Brokers API Reference Guide. Use the sample event handler ibExampleRealtimeEventHandler to process the real-time data events or write a custom event handler function.

contracts = {ibContract1;ibContract2;ibContract3};
f = '100';

tickerID = realtime(ib,contracts,f,...

MATLAB displays the figure populated with real-time data for stock symbols AAA, BBB, and DDDD.

Close the Connection

Close the IB Trader Workstation connection ib.


See Also

| | | | |

Related Examples

More About

External Websites