> ## Documentation Index
> Fetch the complete documentation index at: https://docs.upsidemax.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket trades Channel: Real-Time Public Trade Stream

> Subscribe to the trades WebSocket channel to receive a real-time stream of all public trades for a contract as they execute on-chain.

The `trades` channel streams every public trade for a contract — regardless of which account was involved. Each push message contains an array of one or more trade objects, all of which executed in the same block. If you only care about fills from your own account, use the [`userFills`](/websocket/user-fills) channel instead.

## Subscribing

```json theme={"system"}
{"method": "subscribe", "subscription": {"type": "trades", "asset": "1"}}
```

Replace `"1"` with the numeric contract ID whose trade stream you want to receive.

## Push message format

```json theme={"system"}
{
  "channel": "trades",
  "ts": 1782279307885,
  "data": [
    {
      "asset": "1",
      "px": "1133770",
      "sz": "5",
      "time": 0,
      "side": "B",
      "tid": 8842931
    }
  ]
}
```

Multiple trade objects may appear in the `data` array when several trades execute within the same block. Process them in array order.

## Trade object fields

<ResponseField name="asset" type="string">
  Contract ID. Matches the `asset` in your subscription.
</ResponseField>

<ResponseField name="px" type="string">
  Trade execution price (raw string). Apply the contract's decimal precision before displaying to users.
</ResponseField>

<ResponseField name="sz" type="string">
  Trade size (raw string). This is the notional quantity that changed hands at `px`.
</ResponseField>

<ResponseField name="side" type="string">
  Indicates which side initiated the trade. `"B"` means the buyer was the aggressor (taker); `"S"` means the seller was the aggressor (taker).
</ResponseField>

<ResponseField name="tid" type="number">
  Globally unique trade ID assigned on-chain. Use this to deduplicate trades if you receive the same block's data from multiple sources.
</ResponseField>

<ResponseField name="time" type="number">
  Reserved field; currently `0`. Use the outer `ts` field on the envelope for event timing.
</ResponseField>

## Outer envelope fields

<ResponseField name="ts" type="number">
  Server send timestamp in Unix milliseconds. Use this as the authoritative timestamp for all trades in the `data` array.
</ResponseField>

## Building a trade history feed

<Steps>
  <Step title="Subscribe to receive the live stream">
    Send the subscribe message. The server starts pushing trades from the moment of subscription. There is no backfill on subscribe.
  </Step>

  <Step title="Bootstrap history from candleSnapshot">
    To display historical trades or fill in chart data, call the REST `candleSnapshot` endpoint before or alongside subscribing. This gives you aggregated OHLCV history, and the WebSocket stream picks up from the current point in time.
  </Step>

  <Step title="Deduplicate using tid">
    If you consume trades from multiple sources (e.g., a REST snapshot and the WebSocket stream), use the `tid` field as the unique key to avoid counting the same trade twice.
  </Step>
</Steps>

<Note>
  The `trades` channel pushes from subscription time only — there is no historical backfill. Use `candleSnapshot` via the REST API to retrieve historical OHLCV data for chart initialisation.
</Note>

<Tip>
  To build a volume-weighted average price (VWAP) over a rolling window, accumulate `px × sz` from incoming trade messages, divide by the rolling total `sz`, and reset the window at your chosen interval.
</Tip>
