From 8064ff8b7c8dccc89a53eacdba3f2e2f2d40f37b Mon Sep 17 00:00:00 2001 From: Abhishek Shrestha Date: Fri, 10 Jun 2022 15:37:25 +0545 Subject: [PATCH 1/3] format strategy results --- index.js | 89 ++-------------------------------------------------- package.json | 1 - 2 files changed, 2 insertions(+), 88 deletions(-) diff --git a/index.js b/index.js index 42e57cc..8ef33a4 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,19 @@ 'use strict' -const { std } = require('mathjs') -const _sum = require('lodash/sum') -const _min = require('lodash/min') -const _max = require('lodash/max') -const _isNil = require('lodash/isNil') const _isEmpty = require('lodash/isEmpty') const _reverse = require('lodash/reverse') const _isFunction = require('lodash/isFunction') -const _isPlainObject = require('lodash/isPlainObject') const { candleWidth } = require('bfx-hf-util') const { subscribe } = require('bfx-api-node-core') const { padCandles } = require('bfx-api-node-util') const PromiseThrottle = require('promise-throttle') const debug = require('debug')('bfx:hf:strategy-exec') - const { onSeedCandle, onCandle, onTrade, closeOpenPositions, getPosition, positionPl } = require('bfx-hf-strategy') +const _generateStrategyResults = require('bfx-hf-strategy/lib/util/generate_strategy_results') const EventEmitter = require('events') @@ -332,86 +326,7 @@ class LiveStrategyExecution extends EventEmitter { * @returns {object} */ generateResults (openPosition = null) { - const { symbol, timeframe } = this.strategyOptions - const { trades: strategyTrades = [], marketData = {} } = this.strategyState - - if (_isPlainObject(openPosition)) { - const openOrder = strategyTrades.find(st => st.position_id === openPosition.id) - if (openOrder) { - openOrder.pl = openPosition.pl - } - } - - const candles = marketData[`candles-${symbol}-${timeframe}`] || [] - const trades = marketData[`trades-${symbol}`] || [] - - const nCandles = candles.length - const nTrades = trades.length - - const nStrategyTrades = strategyTrades.length - const pls = strategyTrades.map(t => t.pl) - const gains = pls.filter(pl => pl > 0) - const losses = pls.filter(pl => pl < 0) - const nOpens = pls.filter(pl => pl === 0).length - const vol = _sum(strategyTrades.map(t => Math.abs(t.price * t.amount))) - const fees = _sum(strategyTrades.map(t => t.fee)) - const totalGain = _sum(gains) - const totalLoss = _sum(losses) - const pf = totalGain / Math.abs(totalLoss) - const pl = _sum(pls) - const minPL = _min(pls) - const maxPL = _max(pls) - const accumulatedPLs = strategyTrades.map(x => x.pl) - const stdDeviation = std(accumulatedPLs.length > 0 ? accumulatedPLs : [0]) - const avgPL = _sum(accumulatedPLs) / accumulatedPLs.length - const allocation = this.perfManager.allocation - const positionSize = this.perfManager.positionSize() - const currentAllocation = this.perfManager.currentAllocation() - const availableFunds = this.perfManager.availableFunds - const equityCurve = this.perfManager.equityCurve() - const ret = this.perfManager.return() - const retPerc = this.perfManager.returnPerc() - const drawdown = this.perfManager.drawdown() - - return { - vol, - fees, - candles, - trades: trades.map(t => ({ - ...t, - date: new Date(t.mts) - })), - - nTrades, - nCandles, - nStrategyTrades, - nOpens, - nGains: gains.length, - nLosses: losses.length, - - stdDeviation, - pl, - pf: isNaN(pf) ? 0 : pf, - avgPL: isNaN(avgPL) ? 0 : avgPL, - minPL: _isNil(minPL) ? 0 : minPL, - maxPL: _isNil(maxPL) ? 0 : maxPL, - - allocation, - positionSize, - currentAllocation, - availableFunds, - equityCurve, - return: ret, - returnPerc: retPerc, - drawdown, - - strategy: { - trades: strategyTrades.map(t => ({ - ...t, - date: new Date(t.mts) - })) - } - } + return _generateStrategyResults(this.perfManager, this.strategyState, openPosition) } } diff --git a/package.json b/package.json index 776b38b..f9b4b22 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "bfx-hf-util": "git+https://github.com/bitfinexcom/bfx-hf-util.git#v1.0.12", "debug": "^4.3.3", "lodash": "^4.17.10", - "mathjs": "^10.1.0", "promise-throttle": "^1.0.0" }, "devDependencies": { From 498ef795de9d5b1a7d66538b2f74060cf91c88ba Mon Sep 17 00:00:00 2001 From: Abhishek Shrestha Date: Fri, 10 Jun 2022 15:38:51 +0545 Subject: [PATCH 2/3] strategy package fork --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f9b4b22..61994b6 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "dependencies": { "bfx-api-node-core": "^1.5.8", "bfx-api-node-util": "^1.0.2", - "bfx-hf-strategy": "git+https://github.com/bitfinexcom/bfx-hf-strategy.git#v1.3.0", + "bfx-hf-strategy": "git+https://github.com/avsek477/bfx-hf-strategy.git#generate-strategy-results", "bfx-hf-util": "git+https://github.com/bitfinexcom/bfx-hf-util.git#v1.0.12", "debug": "^4.3.3", "lodash": "^4.17.10", From fbddc32158d91feba83d35c4965218a667770b4c Mon Sep 17 00:00:00 2001 From: Tarcisio Ferraz Date: Fri, 10 Jun 2022 12:44:40 -0300 Subject: [PATCH 3/3] update version --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 61994b6..9d7d6ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bfx-hf-strategy-exec", - "version": "2.0.0", + "version": "2.0.1", "description": "Execution logic for bfx-hf-strategy", "main": "./index.js", "directories": { @@ -37,7 +37,7 @@ "dependencies": { "bfx-api-node-core": "^1.5.8", "bfx-api-node-util": "^1.0.2", - "bfx-hf-strategy": "git+https://github.com/avsek477/bfx-hf-strategy.git#generate-strategy-results", + "bfx-hf-strategy": "git+https://github.com/bitfinexcom/bfx-hf-strategy.git#v1.5.0", "bfx-hf-util": "git+https://github.com/bitfinexcom/bfx-hf-util.git#v1.0.12", "debug": "^4.3.3", "lodash": "^4.17.10",