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.

orders

Request Interactive Brokers open order data

Syntax

o = orders(ib)
o = orders(ib,client)
o = orders(ib,client,eventhandler)

Description

example

o = orders(ib) requests Interactive Brokers® open order data using IB Trader WorkstationSM connection ib for the current client only.

example

o = orders(ib,client) requests Interactive Brokers open order data using IB Trader Workstation connection ib and a client flag. client denotes requesting data from the current client or all clients.

example

o = orders(ib,client,eventhandler) requests Interactive Brokers open order data using an event handler function eventhandler. Use the sample event handler ibExampleEventHandler or write a custom event handler function.

Examples

collapse all

Create the IB Trader Workstation connection ib on the local machine using port number 7496.

 ib = ibtws('',7496); 

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

  • Google® symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

EX is a sample primary exchange name. Substitute your primary exchange name in ibContract.primaryExchange.

ibContract = ib.Handle.createContract;
ibContract.symbol = 'GOOG';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'EX';
ibContract.currency = 'USD';

Create the IB Trader Workstation IOrder object ibOrder. Here, this object describes a limit order to sell two shares with a limit price of $590.

ibOrder = ib.Handle.createOrder;
ibOrder.action = 'SELL';
ibOrder.totalQuantity = 2;
ibOrder.orderType = 'LMT'
ibOrder.lmtPrice = 590;

For details about the IContract and IOrder objects, see Interactive Brokers API Reference Guide.

Create a unique order identifier id.

id = orderid(ib);

Execute the order using:

  • IB Trader Workstation connection ib

  • IB Trader Workstation IContract object ibContract

  • IB Trader Workstation IOrder object ibOrder

  • Unique order identifier id

d = createOrder(ib,ibContract,ibOrder,id);

Retrieve order information o.

o = orders(ib)
o = 

1x2 struct array with fields:

    Type
    EventID
    orderId
    contract
    order
    orderState

o contains a structure array. The array contains a structure with data for each open order. The structure fields are:

  • Order type

  • Event identifier

  • Order identifier

  • Contract data

  • Order data

  • Order status

Retrieve the current status of the order.

o.orderState
ans = 

                status: 'Submitted'
            initMargin: '1.7976931348623157E308'
           maintMargin: '1.7976931348623157E308'
           ...

orderState is a structure with fields corresponding to the status of the order. The fields are order status, initial margin, and maintenance margin. For details on these fields and the additional fields in orderState, see Interactive Brokers API Reference Guide.

Close the IB Trader Workstation connection.

close(ib)

Create the IB Trader Workstation connection ib on the local machine using port number 7496.

 ib = ibtws('',7496); 

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

  • Google symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

EX is a sample primary exchange name. Substitute your primary exchange name in ibContract.primaryExchange.

ibContract = ib.Handle.createContract;
ibContract.symbol = 'GOOG';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'EX';
ibContract.currency = 'USD';

Create the IB Trader Workstation IOrder object ibOrder. Here, this object describes a limit order to sell two shares with a limit price of $590.

ibOrder = ib.Handle.createOrder;
ibOrder.action = 'SELL';
ibOrder.totalQuantity = 2;
ibOrder.orderType = 'LMT'
ibOrder.lmtPrice = 590;

For details about the IContract and IOrder objects, see Interactive Brokers API Reference Guide.

Create a unique order identifier id.

id = orderid(ib);

Execute the order using:

  • IB Trader Workstation connection ib

  • IB Trader Workstation IContract object ibContract

  • IB Trader Workstation IOrder object ibOrder

  • Unique order identifier id

d = createOrder(ib,ibContract,ibOrder,id);

Retrieve order information o from all clients by setting client to false.

o = orders(ib,false)
o = 

1x2 struct array with fields:

    Type
    EventID
    orderId
    contract
    order
    orderState

o contains a structure array. The array contains a structure with data for each open order. The structure fields are:

  • Order type

  • Event identifier

  • Order identifier

  • Contract data

  • Order data

  • Order status

Retrieve the current status of the order.

o.orderState
ans = 

                status: 'Submitted'
            initMargin: '1.7976931348623157E308'
           maintMargin: '1.7976931348623157E308'
           ...

orderState is a structure with fields corresponding to the status of the order. The fields are order status, initial margin, and maintenance margin. For details on these fields and the additional fields in orderState, see Interactive Brokers API Reference Guide.

Close the IB Trader Workstation connection.

close(ib)

Create the IB Trader Workstation connection ib on the local machine using port number 7496.

 ib = ibtws('',7496); 

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

  • Google symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

EX is a sample primary exchange name. Substitute your primary exchange name in ibContract.primaryExchange.

ibContract = ib.Handle.createContract;
ibContract.symbol = 'GOOG';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'EX';
ibContract.currency = 'USD';

Create the IB Trader Workstation IOrder object ibOrder. Here, this object describes a limit order to sell two shares with a limit price of $590.

ibOrder = ib.Handle.createOrder;
ibOrder.action = 'SELL';
ibOrder.totalQuantity = 2;
ibOrder.orderType = 'LMT'
ibOrder.lmtPrice = 590;

For details about the IContract and IOrder objects, see Interactive Brokers API Reference Guide.

Create a unique order identifier id.

id = orderid(ib);

Execute the order using:

  • IB Trader Workstation connection ib

  • IB Trader Workstation IContract object ibContract

  • IB Trader Workstation IOrder object ibOrder

  • Unique order identifier id

d = createOrder(ib,ibContract,ibOrder,id);

Retrieve order information from all clients by setting client to false and using the sample event handler function ibExampleEventHandler. Use ibExampleEventHandler or write a custom event handler function.

o = orders(ib,false,@ibExampleEventHandler)
o =

     []

  Columns 1 through 4

    [1x1 COM.TWS_TwsCtrl_1]    [101]    [56947638]    [1x1 Interface.Tws_ActiveX_Control_module.IContract]

  Columns 5 through 6

    [1x1 Interface.Tws_ActiveX_Control_module.IOrder]    [1x1 Interface.Tws_ActiveX_Control_module.IOrderState]

  Columns 7 through 8

    [1x1 struct]    'openOrderEx'

o contains an empty double because the event handler ibExampleEventHandler processes the output data.

ibExampleEventHandler displays the output data in the Command Window. Here, IB Trader Workstation returns:

  • Interactive Brokers ActiveX® object

  • Event identifier

  • Unique order identifier

  • IB Trader Workstation IContract object

  • IB Trader Workstation IOrder object

  • IB Trader Workstation IOrderState object

  • Structure that repeats the contents of the columns

  • Request type

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

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.

Client flag, specified as a logical. true denotes returning data from the current client only. false denotes returning data from all clients.

Data Types: logical

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 open order data, returned as a structure or an empty double. The structure contains these fields:

  • Order type

  • Event identifier

  • Order identifier

  • Contract data

  • Order data

  • Order status

When using an event handler function, o is an empty double.

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

  • Executing orders multiple times using the same IB Trader Workstation connection can cause this kind of warning message: Warning: Cannot unregister ‘openOrderEx’. Invalid event name/handler combination. To fix this warning, close the IB Trader Workstation connection and create a new connection using ibtws.

Introduced in R2015a

Was this topic helpful?