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.
Note: 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 v973+. 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 IBApi::EWrapper::positionEnd callback. 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.
-
-
-
-
m_pClient->reqPositions();
-
Receiving
After invoking the above, the positions will then be received through the IBApi.EWrapper.position callback. After the initial callback (only) of all positions, the IBApi.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
-
public class EWrapperImpl : EWrapper
{
... public virtual void position(string account, Contract contract, decimal pos, double avgCost)
{
Console.WriteLine("Position. " + account + " - Symbol: " + contract.Symbol + ", SecType: " + contract.SecType + ", Currency: " + contract.Currency +
", Position: " + Util.DecimalMaxString(pos) + ", Avg cost: " + Util.DoubleMaxString(avgCost));
}
... public virtual void positionEnd()
{
Console.WriteLine("PositionEnd \n");
}
-
public class EWrapperImpl implements EWrapper {
... @Override
public void position(String account, Contract contract, Decimal pos, double avgCost) {
System.out.println(EWrapperMsgGenerator.position(account, contract, pos, avgCost));
}
... @Override
public void positionEnd() {
System.out.println("Position End: " + EWrapperMsgGenerator.positionEnd());
}
-
Public Class EWrapperImpl
Implements EWrapper
... Public Sub position(account As String, contract As IBApi.Contract, pos As Decimal, avgCost As Double) Implements IBApi.EWrapper.position
Console.WriteLine("Position. " & account & " - Symbol: " & contract.Symbol & ", SecType: " & contract.SecType & ", Currency: " &
contract.Currency & ", Position: " & Util.DecimalMaxString(pos) & ", Avg cost: " & Util.DoubleMaxString(avgCost))
End Sub
... Public Sub positionEnd() Implements IBApi.EWrapper.positionEnd
Console.WriteLine("PositionEnd")
End Sub
-
class TestCppClient : public EWrapper
{
... void TestCppClient::position( const std::string& account, const Contract& contract, Decimal position, double avgCost) {
printf( "Position. %s - Symbol: %s, SecType: %s, Currency: %s, Position: %s, Avg Cost: %s\n", account.c_str(), contract.symbol.c_str(), contract.secType.c_str(), contract.currency.c_str(), DecimalFunctions::decimalStringToDisplay(position).c_str(), Utils::doubleMaxString(avgCost).c_str());
}
... void TestCppClient::positionEnd() {
printf( "PositionEnd\n");
}
-
1 class TestWrapper(wrapper.EWrapper):
... 1 def position(self, account: str, contract: Contract, position: Decimal,
3 super().position(account, contract, position, avgCost)
4 print(
"Position.",
"Account:", account,
"Symbol:", contract.symbol,
"SecType:",
5 contract.secType,
"Currency:", contract.currency,
6 "Position:", decimalMaxString(position),
"Avg cost:", floatMaxString(avgCost))
...
Cancelling
To cancel the reqPosition subscription, invoke IBApi::EClient::cancelPositions:
-
client.cancelPositions();
-
client.cancelPositions();
-
-
m_pClient->cancelPositions();
-
Position Update Subscription by Model
The function IBApi.EClient.reqPositionsMulti can be used with any account structure to subscribe to positions updates for multiple accounts and/or models. The account and model parameters are optional if there are not multiple accounts or models available. It is more efficient to use this function for a specific subset of accounts than using IBApi.EClient.reqPositions. If using TWS v983+ a profile name can be accepted in place of group in the account parameter. See Unification of Groups and Profiles
-
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. To cancel a positionsMulti subscription the function IBApi.EClient.cancelPositionsMulti is invoked with the same request ID used to create the subscription.
-
public class EWrapperImpl : EWrapper
{
... public virtual void positionMulti(int reqId, string account, string modelCode, Contract contract, decimal pos, double avgCost)
{
Console.WriteLine("Position Multi. Request: " + reqId + ", Account: " + account + ", ModelCode: " + modelCode + ", Symbol: " + contract.Symbol + ", SecType: " + contract.SecType +
", Currency: " + contract.Currency + ", Position: " + Util.DecimalMaxString(pos) + ", Avg cost: " + Util.DoubleMaxString(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, Decimal pos, double avgCost) {
System.out.println(EWrapperMsgGenerator.positionMulti(reqId, account, modelCode, contract, pos, avgCost));
}
... @Override
public void positionMultiEnd(int reqId) {
System.out.println("Position Multi End: " + EWrapperMsgGenerator.positionMultiEnd(reqId));
}
-
Public Class EWrapperImpl
Implements EWrapper
... Public Sub positionMulti(requestId As Integer, account As String, modelCode As String, contract As Contract, pos As Decimal, avgCost As Double) Implements IBApi.EWrapper.positionMulti
Console.WriteLine("PositionMulti. Id: " & requestId & ", Account: " & account & ", ModelCode: " & modelCode & ", Contract: " & contract.Symbol & ", pos: " & Util.DecimalMaxString(pos) &
", avgCost: " & Util.DoubleMaxString(avgCost))
End Sub
... Public Sub positionMultiEnd(requestId As Integer) Implements IBApi.EWrapper.positionMultiEnd
Console.WriteLine("PositionMultiEnd")
End Sub
-
class TestCppClient : public EWrapper
{
... void TestCppClient::positionMulti( int reqId, const std::string& account,const std::string& modelCode, const Contract& contract, Decimal pos, double avgCost) {
printf("Position Multi. Request: %d, Account: %s, ModelCode: %s, Symbol: %s, SecType: %s, Currency: %s, Position: %s, Avg Cost: %s\n", reqId, account.c_str(), modelCode.c_str(), contract.symbol.c_str(), contract.secType.c_str(), contract.currency.c_str(), DecimalFunctions::decimalStringToDisplay(pos).c_str(), Utils::doubleMaxString(avgCost).c_str());
}
... 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: Decimal, avgCost: float):
3 super().positionMulti(reqId, account, modelCode, contract, pos, avgCost)
4 print(
"PositionMulti. RequestId:", reqId,
"Account:", account,
5 "ModelCode:", modelCode,
"Symbol:", contract.symbol,
"SecType:",
6 contract.secType,
"Currency:", contract.currency,
",Position:",
7 decimalMaxString(pos),
"AvgCost:", floatMaxString(avgCost))
... 1 def positionMultiEnd(self, reqId: int):
2 super().positionMultiEnd(reqId)
3 print(
"PositionMultiEnd. RequestId:", reqId)