> For the complete documentation index, see [llms.txt](https://docs.hypertracker.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hypertracker.io/order-book-data/market-streams.md). Agents can also search and read this documentation through its [MCP server](https://docs.hypertracker.io/~gitbook/mcp).

# Market Streams

Prices and executions of Hyperliquid markets, without book depth: best bid and offer, the mid price of one market, the mid prices of all markets, and trades. For depth see [L2 Order Book](https://docs.coinmarketman.com/order-book-data/l2-order-book), for individual orders see [L4 Order Book](https://docs.coinmarketman.com/order-book-data/l4-order-book).

| Stream         | Channel          | Message type | Subscribe (data) | First publication     |
| -------------- | ---------------- | ------------ | ---------------- | --------------------- |
| Best bid/offer | `bbo:<coin>`     | `bbo`        | Omit             | Next BBO change       |
| Mid price      | `mids:<coin>`    | `mids`       | Omit             | Next mid price change |
| All mid prices | `allmids:stream` | `allMids`    | Omit or `{}`     | Immediate snapshot    |
| Trades         | `trades:<coin>`  | `trades`     | Omit             | Next trade batch      |

`<coin>` is the canonical, case-sensitive market name, for example `BTC`, `xyz:GOLD`, or `@107`. Names with reserved characters must be encoded. See [Market names](https://docs.coinmarketman.com/order-book-data/getting-started#market-names).

Connection, subscribing, errors, and message envelopes are described in [Getting Started](https://docs.coinmarketman.com/order-book-data/getting-started). The examples below use the inner message: `ctx.data` in JavaScript, `ctx.pub.data` in Python.

### Best bid and offer: `bbo:<coin>`

The best bid and the best ask of one market, with price, total size, and order count. A message is published when the best bid or offer changes.

#### **Event format**

<table data-search="false"><thead><tr><th>Field</th><th>Type</th><th>Notes</th></tr></thead><tbody><tr><td><code>coin</code></td><td>string</td><td>Canonical market name.</td></tr><tr><td><code>time</code></td><td>number</td><td>Source block time in Unix epoch milliseconds.</td></tr><tr><td><code>bbo[0]</code></td><td>object or null</td><td>Best bid. <code>null</code> when the bid side has no resting orders.</td></tr><tr><td><code>bbo[1]</code></td><td>object or null</td><td>Best ask. <code>null</code> when the ask side has no resting orders.</td></tr><tr><td><code>px</code></td><td>string</td><td>Price of the level. Decimal string.</td></tr><tr><td><code>sz</code></td><td>string</td><td>Total resting size at that price. Decimal string.</td></tr><tr><td><code>n</code></td><td>number</td><td>Number of resting orders at that price.</td></tr></tbody></table>

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
{
  "channel": "bbo",
  "data": {
    "coin": "BTC",
    "time": 1786905889616,
    "bbo": [
      { "px": "63100.0", "sz": "12.5", "n": 4 },
      { "px": "63101.0", "sz": "3.2", "n": 2 }
    ]
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
{
    "channel": "bbo",
    "data": {
        "coin": "BTC",
        "time": 1786905889616,
        "bbo": [
            {"px": "63100.0", "sz": "12.5", "n": 4},
            {"px": "63101.0", "sz": "3.2", "n": 2},
        ],
    },
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

#### Subscribe

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
const sub = client.newSubscription('bbo:BTC');

sub.on('publication', (ctx) => {
  const [bid, ask] = ctx.data.data.bbo;
  console.log(bid?.px, ask?.px); // either side can be null
});

sub.subscribe();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
class BboHandler(SubscriptionEventHandler):
    async def on_publication(self, ctx):
        bid, ask = ctx.pub.data["data"]["bbo"]
        print(bid and bid["px"], ask and ask["px"])  # either side can be None


sub = client.new_subscription("bbo:BTC", events=BboHandler())
await sub.subscribe()
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Mid price: `mids:<coin>`

The mid price of one market. A message is published when the mid price changes.

#### **Event format**

| Field  | Type   | Notes                                                  |
| ------ | ------ | ------------------------------------------------------ |
| `coin` | string | Canonical market name.                                 |
| `time` | number | Source event or block time in Unix epoch milliseconds. |
| `mid`  | string | Mid price. Decimal string.                             |

For a book with both sides, `mid` is `(best bid + best ask) / 2`. For an empty or one-sided book, the last observed trade price is used when available. A message is published only when the `mid` string changes.

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
{
  "channel": "mids",
  "data": { "coin": "BTC", "time": 1786905889616, "mid": "63100.5" }
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
{
    "channel": "mids",
    "data": {"coin": "BTC", "time": 1786905889616, "mid": "63100.5"},
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

#### Subscribe

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
const sub = client.newSubscription('mids:BTC');

sub.on('publication', (ctx) => {
  console.log(ctx.data.data.coin, ctx.data.data.mid);
});

sub.subscribe();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
class MidHandler(SubscriptionEventHandler):
    async def on_publication(self, ctx):
        payload = ctx.pub.data["data"]
        print(payload["coin"], payload["mid"])


sub = client.new_subscription("mids:BTC", events=MidHandler())
await sub.subscribe()
```

{% endcode %}
{% endtab %}
{% endtabs %}

### All mid prices: `allmids:stream`

The mid price of every market in one channel. One message arrives right after you subscribe. After that, a complete map arrives whenever at least one mid price changes.

`allmids:stream` is not part of any plan and is enabled on request, see [Stream availability per plan](https://docs.hypertracker.io/order-book-data.md#stream-availability-per-plan). It counts as one subscription, not as an order book stream.

#### **Event format**

| Field  | Type   | Notes                                                        |
| ------ | ------ | ------------------------------------------------------------ |
| `mids` | object | Maps canonical market names to mid prices (decimal strings). |

This payload has no `time` field. A market is left out of `mids` while its mid price cannot be derived.

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
{
  "channel": "allMids",
  "data": {
    "mids": {
      "BTC": "63100.5",
      "ETH": "3100.25",
      "xyz:GOLD": "2398.4"
    }
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
{
    "channel": "allMids",
    "data": {
        "mids": {
            "BTC": "63100.5",
            "ETH": "3100.25",
            "xyz:GOLD": "2398.4",
        },
    },
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

#### Subscribe

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
const sub = client.newSubscription('allmids:stream');

sub.on('publication', (ctx) => {
  const { mids } = ctx.data.data;
  console.log(mids['BTC']);
});

sub.subscribe();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
class AllMidsHandler(SubscriptionEventHandler):
    async def on_publication(self, ctx):
        mids = ctx.pub.data["data"]["mids"]
        print(mids.get("BTC"))


sub = client.new_subscription("allmids:stream", events=AllMidsHandler())
await sub.subscribe()
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Trades: `trades:<coin>`

Every trade of one market, with the aggressor side and both counterparties. A message is published for each batch of trades.

#### **Event format**

The payload is an array of trades in chronological order. One batch can hold several trades.

<table data-search="false"><thead><tr><th width="141.010498046875">Field</th><th width="158.921875">Type</th><th>Notes</th></tr></thead><tbody><tr><td><code>coin</code></td><td>string</td><td>Canonical market name.</td></tr><tr><td><code>side</code></td><td>string</td><td>Aggressor (taker) side. <code>B</code> for buy or <code>A</code> for sell.</td></tr><tr><td><code>px</code></td><td>string</td><td>Trade price. Decimal string.</td></tr><tr><td><code>sz</code></td><td>string</td><td>Trade size. Decimal string.</td></tr><tr><td><code>hash</code></td><td>string</td><td>Transaction hash.</td></tr><tr><td><code>time</code></td><td>number</td><td>Source event or block time in Unix epoch milliseconds.</td></tr><tr><td><code>tid</code></td><td>number</td><td>Trade ID.</td></tr><tr><td><code>users</code></td><td>array</td><td>Two account addresses in the order <code>[buyer, seller]</code>.</td></tr></tbody></table>

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
{
  "channel": "trades",
  "data": [
    {
      "coin": "BTC",
      "side": "A",
      "px": "63100.0",
      "sz": "0.025",
      "hash": "0xde93...3503",
      "time": 1786905889616,
      "tid": 293353986402527,
      "users": ["0xbuyer...", "0xseller..."]
    }
  ]
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
{
    "channel": "trades",
    "data": [
        {
            "coin": "BTC",
            "side": "A",
            "px": "63100.0",
            "sz": "0.025",
            "hash": "0xde93...3503",
            "time": 1786905889616,
            "tid": 293353986402527,
            "users": ["0xbuyer...", "0xseller..."],
        }
    ],
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Trades have no replay. A batch your client drops cannot be requested again.
{% endhint %}

Trades are delivered on a best-effort basis. In rare cases of server-side delivery trouble, trades can be skipped. A gap message then follows on the same channel, for example `{"channel":"trades","data":[],"gap":{"fromHeight":1171300001,"toHeight":1171300090,"dropped":12}}`.

* `fromHeight` and `toHeight` are the first and last block heights (inclusive) whose trades may be missing. `fromHeight` is `0` if the start is unknown.
* `dropped` is the number of missed trade messages. It is left out when the number is unknown.
* `data` is always empty. The gap message can arrive after newer trades. A server restart sends no gap message.

Messages carry no sequence number. Use `coin` and `tid` to identify each trade, and ignore any you have already seen.

#### Subscribe

{% tabs %}
{% tab title="JavaScript" %}
{% code expandable="true" %}

```
const sub = client.newSubscription('trades:BTC');

sub.on('publication', (ctx) => {
  for (const trade of ctx.data.data) {
    console.log(trade.tid, trade.side, trade.px, trade.sz);
  }
});

sub.subscribe();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```
class TradesHandler(SubscriptionEventHandler):
    async def on_publication(self, ctx):
        for trade in ctx.pub.data["data"]:
            print(trade["tid"], trade["side"], trade["px"], trade["sz"])


sub = client.new_subscription("trades:BTC", events=TradesHandler())
await sub.subscribe()
```

{% endcode %}
{% endtab %}
{% endtabs %}

### **Trades and Fills**

<table data-search="false"><thead><tr><th width="182.6875">Topic</th><th width="292.9114990234375">Trades (trades:&#x3C;coin>)</th><th>Fills (rooms:fills)</th></tr></thead><tbody><tr><td>Scope</td><td>One market per subscription</td><td>All fills, narrowed with a tag filter in <code>tf</code></td></tr><tr><td>Counterparties</td><td>Both, in <code>users</code> as <code>[buyer, seller]</code></td><td>The account of the fill, in <code>address</code></td></tr><tr><td>Fees and closed PnL</td><td>Not included</td><td>Included</td></tr><tr><td>Replay</td><td>No</td><td>Yes, short window</td></tr><tr><td>Deduplication</td><td><code>coin</code> and <code>tid</code></td><td><code>id</code></td></tr><tr><td>Added metadata</td><td>No <code>id</code> or <code>blockNumber</code></td><td><code>id</code>, <code>blockNumber</code>, and timestamps</td></tr></tbody></table>

`tid` is Hyperliquid’s trade ID. Every trade is made of two fills, one buy and one sell, and both fills carry the same `tid` as the trade. To match a trade on `trades:<coin>` with its two fills, join on `coin` and `tid`.

Use **Trades** to follow all executions of a market. Use [**Fills**](https://docs.hypertracker.io/websocket-streams/stream-reference/fills.md) to follow the executions of specific accounts, with fees and PnL.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hypertracker.io/order-book-data/market-streams.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
