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

# Profit & Loss

## Introduction

Requests can be made to receive real time updates about the daily P\&L and unrealized P\&L for an account, or for individual positions. Financial Advisors can also request P\&L figures for 'All' subaccounts, or for a portfolio model. This is further extended to include realized P\&L information at the account or individual position level.

The P\&L API functions demonstrated below return the data which is displayed in the TWS Portfolio Window in current versions of TWS. As such, the P\&L values are calculated based on the reset schedule specified in TWS Global Configuration (by default an instrument-specific reset schedule) and this setting affects values sent to the associated API functions as well. Also in TWS, P\&L data from virtual forex positions will be included in the account P\&L if and only if the Virtual Fx section of the Account Window is expanded.

See [Account Updates](/docs/tws-api/doc/accounts/updates) for alternative PnL data.

---

## Request P\&L for Accounts

`EClient.reqPnL(reqId, account, modelCode)`

Creates subscription for real time daily PnL and unrealized PnL updates. Updates are sent to `EWrapper.pnl`.

* Introducing broker accounts with less than 50 subaccounts can receive aggregate PnL for all subaccounts by specifying 'All' as the account code.
* With requests for advisor accounts with many subaccounts and/or positions can take several seconds for aggregated P\&L to be computed and returned.
* For account P\&L data the TWS setting "Prepare portfolio PnL data when downloading positions" must be checked.

**`reqId`** `int`

Request ID to track the data.

---

**`account`** `string`

Account for which to receive PnL updates.

---

**`modelCode`** `string`

Specify to request PnL updates for a specific model.

---

\


#### Python

```python
self.reqPnL(reqId, "U1234567", "")
```

#### Python (Full Example)

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

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

    def pnl(self, reqId: int, dailyPnL: float, unrealizedPnL: float, realizedPnL: float):
        print("Daily PnL. ReqId:", reqId, "DailyPnL:", dailyPnL, "UnrealizedPnL:", unrealizedPnL, "RealizedPnL:", realizedPnL)

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

time.sleep(1)
app.reqPnL(102, "U123456", "")

app.run()
```

#### C++

```cpp
m_pClient->reqPnL(reqId, "U1234567", "");
```

#### C\#

```csharp
client.reqPnL(reqId, "U1234567", "");
```

#### VB.NET

```vbnet
client.reqPnL(reqId, "U1234567", "")
```

---

## Receive P\&L for Accounts

`EWrapper.pnl(reqId, dailyPnL, unrealizedPnL, realizedPnL)`

**`reqId`** `int`

Request identifier for tracking data.

---

**`dailyPnL`** `double`

DailyPnL updates for the account in real time.

---

**`unrealizedPnL`** `double`

Total Unrealized PnL updates for the account in real time.

---

**`realizedPnL`** `double`

Total Realized PnL updates for the account in real time.

---

\


#### Python

```python
def pnl(self, reqId: int, dailyPnL: float, unrealizedPnL: float, realizedPnL: float):
  print("Daily PnL. ReqId:", reqId, "DailyPnL:", dailyPnL, "UnrealizedPnL:", unrealizedPnL, "RealizedPnL:", realizedPnL)
```

#### Java

```java
@Override
public void pnl(int reqId, double dailyPnL, double unrealizedPnL, double realizedPnL) {
	System.out.println(EWrapperMsgGenerator.pnl(reqId, dailyPnL, unrealizedPnL, realizedPnL));
}
```

#### C++

```cpp
void TestCppClient::pnl(int reqId, double dailyPnL, double unrealizedPnL, double realizedPnL) {
    printf("PnL. ReqId: %d, daily PnL: %s, unrealized PnL: %s, realized PnL: %s\n", reqId, Utils::doubleMaxString(dailyPnL).c_str(), Utils::doubleMaxString(unrealizedPnL).c_str(), 
        Utils::doubleMaxString(realizedPnL).c_str());
}
```

#### C\#

```csharp
public void pnl(int reqId, double dailyPnL, double unrealizedPnL, double realizedPnL)
{
	Console.WriteLine("PnL. Request Id: {0}, Daily PnL: {1}, Unrealized PnL: {2}, Realized PnL: {3}", reqId, Util.DoubleMaxString(dailyPnL), Util.DoubleMaxString(unrealizedPnL), Util.DoubleMaxString(realizedPnL));
}
```

#### VB.NET

```vbnet
 Public Sub pnl(reqId As Integer, dailyPnL As Double, unrealizedPnL As Double, realizedPnL As Double) Implements EWrapper.pnl
	Console.WriteLine("PnL. Request Id: {0}, daily PnL: {1}, unrealized PnL: {2}, realized PnL: {3}", reqId, Util.DoubleMaxString(dailyPnL), Util.DoubleMaxString(unrealizedPnL), Util.DoubleMaxString(realizedPnL))
End Sub
```

---

## Cancel P\&L for Accounts

`EClient.cancelPnL(reqId)`

Cancels subscription for real time updated daily PnL.

**`reqId`** `int`

Request identifier for tracking data.

---

\


#### Python

```python
self.cancelPnL(reqId)
```

#### Java

```java
client.cancelPnL(reqId);
```

#### C++

```cpp
m_pClient->cancelPnL(7001);
```

#### C\#

```csharp
client.cancelPnL(reqId);
```

#### VB.NET

```vbnet
client.cancelPnL(reqId)
```

---

## Request P\&L for Individual Positions

`EClient.reqPnLSingle(reqId, account, modelCode, conId)`

Requests real time updates for daily PnL of individual positions. Updates are returned to `EWrapper.pnlSingle` approximately once per second.

* If a P\&L subscription request is made for an invalid conId or contract not in the account, there will not be a response.
* As elsewhere in the API, a max double value will indicate an 'unset' value. This corresponds to an empty cell in TWS.
* Introducing broker accounts without a large number of subaccounts (\<50) can receive aggregate data by specifying the account as "All".
* Cannot be used with IBroker accounts configured for on-demand lookup with `account = 'All'`.

**`reqId`** `int`

Request identifier to track the data.

---

**`account`** `string`

Account in which position exists.

---

**`modelCode`** `string`

Model in which position exists.

---

**`conId`** `int`

Contract ID (conId) of contract to receive daily PnL updates for. Note: does not return message if invalid conId is entered.

---

\


#### Python

```python
self.reqPnLSingle(requestId, "U1234567", "", 265598)
```

#### Python (Full Example)

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

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

    def pnlSingle(self, reqId: int, pos: Decimal, dailyPnL: float, unrealizedPnL: float, realizedPnL: float, value: float):
        print("Daily PnL Single. ReqId:", reqId, "Position:", pos, "DailyPnL:", dailyPnL, "UnrealizedPnL:", unrealizedPnL, "RealizedPnL:", realizedPnL, "Value:", value)

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

time.sleep(1)
app.reqPnLSingle(101, "U123456", "", 8314) #IBM conId: 8314

app.run()
```

#### Java

```java
client.reqPnLSingle(requestId, "U1234567", "", 265598);
```

#### C++

```cpp
m_pClient->reqPnLSingle(requestId, "U1234567", "", 265598);
```

#### C\#

```csharp
client.reqPnLSingle(requestId, "U1234567", "", 265598);
```

---

## Receive P\&L for Individual Positions

`EWrapper.pnlSingle(reqId, pos, dailyPnL, unrealizedPnL, realizedPnL, value)`

Receives real time updates for single position daily PnL values.

**`reqId`** `int`

Request identifier used for tracking.

---

**`pos`** `decimal`

Current size of the position.

---

**`dailyPnL`** `double`

DailyPnL for the position.

---

**`unrealizedPnL`** `double`

Total unrealized PnL for the position (since inception) updating in real time.

---

**`realizedPnL`** `double`

Total realized PnL for the position (since inception) updating in real time.

---

**`value`** `double`

Current market value of the position.

---

\


#### Python

```python
def pnlSingle(self, reqId: int, pos: Decimal, dailyPnL: float, unrealizedPnL: float, realizedPnL: float, value: float):
  print("Daily PnL Single. ReqId:", reqId, "Position:", pos, "DailyPnL:", dailyPnL, "UnrealizedPnL:", unrealizedPnL, "RealizedPnL:", realizedPnL, "Value:", value)
```

#### Java

```java
@Override
public void pnlSingle(int reqId, Decimal pos, double dailyPnL, double unrealizedPnL, double realizedPnL, double value) {
  System.out.println(EWrapperMsgGenerator.pnlSingle(reqId, pos, dailyPnL, unrealizedPnL, realizedPnL, value));                
}
```

#### C++

```cpp
void TestCppClient::pnlSingle(int reqId, Decimal pos, double dailyPnL, double unrealizedPnL, double realizedPnL, double value) {
    printf("PnL Single. ReqId: %d, pos: %s, daily PnL: %s, unrealized PnL: %s, realized PnL: %s, value: %s\n", reqId, decimalStringToDisplay(pos).c_str(), Utils::doubleMaxString(dailyPnL).c_str(), Utils::doubleMaxString(unrealizedPnL).c_str(), Utils::doubleMaxString(realizedPnL).c_str(), Utils::doubleMaxString(value).c_str());
}
```

#### C\#

```csharp
public void pnlSingle(int reqId, decimal pos, double dailyPnL, double unrealizedPnL, double realizedPnL, double value)
{
	Console.WriteLine("PnL Single. Request Id: {0}, Pos {1}, Daily PnL {2}, Unrealized PnL {3}, Realized PnL: {4}, Value: {5}", reqId, Util.DecimalMaxString(pos), Util.DoubleMaxString(dailyPnL), Util.DoubleMaxString(unrealizedPnL),
		Util.DoubleMaxString(realizedPnL), Util.DoubleMaxString(value));
}
```

#### VB.NET

```vbnet
Public Sub pnlSingle(reqId As Integer, pos As Decimal, dailyPnL As Double, unrealizedPnL As Double, realizedPnL As Double, value As Double) Implements EWrapper.pnlSingle
	Console.WriteLine("PnL Single. Request Id: {0}, pos: {1}, daily PnL: {2}, unrealized PnL: {3}, realized PnL: {4}, value: {5}", reqId, Util.DecimalMaxString(pos), Util.DoubleMaxString(dailyPnL), Util.DoubleMaxString(unrealizedPnL), Util.DoubleMaxString(realizedPnL), Util.DoubleMaxString(value))
End Sub
```

---

## Cancel P\&L for Individual Positions

`EClient.cancelPnLSingle(reqId)`

Cancels real time subscription for a position's daily PnL information.

**`reqId`** `int`

Request identifier to cancel the P\&L subscription for.

---

\


#### Python

```python
self.cancelPnLSingle(requestId)
```

#### Java

```java
client.cancelPnLSingle(reqId);
```

#### C++

```cpp
m_pClient->cancelPnLSingle(reqId);
```

#### C\#

```csharp
client.cancelPnLSingle(reqId);
```

#### VB.NET

```vbnet
client.cancelPnLSingle(reqId)
```