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

# 市场统计

> 汇总最优买卖价、标记价格、未平仓量、资金费率及滚动 24 小时成交量快照

订阅单个或全部交易品种的汇总市场统计。

`market_stats` 数据流将最新 BBO、标记价格、未平仓量、资金费率，以及滚动 24 小时成交数量和名义成交金额合并为一条消息。

订阅时，MDS 先发送 `subscribed` 确认，随后在有数据时发送快照。指定品种的订阅每秒刷新一次；通配符订阅（`symbols: ["*"]`）目前每 10 秒刷新一次。每条消息都是所请求品种的完整快照，因此客户端可替换上一份快照，而无需应用增量。

## 使用方式

连接公开 WebSocket：

* URL：`wss://mds.qfex.com`。
* 身份认证：不需要。

然后发送订阅消息：

```json theme={null}
{
  "type": "subscribe",
  "channels": ["market_stats"],
  "symbols": ["AAPL-USD"]
}
```

也可使用通配符订阅所有品种：

```json theme={null}
{ "type": "subscribe", "channels": ["market_stats"], "symbols": ["*"] }
```

当请求只包含 `market_stats` 一个频道时，省略 `symbols` 或发送空数组也会订阅所有品种：

```json theme={null}
{ "type": "subscribe", "channels": ["market_stats"] }
```

请求多个指定品种时，MDS 为每个品种单独发送快照。通配符订阅收到包含所有可用品种的一份快照，顶层 `symbol` 为 `"*"`。

## 示例代码

```python python theme={null}
# Python (websocket-client)
import json
import websocket

def on_open(ws):
    sub = {
        "type": "subscribe",
        "channels": ["market_stats"],
        "symbols": ["*"]
    }
    ws.send(json.dumps(sub))

def on_message(ws, message):
    print("Market stats:", message)

if __name__ == "__main__":
    ws = websocket.WebSocketApp(
        "wss://mds.qfex.com",
        on_open=on_open,
        on_message=on_message
    )
    ws.run_forever()
```

```javascript node theme={null}
// Node.js (ws)
import WebSocket from "ws";

const ws = new WebSocket("wss://mds.qfex.com");

ws.on("open", () => {
  const sub = {
    type: "subscribe",
    channels: ["market_stats"],
    symbols: ["*"],
  };
  ws.send(JSON.stringify(sub));
});

ws.on("message", (msg) => {
  console.log("Market stats:", msg.toString());
});
```

## 响应示例

以下通配符快照示例包含一个品种：

```json theme={null}
{
  "type": "market_stats",
  "sequence": 30272540,
  "time": "2025-09-04T09:26:40Z",
  "symbol": "*",
  "bbos": [
    {
      "time": "2025-09-04T09:26:39.545268322Z",
      "symbol": "AAPL-USD",
      "bid": [["10101.1", "0.4505414"]],
      "ask": [["10102.55", "0.57753524"]]
    }
  ],
  "mark_prices": [
    {
      "time": "2025-09-04T09:26:39.545268322Z",
      "symbol": "AAPL-USD",
      "price": "10102"
    }
  ],
  "open_interests": [
    {
      "time": "2025-09-04T09:26:39.545268322Z",
      "symbol": "AAPL-USD",
      "open_interest": "12.5"
    }
  ],
  "funding_rates": [
    {
      "time": "2025-09-04T09:26:39.545268322Z",
      "symbol": "AAPL-USD",
      "funding_rate": "0.00001",
      "annualised_funding_rate": "0.0876",
      "time_remaining": 200
    }
  ],
  "daily_volumes": [
    {
      "symbol": "AAPL-USD",
      "volume": "12.5"
    }
  ],
  "daily_volumes_notional": [
    {
      "symbol": "AAPL-USD",
      "volume": "126275"
    }
  ]
}
```

## 响应字段

| 字段 | 说明 |
| - | - |
| `type` | 始终为 `"market_stats"`。 |
| `sequence` | 与同一连接上其他行情消息共享的序列号。 |
| `time` | 构建快照时的 UTC 时间戳；各组成部分仍保留各自最新的时间戳。 |
| `symbol` | 所请求的品种；全部品种快照使用 `"*"`。 |
| `bbos` | 各可用品种最新的[最优买卖价](https://docs.qfex.com/cn/websocket/channels/mds/bbo.md)。每个报价是由十进制字符串组成的 `[price, quantity]` 数组。 |
| `mark_prices` | 各可用品种最新的[标记价格](https://docs.qfex.com/cn/websocket/channels/mds/mark_price.md)。 |
| `open_interests` | 各可用品种最新的[未平仓量](https://docs.qfex.com/cn/websocket/channels/mds/open_interest.md)。 |
| `funding_rates` | 各可用品种最新的[资金费率](https://docs.qfex.com/cn/websocket/channels/mds/funding_rate.md)，包括 `funding_rate`、`annualised_funding_rate` 及以秒表示的 `time_remaining`。费率是十进制字符串，不是百分比字符串。 |
| `daily_volumes` | 滚动 24 小时成交数量；每项包含 `symbol` 和十进制字符串 `volume`。 |
| `daily_volumes_notional` | 滚动 24 小时美元名义成交金额；每项包含 `symbol` 和十进制字符串 `volume`。 |

六个组成字段始终为数组，缺少最新值时可以为空。请按 `symbol` 关联各项；不同数组可能包含不同品种集合。价格、数量、未平仓量、成交量和资金费率均序列化为十进制字符串。

### 取消订阅（可选）

```json theme={null}
{ "type": "unsubscribe", "channels": ["market_stats"], "symbols": ["*"] }
```

## 注意事项

* `symbols` 支持单个品种、多个品种或通配符 `"*"`。
* `daily_volumes` 和 `daily_volumes_notional` 的名称为兼容性而保留；两者均衡量过去 24 小时，没有固定的每日重置点。随着较早成交移出窗口，数值可能下降。
* 只有至少一个组成部分有数据时才发送快照。组成部分的时间戳可能早于快照时间戳。
* `market_stats` 忽略 `intervals` 和 `sig_figs`，始终使用默认订阅过滤器。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.