Skip to content

Commit

Permalink
fix abort strategy (#192)
Browse files Browse the repository at this point in the history

Co-authored-by: Tarcisio Ferraz <[email protected]>
  • Loading branch information
tarcisiozf and tferraz-bitfinex authored Aug 25, 2022
1 parent 6eedca1 commit a604eb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
32 changes: 13 additions & 19 deletions lib/ws_servers/api/handlers/strategy/strategy_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ const { Manager, findChannelId, unsubscribe } = require('bfx-api-node-core')
const {
PriceFeed,
PerformanceManager,
StartWatchers: startPerformanceWatchers,
ExitModes
StartWatchers: startPerformanceWatchers
} = require('bfx-hf-strategy-perf')

const { apply: applyI18N } = require('../../../../util/i18n')
const { WD_PACKET_DELAY, WD_RECONNECT_DELAY } = require('../../../../constants')
const { closeOpenPositions, closePendingOrders } = require('bfx-hf-strategy')

const debug = require('debug')('bfx:hf:server:strategy-manager')

Expand Down Expand Up @@ -159,7 +157,7 @@ class StrategyManager {
console.error(error)

const errorMessage = error.text || error
this._sendError(errorMessage, strategyMapKey, strategyOptions)
this._sendError(errorMessage)

this.close(strategyMapKey)
})
Expand Down Expand Up @@ -198,11 +196,10 @@ class StrategyManager {
/**
* @private
* @param {Error|Object|*} err
* @param strategyMapKey
* @param label
*/
_sendError (err, strategyMapKey, { label }) {
this.pub(['notify', 'error', `Strategy Execution Error: ${err}`])
_sendError (err) {
const msg = err.text || err.message || err
this.pub(['notify', 'error', `Strategy Execution Error: ${msg}`])
}

/**
Expand Down Expand Up @@ -346,21 +343,14 @@ class StrategyManager {
return activeStrategyKeys.map(strategyMapKey => this._formatStrategy(strategyMapKey))
}

async _abortStrategy (strategyMapKey, mode) {
async _abortStrategy (strategyMapKey) {
const strategy = this.strategy.get(strategyMapKey)

if (strategy.isClosing) {
return
}

strategy.isClosing = true
const { liveStrategyExecutor } = strategy

await liveStrategyExecutor.invoke(
mode === ExitModes.KEEP_ASSETS
? closePendingOrders
: closeOpenPositions
)

await this.close(strategyMapKey)
}
Expand Down Expand Up @@ -521,9 +511,13 @@ class StrategyManager {
let strategyExecutionResults = {}

if (liveStrategyExecutor) {
await liveStrategyExecutor.stopExecution()
liveStrategyExecutor.removeAllListeners()
strategyExecutionResults = liveStrategyExecutor.generateResults()
try {
await liveStrategyExecutor.stopExecution()
liveStrategyExecutor.removeAllListeners()
strategyExecutionResults = liveStrategyExecutor.generateResults()
} catch (e) {
this._sendError(e)
}
}

this.pub(['strategy.live_execution_results', strategyMapKey, strategyExecutionResults])
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-server",
"version": "7.6.2",
"version": "7.6.3",
"description": "HF server bundle",
"author": "Bitfinex",
"license": "Apache-2.0",
Expand Down

0 comments on commit a604eb1

Please sign in to comment.