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.

portfolio

Retrieve current Interactive Brokers portfolio data

Syntax

p = portfolio(ib)
p = portfolio(ib,acctno)
p = portfolio(ib,acctno,eventhandler)

Description

example

p = portfolio(ib) retrieves current Interactive Brokers® portfolio data for the active account number using the IB Trader WorkstationSM connection ib.

example

p = portfolio(ib,acctno) retrieves current Interactive Brokers portfolio data using the account number acctno.

example

p = portfolio(ib,acctno,eventhandler) retrieves current Interactive Brokers portfolio 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); 

Retrieve current Interactive Brokers portfolio data using ib.

p = portfolio(ib)
p = 

             Type: {5x1 cell}
           Source: {5x1 cell}
          EventID: {5x1 cell}
         contract: {5x1 cell}
         position: {5x1 cell}
      marketPrice: {5x1 cell}
      marketValue: {5x1 cell}
      averageCost: {5x1 cell}
    unrealizedPNL: {5x1 cell}
      realizedPNL: {5x1 cell}
      accountName: {5x1 cell}

p is a structure that contains these fields:

  • Event type

  • Interactive Brokers ActiveX® object

  • Event identifier

  • Contract details

  • Number of shares for each contract

  • Price of the shares for each contract

  • Number of shares multiplied by the price of the shares for each contract

  • Average price when the shares are purchased for each contract

  • Unrealized profit and loss for each contract

  • Actual profit and loss for each contract

  • Account number

5x1 means there are five contracts in this portfolio. For details about this data, see Interactive Brokers API Reference Guide.

Display the market price for each contract in the portfolio.

p.marketPrice
ans = 

    [  8.60]
    [582.95]
    [591.79]
    [188.44]
    [ 42.24]

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); 

Retrieve current Interactive Brokers portfolio data using ib and account number acctno.

acctno = 'DU111111';

p = portfolio(ib,acctno)
p = 

             Type: {5x1 cell}
           Source: {5x1 cell}
          EventID: {5x1 cell}
         contract: {5x1 cell}
         position: {5x1 cell}
      marketPrice: {5x1 cell}
      marketValue: {5x1 cell}
      averageCost: {5x1 cell}
    unrealizedPNL: {5x1 cell}
      realizedPNL: {5x1 cell}
      accountName: {5x1 cell}

p is a structure that contains these fields:

  • Event type

  • Interactive Brokers ActiveX object

  • Event identifier

  • Contract details

  • Number of shares for each contract

  • Price of the shares for each contract

  • Number of shares multiplied by the price of the shares for each contract

  • Average price when the shares are purchased for each contract

  • Unrealized profit and loss for each contract

  • Actual profit and loss for each contract

  • Account number

5x1 means there are five contracts in this portfolio. For details about this data, see Interactive Brokers API Reference Guide.

Display the market price for each contract in the portfolio.

p.marketPrice
ans = 

    [  8.60]
    [582.95]
    [591.79]
    [188.44]
    [ 42.24]

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); 

Retrieve current Interactive Brokers portfolio data using ib, account number acctno, and sample event handler ibExampleEventHandler. Use ibExampleEventHandler or write a custom event handler function.

acctno = 'DU111111';

p = portfolio(ib,acctno,@ibExampleEventHandler)
p = 

     []

  Columns 1 through 5

    [1x1 COM.TWS_TwsCtrl_1]    [103]    [1x1 Interface.Tws_ActiveX_Control_module.IContract]    [60]    [8.58]

  Columns 6 through 12

    [515.10]    [8.22]    [21.68]    [0]    'DU111111'    [1x1 struct]    'updatePortfolioEx'
  ...

p is an empty double because ibExampleEventHandler displays the current Interactive Brokers portfolio data for each security in the Command Window.

The columns are:

  • Interactive Brokers ActiveX object

  • Event identifier

  • IB Trader Workstation IContract object

  • Number of shares

  • Price of the shares

  • Number of shares multiplied by the price of the shares

  • Average price when the shares are purchased

  • Unrealized profit and loss

  • Actual profit and loss

  • Account number

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

Account number, specified as a character vector or string scalar that identifies the Interactive Brokers account number.

Data Types: char | string

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 portfolio data, returned as a structure. The structure contains these fields. When using an event handler function, p is an empty double.

Field

Description

Type

Interactive Brokers event type name

Source

Interactive Brokers ActiveX object

EventID

Number that identifies the event type

contract

Structure that contains details for each contract in the portfolio

position

Number of shares for each contract in the portfolio

marketPrice

Price of the shares for each contract in the portfolio

marketValue

Number of shares multiplied by the price of the shares for each contract in the portfolio

averageCost

Average price when the shares are purchased for each contract in the portfolio

unrealizedPNL

Unrealized profit and loss for each contract in the portfolio

realizedPNL

Actual profit and loss for each contract in the portfolio

accountName

Account number

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 R2015a

Was this topic helpful?