行情(Ticker)
通道名
spot:ticker:{symbol}
鉴权:不需要(公开通道)。
描述
订阅时推送滚动 24 小时统计快照,此后每次成交以及每隔 60 秒的定期重算均会推送更新快照。每次推送均为完整替换 — 客户端无需进行增量计算。
订阅
发送:
{ "type": "subscribe", "channel": "spot:ticker:DFUSDT" }
响应:
{ "type": "subscribed", "channel": "spot:ticker:DFUSDT" }
确认后立即推送一条 spot_ticker 快照(格式与后续实时推送完全相同)。
推送格式
spot_ticker
订阅时的初始快照和后续所有实时更新均使用此类型。两种情况下格式完全一致 — 不存在单独的 spot_ticker_snapshot 类型。
{
"type": "spot_ticker",
"channel": "spot:ticker:DFUSDT",
"data": {
"symbol": "DFUSDT",
"last_price": "0.5000",
"open_price": "0.4800",
"high": "0.5100",
"low": "0.4700",
"volume": "10000",
"quote_volume": "5000",
"trade_count": 234,
"open_time": 1778313600,
"close_time": 1778400000,
"ts": 1778400000
}
}
| 字段 | 类型 | 描述 |
|---|---|---|
symbol | string | 交易对标识符。 |
last_price | string (decimal) | 最近一笔成交价格。 |
open_price | string (decimal) | 24 小时窗口内(open_time)第一笔成交价格。 |
high | string (decimal) | 24 小时窗口内最高成交价格。 |
low | string (decimal) | 24 小时窗口内最低成交价格。 |
volume | string (decimal) | 24 小时窗口内基础资产总成交量(DF)。 |
quote_volume | string (decimal) | 24 小时窗口内计价资产总成交量(USDT)。 |
trade_count | integer | 24 小时窗口内成交笔数。 |
open_time | integer | 24 小时窗口开始时间 — Unix 秒(约为 now − 86400)。 |
close_time | integer | 24 小时窗口结束时间 — Unix 秒(约为 now)。 |
ts | integer | 本次快照的计算时间 — Unix 秒。 |
所有时间戳均为 Unix 秒。24 小时窗口为 [now − 86400, now](滚动,非日历对齐)。
推送频率
以下情况会触发 spot_ticker 推送:
- 每次成交时 — 撮合引擎记录到撮合后立即推送。
- 每隔 60 秒 — 后台重算任务刷新 24 小时统计数据,即使无成交也会推送(确保
open_time/close_time正确老化)。
由于每次推送均为完整快照,漏掉的推送会被下一次推送无损覆盖。无需重新同步逻辑。
代码示例
const ws = new WebSocket('wss://api-sepolia.p99.world/ws');
ws.onopen = () => {
ws.send(JSON.stringify({ type: 'subscribe', channel: 'spot:ticker:DFUSDT' }));
setInterval(() => ws.send(JSON.stringify({ type: 'ping' })), 30000);
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'spot_ticker') {
const d = msg.data;
// Update your UI header stats
renderTicker({
lastPrice: d.last_price,
change24h: ((parseFloat(d.last_price) - parseFloat(d.open_price)) / parseFloat(d.open_price) * 100).toFixed(2) + '%',
high24h: d.high,
low24h: d.low,
volume24h: d.volume,
});
}
};
function renderTicker(stats) {
console.log(`Last: ${stats.lastPrice} Change: ${stats.change24h} High: ${stats.high24h} Low: ${stats.low24h} Vol: ${stats.volume24h}`);
}