Skip to main content

Profit & Loss Stream

Beta

The WebSocket API is currently in beta. Features and endpoints are subject to change.

Overview

The PnL Stream provides real-time streaming of account profit and loss updates with position-level details. It updates based on price changes and order fills. This stream is designed to give you up-to-the-second insights into your account's performance, including both aggregate account values and individual position metrics.

Endpoint: wss://webapi.tradezero.com/stream/pnl

Usage

Step 1: Connect and Authenticate

First, connect to the PnL endpoint and authenticate as described in the Introduction page.

Step 2: Subscribe to an Account

After authentication, subscribe to PnL updates for a specific account by sending:

{
"account": "YOUR_ACCOUNT_ID"
}

Subscription Responses

Success - Initial Snapshot:

You'll receive a complete snapshot of the account's current state:

{
"ts": 1770232416261,
"action": "init",
"target": "pnlReturn",
"pnlReturn": {
"accountValue": 9883.69,
"availableCash": 9755.18,
"optionCashUsed": 0.0,
"usedLeverage": 1.87,
"allowedLeverage": 2.0,
"sharesTraded": 0.0,
"exposure": 18447.89,
"dayUnrealized": -116.31,
"dayRealized": 0.0,
"dayPnl": -116.31,
"totalUnrealized": -1068.22,
"equityRatio": 0.535762704720142653,
"positions": [
{
"positionId": "7260105185048870000",
"symbol": "AAPL",
"pnlCalc": {
"unrealizedPnL": 60.08,
"dayUnrealizedPnL": 23.44,
"pctPnLMove": 5.77,
"dayPctPnLMove": 2.17,
"exposure": 1101.36
},
"realizedPnl": 0.0,
"dayRealizedPnl": 0.0
}
]
}
}

Error - Invalid Account:

{
"ts": 1770237416195,
"action": "error",
"message": "account not found"
}

Real-Time Updates

After receiving the initial snapshot, you'll receive two types of update messages:

1. Aggregate Account Updates

Updates to account-level aggregate values:

{
"ts": 1770232416799,
"action": "update",
"target": "aggCalcs",
"aggCalcs": {
"accountValue": 9883.82,
"exposure": 18448.02,
"dayUnrealized": -116.18,
"dayPnl": -116.18,
"totalUnrealized": -1068.09,
"equityRatio": 0.535765976119629001
}
}

2. Position-Level Updates

Updates to individual position values:

{
"ts": 1770232416799,
"action": "update",
"target": "position",
"position": {
"positionId": "7260109165951251001",
"symbol": "MSFT",
"pnlCalc": {
"unrealizedPnL": -103.14,
"dayUnrealizedPnL": 11.36,
"pctPnLMove": -11.01,
"dayPctPnLMove": 1.38,
"exposure": 833.78
}
}
}

Multiple Account Subscriptions

You can subscribe to multiple accounts by sending additional account messages:

{
"account": "ANOTHER_ACCOUNT_ID"
}

Each account will send its own initial snapshot followed by real-time updates.

Field Definitions

Account-Level Fields

FieldTypeDescription
accountValuenumberTotal account value
availableCashnumberCash available for trading
optionCashUsednumberCash allocated to options positions
usedLeveragenumberCurrent leverage being used
allowedLeveragenumberMaximum allowed leverage
sharesTradednumberNumber of shares traded
exposurenumberTotal market exposure
dayUnrealizednumberUnrealized profit/loss for the day
dayRealizednumberRealized profit/loss for the day
dayPnlnumberTotal profit/loss for the day
totalUnrealizednumberTotal unrealized profit/loss
equityRationumberEquity to account value ratio

Position-Level Fields

FieldTypeDescription
positionIdstringUnique identifier for the position
symbolstringStock symbol
unrealizedPnLnumberTotal unrealized profit/loss
dayUnrealizedPnLnumberUnrealized profit/loss for the day
pctPnLMovenumberPercentage change in total P&L
dayPctPnLMovenumberPercentage change in day P&L
exposurenumberPosition's market exposure
realizedPnlnumberTotal realized profit/loss
dayRealizedPnlnumberRealized profit/loss for the day