Skip to content

Commit

Permalink
fix: exceeding max open trades
Browse files Browse the repository at this point in the history
  • Loading branch information
uhliksk committed Aug 8, 2022
1 parent 8160e25 commit 9b7b396
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
41 changes: 1 addition & 40 deletions app/cronjob/trailingTrade/step/determine-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { slack } = require('../../../helpers');
const {
isActionDisabled,
getNumberOfBuyOpenOrders,
getNumberOfOpenTrades,
isExceedingMaxOpenTrades,
getAPILimit
} = require('../../trailingTradeHelper/common');
const { getGridTradeOrder } = require('../../trailingTradeHelper/order');
Expand Down Expand Up @@ -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
*
Expand Down
14 changes: 13 additions & 1 deletion app/cronjob/trailingTrade/step/handle-open-orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {
getAccountInfo,
updateAccountInfo,
saveOverrideAction,
getAndCacheOpenOrdersForSymbol
getAndCacheOpenOrdersForSymbol,
isExceedingMaxOpenTrades
} = require('../../trailingTradeHelper/common');

/**
Expand Down Expand Up @@ -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 },
Expand Down
35 changes: 34 additions & 1 deletion app/cronjob/trailingTradeHelper/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1107,5 +1139,6 @@ module.exports = {
updateAccountInfo,
getCacheTrailingTradeSymbols,
getCacheTrailingTradeTotalProfitAndLoss,
getCacheTrailingTradeQuoteEstimates
getCacheTrailingTradeQuoteEstimates,
isExceedingMaxOpenTrades
};

0 comments on commit 9b7b396

Please sign in to comment.