跳到主要内容

行情(Ticker)

通道名

spot:ticker:{symbol}

鉴权:不需要(公开通道)。

描述

订阅时推送滚动 24 小时统计快照,此后每次成交均会推送更新快照。每次推送均为完整替换 — 客户端无需进行增量计算。

订阅

发送:

{ "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
}
}
字段类型描述
symbolstring交易对标识符。
last_pricestring (decimal)最近一笔成交价格。
open_pricestring (decimal)24 小时窗口内(open_time)第一笔成交价格。
highstring (decimal)24 小时窗口内最高成交价格。
lowstring (decimal)24 小时窗口内最低成交价格。
volumestring (decimal)24 小时窗口内基础资产总成交量(DF)。
quote_volumestring (decimal)24 小时窗口内计价资产总成交量(USDT)。
trade_countinteger24 小时窗口内成交笔数。
open_timeinteger24 小时窗口开始时间 — Unix (约为 now − 86400)。
close_timeinteger24 小时窗口结束时间 — Unix (约为 now)。
tsinteger本次快照的计算时间 — Unix

所有时间戳均为 Unix 。24 小时窗口为 [now − 86400, now](滚动,非日历对齐)。

推送频率

spot_ticker 推送仅在每次成交时发出 — 撮合引擎记录到撮合后立即推送。不存在定期推送:后台任务确实会每隔 60 秒在数据库中重算 24 小时统计数据(处理滑出窗口的成交),但不会发出 WebSocket 帧。在无成交的静默期内不会收到任何推送 — 如需在不等待下一笔成交的情况下获取已老化的窗口统计数据,请轮询 GET /spot/ticker/24hr

由于每次推送均为完整快照,漏掉的推送会被下一次推送无损覆盖。无需重新同步逻辑。

代码示例

const ws = new WebSocket('wss://api.prex.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}`);
}