createOrder

Create IB Trader Workstation order

Description

example

d = createOrder(ib,ibContract,ibOrder,id) creates an IB Trader WorkstationSM order over the IB Trader Workstation connection ib using the IB Trader Workstation IOrder object ibOrder with a unique order identifier id to denote the order information. createOrder uses the IB Trader Workstation IContract object ibContract to signify the instrument for the transaction. createOrder returns the Interactive Brokers® order data d containing data about the completed order.

example

d = createOrder(ib,ibContract,ibOrder,id,eventhandler) creates an IB Trader Workstation order using an event handler function eventhandler. Use the sample event handler ibExampleEventHandler or write a custom event handler function.

Examples

collapse all

To create an order, set up the IB Trader Workstation connection ib using ibtws. Create an IB Trader Workstation IContract object ibContract. An IContract object is an Interactive Brokers object for containing the data about a security to process transactions. Then, create an IB Trader Workstation IOrder object ibOrder. An IOrder object is an Interactive Brokers object that contains the order conditions to place an order. For an example showing how to create these objects, see Create and Manage an Interactive Brokers Order. For details about creating these objects, see Interactive Brokers API Reference Guide.

Obtain the next valid order identification number id using ib.

id = orderid(ib)
id =

    54110686

Execute the order using ib, ibContract, ibOrder, and id. This code assumes a buy market order for two shares.

d = createOrder(ib,ibContract,ibOrder,id) 
d = 
    STATUS: 'Filled'
    FILLED: 2
    REMAINING: 0
    AVG_FILL_PRICE: 787.5600
    PERM_ID: '1979798454'
    PARENT_ID: 0
    LAST_FILL_PRICE: 787.5600
    CLIENT_ID: 0
    WHY_HELD: ''

d contains these fields:

  • Status

  • Filled

  • Remaining

  • Average fill price

  • Permanent identifier

  • Parent identifier

  • Last fill price

  • Client identifier

  • Why held

Display the data in the STATUS property of d.

d(1,1).STATUS
ans =
    Filled

Close the IB Trader Workstation connection.

 close(ib) 

To create an order, set up the IB Trader Workstation connection ib using ibtws. Create an IB Trader Workstation IContract object ibContract. An IContract object is an Interactive Brokers object for containing the data about a security to process transactions. Then, create an IB Trader Workstation IOrder object ibOrder. An IOrder object is an Interactive Brokers object that contains the order conditions to place an order. For an example showing how to create these objects, see Create and Manage an Interactive Brokers Order. For details about creating these objects, see Interactive Brokers API Reference Guide.

Obtain the next valid order identification number id using ib.

id = orderid(ib)
id =

    768409.00

Execute the order using ib, ibContract, ibOrder, and id. This code assumes a buy market order for two shares. Use the sample event handler function ibExampleEventHandler or write a custom event handler function.

d = createOrder(ib,ibContract,ibOrder,id,@ibExampleEventHandler) 
d =

     768409.00

  Columns 1 through 5

    [1x1 COM.TWS_TwsCtrl_1]    [13.00]    [768409.00]    'Submitted'    [0]

  Columns 6 through 12

    [2.00]    [0]    [1679681704.00]    [0]    [0]    [0]    ''

  Columns 13 through 14

    [1x1 struct]    'orderStatus'
  ...

d contains the unique order identifier id.

ibExampleEventHandler displays order status data in the Command Window. The columns are:

  • Interactive Brokers ActiveX® object

  • Event identifier

  • Unique order identifier

  • Order status

  • Filled

  • Remaining

  • Average fill price

  • Permanent identifier

  • Parent identifier

  • Last fill price

  • Client identifier

  • Why held

  • Structure that repeats the contents of the columns

  • Event 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.

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 order, specified as an IB Trader Workstation IOrder object. This object contains the order conditions, which are: the action of the order, for example, buy or sell; the order quantity; and the type of order, for example, market or limit. Create this object by calling the Interactive Brokers API function createOrder. For details about the attributes that you can set and createOrder, see Interactive Brokers API Reference Guide.

IB Trader Workstation order unique identifier, specified as a numeric scalar.

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

Interactive Brokers order data, returned as a structure containing these fields:

  • Status

  • Filled

  • Remaining

  • Average fill price

  • Permanent identifier

  • Parent identifier

  • Last fill price

  • Client identifier

  • Why held

When using an event handler function, d is a double containing the unique order identifier.

Tips

If the variable ibBuiltInErrMsg appears in the MATLAB® workspace, check the status of the 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 R2013b