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

use trade data for price feed #30

Merged
merged 4 commits into from
Aug 16, 2022
Merged
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
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