forked from binance-exchange/node-binance-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe-to-all.js
31 lines (26 loc) · 1.11 KB
/
subscribe-to-all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const Binance = require('node-binance-api');
const binance = new Binance().options('options.json');
global.ticker = {};
// Get all symbols
binance.prevDay(false, (error, prevDay) => {
if ( error ) return console.log(error.body);
let markets = [];
for ( let obj of prevDay ) {
let symbol = obj.symbol;
// Filter BTC & USDT markets only (example)
if ( !symbol.endsWith('BTC') && !symbol.endsWith('USDT') ) continue;
console.log(`${symbol} price: ${obj.lastPrice} volume: ${obj.volume} change: ${obj.priceChangePercent}%`);
global.ticker[symbol] = obj.lastPrice;
markets.push(symbol);
}
// Subscribe to trades endpoint for all markets
binance.websockets.trades(markets, (trades) => {
let { e: eventType, E: eventTime, s: symbol, p: price, q: quantity, m: maker, a: tradeId } = trades;
console.log(`${symbol} price: ${price}`);
global.ticker[symbol] = price;
});
// You can use global.ticker anywhere in your program now
setInterval(() => {
console.log("*** Price of BTC: " + global.ticker.BTCUSDT);
}, 3000);
});