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.

ibtws

Create IB Trader Workstation connection

Syntax

ib = ibtws(host,port)
ib = ibtws(host,port,clientid)

Description

example

ib = ibtws(host,port) creates a connection to IB Trader WorkstationSM on a machine with IP address host and port number port. The function returns the IB Trader Workstation connection object ib.

example

ib = ibtws(host,port,clientid) specifies the client identifier clientid.

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_1]
        Host: ''
        Port: 7496

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

Display the Handle property of ib.

ib.Handle
ans =

	COM.TWS_TwsCtrl_1

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

  • 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:

     BID_PRICE: 160.1900
      BID_SIZE: 1
     ASK_PRICE: 160.2500
      ASK_SIZE: 1
    LAST_PRICE: 160.1100
     LAST_SIZE: 1
        VOLUME: 2877

d is a structure containing these fields:

  • BID_PRICE -- Bid price

  • BID_SIZE -- Bid size

  • ASK_PRICE -- Ask price

  • ASK_SIZE -- Ask size

  • LAST_PRICE -- Last price

  • LAST_SIZE -- Last size

  • VOLUME -- Volume

Display the data in the BID_PRICE field of d.

d.BID_PRICE
ans =

  160.1900

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 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, the IP address that you choose, and the 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 port number 7496 and client identifier 1.

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

  ibtws with properties:

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

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

Display the ClientId property of ib.

ib.ClientId
ans =

     1

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

  • 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:

     BID_PRICE: 160.1900
      BID_SIZE: 2
     ASK_PRICE: 160.2500
      ASK_SIZE: 2
    LAST_PRICE: 161.1100
     LAST_SIZE: 1
        VOLUME: 2877

d is a structure containing these fields:

  • BID_PRICE -- Bid price

  • BID_SIZE -- Bid size

  • ASK_PRICE -- Ask price

  • ASK_SIZE -- Ask size

  • LAST_PRICE -- Last price

  • LAST_SIZE -- Last size

  • VOLUME -- Volume

Display the data in the BID_PRICE field of d.

d.BID_PRICE
ans =

  160.1900

Close the IB Trader Workstation℠ connection.

close(ib)

Input Arguments

collapse all

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

Data Types: char | string

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

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.

Data Types: double

Output Arguments

collapse all

IB Trader Workstation connection, returned as an IB Trader Workstation connection object. The properties of this object are as follows.

Property

Description

ClientId

Application identifier where the connection originated

Handle

Interactive Brokers ActiveX object

Host

host argument

Port

port argument

The Interactive Brokers API determines these properties.

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

Introduced in R2013b

Was this topic helpful?