ibtws

Create IB Trader Workstation connection

Description

The ibtws function creates an ibtws object, which represents an IB Trader WorkstationSM connection. After you create an ibtws object, you can use the object functions to retrieve data, create orders, and obtain account and portfolio information.

Creation

Description

example

ib = ibtws(host,port) creates a connection to IB Trader Workstation and sets the Host and Port properties.

example

ib = ibtws(host,port,clientid) also sets the ClientId property.

Properties

expand all

IP address of the machine where IB Trader Workstation is running, specified as '', a character vector, or a string scalar. '' specifies the local machine. A character vector or string scalar specifies the IP address of another machine.

Example: '1111.222.333.44'

Data Types: char | string

IB Trader Workstation port number, specified as a numeric scalar designating the connection port of the machine.

Example: 7496

Data Types: double

IB Trader Workstation client identifier, specified as a numeric scalar designating the client machine. This number must be unique to the client.

Example: 0

Data Types: double

Handle, specified as an Interactive Brokers ActiveX object.

Example: [1x1 COM.TWS_TwsCtrl_1]

Object Functions

expand all

getdataRequest current Interactive Brokers data
historyRequest Interactive Brokers historical data
marketdepthRequest Interactive Brokers market depth data
realtimeRequest Interactive Brokers real-time data
timeseriesRequest Interactive Brokers aggregated intraday data
closeClose IB Trader Workstation connection
createOrderCreate IB Trader Workstation order
executionsRequest Interactive Brokers execution data
orderidObtain next valid order identification number
ordersRequest Interactive Brokers open order data
accountsRetrieve Interactive Brokers account information
contractdetailsRequest Interactive Brokers contract details
portfolioRetrieve current Interactive Brokers portfolio data

Examples

collapse all

Create an IB Trader Workstation℠ connection on the local machine and request current data for the IBM® security.

Connect to the IB Trader Workstation using port number 7496.

ib = ibtws('',7496)
ib = 

  ibtws with properties:

    ClientId: 0
      Handle: [1×1 COM.TWS_TwsCtrl]
        Host: ''
        Port: 7496

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

Display the Handle property of ib.

ib.Handle
ans =

	COM.TWS_TwsCtrl

Create the IB Trader Workstation IContract object for IBM. This object describes a security with values for these properties:

  • Security symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

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

Format output data for currency.

format bank

Request current data using ibContract.

d = getdata(ib,ibContract)
d = 

  struct with fields:

    LAST_PRICE: 152.50
     LAST_SIZE: 1.00
        VOLUME: 31156.00
     BID_PRICE: 152.48
      BID_SIZE: 1.00
     ASK_PRICE: 152.51
      ASK_SIZE: 1.00

Display the data in the BID_PRICE field of the structure d.

d.BID_PRICE
ans =

        152.48

Close the IB Trader Workstation connection.

close(ib)

Note

The IP address for this example does not represent a real Interactive Brokers machine.

Connect to the IB Trader Workstation on another machine using the IP address 1111.222.333.44 and the port number 7496.

ib = ibtws('1111.222.333.44',7496) 
ib = 

  ibtws with properties:

    ClientId: 0
      Handle: [1x1 COM.TWS_TwsCtrl_1]
        Host: '1111.222.333.44'
        Port: 7496

MATLAB® returns ib as the connection to the IB Trader Workstation with the Interactive Brokers ActiveX object, specified IP address, and specified port number.

Display the Handle property of ib.

ib.Handle
ans =
 
    COM.TWS_TwsCtrl_1

Close the IB Trader Workstation connection.

close(ib)

Create an IB Trader Workstation℠ connection on the local machine and request current data for the IBM® security.

Connect to the IB Trader Workstation using the port number 7496 and the client identifier 1.

ib = ibtws('',7496,1)
ib = 

  ibtws with properties:

    ClientId: 1
      Handle: [1×1 COM.TWS_TwsCtrl]
        Host: ''
        Port: 7496

MATLAB® returns ib as the IB Trader Workstation connection with the client identifier, Interactive Brokers® ActiveX® object, local host, and specified port number.

Display the ClientId property of ib.

ib.ClientId
ans =

     1

Format output data for currency.

format bank

Create the IB Trader Workstation IContract object for IBM. This object describes a security with these values for these properties:

  • Security symbol

  • Stock security type

  • Aggregate exchange

  • Primary exchange

  • USD currency

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

Request current data using ibContract.

d = getdata(ib,ibContract)
d = 

  struct with fields:

    LAST_PRICE: 152.38
     LAST_SIZE: 1.00
        VOLUME: 32283.00
     BID_PRICE: 152.37
      BID_SIZE: 3.00
     ASK_PRICE: 152.40
      ASK_SIZE: 1.00

Display the data in the BID_PRICE field of the structure d.

d.BID_PRICE
ans =

        152.37

Close the IB Trader Workstation connection.

close(ib)

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