From 9b7b3968c55640c5b50947ed5076eb55d8e80d2d Mon Sep 17 00:00:00 2001 From: uhliksk Date: Mon, 8 Aug 2022 16:27:40 +0200 Subject: [PATCH] fix: exceeding max open trades --- .../trailingTrade/step/determine-action.js | 41 +------------------ .../trailingTrade/step/handle-open-orders.js | 14 ++++++- app/cronjob/trailingTradeHelper/common.js | 35 +++++++++++++++- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/app/cronjob/trailingTrade/step/determine-action.js b/app/cronjob/trailingTrade/step/determine-action.js index a2fcb740d..4a6e9addd 100644 --- a/app/cronjob/trailingTrade/step/determine-action.js +++ b/app/cronjob/trailingTrade/step/determine-action.js @@ -5,7 +5,7 @@ const { slack } = require('../../../helpers'); const { isActionDisabled, getNumberOfBuyOpenOrders, - getNumberOfOpenTrades, + isExceedingMaxOpenTrades, getAPILimit } = require('../../trailingTradeHelper/common'); const { getGridTradeOrder } = require('../../trailingTradeHelper/order'); @@ -122,45 +122,6 @@ const isExceedingMaxBuyOpenOrders = async (logger, data) => { return false; }; -/** - * Check whether max number of open trades has reached - * - * @param {*} logger - * @param {*} data - * @returns - */ -const isExceedingMaxOpenTrades = async (logger, data) => { - const { - symbolConfiguration: { - botOptions: { - orderLimit: { - enabled: orderLimitEnabled, - maxOpenTrades: orderLimitMaxOpenTrades - } - } - }, - sell: { lastBuyPrice } - } = data; - - if (orderLimitEnabled === false) { - return false; - } - - let currentOpenTrades = await getNumberOfOpenTrades(logger); - - // If the last buy price is recorded, this is one of open trades. - // Deduct 1 from the current open trades and calculate it. - if (lastBuyPrice) { - currentOpenTrades -= 1; - } - - if (currentOpenTrades >= orderLimitMaxOpenTrades) { - return true; - } - - return false; -}; - /** * Set buy action and message * diff --git a/app/cronjob/trailingTrade/step/handle-open-orders.js b/app/cronjob/trailingTrade/step/handle-open-orders.js index e97563be7..1cdc590ac 100644 --- a/app/cronjob/trailingTrade/step/handle-open-orders.js +++ b/app/cronjob/trailingTrade/step/handle-open-orders.js @@ -7,7 +7,8 @@ const { getAccountInfo, updateAccountInfo, saveOverrideAction, - getAndCacheOpenOrdersForSymbol + getAndCacheOpenOrdersForSymbol, + isExceedingMaxOpenTrades } = require('../../trailingTradeHelper/common'); /** @@ -156,6 +157,17 @@ const execute = async (logger, rawData) => { moment().toISOString() ); } + } else if (await isExceedingMaxOpenTrades(logger, data)) { + // Cancel the initial buy order if max. open trades exceeded + data.action = 'buy-order-cancelled'; + logger.info( + { data, saveLog: true }, + `The current number of open trades has reached the maximum number of open trades. ` + + `The buy order will be cancelled.` + ); + + // Cancel current order + await cancelOrder(logger, symbol, order); } else { logger.info( { stopPrice: order.stopPrice, buyLimitPrice }, diff --git a/app/cronjob/trailingTradeHelper/common.js b/app/cronjob/trailingTradeHelper/common.js index f9fd03172..a1be3cbc2 100644 --- a/app/cronjob/trailingTradeHelper/common.js +++ b/app/cronjob/trailingTradeHelper/common.js @@ -1068,6 +1068,38 @@ const getCacheTrailingTradeQuoteEstimates = logger => } ]); +/** + * Check whether max number of open trades has reached + * + * @param {*} logger + * @param {*} data + * @returns + */ +const isExceedingMaxOpenTrades = async (logger, data) => { + const { + symbolConfiguration: { + botOptions: { + orderLimit: { + enabled: orderLimitEnabled, + maxOpenTrades: orderLimitMaxOpenTrades + } + } + }, + sell: { lastBuyPrice } + } = data; + + if (orderLimitEnabled === false) { + return false; + } + + // If the last buy price is recorded, this is one of open trades. + if (lastBuyPrice) { + return false; + } + + return (await getNumberOfOpenTrades(logger)) >= orderLimitMaxOpenTrades; +}; + module.exports = { cacheExchangeSymbols, getCachedExchangeSymbols, @@ -1107,5 +1139,6 @@ module.exports = { updateAccountInfo, getCacheTrailingTradeSymbols, getCacheTrailingTradeTotalProfitAndLoss, - getCacheTrailingTradeQuoteEstimates + getCacheTrailingTradeQuoteEstimates, + isExceedingMaxOpenTrades };