UNPKG

@proton/ccxt

Version:

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading library with support for 130+ exchanges

1,104 lines (1,102 loc) 83.7 kB
// ---------------------------------------------------------------------------- // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN: // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code // EDIT THE CORRESPONDENT .ts FILE INSTEAD // ---------------------------------------------------------------------------- import binanceRest from '../binance.js'; import { Precise } from '../base/Precise.js'; import { ExchangeError, ArgumentsRequired } from '../base/errors.js'; import { ArrayCache, ArrayCacheByTimestamp, ArrayCacheBySymbolById } from '../base/ws/Cache.js'; // ----------------------------------------------------------------------------- export default class binance extends binanceRest { describe() { return this.deepExtend(super.describe(), { 'has': { 'ws': true, 'watchBalance': true, 'watchMyTrades': true, 'watchOHLCV': true, 'watchOrderBook': true, 'watchOrders': true, 'watchTicker': true, 'watchTickers': true, 'watchTrades': true, }, 'urls': { 'test': { 'ws': { 'spot': 'wss://testnet.binance.vision/ws', 'margin': 'wss://testnet.binance.vision/ws', 'future': 'wss://stream.binancefuture.com/ws', 'delivery': 'wss://dstream.binancefuture.com/ws', }, }, 'api': { 'ws': { 'spot': 'wss://stream.binance.com:9443/ws', 'margin': 'wss://stream.binance.com:9443/ws', 'future': 'wss://fstream.binance.com/ws', 'delivery': 'wss://dstream.binance.com/ws', }, }, }, 'streaming': { 'keepAlive': 180000, }, 'options': { 'streamLimits': { 'spot': 50, 'margin': 50, 'future': 50, 'delivery': 50, // max 200 }, 'streamBySubscriptionsHash': {}, 'streamIndex': -1, // get updates every 1000ms or 100ms // or every 0ms in real-time for futures 'watchOrderBookRate': 100, 'tradesLimit': 1000, 'ordersLimit': 1000, 'OHLCVLimit': 1000, 'requestId': {}, 'watchOrderBookLimit': 1000, 'watchTrades': { 'name': 'trade', // 'trade' or 'aggTrade' }, 'watchTicker': { 'name': 'ticker', // ticker = 1000ms L1+OHLCV, bookTicker = real-time L1 }, 'watchTickers': { 'name': 'ticker', // ticker or miniTicker or bookTicker }, 'watchOHLCV': { 'name': 'kline', // or indexPriceKline or markPriceKline (coin-m futures) }, 'watchBalance': { 'fetchBalanceSnapshot': false, 'awaitBalanceSnapshot': true, // whether to wait for the balance snapshot before providing updates }, 'wallet': 'wb', 'listenKeyRefreshRate': 1200000, 'ws': { 'cost': 5, }, }, }); } requestId(url) { const options = this.safeValue(this.options, 'requestId', {}); const previousValue = this.safeInteger(options, url, 0); const newValue = this.sum(previousValue, 1); this.options['requestId'][url] = newValue; return newValue; } stream(type, subscriptionHash) { const streamBySubscriptionsHash = this.safeValue(this.options, 'streamBySubscriptionsHash', {}); let stream = this.safeString(streamBySubscriptionsHash, subscriptionHash); if (stream === undefined) { let streamIndex = this.safeInteger(this.options, 'streamIndex', -1); const streamLimits = this.safeValue(this.options, 'streamLimits'); const streamLimit = this.safeInteger(streamLimits, type); streamIndex = streamIndex + 1; const normalizedIndex = streamIndex % streamLimit; this.options['streamIndex'] = streamIndex; stream = this.numberToString(normalizedIndex); this.options['streamBySubscriptionsHash'][subscriptionHash] = stream; } return stream; } async watchOrderBook(symbol, limit = undefined, params = {}) { /** * @method * @name binance#watchOrderBook * @description watches information on open orders with bid (buy) and ask (sell) prices, volumes and other data * @param {string} symbol unified symbol of the market to fetch the order book for * @param {int|undefined} limit the maximum amount of order book entries to return * @param {object} params extra parameters specific to the binance api endpoint * @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols */ // // todo add support for <levels>-snapshots (depth) // https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#partial-book-depth-streams // <symbol>@depth<levels>@100ms or <symbol>@depth<levels> (1000ms) // valid <levels> are 5, 10, or 20 // // default 100, max 1000, valid limits 5, 10, 20, 50, 100, 500, 1000 if (limit !== undefined) { if ((limit !== 5) && (limit !== 10) && (limit !== 20) && (limit !== 50) && (limit !== 100) && (limit !== 500) && (limit !== 1000)) { throw new ExchangeError(this.id + ' watchOrderBook limit argument must be undefined, 5, 10, 20, 50, 100, 500 or 1000'); } } // await this.loadMarkets(); const market = this.market(symbol); let type = market['type']; if (market['contract']) { type = market['linear'] ? 'future' : 'delivery'; } // // notice the differences between trading futures and spot trading // the algorithms use different urls in step 1 // delta caching and merging also differs in steps 4, 5, 6 // // spot/margin // https://binance-docs.github.io/apidocs/spot/en/#how-to-manage-a-local-order-book-correctly // // 1. Open a stream to wss://stream.binance.com:9443/ws/bnbbtc@depth. // 2. Buffer the events you receive from the stream. // 3. Get a depth snapshot from https://www.binance.com/api/v1/depth?symbol=BNBBTC&limit=1000 . // 4. Drop any event where u is <= lastUpdateId in the snapshot. // 5. The first processed event should have U <= lastUpdateId+1 AND u >= lastUpdateId+1. // 6. While listening to the stream, each new event's U should be equal to the previous event's u+1. // 7. The data in each event is the absolute quantity for a price level. // 8. If the quantity is 0, remove the price level. // 9. Receiving an event that removes a price level that is not in your local order book can happen and is normal. // // futures // https://binance-docs.github.io/apidocs/futures/en/#how-to-manage-a-local-order-book-correctly // // 1. Open a stream to wss://fstream.binance.com/stream?streams=btcusdt@depth. // 2. Buffer the events you receive from the stream. For same price, latest received update covers the previous one. // 3. Get a depth snapshot from https://fapi.binance.com/fapi/v1/depth?symbol=BTCUSDT&limit=1000 . // 4. Drop any event where u is < lastUpdateId in the snapshot. // 5. The first processed event should have U <= lastUpdateId AND u >= lastUpdateId // 6. While listening to the stream, each new event's pu should be equal to the previous event's u, otherwise initialize the process from step 3. // 7. The data in each event is the absolute quantity for a price level. // 8. If the quantity is 0, remove the price level. // 9. Receiving an event that removes a price level that is not in your local order book can happen and is normal. // const name = 'depth'; const messageHash = market['lowercaseId'] + '@' + name; const url = this.urls['api']['ws'][type] + '/' + this.stream(type, messageHash); const requestId = this.requestId(url); const watchOrderBookRate = this.safeString(this.options, 'watchOrderBookRate', '100'); const request = { 'method': 'SUBSCRIBE', 'params': [ messageHash + '@' + watchOrderBookRate + 'ms', ], 'id': requestId, }; const subscription = { 'id': requestId.toString(), 'messageHash': messageHash, 'name': name, 'symbol': market['symbol'], 'method': this.handleOrderBookSubscription, 'limit': limit, 'type': type, 'params': params, }; const message = this.extend(request, params); // 1. Open a stream to wss://stream.binance.com:9443/ws/bnbbtc@depth. const orderbook = await this.watch(url, messageHash, message, messageHash, subscription); return orderbook.limit(); } async fetchOrderBookSnapshot(client, message, subscription) { const messageHash = this.safeString(subscription, 'messageHash'); const symbol = this.safeString(subscription, 'symbol'); try { const defaultLimit = this.safeInteger(this.options, 'watchOrderBookLimit', 1000); const type = this.safeValue(subscription, 'type'); const limit = this.safeInteger(subscription, 'limit', defaultLimit); const params = this.safeValue(subscription, 'params'); // 3. Get a depth snapshot from https://www.binance.com/api/v1/depth?symbol=BNBBTC&limit=1000 . // todo: this is a synch blocking call in ccxt.php - make it async // default 100, max 1000, valid limits 5, 10, 20, 50, 100, 500, 1000 const snapshot = await this.fetchOrderBook(symbol, limit, params); const orderbook = this.safeValue(this.orderbooks, symbol); if (orderbook === undefined) { // if the orderbook is dropped before the snapshot is received return; } orderbook.reset(snapshot); // unroll the accumulated deltas const messages = orderbook.cache; for (let i = 0; i < messages.length; i++) { const message = messages[i]; const U = this.safeInteger(message, 'U'); const u = this.safeInteger(message, 'u'); const pu = this.safeInteger(message, 'pu'); if (type === 'future') { // 4. Drop any event where u is < lastUpdateId in the snapshot if (u < orderbook['nonce']) { continue; } // 5. The first processed event should have U <= lastUpdateId AND u >= lastUpdateId if ((U <= orderbook['nonce']) && (u >= orderbook['nonce']) || (pu === orderbook['nonce'])) { this.handleOrderBookMessage(client, message, orderbook); } } else { // 4. Drop any event where u is <= lastUpdateId in the snapshot if (u <= orderbook['nonce']) { continue; } // 5. The first processed event should have U <= lastUpdateId+1 AND u >= lastUpdateId+1 if (((U - 1) <= orderbook['nonce']) && ((u - 1) >= orderbook['nonce'])) { this.handleOrderBookMessage(client, message, orderbook); } } } this.orderbooks[symbol] = orderbook; client.resolve(orderbook, messageHash); } catch (e) { delete client.subscriptions[messageHash]; client.reject(e, messageHash); } } handleDelta(bookside, delta) { const price = this.safeFloat(delta, 0); const amount = this.safeFloat(delta, 1); bookside.store(price, amount); } handleDeltas(bookside, deltas) { for (let i = 0; i < deltas.length; i++) { this.handleDelta(bookside, deltas[i]); } } handleOrderBookMessage(client, message, orderbook) { const u = this.safeInteger(message, 'u'); this.handleDeltas(orderbook['asks'], this.safeValue(message, 'a', [])); this.handleDeltas(orderbook['bids'], this.safeValue(message, 'b', [])); orderbook['nonce'] = u; const timestamp = this.safeInteger(message, 'E'); orderbook['timestamp'] = timestamp; orderbook['datetime'] = this.iso8601(timestamp); return orderbook; } handleOrderBook(client, message) { // // initial snapshot is fetched with ccxt's fetchOrderBook // the feed does not include a snapshot, just the deltas // // { // "e": "depthUpdate", // Event type // "E": 1577554482280, // Event time // "s": "BNBBTC", // Symbol // "U": 157, // First update ID in event // "u": 160, // Final update ID in event // "b": [ // bids // [ "0.0024", "10" ], // price, size // ], // "a": [ // asks // [ "0.0026", "100" ], // price, size // ] // } // const isTestnetSpot = client.url.indexOf('testnet') > 0; const isSpotMainNet = client.url.indexOf('/stream.binance.') > 0; const isSpot = isTestnetSpot || isSpotMainNet; const marketType = isSpot ? 'spot' : 'contract'; const marketId = this.safeString(message, 's'); const market = this.safeMarket(marketId, undefined, undefined, marketType); const symbol = market['symbol']; const name = 'depth'; const messageHash = market['lowercaseId'] + '@' + name; const orderbook = this.safeValue(this.orderbooks, symbol); if (orderbook === undefined) { // // https://github.com/ccxt/ccxt/issues/6672 // // Sometimes Binance sends the first delta before the subscription // confirmation arrives. At that point the orderbook is not // initialized yet and the snapshot has not been requested yet // therefore it is safe to drop these premature messages. // return; } const nonce = this.safeInteger(orderbook, 'nonce'); if (nonce === undefined) { // 2. Buffer the events you receive from the stream. orderbook.cache.push(message); } else { try { const U = this.safeInteger(message, 'U'); const u = this.safeInteger(message, 'u'); const pu = this.safeInteger(message, 'pu'); if (pu === undefined) { // spot // 4. Drop any event where u is <= lastUpdateId in the snapshot if (u > orderbook['nonce']) { const timestamp = this.safeInteger(orderbook, 'timestamp'); let conditional = undefined; if (timestamp === undefined) { // 5. The first processed event should have U <= lastUpdateId+1 AND u >= lastUpdateId+1 conditional = ((U - 1) <= orderbook['nonce']) && ((u - 1) >= orderbook['nonce']); } else { // 6. While listening to the stream, each new event's U should be equal to the previous event's u+1. conditional = ((U - 1) === orderbook['nonce']); } if (conditional) { this.handleOrderBookMessage(client, message, orderbook); if (nonce < orderbook['nonce']) { client.resolve(orderbook, messageHash); } } else { // todo: client.reject from handleOrderBookMessage properly throw new ExchangeError(this.id + ' handleOrderBook received an out-of-order nonce'); } } } else { // future // 4. Drop any event where u is < lastUpdateId in the snapshot if (u >= orderbook['nonce']) { // 5. The first processed event should have U <= lastUpdateId AND u >= lastUpdateId // 6. While listening to the stream, each new event's pu should be equal to the previous event's u, otherwise initialize the process from step 3 if ((U <= orderbook['nonce']) || (pu === orderbook['nonce'])) { this.handleOrderBookMessage(client, message, orderbook); if (nonce <= orderbook['nonce']) { client.resolve(orderbook, messageHash); } } else { // todo: client.reject from handleOrderBookMessage properly throw new ExchangeError(this.id + ' handleOrderBook received an out-of-order nonce'); } } } } catch (e) { delete this.orderbooks[symbol]; delete client.subscriptions[messageHash]; client.reject(e, messageHash); } } } handleOrderBookSubscription(client, message, subscription) { const defaultLimit = this.safeInteger(this.options, 'watchOrderBookLimit', 1000); const symbol = this.safeString(subscription, 'symbol'); const limit = this.safeInteger(subscription, 'limit', defaultLimit); if (symbol in this.orderbooks) { delete this.orderbooks[symbol]; } this.orderbooks[symbol] = this.orderBook({}, limit); // fetch the snapshot in a separate async call this.spawn(this.fetchOrderBookSnapshot, client, message, subscription); } handleSubscriptionStatus(client, message) { // // { // "result": null, // "id": 1574649734450 // } // const id = this.safeString(message, 'id'); const subscriptionsById = this.indexBy(client.subscriptions, 'id'); const subscription = this.safeValue(subscriptionsById, id, {}); const method = this.safeValue(subscription, 'method'); if (method !== undefined) { method.call(this, client, message, subscription); } return message; } async watchTrades(symbol, since = undefined, limit = undefined, params = {}) { /** * @method * @name binance#watchTrades * @description get the list of most recent trades for a particular symbol * @param {string} symbol unified symbol of the market to fetch trades for * @param {int|undefined} since timestamp in ms of the earliest trade to fetch * @param {int|undefined} limit the maximum amount of trades to fetch * @param {object} params extra parameters specific to the binance api endpoint * @returns {[object]} a list of [trade structures]{@link https://docs.ccxt.com/en/latest/manual.html?#public-trades} */ await this.loadMarkets(); const market = this.market(symbol); const options = this.safeValue(this.options, 'watchTrades', {}); const name = this.safeString(options, 'name', 'trade'); const messageHash = market['lowercaseId'] + '@' + name; let type = market['type']; if (market['contract']) { type = market['linear'] ? 'future' : 'delivery'; } const query = this.omit(params, 'type'); const url = this.urls['api']['ws'][type] + '/' + this.stream(type, messageHash); const requestId = this.requestId(url); const request = { 'method': 'SUBSCRIBE', 'params': [ messageHash, ], 'id': requestId, }; const subscribe = { 'id': requestId, }; const trades = await this.watch(url, messageHash, this.extend(request, query), messageHash, subscribe); if (this.newUpdates) { limit = trades.getLimit(market['symbol'], limit); } return this.filterBySinceLimit(trades, since, limit, 'timestamp', true); } parseTrade(trade, market = undefined) { // // public watchTrades // // { // e: 'trade', // event type // E: 1579481530911, // event time // s: 'ETHBTC', // symbol // t: 158410082, // trade id // p: '0.01914100', // price // q: '0.00700000', // quantity // b: 586187049, // buyer order id // a: 586186710, // seller order id // T: 1579481530910, // trade time // m: false, // is the buyer the market maker // M: true // binance docs say it should be ignored // } // // { // "e": "aggTrade", // Event type // "E": 123456789, // Event time // "s": "BNBBTC", // Symbol // "a": 12345, // Aggregate trade ID // "p": "0.001", // Price // "q": "100", // Quantity // "f": 100, // First trade ID // "l": 105, // Last trade ID // "T": 123456785, // Trade time // "m": true, // Is the buyer the market maker? // "M": true // Ignore // } // // private watchMyTrades spot // // { // e: 'executionReport', // E: 1611063861489, // s: 'BNBUSDT', // c: 'm4M6AD5MF3b1ERe65l4SPq', // S: 'BUY', // o: 'MARKET', // f: 'GTC', // q: '2.00000000', // p: '0.00000000', // P: '0.00000000', // F: '0.00000000', // g: -1, // C: '', // x: 'TRADE', // X: 'PARTIALLY_FILLED', // r: 'NONE', // i: 1296882607, // l: '0.33200000', // z: '0.33200000', // L: '46.86600000', // n: '0.00033200', // N: 'BNB', // T: 1611063861488, // t: 109747654, // I: 2696953381, // w: false, // m: false, // M: true, // O: 1611063861488, // Z: '15.55951200', // Y: '15.55951200', // Q: '0.00000000' // } // // private watchMyTrades future/delivery // // { // s: 'BTCUSDT', // c: 'pb2jD6ZQHpfzSdUac8VqMK', // S: 'SELL', // o: 'MARKET', // f: 'GTC', // q: '0.001', // p: '0', // ap: '33468.46000', // sp: '0', // x: 'TRADE', // X: 'FILLED', // i: 13351197194, // l: '0.001', // z: '0.001', // L: '33468.46', // n: '0.00027086', // N: 'BNB', // T: 1612095165362, // t: 458032604, // b: '0', // a: '0', // m: false, // R: false, // wt: 'CONTRACT_PRICE', // ot: 'MARKET', // ps: 'BOTH', // cp: false, // rp: '0.00335000', // pP: false, // si: 0, // ss: 0 // } // const executionType = this.safeString(trade, 'x'); const isTradeExecution = (executionType === 'TRADE'); if (!isTradeExecution) { return super.parseTrade(trade, market); } const id = this.safeString2(trade, 't', 'a'); const timestamp = this.safeInteger(trade, 'T'); const price = this.safeString2(trade, 'L', 'p'); let amount = this.safeString(trade, 'q'); if (isTradeExecution) { amount = this.safeString(trade, 'l', amount); } let cost = this.safeString(trade, 'Y'); if (cost === undefined) { if ((price !== undefined) && (amount !== undefined)) { cost = Precise.stringMul(price, amount); } } const marketId = this.safeString(trade, 's'); const marketType = ('ps' in trade) ? 'contract' : 'spot'; const symbol = this.safeSymbol(marketId, undefined, undefined, marketType); let side = this.safeStringLower(trade, 'S'); let takerOrMaker = undefined; const orderId = this.safeString(trade, 'i'); if ('m' in trade) { if (side === undefined) { side = trade['m'] ? 'sell' : 'buy'; // this is reversed intentionally } takerOrMaker = trade['m'] ? 'maker' : 'taker'; } let fee = undefined; const feeCost = this.safeString(trade, 'n'); if (feeCost !== undefined) { const feeCurrencyId = this.safeString(trade, 'N'); const feeCurrencyCode = this.safeCurrencyCode(feeCurrencyId); fee = { 'cost': feeCost, 'currency': feeCurrencyCode, }; } const type = this.safeStringLower(trade, 'o'); return this.safeTrade({ 'info': trade, 'timestamp': timestamp, 'datetime': this.iso8601(timestamp), 'symbol': symbol, 'id': id, 'order': orderId, 'type': type, 'takerOrMaker': takerOrMaker, 'side': side, 'price': price, 'amount': amount, 'cost': cost, 'fee': fee, }); } handleTrade(client, message) { // the trade streams push raw trade information in real-time // each trade has a unique buyer and seller const index = client.url.indexOf('/stream'); const marketType = (index >= 0) ? 'spot' : 'contract'; const marketId = this.safeString(message, 's'); const market = this.safeMarket(marketId, undefined, undefined, marketType); const symbol = market['symbol']; const lowerCaseId = this.safeStringLower(message, 's'); const event = this.safeString(message, 'e'); const messageHash = lowerCaseId + '@' + event; const trade = this.parseTrade(message, market); let tradesArray = this.safeValue(this.trades, symbol); if (tradesArray === undefined) { const limit = this.safeInteger(this.options, 'tradesLimit', 1000); tradesArray = new ArrayCache(limit); } tradesArray.append(trade); this.trades[symbol] = tradesArray; client.resolve(tradesArray, messageHash); } async watchOHLCV(symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) { /** * @method * @name binance#watchOHLCV * @description watches historical candlestick data containing the open, high, low, and close price, and the volume of a market * @param {string} symbol unified symbol of the market to fetch OHLCV data for * @param {string} timeframe the length of time each candle represents * @param {int|undefined} since timestamp in ms of the earliest candle to fetch * @param {int|undefined} limit the maximum amount of candles to fetch * @param {object} params extra parameters specific to the binance api endpoint * @returns {[[int]]} A list of candles ordered as timestamp, open, high, low, close, volume */ await this.loadMarkets(); const market = this.market(symbol); let marketId = market['lowercaseId']; const interval = this.safeString(this.timeframes, timeframe, timeframe); const options = this.safeValue(this.options, 'watchOHLCV', {}); const nameOption = this.safeString(options, 'name', 'kline'); const name = this.safeString(params, 'name', nameOption); if (name === 'indexPriceKline') { // weird behavior for index price kline we can't use the perp suffix marketId = marketId.replace('_perp', ''); } params = this.omit(params, 'name'); const messageHash = marketId + '@' + name + '_' + interval; let type = market['type']; if (market['contract']) { type = market['linear'] ? 'future' : 'delivery'; } const url = this.urls['api']['ws'][type] + '/' + this.stream(type, messageHash); const requestId = this.requestId(url); const request = { 'method': 'SUBSCRIBE', 'params': [ messageHash, ], 'id': requestId, }; const subscribe = { 'id': requestId, }; const ohlcv = await this.watch(url, messageHash, this.extend(request, params), messageHash, subscribe); if (this.newUpdates) { limit = ohlcv.getLimit(symbol, limit); } return this.filterBySinceLimit(ohlcv, since, limit, 0, true); } handleOHLCV(client, message) { // // { // e: 'kline', // E: 1579482921215, // s: 'ETHBTC', // k: { // t: 1579482900000, // T: 1579482959999, // s: 'ETHBTC', // i: '1m', // f: 158411535, // L: 158411550, // o: '0.01913200', // c: '0.01913500', // h: '0.01913700', // l: '0.01913200', // v: '5.08400000', // n: 16, // x: false, // q: '0.09728060', // V: '3.30200000', // Q: '0.06318500', // B: '0' // } // } // let event = this.safeString(message, 'e'); const eventMap = { 'indexPrice_kline': 'indexPriceKline', 'markPrice_kline': 'markPriceKline', }; event = this.safeString(eventMap, event, event); const kline = this.safeValue(message, 'k'); let marketId = this.safeString2(kline, 's', 'ps'); if (event === 'indexPriceKline') { // indexPriceKline doesn't have the _PERP suffix marketId = this.safeString(message, 'ps'); } const lowercaseMarketId = marketId.toLowerCase(); const interval = this.safeString(kline, 'i'); // use a reverse lookup in a static map instead const timeframe = this.findTimeframe(interval); const messageHash = lowercaseMarketId + '@' + event + '_' + interval; const parsed = [ this.safeInteger(kline, 't'), this.safeFloat(kline, 'o'), this.safeFloat(kline, 'h'), this.safeFloat(kline, 'l'), this.safeFloat(kline, 'c'), this.safeFloat(kline, 'v'), ]; const index = client.url.indexOf('/stream'); const marketType = (index >= 0) ? 'spot' : 'contract'; const symbol = this.safeSymbol(marketId, undefined, undefined, marketType); this.ohlcvs[symbol] = this.safeValue(this.ohlcvs, symbol, {}); let stored = this.safeValue(this.ohlcvs[symbol], timeframe); if (stored === undefined) { const limit = this.safeInteger(this.options, 'OHLCVLimit', 1000); stored = new ArrayCacheByTimestamp(limit); this.ohlcvs[symbol][timeframe] = stored; } stored.append(parsed); client.resolve(stored, messageHash); } async watchTicker(symbol, params = {}) { /** * @method * @name binance#watchTicker * @description watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market * @param {string} symbol unified symbol of the market to fetch the ticker for * @param {object} params extra parameters specific to the binance api endpoint * @param {string} params.name stream to use can be ticker or bookTicker * @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure} */ await this.loadMarkets(); const market = this.market(symbol); const marketId = market['lowercaseId']; let type = market['type']; if (market['contract']) { type = market['linear'] ? 'future' : 'delivery'; } const options = this.safeValue(this.options, 'watchTicker', {}); let name = this.safeString(options, 'name', 'ticker'); name = this.safeString(params, 'name', name); params = this.omit(params, 'name'); const messageHash = marketId + '@' + name; const url = this.urls['api']['ws'][type] + '/' + this.stream(type, messageHash); const requestId = this.requestId(url); const request = { 'method': 'SUBSCRIBE', 'params': [ messageHash, ], 'id': requestId, }; const subscribe = { 'id': requestId, }; return await this.watch(url, messageHash, this.extend(request, params), messageHash, subscribe); } async watchTickers(symbols = undefined, params = {}) { /** * @method * @name binance#watchTickers * @description watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list * @param {[string]} symbols unified symbol of the market to fetch the ticker for * @param {object} params extra parameters specific to the binance api endpoint * @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure} */ await this.loadMarkets(); symbols = this.marketSymbols(symbols); const marketIds = this.marketIds(symbols); let market = undefined; let type = undefined; if (symbols !== undefined) { market = this.market(symbols[0]); } [type, params] = this.handleMarketTypeAndParams('watchTickers', market, params); let subType = undefined; [subType, params] = this.handleSubTypeAndParams('watchTickers', market, params); if (this.isLinear(type, subType)) { type = 'future'; } else if (this.isInverse(type, subType)) { type = 'delivery'; } const options = this.safeValue(this.options, 'watchTickers', {}); let name = this.safeString(options, 'name', 'ticker'); name = this.safeString(params, 'name', name); params = this.omit(params, 'name'); let wsParams = []; let messageHash = 'tickers'; if (symbols !== undefined) { messageHash = 'tickers::' + symbols.join(','); } if (name === 'bookTicker') { if (marketIds === undefined) { throw new ArgumentsRequired(this.id + ' watchTickers() requires symbols for bookTicker'); } // simulate watchTickers with subscribe multiple individual bookTicker topic for (let i = 0; i < marketIds.length; i++) { wsParams.push(marketIds[i].toLowerCase() + '@bookTicker'); } } else { wsParams = [ '!' + name + '@arr', ]; } const url = this.urls['api']['ws'][type] + '/' + this.stream(type, messageHash); const requestId = this.requestId(url); const request = { 'method': 'SUBSCRIBE', 'params': wsParams, 'id': requestId, }; const subscribe = { 'id': requestId, }; const newTickers = await this.watch(url, messageHash, this.extend(request, params), messageHash, subscribe); if (this.newUpdates) { return newTickers; } return this.filterByArray(this.tickers, 'symbol', symbols); } parseWsTicker(message, marketType) { // // ticker // { // e: '24hrTicker', // event type // E: 1579485598569, // event time // s: 'ETHBTC', // symbol // p: '-0.00004000', // price change // P: '-0.209', // price change percent // w: '0.01920495', // weighted average price // x: '0.01916500', // the price of the first trade before the 24hr rolling window // c: '0.01912500', // last (closing) price // Q: '0.10400000', // last quantity // b: '0.01912200', // best bid // B: '4.10400000', // best bid quantity // a: '0.01912500', // best ask // A: '0.00100000', // best ask quantity // o: '0.01916500', // open price // h: '0.01956500', // high price // l: '0.01887700', // low price // v: '173518.11900000', // base volume // q: '3332.40703994', // quote volume // O: 1579399197842, // open time // C: 1579485597842, // close time // F: 158251292, // first trade id // L: 158414513, // last trade id // n: 163222, // total number of trades // } // // miniTicker // { // e: '24hrMiniTicker', // E: 1671617114585, // s: 'MOBBUSD', // c: '0.95900000', // o: '0.91200000', // h: '1.04000000', // l: '0.89400000', // v: '2109995.32000000', // q: '2019254.05788000' // } // let event = this.safeString(message, 'e', 'bookTicker'); if (event === '24hrTicker') { event = 'ticker'; } let timestamp = undefined; const now = this.milliseconds(); if (event === 'bookTicker') { // take the event timestamp, if available, for spot tickers it is not timestamp = this.safeInteger(message, 'E', now); } else { // take the timestamp of the closing price for candlestick streams timestamp = this.safeInteger(message, 'C', now); } const marketId = this.safeString(message, 's'); const symbol = this.safeSymbol(marketId, undefined, undefined, marketType); const last = this.safeFloat(message, 'c'); const ticker = { 'symbol': symbol, 'timestamp': timestamp, 'datetime': this.iso8601(timestamp), 'high': this.safeFloat(message, 'h'), 'low': this.safeFloat(message, 'l'), 'bid': this.safeFloat(message, 'b'), 'bidVolume': this.safeFloat(message, 'B'), 'ask': this.safeFloat(message, 'a'), 'askVolume': this.safeFloat(message, 'A'), 'vwap': this.safeFloat(message, 'w'), 'open': this.safeFloat(message, 'o'), 'close': last, 'last': last, 'previousClose': this.safeFloat(message, 'x'), 'change': this.safeFloat(message, 'p'), 'percentage': this.safeFloat(message, 'P'), 'average': undefined, 'baseVolume': this.safeFloat(message, 'v'), 'quoteVolume': this.safeFloat(message, 'q'), 'info': message, }; return ticker; } handleTicker(client, message) { // // 24hr rolling window ticker statistics for a single symbol // These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs // Update Speed 1000ms // // { // e: '24hrTicker', // event type // E: 1579485598569, // event time // s: 'ETHBTC', // symbol // p: '-0.00004000', // price change // P: '-0.209', // price change percent // w: '0.01920495', // weighted average price // x: '0.01916500', // the price of the first trade before the 24hr rolling window // c: '0.01912500', // last (closing) price // Q: '0.10400000', // last quantity // b: '0.01912200', // best bid // B: '4.10400000', // best bid quantity // a: '0.01912500', // best ask // A: '0.00100000', // best ask quantity // o: '0.01916500', // open price // h: '0.01956500', // high price // l: '0.01887700', // low price // v: '173518.11900000', // base volume // q: '3332.40703994', // quote volume // O: 1579399197842, // open time // C: 1579485597842, // close time // F: 158251292, // first trade id // L: 158414513, // last trade id // n: 163222, // total number of trades // } // let event = this.safeString(message, 'e', 'bookTicker'); if (event === '24hrTicker') { event = 'ticker'; } else if (event === '24hrMiniTicker') { event = 'miniTicker'; } const wsMarketId = this.safeStringLower(message, 's'); const messageHash = wsMarketId + '@' + event; const index = client.url.indexOf('/stream'); const marketType = (index >= 0) ? 'spot' : 'contract'; const result = this.parseWsTicker(message, marketType); const symbol = result['symbol']; this.tickers[symbol] = result; client.resolve(result, messageHash); if (event === 'bookTicker') { // watch bookTickers client.resolve([result], '!' + 'bookTicker@arr'); } } handleTickers(client, message) { const index = client.url.indexOf('/stream'); const marketType = (index >= 0) ? 'spot' : 'contract'; let rawTickers = []; const newTickers = []; if (Array.isArray(message)) { rawTickers = message; } else { rawTickers.push(message); } for (let i = 0; i < rawTickers.length; i++) { const ticker = rawTickers[i]; const result = this.parseWsTicker(ticker, marketType); const symbol = result['symbol']; this.tickers[symbol] = result; newTickers.push(result); } const messageHashes = this.findMessageHashes(client, 'tickers::'); for (let i = 0; i < messageHashes.length; i++) { const messageHash = messageHashes[i]; const parts = messageHash.split('::'); const symbolsString = parts[1]; const symbols = symbolsString.split(','); const tickers = this.filterByArray(newTickers, 'symbol', symbols); const tickersSymbols = Object.keys(tickers); const numTickers = tickersSymbols.length; if (numTickers > 0) { client.resolve(tickers, messageHash); } } client.resolve(newTickers, 'tickers'); } async authenticate(params = {}) { const time = this.milliseconds(); let type = this.safeString2(this.options, 'defaultType', 'authenticate', 'spot'); type = this.safeString(params, 'type', type); let subType = undefined; [subType, params] = this.handleSubTypeAndParams('authenticate', undefined, params); if (this.isLinear(type, subType)) { type = 'future'; } else if (this.isInverse(type, subType)) { type = 'delivery'; } let marginMode = undefined; [marginMode, params] = this.handleMarginModeAndParams('authenticate', params); const isIsolatedMargin = (marginMode === 'isolated'); const isCrossMargin = (marginMode === 'cross') || (marginMode === undefined); const symbol = this.safeString(params, 'symbol'); params = this.omit(params, 'symbol'); const options = this.safeValue(this.options, type, {}); const lastAuthenticatedTime = this.safeInteger(options, 'lastAuthenticatedTime', 0); const listenKeyRefreshRate = this.safeInteger(this.options, 'listenKeyRefreshRate', 1200000); const delay = this.sum(listenKeyRefreshRate, 10000); if (time - lastAuthenticatedTime > delay) { let method = 'publicPostUserDataStream'; if (type === 'future') { method = 'fapiPrivatePostListenKey'; } else if (type === 'delivery') { method = 'dapiPrivatePostListenKey'; } else if (type === 'margin' && isCrossMargin) { method = 'sapiPostUserDataStream'; } else if (isIsolatedMargin) { method = 'sapiPostUserDataStreamIsolated'; if (symbol === undefined) { throw new ArgumentsRequired(this.id + ' authenticate() requires a symbol argument for isolated margin mode'); } const marketId = this.marketId(symbol); params = this.extend(params, { 'symbol': marketId }); } const response = await this[method](params); this.options[type] = this.extend(options, { 'listenKey': this.safeString(response, 'listenKey'), 'lastAuthenticatedTime': time, }); this.delay(listenKeyRefreshRate, this.keepAliveListenKey, params); } } async keepAliveListenKey(params = {}) { // https://binance-docs.github.io/apidocs/spot/en/#listen-key-spot let type = this.safeString2(this.options, 'defaultType', 'authenticate', 'spot'); type = this.safeString(params, 'type', type); let subType = undefined; [subType, params] = this.handleSubTypeAndParams('keepAliveListenKey', undefined, params); if (this.isLinear(type, subType)) { type = 'future'; } else if (this.isInverse(type, subType)) { type = 'delivery'; } const options = this.safeValue(this.options, type, {}); const listenKey = this.safeString(options, 'listenKey'); if (listenKey === undefined) { // A network error happened: we can't renew a listen key that does not exist. return; } let method = 'publicPutUserDataStream'; if (type === 'future') { method = 'fapiPrivatePutListenKey'; } else if (type === 'delivery') { method = 'dapiPrivatePutListenKey'; } else if (type === 'margin') { method = 'sapiPutUserDataStream'; } const request = { 'listenKey': listenKey, }; const time = this.milliseconds(); const sendParams = this.omit(params, 'type'); try { await this[method](this.extend(request, sendParams)); } catch (error) { const url = this.urls['api']['ws'][type] + '/' + this.options[type]['listenKey']; const client = this.client(url); const messageHashes = Object.keys(client.futures); for (let i = 0; i < messageHashes.length; i++) { const messageHash = messageHashes[i]; client.reject(error, messageHash); } this.options[type] = this.extend(options, { 'listenKey': undefined, 'lastAuthenticatedTime': 0, }); return; } this.options[type] = this.extend(options, { 'listenKey': listenKey, 'lastAuthenticatedTime':