From ce1a4fb64fdfb33da111504b2da59b7c3744c0b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarc=C3=ADsio=20Zotelli=20Ferraz?= Date: Tue, 16 Aug 2022 09:42:40 -0300 Subject: [PATCH] use trade data for price feed (#30) * wip * update with mts * update version Co-authored-by: Tarcisio Ferraz --- index.js | 47 +++++++++++++++++++++-------------------------- package.json | 2 +- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index db0c494..ed3e102 100644 --- a/index.js +++ b/index.js @@ -40,16 +40,16 @@ class LiveStrategyExecution extends EventEmitter { constructor (args) { super() - const { strategy = {}, ws2Manager, rest, strategyOptions, priceFeed, perfManager } = args + const { strategy, ws2Manager, rest, strategyOptions, priceFeed, perfManager } = args this.strategyState = { ...strategy, emit: this.emit.bind(this) } - this.ws2Manager = ws2Manager || {} - this.rest = rest || {} - this.strategyOptions = strategyOptions || {} + this.ws2Manager = ws2Manager + this.rest = rest + this.strategyOptions = strategyOptions this.priceFeed = priceFeed this.perfManager = perfManager @@ -108,18 +108,23 @@ class LiveStrategyExecution extends EventEmitter { throw new Error('WS2 manager not available') } - const { trades, symbol, timeframe } = this.strategyOptions + const { trades: includeTrades, symbol, timeframe } = this.strategyOptions const candleKey = `trade:${timeframe}:${symbol}` - if (trades) { - this.ws2Manager.onWS('trades', { symbol }, async (trades) => { - if (trades.length > 1) { // we don't pass snapshots through - return - } + this.ws2Manager.onWS('trades', { symbol }, async (trades) => { + if (trades.length > 1) { // we don't pass snapshots through + return + } + if (includeTrades) { this._enqueueMessage('trade', trades) - }) - } + } + + if (trades.mts > this.lastPriceFeedUpdate) { + this.priceFeed.update(trades.price, trades.mts) + this.lastPriceFeedUpdate = trades.mts + } + }) this.ws2Manager.onWS('candles', { key: candleKey }, async (candles) => { if (candles.length > 1) { // seeding happens at start via RESTv2 @@ -322,11 +327,6 @@ class LiveStrategyExecution extends EventEmitter { return } - if (data.mts > this.lastPriceFeedUpdate) { - this.priceFeed.update(data.price) - this.lastPriceFeedUpdate = data.mts - } - const { symbol } = this.strategyState data.symbol = symbol debug('recv trade: %j', data) @@ -341,7 +341,7 @@ class LiveStrategyExecution extends EventEmitter { */ async _processCandleData (data) { if (data.mts > this.lastPriceFeedUpdate) { - this.priceFeed.update(data[this.candlePrice]) + this.priceFeed.update(data[this.candlePrice], data.mts) this.lastPriceFeedUpdate = data.mts } @@ -385,17 +385,12 @@ class LiveStrategyExecution extends EventEmitter { * @private */ _subscribeCandleAndTradeEvents () { - const { trades, symbol, timeframe } = this.strategyOptions + const { symbol, timeframe } = this.strategyOptions const candleKey = `trade:${timeframe}:${symbol}` this.ws2Manager.withSocket((socket) => { - let nextSocket = subscribe(socket, 'candles', { key: candleKey }) - - if (trades) { - nextSocket = subscribe(nextSocket, 'trades', { symbol }) - } - - return nextSocket + const nextSocket = subscribe(socket, 'candles', { key: candleKey }) + return subscribe(nextSocket, 'trades', { symbol }) }) } diff --git a/package.json b/package.json index 71148b5..8526b4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bfx-hf-strategy-exec", - "version": "2.2.2", + "version": "2.3.0", "description": "Execution logic for bfx-hf-strategy", "main": "./index.js", "directories": {