Skip to content

Portfolio

ibc.rest.portfolio.PortfolioAccounts

Client for managing portfolio and account-related operations via the Interactive Brokers API.

__init__(ib_client, ib_session)

Initializes the PortfolioAccounts client.

Parameters

ib_client : InteractiveBrokersClient The InteractiveBrokersClient Python Client.

InteractiveBrokersSession

The IB session handler.

accounts()

Returns the portfolio accounts

Overview

In non-tiered account structures, returns a list of accounts for which the user can view position and account information. This endpoint must be called prior to calling other /portfolio endpoints for those accounts. For querying a list of accounts which the user can trade, see /iserver/accounts. For a list of subaccounts in tiered account structures (e.g. financial advisor or ibroker accounts) see /portfolio/subaccounts.

Returns

list[dict]: A collection of PortfolioAccount resources.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.accounts()

subaccounts()

Returns the portfolio subaccounts

Overview

Used in tiered account structures (such as financial advisor and ibroker accounts) to return a list of sub-accounts for which the user can view position and account-related information. This endpoint must be called prior to calling other /portfolio endpoints for those subaccounts. To query a list of accounts the user can trade, see /iserver/accounts.

Returns

list[dict]: A collection of PortfolioSubAccount resources.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.subaccounts()

subaccounts2(page=0)

Returns a list of sub-accounts for large account structures.

Overview

Used in tiered account structures (such as financial advisor and ibroker accounts) with many sub-accounts. Supports pagination. Use this endpoint instead of /portfolio/subaccounts when dealing with more than 100 sub-accounts.

Parameters

page : int (optional, Default=0) The page number (zero-indexed).

Returns

dict: A paginated collection of PortfolioSubAccount resources.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_service.subaccounts2(page=0)

account_metadata(account_id)

Account information related to account Id.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint.

Returns

dict: A AccountInfo resource.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.account_metadata(
    account_id=ibc_client.account_number
)

account_summary(account_id)

Returns information about margin, cash balances and other information related to specified account.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint.

Returns

dict: A AccountSummary resource.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.account_summary(
    account_id=ibc_client.account_number
)

account_ledger(account_id)

Information regarding settled cash, cash balances, etc. in the account’s base currency and any other cash balances hold in other currencies.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint. The list of supported currencies is available at: https://www.interactivebrokers.com/en/index.php?f=3185

Returns

dict[str, Ledger]: A Ledger resource keyed by currency.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.account_ledger(
    account_id=ibc_client.account_number
)

account_allocation(account_id)

Information about the account’s portfolio by Asset Class, Industry and Category.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint. The list of supported currencies is available at: https://www.interactivebrokers.com/en/index.php?f=3185

Returns

dict: A AccountAllocation resource.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.account_allocation(
    account_id=ibc_client.account_number
)

portfolio_allocation(account_ids)

Similar to /portfolio/{accountId}/allocation but returns a consolidated view of of all the accounts returned by /portfolio/accounts

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint.

Parameters

account_ids : List[str] A list of accounts that you want to be consolidated into the view.

Returns

dict: A consolidated AccountAllocation resource.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.portfolio_allocation(
    account_ids=[ibc_client.account_number]
)

portfolio_positions(account_id, page_id=0, sort=None, direction=None, period=None)

Returns a list of positions for the given account. The endpoint supports paging, page’s default size is 30 positions.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint.

Parameters

account_id : str The account you want to query for positions.

int (optional, Default=0)

The page you want to query.

Union[str, Enum] (optional, Default=None)

The field on which to sort the data on.

Union[str, Enum] (optional, Default=None)

The order of the sort, a means ascending and d means descending

str (optional, Default=None)

The period for pnl column, can be 1D, 7D, 1M...

Returns

dict: A collection of PortfolioPosition resources.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.portfolio_positions(
        account_id=ibc_client.account_number,
        page_id=0,
        sort=SortFields.BaseUnrealizedPnl,
        direction=SortDirection.Descending
    )

position_by_contract_id(account_id, contract_id)

Returns a list of all positions matching the conid. For portfolio models the conid could be in more than one model, returning an array with the name of model it belongs to.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint.

Parameters

account_id : str The account you want to query for positions.

str

The contract ID you want to query.

Returns

dict: A collection of PortfolioPosition resources.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.position_by_contract_id(
    account_id=ibc_client.account_number,
    contract_id='251962528'
)

positions_by_contract_id(contract_id)

Returns an object of all positions matching the conid for all the selected accounts. For portfolio models the conid could be in more than one model, returning an array with the name of the model it belongs to.

Overview

/portfolio/accounts or /portfolio/subaccounts must be called prior to this endpoint.

Parameters

contract_id : str The contract ID you want to query.

Returns

dict: A collection of PortfolioPosition resources.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.positions_by_contract_id(
    contract_id='251962528'
)

invalidate_positions_cache(account_id)

Invalidates the backend cache of the Portfolio.

Parameters

account_id : str The account you want to query for positions.

Returns

Union[dict, None]: Nothing is returned if successful.

Usage

>>> portfolio_accounts_service = ibc_client.portfolio_accounts
>>> portfolio_accounts_services.invalidate_positions_cache()