Real-time market data
All streaming subscriptions return a callback-based handle. The pattern is the same across
data types: subscribeX(params, callback) to start, unsubscribeX(params, callback) to stop.
The socket connects automatically on the first subscription and re-subscribes on reconnect.
Trades
const onTrades = ({coinraySymbol, trades}) => {
for (const t of trades) {
console.log(coinraySymbol, t.price, t.quantity, t.time);
}
};
client.subscribeTrades({coinraySymbol: "BINA_USDT_BTC"}, onTrades);
// later
client.unsubscribeTrades({coinraySymbol: "BINA_USDT_BTC"}, onTrades);
Order book
You receive a snapshot first, then incremental updates. The payload carries bids and asks.
const onBook = ({coinraySymbol, type, orderBook}) => {
// type is "orderBook:snapshot" or "orderBook:update"
console.log(coinraySymbol, type, orderBook.bids[0], orderBook.asks[0]);
};
client.subscribeOrderBook({coinraySymbol: "BINA_USDT_BTC"}, onBook);
client.unsubscribeOrderBook({coinraySymbol: "BINA_USDT_BTC"}, onBook);
Tickers
Tickers are subscribed in batches by Altrady Data symbol. Pass reset: true to replace the current
set instead of adding to it.
const onTicker = (ticker) => console.log(ticker.coinraySymbol, ticker.lastPrice);
client.subscribeTickers(["BINA_USDT_BTC", "BINA_USDT_ETH"], false, onTicker);
client.unsubscribeTickers(["BINA_USDT_ETH"], onTicker);
client.unsubscribeAllTickers();
Candles (live)
Live candles are built from the trade stream. Provide a resolution (in minutes, or with an
S suffix for seconds — e.g. "1", "60", "1D").
const onCandle = ({coinraySymbol, candle}) => {
console.log(coinraySymbol, candle.time, candle.open, candle.close);
};
client.subscribeCandles(
{coinraySymbol: "BINA_USDT_BTC", resolution: "60"},
onCandle,
);
client.unsubscribeCandles(
{coinraySymbol: "BINA_USDT_BTC", resolution: "60"},
onCandle,
);
Per-market helper: CurrentMarket
When you're focused on a single market (e.g. a trading screen), CurrentMarket wraps the
subscriptions for one symbol and tracks the latest ticker, trades, and order book for you.
import {AltradyDataCache, CurrentMarket} from "@altrady/data";
const cache = new AltradyDataCache(token);
await cache.initialize();
const current = new CurrentMarket(cache);
current.setMarket(cache.getMarket("BINA_USDT_BTC"));
current.subscribeTrades((trades) => console.log("trades", trades));
current.subscribeOrderBook((book) => console.log("book", book));
current.subscribeMarketUpdates((ticker) => console.log("ticker", ticker));