Skip to navigation

Introduction

A limitation of the function EClient.reqAccountUpdates is that it can only be used with a single account at a time. To create a subscription for position updates from multiple accounts, the function EClient.reqPositions is available.

The reqPositions function is not available in Introducing Broker or Financial Advisor master accounts that have very large numbers of subaccounts (> 50) to optimize the performance of TWS/IB Gateway. Instead the function reqPositionsMulti can be used to subscribe to updates from individual subaccounts. Also not available with IBroker accounts configured for on-demand account lookup.

After initially invoking reqPositions, information about all positions in all associated accounts will be returned, followed by the EWrapper.positionEnd callback. Thereafter, when a position has changed an update will be returned to the EWrapper.position function. To cancel a reqPositions subscription, invoke EClient.cancelPositions.


Request Positions

EClient.reqPositions()

Subscribes to position updates for all accessible accounts. All positions sent initially, and then only updates as positions change.

self.reqPositions()

Receive Positions

EWrapper.position(account, contract, pos, avgCost)

Provides the portfolio’s open positions. After the initial callback (only) of all positions, the EWrapper.positionEnd function will be triggered.

For futures, the exchange field will not be populated in the position callback as some futures trade on multiple exchanges.
account
string

The account holding the position.

contract
Contract

The position’s Contract.

pos
decimal

The number of positions held.

avgCost
double

The total average cost of all trades for the currently held position.


def position(self, account: str, contract: Contract, position: Decimal, avgCost: float):
print("Position.", "Account:", account, "Contract:", contract, "Position:", position, "Avg cost:", avgCost)

EWrapper.positionEnd

EWrapper.positionEnd()

Indicates all the positions have been transmitted. Only returned after the initial callback of EWrapper.position.

def positionEnd(self):
print("PositionEnd")

Cancel Positions Request

EClient.cancelPositions()

Cancels a previous position subscription request made with EClient.reqPositions().

self.cancelPositions()