|
| 1 | +const moment = require('moment'); |
| 2 | +const ccxtpro = require('ccxt').pro; |
| 3 | +const Ticker = require('../dict/ticker'); |
| 4 | +const TickerEvent = require('../event/ticker_event'); |
| 5 | +const ExchangeCandlestick = require('../dict/exchange_candlestick'); |
| 6 | + |
| 7 | +module.exports = class BybitUnified { |
| 8 | + constructor(eventEmitter, requestClient, candlestickResample, logger, queue, candleImporter, throttler) { |
| 9 | + this.eventEmitter = eventEmitter; |
| 10 | + this.logger = logger; |
| 11 | + this.queue = queue; |
| 12 | + this.candleImporter = candleImporter; |
| 13 | + |
| 14 | + this.apiKey = undefined; |
| 15 | + this.apiSecret = undefined; |
| 16 | + this.tickSizes = {}; |
| 17 | + this.lotSizes = {}; |
| 18 | + |
| 19 | + this.positions = {}; |
| 20 | + this.orders = {}; |
| 21 | + this.tickers = {}; |
| 22 | + this.symbols = []; |
| 23 | + this.intervals = []; |
| 24 | + } |
| 25 | + |
| 26 | + async start(config, symbols) { |
| 27 | + const { eventEmitter } = this; |
| 28 | + const { logger } = this; |
| 29 | + const { tickSizes } = this; |
| 30 | + const { lotSizes } = this; |
| 31 | + this.intervals = []; |
| 32 | + const me = this; |
| 33 | + |
| 34 | + this.symbols = symbols; |
| 35 | + this.positions = {}; |
| 36 | + this.orders = {}; |
| 37 | + |
| 38 | + const exchange = new ccxtpro.bybit({ newUpdates: false }); |
| 39 | + |
| 40 | + setTimeout(async () => { |
| 41 | + const result = (await exchange.fetch_markets()).filter(i => i.type === 'swap' && i.quote === 'USDT'); |
| 42 | + result.forEach(instrument => { |
| 43 | + tickSizes[instrument.symbol] = parseFloat(instrument.precision.price); |
| 44 | + lotSizes[instrument.symbol] = parseFloat(instrument.precision.amount); |
| 45 | + }); |
| 46 | + }, 5000); |
| 47 | + |
| 48 | + setTimeout(async () => { |
| 49 | + const symbolPeriods = []; |
| 50 | + symbols.forEach(symbol => { |
| 51 | + symbolPeriods.push(...symbol.periods.map(p => [symbol.symbol, p])); |
| 52 | + }); |
| 53 | + |
| 54 | + while (true) { |
| 55 | + try { |
| 56 | + const event = await exchange.watchOHLCVForSymbols(symbolPeriods); |
| 57 | + |
| 58 | + const cxchangeCandlesticks = []; |
| 59 | + |
| 60 | + for (const [symbol, tickers] of Object.entries(event)) { |
| 61 | + for (const [period, candles] of Object.entries(tickers)) { |
| 62 | + // timestamp, open, high, low, close, volume |
| 63 | + cxchangeCandlesticks.push( |
| 64 | + ...candles.map(t => new ExchangeCandlestick(me.getName(), symbol, period, Math.round(t[0] / 1000), t[1], t[2], t[3], t[4], t[5])) |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + me.candleImporter.insertThrottledCandles(cxchangeCandlesticks); |
| 70 | + } catch (e) { |
| 71 | + logger.error('watchOHLCVForSymbols error', e); |
| 72 | + } |
| 73 | + } |
| 74 | + }, 1000); |
| 75 | + |
| 76 | + const symboleIds = symbols.map(symbol => symbol.symbol); |
| 77 | + |
| 78 | + setTimeout(async () => { |
| 79 | + while (true) { |
| 80 | + try { |
| 81 | + const event = await exchange.watchTickers(symboleIds); |
| 82 | + |
| 83 | + for (const [symbol, ticker] of Object.entries(event)) { |
| 84 | + const tickerEvent = new TickerEvent( |
| 85 | + me.getName(), |
| 86 | + symbol, |
| 87 | + (me.tickers[symbol] = new Ticker(me.getName(), symbol, moment().format('X'), ticker.bid, ticker.ask)) |
| 88 | + ); |
| 89 | + eventEmitter.emit('ticker', tickerEvent); |
| 90 | + } |
| 91 | + } catch (e) { |
| 92 | + logger.error('watchTickers error', e); |
| 93 | + } |
| 94 | + } |
| 95 | + }, 1000); |
| 96 | + |
| 97 | + // const tickers = await exchange.watchTickers(['BTC/USDT:USDT']); |
| 98 | + // console.log(tickers); |
| 99 | + |
| 100 | + /** |
| 101 | + * if (config.key && config.secret && config.key.length > 0 && config.secret.length > 0) { |
| 102 | + * me.logger.info('BybitLinear: sending auth request'); |
| 103 | + * me.apiKey = config.key; |
| 104 | + * me.apiSecret = config.secret; |
| 105 | + * } else { |
| 106 | + * me.logger.info('BybitLinear: Starting as anonymous; no trading possible'); |
| 107 | + * } |
| 108 | + */ |
| 109 | + |
| 110 | + symbols.forEach(symbol => { |
| 111 | + symbol.periods.forEach(period => { |
| 112 | + // for bot init prefill data: load latest candles from api |
| 113 | + this.queue.add(async () => { |
| 114 | + let candles; |
| 115 | + try { |
| 116 | + candles = await exchange.fetchOHLCV(symbol.symbol, period, undefined, 500); |
| 117 | + } catch (e) { |
| 118 | + logger.error('backfill error bybit', symbol.symbol, period, e); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + const candleSticks = candles.map( |
| 123 | + t => new ExchangeCandlestick(me.getName(), symbol.symbol, period, Math.round(t[0] / 1000), t[1], t[2], t[3], t[4], t[5]) |
| 124 | + ); |
| 125 | + |
| 126 | + await this.candleImporter.insertThrottledCandles(candleSticks); |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + async getOrders() { |
| 133 | + return []; |
| 134 | + } |
| 135 | + |
| 136 | + async findOrderById(id) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + async getOrdersForSymbol(symbol) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + async getPositions() { |
| 145 | + return []; |
| 146 | + } |
| 147 | + |
| 148 | + async getPositionForSymbol(symbol) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * LTC: 0.008195 => 0.00820 |
| 154 | + * |
| 155 | + * @param price |
| 156 | + * @param symbol |
| 157 | + * @returns {*} |
| 158 | + */ |
| 159 | + calculatePrice(price, symbol) { |
| 160 | + throw Error('not supported'); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Force an order update only if order is "not closed" for any reason already by exchange |
| 165 | + * |
| 166 | + * @param order |
| 167 | + */ |
| 168 | + triggerOrder(order) { |
| 169 | + throw Error('not supported'); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * LTC: 0.65 => 1 |
| 174 | + * |
| 175 | + * @param amount |
| 176 | + * @param symbol |
| 177 | + * @returns {*} |
| 178 | + */ |
| 179 | + calculateAmount(amount, symbol) { |
| 180 | + throw Error('not supported'); |
| 181 | + } |
| 182 | + |
| 183 | + getName() { |
| 184 | + return 'bybit_unified'; |
| 185 | + } |
| 186 | + |
| 187 | + async order(order) { |
| 188 | + throw Error('not supported'); |
| 189 | + } |
| 190 | + |
| 191 | + async cancelOrder(id) { |
| 192 | + throw Error('not supported'); |
| 193 | + } |
| 194 | + |
| 195 | + async cancelAll(symbol) { |
| 196 | + throw Error('not supported'); |
| 197 | + } |
| 198 | +}; |
0 commit comments