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

# Trades Channel

> Subscribe to real-time trade execution updates

The `trades` channel streams real-time trade execution updates for prediction markets. Use this channel to build trade feeds, activity monitors, and market analysis tools.

## Connection

See the [WebSocket overview](/resources/metadata-api/websockets/overview#connection) for connection setup and endpoint details.

## Subscribe

### Subscribe to All Trades

```json theme={null}
{
  "type": "subscribe",
  "channel": "trades",
  "all": true
}
```

<Note>
  `all: true` streams all trades on the channel, including trades from markets created after the subscription is established.
</Note>

### Subscribe to Specific Tickers

```json theme={null}
{
  "type": "subscribe",
  "channel": "trades",
  "tickers": ["BTCD-25DEC0313-T92749.99", "SPX-25DEC0313-T5000"]
}
```

## Unsubscribe

### Unsubscribe from All Trades

```json theme={null}
{
  "type": "unsubscribe",
  "channel": "trades",
  "all": true
}
```

### Unsubscribe from Specific Tickers

```json theme={null}
{
  "type": "unsubscribe",
  "channel": "trades",
  "tickers": ["BTCD-25DEC0313-T92749.99"]
}
```

## Response Format

When subscribed to the `trades` channel, you'll receive messages with the following structure:

```json theme={null}
{
  "channel": "trades",
  "type": "trade",
  "market_ticker": "BTCD-25DEC0313-T92749.99",
  "trade_id": "abc123",
  "price": 47,
  "count": 100,
  "yes_price": 47,
  "no_price": 53,
  "yes_price_dollars": "0.47",
  "no_price_dollars": "0.53",
  "taker_side": "yes",
  "created_time": 1702744800000
}
```

### Response Fields

| Field               | Type   | Description                           |
| ------------------- | ------ | ------------------------------------- |
| `channel`           | string | Always `"trades"`                     |
| `type`              | string | Always `"trade"`                      |
| `market_ticker`     | string | Market identifier                     |
| `trade_id`          | string | Unique trade identifier               |
| `price`             | number | Trade execution price                 |
| `count`             | number | Number of contracts traded            |
| `yes_price`         | number | YES outcome price at execution        |
| `no_price`          | number | NO outcome price at execution         |
| `yes_price_dollars` | string | YES price formatted in dollars        |
| `no_price_dollars`  | string | NO price formatted in dollars         |
| `taker_side`        | string | Side of the taker (`"yes"` or `"no"`) |
| `created_time`      | number | Unix timestamp in milliseconds        |

## Code Examples

<AccordionGroup>
  <Accordion title="Subscribe to All Trades">
    ```typescript theme={null}
    // Dev endpoint — no API key required, but rate-limited.
    // For production, use your production WS URL and add:
    //   { headers: { "x-api-key": "YOUR_API_KEY" } }
    // as the second argument to new WebSocket().
    const WS_URL = "wss://dev-prediction-markets-api.dflow.net/api/v1/ws";

    const ws = new WebSocket(WS_URL);

    ws.onopen = () => {
      console.log("Connected to WebSocket");

      // Subscribe to all trade updates
      ws.send(
        JSON.stringify({
          type: "subscribe",
          channel: "trades",
          all: true,
        })
      );
    };

    ws.onmessage = (event) => {
      const message = JSON.parse(event.data);

      if (message.channel === "trades") {
        console.log("Trade executed:", {
          ticker: message.market_ticker,
          tradeId: message.trade_id,
          side: message.taker_side,
          count: message.count,
          yesPrice: message.yes_price_dollars,
          noPrice: message.no_price_dollars,
          time: new Date(message.created_time).toISOString(),
        });
      }
    };

    ws.onerror = (error) => {
      console.error("WebSocket error:", error);
    };

    ws.onclose = () => {
      console.log("WebSocket connection closed");
    };
    ```
  </Accordion>

  <Accordion title="Subscribe to Specific Markets">
    ```typescript theme={null}
    // Dev endpoint — no API key required, but rate-limited.
    // For production, use your production WS URL and add:
    //   { headers: { "x-api-key": "YOUR_API_KEY" } }
    // as the second argument to new WebSocket().
    const WS_URL = "wss://dev-prediction-markets-api.dflow.net/api/v1/ws";

    // Market tickers to subscribe to
    const marketTickers = ["BTCD-25DEC0313-T92749.99", "SPX-25DEC0313-T5000"];

    const ws = new WebSocket(WS_URL);

    ws.onopen = () => {
      console.log("Connected to WebSocket");

      // Subscribe to specific tickers for trades
      ws.send(
        JSON.stringify({
          type: "subscribe",
          channel: "trades",
          tickers: marketTickers,
        })
      );
    };

    ws.onmessage = (event) => {
      const message = JSON.parse(event.data);
      console.log(`Trade on ${message.market_ticker}:`, message);
    };
    ```
  </Accordion>

  <Accordion title="TypeScript Interface">
    ```typescript theme={null}
    interface TradeUpdate {
      channel: "trades";
      type: "trade";
      market_ticker: string;
      trade_id: string;
      price: number;
      count: number;
      yes_price: number;
      no_price: number;
      yes_price_dollars: string;
      no_price_dollars: string;
      taker_side: "yes" | "no";
      created_time: number;
    }

    function handleTradeUpdate(update: TradeUpdate) {
      console.log(`Trade executed on ${update.market_ticker}:`, {
        tradeId: update.trade_id,
        side: update.taker_side,
        count: update.count,
        yesPrice: update.yes_price_dollars,
        noPrice: update.no_price_dollars,
        time: new Date(update.created_time).toISOString(),
      });
    }
    ```
  </Accordion>
</AccordionGroup>

## Related

<CardGroup cols={3}>
  <Card title="Prices Channel" href="prices" icon="chart-line">
    Subscribe to real-time bid/ask price updates
  </Card>

  <Card title="Orderbook Channel" href="orderbook" icon="list">
    Subscribe to real-time orderbook depth updates
  </Card>

  <Card title="Trades API" href="/resources/metadata-api/trades/trades" icon="receipt">
    Query historical trade data via REST API
  </Card>
</CardGroup>
