Create and Manage an Interactive Brokers Order

This example shows how to connect to the IB Trader WorkstationSM, request open order data, create IB Trader Workstation IContract and IOrder objects, and execute the order. For details about the IContract and IOrder objects, see Interactive Brokers API Reference Guide.

This example uses the sample event handler function ibExampleOrderEventHandler to populate an order blotter figure with Interactive Brokers® order information. Use this event handler or write a custom event handler function. For details, see Writing and Running Custom Event Handler Functions with Interactive Brokers.

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

Connect to the IB Trader Workstation

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

 ib = ibtws('',7496); 

Create an Example Order Blotter

Create an example order blotter that the event handler populates.

This MATLAB® code creates a MATLAB figure to contain the Interactive Brokers order information.

f = findobj('Tag','IBOrderBlotter');
if isempty(f)
    f = figure('Tag','IBOrderBlotter','MenuBar','none', ...
        'NumberTitle','off','Name','IB Order Blotter')
    pos = f.Position;
    f.Position = [pos(1) pos(2) 687 335];
    colnames = {'Status','Filled','Remaining','Avg Fill Price','Id', ...
        'Parent Id','Last Fill Price','Client Id','Why Held'};
    data = cell(15,9);
    uitable(f,'Data',data,'RowName',[],'ColumnName',colnames, ...
        'Position',[10 30 677 300],'Tag','OrderDataTable')
    uicontrol('Style','text','Position',[10 5 592 20], ...
        'Tag','IBOrderMessage')
    uicontrol('Style','pushbutton','String','Close', ...
        'Callback','evalin(''base'',''close(ib);close(findobj(''''Tag'''',''''IBOrderBlotter''''));'')',...
        'Position',[607 5 80 20])
end

MATLAB displays the IB Order Blotter.

Request Open Order Data

Request information for all open orders using only this client and the sample event handler ibExampleOrderEventHandler.

o = orders(ib,true,@ibExampleOrderEventHandler);

o is an empty double because ibExampleOrderEventHandler displays the data for all open orders in the IB Order Blotter.

Create the IB Trader Workstation IContract and IOrder Objects

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

  • XYZ symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

XYZ is a sample symbol name and EX is a sample primary exchange name. To create orders for your security, substitute the symbol name in ibContract.symbol and primary exchange name in ibContract.primaryExchange.

ibContract = ib.Handle.createContract;
ibContract.symbol = 'XYZ';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'EX';
ibContract.currency = 'USD'
ibContract =
 
	Interface.Tws_ActiveX_Control_module.IContract

Create the IB Trader Workstation IOrder object ibOrder for a buy market order for two shares.

ibOrder = ib.Handle.createOrder;
ibOrder.action = 'BUY';
ibOrder.totalQuantity = 2;
ibOrder.orderType = 'MKT'
ibOrder =
 
	Interface.Tws_ActiveX_Control_module.IOrder

ibOrder contains the action, total quantity, and order type.

Create the Interactive Brokers Order

Obtain the next valid order identification number using IB Trader Workstation connection ib.

id = orderid(ib);

Execute the buy market order for two shares using the unique order identifier id and sample event handler ibExampleOrderEventHandler.

createOrder(ib,ibContract,ibOrder,id,@ibExampleOrderEventHandler)

MATLAB displays order information in the IB Order Blotter. The IB Order Blotter shows the open order and the filled order.

Cancel the Interactive Brokers Order

ib.Handle.cancelOrder(id)

After canceling the existing order, create an order by modifying the IB Trader Workstation IOrder object ibOrder. Then, create the order by executing createOrder.

Cancel all open Interactive Brokers orders.

ib.Handle.reqGlobalCancel

This method cancels all open Interactive Brokers orders globally. The order is canceled despite where it is created.

Close the Connection

Close the IB Trader Workstation connection ib.

close(ib)

See Also

| | | | | | |

Related Examples

More About

External Websites