Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strategy safe guards #21

Merged
merged 6 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const _isEmpty = require('lodash/isEmpty')
const _reverse = require('lodash/reverse')
const _isFunction = require('lodash/isFunction')
const _isPlainObject = require('lodash/isPlainObject')
const BigNumber = require('bignumber.js')

const { candleWidth } = require('bfx-hf-util')
const { subscribe } = require('bfx-api-node-core')
Expand Down Expand Up @@ -40,11 +41,12 @@ class LiveStrategyExecution extends EventEmitter {
* @param {string} args.strategyOpts.tf - time frame to execute on
* @param {boolean} args.strategyOpts.includeTrades - if true, trade data is subscribed to and processed
* @param {number} args.strategyOpts.seedCandleCount - size of indicator candle seed window, before which trading is disabled
* @param {object} args.priceFeed
*/
constructor (args) {
super()

const { strategy, ws2Manager, rest, strategyOpts } = args
const { strategy, ws2Manager, rest, strategyOpts, priceFeed } = args

this.strategyState = {
...(strategy || {}),
Expand All @@ -54,6 +56,7 @@ class LiveStrategyExecution extends EventEmitter {
this.ws2Manager = ws2Manager || {}
this.rest = rest || {}
this.strategyOpts = strategyOpts || {}
this.priceFeed = priceFeed

this.lastCandle = null
this.lastTrade = null
Expand All @@ -74,13 +77,19 @@ class LiveStrategyExecution extends EventEmitter {

const { includeTrades, symbol, tf } = this.strategyOpts
const candleKey = `trade:${tf}:${symbol}`
let lastUpdate = 0
tarcisiozf marked this conversation as resolved.
Show resolved Hide resolved

if (includeTrades) {
this.ws2Manager.onWS('trades', { symbol }, async (trades) => {
if (trades.length > 1) { // we don't pass snapshots through
return
}

if (trades.mts > lastUpdate) {
this.priceFeed.update(new BigNumber(trades.price))
lastUpdate = trades.mts
}

this._enqueueMessage('trade', trades)
})
}
Expand All @@ -94,6 +103,11 @@ class LiveStrategyExecution extends EventEmitter {
candle.symbol = symbol
candle.tf = tf

if (candle.mts > lastUpdate) {
this.priceFeed.update(new BigNumber(candle.close))
tarcisiozf marked this conversation as resolved.
Show resolved Hide resolved
lastUpdate = candle.mts
}

this._enqueueMessage('candle', candle)
})
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"bfx-api-node-util": "^1.0.2",
"bfx-hf-strategy": "git+https://github.com/bitfinexcom/bfx-hf-strategy.git#v1.2.4",
"bfx-hf-util": "git+https://github.com/bitfinexcom/bfx-hf-util.git#v1.0.12",
"bignumber.js": "^9.0.2",
"debug": "^4.3.3",
"lodash": "^4.17.10",
"mathjs": "^10.1.0",
Expand Down