Hyperliquid's
trades WebSocket subscription publishes each executed trade for a coin. This example uses the
@siebly/hyperliquid-api SDK (version 1.0.5, Node.js 22 or newer) to subscribe to three coins on one connection and total the buy and sell notional for 30 seconds. It is a compact starting point for a trade flow panel.
npm install @siebly/hyperliquid-api
Scriptimport { DefaultLogger, WebsocketClient } from '@siebly/hyperliquid-api';
const ws = new WebsocketClient({ connectionTimeout: 5000 }, { ...DefaultLogger, trace: () => {}, info: () => {} });
const coins = ['BTC', 'ETH', 'HYPE'];
const totals = Object.fromEntries(coins.map((coin) => [coin, { count: 0, buyUsd: 0, sellUsd: 0 }]));
ws.on('message', ({ channel, data }) => {
if (channel !== 'trades') return;
for (const trade of data) {
const bucket = totals[trade.coin];
const notional = Number(trade.px) * Number(trade.sz);
bucket.count += 1;
if (trade.side === 'B') bucket.buyUsd += notional;
else bucket.sellUsd += notional;
}
});
await ws.subscribe(coins.map((coin) => ({ type: 'trades', coin })));
setTimeout(() => {
ws.closeAll();
for (const [coin, flow] of Object.entries(totals)) {
const total = flow.buyUsd + flow.sellUsd;
const buyShare = total > 0 ? (flow.buyUsd / total) * 100 : 0;
console.log(`${coin.padEnd(5)} ${String(flow.count).padStart(4)} trades buy $${flow.buyUsd.toFixed(0)} sell $${flow.sellUsd.toFixed(0)} buy share ${buyShare.toFixed(1)}%`);
}
}, 30_000);
Run it with
node trades.mjs in a project with
"type": "module" in
package.json.
Example output (30 seconds of live trades, so the figures will differ on every run):
BTC 569 trades buy $917479 sell $4808030 buy share 16.0%
ETH 153 trades buy $787798 sell $28268 buy share 96.5%
HYPE 455 trades buy $147388 sell $415335 buy share 26.2%
How it works- ws.subscribe() accepts an array, so one call opens the trades subscription for BTC, ETH and HYPE on a single connection.
- Each message is an array of trades with coin, px, sz, side, time, tid and users. The handler multiplies px by sz for the notional in USD.
- side is B for the buy side and A for the sell side.
- A setTimeout closes the connection after 30 seconds and prints the totals. A long running service keeps the connection open and reports on a rolling window.
- connectionTimeout: 5000 gives the first connection five seconds, and the logger override silences connection chatter.
Reading the totalsA buy share close to 50 percent means balanced flow, and a reading far from 50 percent means one side led during the window. In the example output, ETH printed 153 trades with a buy share of 96.5 percent over its 30 seconds, while BTC printed 569 trades at 16.0 percent. A rolling 5 minute window smooths such bursts.
Where to read moreFrequently raised questionsDoes the trades stream need an API key?Trades are public market data, so an API key is unnecessary.
How many coins can one connection follow?Hyperliquid allows up to 1000 subscriptions per IP address, so a list of coins fits on one connection.
What does users hold?A pair of addresses, the buyer and the seller of the trade.
How does the client behave after a disconnect?WebsocketClient reconnects with backoff, resubscribes and emits
reconnected.