@jalmonter/ccxt
Version:
1,081 lines (1,079 loc) • 129 kB
JavaScript
'use strict';
var binance$1 = require('../binance.js');
var Precise = require('../base/Precise.js');
var errors = require('../base/errors.js');
var Cache = require('../base/ws/Cache.js');
var sha256 = require('../static_dependencies/noble-hashes/sha256.js');
var rsa = require('../base/functions/rsa.js');
var crypto = require('../base/functions/crypto.js');
var ed25519 = require('../static_dependencies/noble-curves/ed25519.js');
// ----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class binance extends binance$1 {
describe() {
return this.deepExtend(super.describe(), {
'has': {
'ws': true,
'watchBalance': true,
'watchMyTrades': true,
'watchOHLCV': true,
'watchOHLCVForSymbols': true,
'watchOrderBook': true,
'watchOrderBookForSymbols': true,
'watchOrders': true,
'watchOrdersForSymbols': true,
'watchPositions': true,
'watchTicker': true,
'watchTickers': true,
'watchTrades': true,
'watchTradesForSymbols': true,
'createOrderWs': true,
'editOrderWs': true,
'cancelOrderWs': true,
'cancelOrdersWs': false,
'cancelAllOrdersWs': true,
'fetchOrderWs': true,
'fetchOrdersWs': true,
'fetchBalanceWs': true,
'fetchMyTradesWs': 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',
'ws': 'wss://testnet.binance.vision/ws-api/v3',
},
},
'api': {
'ws': {
'spot': 'wss://stream.binance.com/ws',
'margin': 'wss://stream.binance.com/ws',
'future': 'wss://fstream.binance.com/ws',
'delivery': 'wss://dstream.binance.com/ws',
'ws': 'wss://ws-api.binance.com:443/ws-api/v3',
},
},
},
'streaming': {
'keepAlive': 180000,
},
'options': {
'returnRateLimits': false,
'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)
},
'watchOrderBook': {
'maxRetries': 3,
},
'watchBalance': {
'fetchBalanceSnapshot': false,
'awaitBalanceSnapshot': true, // whether to wait for the balance snapshot before providing updates
},
'watchPositions': {
'fetchPositionsSnapshot': true,
'awaitPositionsSnapshot': true, // whether to wait for the positions 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} [limit] the maximum amount of order book entries to return
* @param {object} [params] extra parameters specific to the exchange 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
//
// 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.
//
return await this.watchOrderBookForSymbols([symbol], limit, params);
}
async watchOrderBookForSymbols(symbols, limit = undefined, params = {}) {
/**
* @method
* @name binance#watchOrderBookForSymbols
* @description watches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
* @param {string[]} symbols unified array of symbols
* @param {int} [limit] the maximum amount of order book entries to return
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols
*/
await this.loadMarkets();
symbols = this.marketSymbols(symbols, undefined, false, true, true);
const firstMarket = this.market(symbols[0]);
let type = firstMarket['type'];
if (firstMarket['contract']) {
type = firstMarket['linear'] ? 'future' : 'delivery';
}
const name = 'depth';
const url = this.urls['api']['ws'][type] + '/' + this.stream(type, 'multipleOrderbook');
const requestId = this.requestId(url);
const watchOrderBookRate = this.safeString(this.options, 'watchOrderBookRate', '100');
const subParams = [];
const messageHashes = [];
for (let i = 0; i < symbols.length; i++) {
const symbol = symbols[i];
const market = this.market(symbol);
const messageHash = market['lowercaseId'] + '@' + name;
messageHashes.push(messageHash);
const symbolHash = messageHash + '@' + watchOrderBookRate + 'ms';
subParams.push(symbolHash);
}
const request = {
'method': 'SUBSCRIBE',
'params': subParams,
'id': requestId,
};
const subscription = {
'id': requestId.toString(),
'name': name,
'symbols': symbols,
'method': this.handleOrderBookSubscription,
'limit': limit,
'type': type,
'params': params,
};
const message = this.extend(request, params);
const orderbook = await this.watchMultiple(url, messageHashes, message, messageHashes, subscription);
return orderbook.limit();
}
async fetchOrderBookSnapshot(client, message, subscription) {
const name = this.safeString(subscription, 'name');
const symbol = this.safeString(subscription, 'symbol');
const market = this.market(symbol);
const messageHash = market['lowercaseId'] + '@' + name;
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 - make it async
// default 100, max 1000, valid limits 5, 10, 20, 50, 100, 500, 1000
const snapshot = await this.fetchRestOrderBookSafe(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 messageItem = messages[i];
const U = this.safeInteger(messageItem, 'U');
const u = this.safeInteger(messageItem, 'u');
const pu = this.safeInteger(messageItem, '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, messageItem, 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, messageItem, 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 errors.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 errors.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 messageHash = this.safeString (subscription, 'messageHash');
const symbolOfSubscription = this.safeString(subscription, 'symbol'); // watchOrderBook
const symbols = this.safeValue(subscription, 'symbols', [symbolOfSubscription]); // watchOrderBookForSymbols
const limit = this.safeInteger(subscription, 'limit', defaultLimit);
// handle list of symbols
for (let i = 0; i < symbols.length; i++) {
const symbol = symbols[i];
if (symbol in this.orderbooks) {
delete this.orderbooks[symbol];
}
this.orderbooks[symbol] = this.orderBook({}, limit);
subscription = this.extend(subscription, { 'symbol': symbol });
// 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 watchTradesForSymbols(symbols, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name binance#watchTradesForSymbols
* @description get the list of most recent trades for a list of symbols
* @param {string[]} symbols unified symbol of the market to fetch trades for
* @param {int} [since] timestamp in ms of the earliest trade to fetch
* @param {int} [limit] the maximum amount of trades to fetch
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
*/
await this.loadMarkets();
symbols = this.marketSymbols(symbols, undefined, false, true, true);
const options = this.safeValue(this.options, 'watchTradesForSymbols', {});
const name = this.safeString(options, 'name', 'trade');
const firstMarket = this.market(symbols[0]);
let type = firstMarket['type'];
if (firstMarket['contract']) {
type = firstMarket['linear'] ? 'future' : 'delivery';
}
const subParams = [];
for (let i = 0; i < symbols.length; i++) {
const symbol = symbols[i];
const market = this.market(symbol);
const currentMessageHash = market['lowercaseId'] + '@' + name;
subParams.push(currentMessageHash);
}
const query = this.omit(params, 'type');
const url = this.urls['api']['ws'][type] + '/' + this.stream(type, 'multipleTrades');
const requestId = this.requestId(url);
const request = {
'method': 'SUBSCRIBE',
'params': subParams,
'id': requestId,
};
const subscribe = {
'id': requestId,
};
const trades = await this.watch(url, subParams, this.extend(request, query), subParams, subscribe);
if (this.newUpdates) {
const first = this.safeValue(trades, 0);
const tradeSymbol = this.safeString(first, 'symbol');
limit = trades.getLimit(tradeSymbol, limit);
}
return this.filterBySinceLimit(trades, since, limit, 'timestamp', true);
}
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} [since] timestamp in ms of the earliest trade to fetch
* @param {int} [limit] the maximum amount of trades to fetch
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
*/
return await this.watchTradesForSymbols([symbol], since, limit, params);
}
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["default"].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 isSpot = ((client.url.indexOf('wss://stream.binance.com') > -1) || (client.url.indexOf('/testnet.binance') > -1));
const marketType = (isSpot) ? '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 Cache.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} [since] timestamp in ms of the earliest candle to fetch
* @param {int} [limit] the maximum amount of candles to fetch
* @param {object} [params] extra parameters specific to the exchange 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') {
marketId = marketId.replace('_perp', '');
// weird behavior for index price kline we can't use the perp suffix
}
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 isSpot = ((client.url.indexOf('/stream') > -1) || (client.url.indexOf('/testnet.binance') > -1));
const marketType = (isSpot) ? '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 Cache.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 exchange 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 exchange API endpoint
* @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure}
*/
await this.loadMarkets();
symbols = this.marketSymbols(symbols, undefined, true, true, true);
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 = [];
const messageHash = 'tickers';
if (name === 'bookTicker') {
if (marketIds === undefined) {
throw new errors.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 isSpot = ((client.url.indexOf('/stream') > -1) || (client.url.indexOf('/testnet.binance') > -1));
const marketType = (isSpot) ? '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');
const messageHashes = this.findMessageHashes(client, 'tickers::');
for (let i = 0; i < messageHashes.length; i++) {
const currentMessageHash = messageHashes[i];
const parts = currentMessageHash.split('::');
const symbolsString = parts[1];
const symbols = symbolsString.split(',');
if (this.inArray(symbol, symbols)) {
client.resolve(result, currentMessageHash);
}
}
}
}
handleTickers(client, message) {
const isSpot = ((client.url.indexOf('/stream') > -1) || (client.url.indexOf('/testnet.binance') > -1));
const marketType = (isSpot) ? '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);
}
client.resolve(newTickers, 'tickers');
}
signParams(params = {}) {
this.checkRequiredCredentials();
let extendedParams = this.extend({
'timestamp': this.nonce(),
'apiKey': this.apiKey,
}, params);
const defaultRecvWindow = this.safeInteger(this.options, 'recvWindow');
if (defaultRecvWindow !== undefined) {
params['recvWindow'] = defaultRecvWindow;
}
const recvWindow = this.safeInteger(params, 'recvWindow');
if (recvWindow !== undefined) {
params['recvWindow'] = recvWindow;
}
extendedParams = this.keysort(extendedParams);
const query = this.urlencode(extendedParams);
let signature = undefined;
if (this.secret.indexOf('PRIVATE KEY') > -1) {
if