* Websocket futures 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} th
(symbols: string[] | string, interval: Interval, callback: Callback, limit = 500)
| 5803 | * @return {string} the websocket endpoint |
| 5804 | */ |
| 5805 | futuresChart(symbols: string[] | string, interval: Interval, callback: Callback, limit = 500) { |
| 5806 | const reconnect = () => { |
| 5807 | if (this.Options.reconnect) this.futuresChart(symbols, interval, callback, limit); |
| 5808 | }; |
| 5809 | |
| 5810 | const futuresChartInit = (symbol: string) => { |
| 5811 | if (typeof this.futuresMeta[symbol] === 'undefined') this.futuresMeta[symbol] = {}; |
| 5812 | if (typeof this.futuresMeta[symbol][interval] === 'undefined') this.futuresMeta[symbol][interval] = {}; |
| 5813 | if (typeof this.futuresTicks[symbol] === 'undefined') this.futuresTicks[symbol] = {}; |
| 5814 | if (typeof this.futuresTicks[symbol][interval] === 'undefined') this.futuresTicks[symbol][interval] = {}; |
| 5815 | if (typeof this.futuresRealtime[symbol] === 'undefined') this.futuresRealtime[symbol] = {}; |
| 5816 | if (typeof this.futuresRealtime[symbol][interval] === 'undefined') this.futuresRealtime[symbol][interval] = {}; |
| 5817 | if (typeof this.futuresKlineQueue[symbol] === 'undefined') this.futuresKlineQueue[symbol] = {}; |
| 5818 | if (typeof this.futuresKlineQueue[symbol][interval] === 'undefined') this.futuresKlineQueue[symbol][interval] = []; |
| 5819 | this.futuresMeta[symbol][interval].timestamp = 0; |
| 5820 | }; |
| 5821 | |
| 5822 | const handleFuturesKlineStream = kline => { |
| 5823 | const symbol = kline.s, interval: Interval = kline.k.i; |
| 5824 | if (!this.futuresMeta[symbol][interval].timestamp) { |
| 5825 | if (typeof (this.futuresKlineQueue[symbol][interval]) !== 'undefined' && kline !== null) { |
| 5826 | this.futuresKlineQueue[symbol][interval].push(kline); |
| 5827 | } |
| 5828 | } else { |
| 5829 | //this.options.log('futures klines at ' + kline.k.t); |
| 5830 | this.futuresKlineHandler(symbol, kline); |
| 5831 | if (callback) callback(symbol, interval, this.futuresKlineConcat(symbol, interval)); |
| 5832 | } |
| 5833 | }; |
| 5834 | |
| 5835 | const getFuturesKlineSnapshot = async (symbol: string, limit = 500) => { |
| 5836 | const data = await this.publicFuturesRequest('v1/klines', { symbol, interval, limit }); |
| 5837 | this.futuresKlineData(symbol, interval, data); |
| 5838 | //this.options.log('/futures klines at ' + this.futuresMeta[symbol][interval].timestamp); |
| 5839 | if (typeof this.futuresKlineQueue[symbol][interval] !== 'undefined') { |
| 5840 | for (const kline of this.futuresKlineQueue[symbol][interval]) this.futuresKlineHandler(symbol, kline, this.futuresMeta[symbol][interval].timestamp); |
| 5841 | delete this.futuresKlineQueue[symbol][interval]; |
| 5842 | } |
| 5843 | if (callback) callback(symbol, interval, this.futuresKlineConcat(symbol, interval)); |
| 5844 | }; |
| 5845 | |
| 5846 | let subscription; |
| 5847 | if (Array.isArray(symbols)) { |
| 5848 | if (!this.isArrayUnique(symbols)) throw Error('futuresChart: "symbols" array cannot contain duplicate elements.'); |
| 5849 | symbols.forEach(futuresChartInit); |
| 5850 | const streams = symbols.map(symbol => `${symbol.toLowerCase()}@kline_${interval}`); |
| 5851 | subscription = this.futuresSubscribe(streams, handleFuturesKlineStream, reconnect); |
| 5852 | symbols.forEach(element => getFuturesKlineSnapshot(element, limit)); |
| 5853 | } else { |
| 5854 | const symbol = symbols; |
| 5855 | futuresChartInit(symbol); |
| 5856 | subscription = this.futuresSubscribeSingle(symbol.toLowerCase() + '@kline_' + interval, handleFuturesKlineStream, { reconnect }); |
| 5857 | getFuturesKlineSnapshot(symbol, limit); |
| 5858 | } |
| 5859 | return (subscription as any).url; |
| 5860 | } |
| 5861 | |
| 5862 | /** |
no test coverage detected