• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Positions

Requesting

A limitation of the function IBApi.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 IBApi.EClient.reqPositions is available. After initially invoking reqPositions, information about all positions in all associated accounts will be returned. Thereafter, when a position has changed an update will be returned to the IBApi::EWrapper::position function. To cancel a reqPositions subscription, invoke IBApi::EClient::cancelPositions.

  • client.reqPositions();
  • client.reqPositions();
  • client.reqPositions()
  • m_pClient->reqPositions();
  • 1  self.reqPositions()

Receiving

After invoking the above, the positions will then be received through the IBApi.EWrapper.position callback. After the initial callback of all positions, the IBApi.EWrapper.positionEnd function will be triggered.

  • public class EWrapperImpl : EWrapper
    {
    ...
    public virtual void position(string account, Contract contract, double pos, double avgCost)
    {
    Console.WriteLine("Position. "+account+" - Symbol: "+contract.Symbol+", SecType: "+contract.SecType+", Currency: "+contract.Currency+", Position: "+pos+", Avg cost: "+avgCost);
    }
    ...
    public virtual void positionEnd()
    {
    Console.WriteLine("PositionEnd \n");
    }
  • public class EWrapperImpl implements EWrapper {
    ...
    @Override
    public void position(String account, Contract contract, double pos,
    double avgCost) {
    System.out.println("Position. "+account+" - Symbol: "+contract.symbol()+", SecType: "+contract.secType()+", Currency: "+contract.currency()+", Position: "+pos+", Avg cost: "+avgCost);
    }
    ...
    @Override
    public void positionEnd() {
    System.out.println("PositionEnd \n");
    }
  • Public Class EWrapperImpl
    Implements EWrapper
    ...
    Public Sub position(account As String, contract As IBApi.Contract, pos As Double, avgCost As Double) Implements IBApi.EWrapper.position
    Console.WriteLine("Position. " & account & " - Symbol: " & contract.Symbol & ", SecType: " & contract.SecType & ", Currency: " &
    contract.Currency & ", Position: " & pos & ", Avg cost: " & avgCost)
    End Sub
    ...
    Public Sub positionEnd() Implements IBApi.EWrapper.positionEnd
    Console.WriteLine("PositionEnd \n")
    End Sub
  • class TestCppClient : public EWrapper
    {
    ...
    void TestCppClient::position( const std::string& account, const Contract& contract, double position, double avgCost) {
    printf( "Position. %s - Symbol: %s, SecType: %s, Currency: %s, Position: %g, Avg Cost: %g\n", account.c_str(), contract.symbol.c_str(), contract.secType.c_str(), contract.currency.c_str(), position, avgCost);
    }
    ...
    void TestCppClient::positionEnd() {
    printf( "PositionEnd\n");
    }
  • 1 class TestWrapper(wrapper.EWrapper):
    ...
    1  def position(self, account: str, contract: Contract, position: float,
    2  avgCost: float):
    3  super().position(account, contract, position, avgCost)
    4  print("Position.", account, "Symbol:", contract.symbol, "SecType:",
    5  contract.secType, "Currency:", contract.currency,
    6  "Position:", position, "Avg cost:", avgCost)
    7 
    ...
    1  def positionEnd(self):
    2  super().positionEnd()
    3  print("PositionEnd")
    4 

Cancelling

To cancel the reqPosition subscription, invoke IBApi::EClient::cancelPositions:

  • client.cancelPositions();
  • client.cancelPositions();
  • client.cancelPositions()
  • m_pClient->cancelPositions();
  • 1  self.cancelPositions()

Position Update Subscription by Model

The function IBApi.EClient.reqPositionsMulti can be used with multiple account structures to subscribe to positions updates for multiple accounts and/or models. The account and model parameters are optional.

  • client.reqPositionsMulti(9003, "DU74649", "EUstocks");
  • client.reqPositionsMulti(9003, "DU74649", "EUstocks");
  • client.reqPositionsMulti(9003, "DU74649", "EUstocks")
  • m_pClient->reqPositionsMulti(9003, "U150462", "EUstocks");
  • 1  self.reqPositionsMulti(9006, self.account, "")

After invoking IBApi.EClient.reqPositionsMulti data will be returned to the IBApi.EWrapper.positionMulti function. After the initial callback of all positions matching the supplied criteria to reqPositionsMulti, the IBApi.EWrapper.positionMultiEnd function will be triggered. Thereafter, there will only be messages sent to positionsMulti when there is a change.

  • public class EWrapperImpl : EWrapper
    {
    ...
    public virtual void positionMulti(int reqId, string account, string modelCode, Contract contract, double pos, double avgCost)
    {
    Console.WriteLine("Position Multi. Request: " + reqId + ", Account: " + account + ", ModelCode: " + modelCode + ", Symbol: " + contract.Symbol + ", SecType: " + contract.SecType + ", Currency: " + contract.Currency + ", Position: " + pos + ", Avg cost: " + avgCost + "\n");
    }
    ...
    public virtual void positionMultiEnd(int reqId)
    {
    Console.WriteLine("Position Multi End. Request: " + reqId + "\n");
    }
  • public class EWrapperImpl implements EWrapper {
    ...
    @Override
    public void positionMulti(int reqId, String account, String modelCode,
    Contract contract, double pos, double avgCost) {
    System.out.println("Position Multi. Request: " + reqId + ", Account: " + account + ", ModelCode: " + modelCode + ", Symbol: " + contract.symbol() + ", SecType: " + contract.secType() + ", Currency: " + contract.currency() + ", Position: " + pos + ", Avg cost: " + avgCost + "\n");
    }
    ...
    @Override
    public void positionMultiEnd(int reqId) {
    System.out.println("Position Multi End. Request: " + reqId + "\n");
    }
  • Public Class EWrapperImpl
    Implements EWrapper
    ...
    Public Sub positionMulti(requestId As Integer, account As String, modelCode As String, contract As Contract, pos As Double, avgCost As Double) Implements IBApi.EWrapper.positionMulti
    Console.WriteLine("positionMulti. Id: " & requestId & ", Account: " & account & ", ModelCode: " & modelCode & ", Contract: " & contract.Symbol & ", pos: " & pos & ", avgCost: " & avgCost)
    End Sub
    ...
    Public Sub positionMultiEnd(requestId As Integer) Implements IBApi.EWrapper.positionMultiEnd
    Console.WriteLine("positionMultiEnd \n")
    End Sub
  • class TestCppClient : public EWrapper
    {
    ...
    void TestCppClient::positionMulti( int reqId, const std::string& account,const std::string& modelCode, const Contract& contract, double pos, double avgCost) {
    printf("Position Multi. Request: %d, Account: %s, ModelCode: %s, Symbol: %s, SecType: %s, Currency: %s, Position: %g, Avg Cost: %g\n", reqId, account.c_str(), modelCode.c_str(), contract.symbol.c_str(), contract.secType.c_str(), contract.currency.c_str(), pos, avgCost);
    }
    ...
    void TestCppClient::positionMultiEnd( int reqId) {
    printf("Position Multi End. Request: %d\n", reqId);
    }
  • 1 class TestWrapper(wrapper.EWrapper):
    ...
    1  def positionMulti(self, reqId: int, account: str, modelCode: str,
    2  contract: Contract, pos: float, avgCost: float):
    3  super().positionMulti(reqId, account, modelCode, contract, pos, avgCost)
    4  print("Position Multi. Request:", reqId, "Account:", account,
    5  "ModelCode:", modelCode, "Symbol:", contract.symbol, "SecType:",
    6  contract.secType, "Currency:", contract.currency, ",Position:",
    7  pos, "AvgCost:", avgCost)
    8 
    ...
    1  def positionMultiEnd(self, reqId: int):
    2  super().positionMultiEnd(reqId)
    3  print("Position Multi End. Request:", reqId)
    4