Skip to content

Commit

Permalink
use trade data for price feed (#30)
Browse files Browse the repository at this point in the history
* wip

* update with mts

* update version

Co-authored-by: Tarcisio Ferraz <[email protected]>
  • Loading branch information
tarcisiozf and tferraz-bitfinex authored Aug 16, 2022
1 parent f382324 commit ce1a4fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
47 changes: 21 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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 })
})
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit ce1a4fb

Please sign in to comment.