* Websocket klines * @param {array/string} symbols - an array or string of symbols to query * @param {string} interval - the time interval * @param {function} callback - callback function * @param {int} limit - maximum results, no more than 1000 * @return {string} the websoc
(symbols: string[] | string, interval: Interval, callback: Callback, limit = 500)
| 6649 | * @return {string} the websocket endpoint |
| 6650 | */ |
| 6651 | chart(symbols: string[] | string, interval: Interval, callback: Callback, limit = 500) { |
| 6652 | const reconnect = () => { |
| 6653 | if (this.Options.reconnect) this.chart(symbols, interval, callback, limit); |
| 6654 | }; |
| 6655 | |
| 6656 | const symbolChartInit = symbol => { |
| 6657 | if (typeof this.info[symbol] === 'undefined') this.info[symbol] = {}; |
| 6658 | if (typeof this.info[symbol][interval] === 'undefined') this.info[symbol][interval] = {}; |
| 6659 | if (typeof this.ohlc[symbol] === 'undefined') this.ohlc[symbol] = {}; |
| 6660 | if (typeof this.ohlc[symbol][interval] === 'undefined') this.ohlc[symbol][interval] = {}; |
| 6661 | if (typeof this.ohlcLatest[symbol] === 'undefined') this.ohlcLatest[symbol] = {}; |
| 6662 | if (typeof this.ohlcLatest[symbol][interval] === 'undefined') this.ohlcLatest[symbol][interval] = {}; |
| 6663 | if (typeof this.klineQueue[symbol] === 'undefined') this.klineQueue[symbol] = {}; |
| 6664 | if (typeof this.klineQueue[symbol][interval] === 'undefined') this.klineQueue[symbol][interval] = []; |
| 6665 | this.info[symbol][interval].timestamp = 0; |
| 6666 | }; |
| 6667 | |
| 6668 | const handleKlineStreamData = kline => { |
| 6669 | const symbol = kline.s, interval: Interval = kline.k.i; |
| 6670 | if (!this.info[symbol][interval].timestamp) { |
| 6671 | if (typeof (this.klineQueue[symbol][interval]) !== 'undefined' && kline !== null) { |
| 6672 | this.klineQueue[symbol][interval].push(kline); |
| 6673 | } |
| 6674 | } else { |
| 6675 | //this.options.log('@klines at ' + kline.k.t); |
| 6676 | this.klineHandler(symbol, kline); |
| 6677 | if (callback) callback(symbol, interval, this.klineConcat(symbol, interval)); |
| 6678 | } |
| 6679 | }; |
| 6680 | |
| 6681 | const getSymbolKlineSnapshot = async (symbol: string, limit = 500) => { |
| 6682 | const data = await this.publicSpotRequest('v3/klines', { symbol: symbol, interval: interval, limit: limit }); |
| 6683 | // function (error, data) { |
| 6684 | // klineData(symbol, interval, data); |
| 6685 | // //this.options.log('/klines at ' +this.info[symbol][interval].timestamp); |
| 6686 | // if (typeof this.klineQueue[symbol][interval] !== 'undefined') { |
| 6687 | // for (let kline of this.klineQueue[symbol][interval]) klineHandler(symbol, kline, this.info[symbol][interval].timestamp); |
| 6688 | // delete this.klineQueue[symbol][interval]; |
| 6689 | // } |
| 6690 | // if (callback) callback(symbol, interval, this.klineConcat(symbol, interval)); |
| 6691 | // } |
| 6692 | this.klineData(symbol, interval, data); |
| 6693 | if (typeof this.klineQueue[symbol][interval] !== 'undefined') { |
| 6694 | for (const kline of this.klineQueue[symbol][interval]) this.klineHandler(symbol, kline, this.info[symbol][interval].timestamp); |
| 6695 | delete this.klineQueue[symbol][interval]; |
| 6696 | } |
| 6697 | if (callback) callback(symbol, interval, this.klineConcat(symbol, interval)); |
| 6698 | }; |
| 6699 | |
| 6700 | let subscription; |
| 6701 | if (Array.isArray(symbols)) { |
| 6702 | if (!this.isArrayUnique(symbols)) throw Error('chart: "symbols" cannot contain duplicate elements.'); |
| 6703 | symbols.forEach(symbolChartInit); |
| 6704 | const streams = symbols.map(function (symbol) { |
| 6705 | return symbol.toLowerCase() + '@kline_' + interval; |
| 6706 | }); |
| 6707 | subscription = this.subscribeCombined(streams, handleKlineStreamData, reconnect); |
| 6708 | symbols.forEach(element => getSymbolKlineSnapshot(element, limit)); |
no test coverage detected