> This page is for TWS API.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://ibkrcampus.com/docs/llms.txt.

# Positions

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

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

#### Python

```python
self.reqPositions()
```

#### Python (Full Example)

```python
from ibapi.client import *
from ibapi.wrapper import *
import threading
import time

class TradingApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self,self)

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

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

def websocket_con():
    app.run()

app = TradingApp()      
app.connect("127.0.0.1", 7496, clientId=1)

con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1) 

app.reqPositions()
time.sleep(1)
```

#### Java

```java
client.reqPositions();
```

#### C++

```cpp
m_pClient->reqPositions();
```

#### C\#

```csharp
client.reqPositions();
```

#### VB.NET

```vbnet
client.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.

> **Info**
>
> 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.

---

\


#### Python

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

#### Java

```java
@Override
public void position(String account, Contract contract, Decimal pos, double avgCost) {
    System.out.println(EWrapperMsgGenerator.position(account, contract, pos, avgCost));
}
```

#### C\#

```csharp
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));
}
```

#### VB.NET

```vbnet
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
```

### EWrapper.positionEnd

`EWrapper.positionEnd()`

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

#### Python

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

#### Java

```java
@Override
public void positionEnd() {
	System.out.println("Position End: " + EWrapperMsgGenerator.positionEnd());
}
```

#### C++

```cpp
void TestCppClient::positionEnd() {
    printf( "PositionEnd\n");
}
```

#### C\#

```csharp
public virtual void positionEnd()
{
	Console.WriteLine("PositionEnd \n");
}
```

#### VB.NET

```vbnet
Public Sub positionEnd() Implements IBApi.EWrapper.positionEnd
	Console.WriteLine("PositionEnd")
End Sub
```

---

## Cancel Positions Request

`EClient.cancelPositions()`

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

#### Python

```python
self.cancelPositions()
```

#### Java

```java
client.cancelPositions();
```

#### C++

```cpp
m_pClient->cancelPositions();
```

#### C\#

```csharp
client.cancelPositions()
```

#### VB.NET

```vbnet
client.cancelPositions()
```