From a28b75a50bb26046cf1648058e2b5a2e96c453f4 Mon Sep 17 00:00:00 2001 From: Brett Taylor Date: Wed, 16 Jun 2021 21:03:44 -0400 Subject: [PATCH] recalc orders if data is defined --- .../Low-Frequency-Trading/APIs/ExchangeAPI.js | 7 ++ .../OrdersCalculations.js | 22 +++-- .../Low-Frequency-Trading/TradingOrders.js | 83 +++++++------------ 3 files changed, 51 insertions(+), 61 deletions(-) diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/APIs/ExchangeAPI.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/APIs/ExchangeAPI.js index 2640a9ae4..94efc1a52 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/APIs/ExchangeAPI.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/APIs/ExchangeAPI.js @@ -98,6 +98,13 @@ tradingSystem.addError([tradingSystemOrder.id, message, docs]) logError("getOrder -> Error = " + err.message); + if ( /[0-9a-z]+ NotFound/.test(err.message) ) { + logInfo("getOrder -> NotFound, so order is actually closed.") + order = { + status: 'NotFound' + } + return order + } } } diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/OrdersCalculations.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/OrdersCalculations.js index 8f8cd4da2..60b78456b 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/OrdersCalculations.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/OrdersCalculations.js @@ -55,8 +55,10 @@ exports.newFoundationsBotModulesOrdersCalculations = function (processIndex) { /* This calculation needs to happen only once, the first time the order is checked. */ if (tradingEngineOrder.orderBaseAsset.actualSize.value !== tradingEngineOrder.orderBaseAsset.actualSize.config.initialValue) { return } - /* We receive the actual size from the exchange at the order.amount field. */ - tradingEngineOrder.orderBaseAsset.actualSize.value = order.amount + if ( order.amount !== undefined) { + /* We receive the actual size from the exchange at the order.amount field. */ + tradingEngineOrder.orderBaseAsset.actualSize.value = order.amount + } recalculateActualSize() recalculateSizePlaced() @@ -122,7 +124,7 @@ exports.newFoundationsBotModulesOrdersCalculations = function (processIndex) { available, but it seems sometimes it is not. In those cases we use the price. */ tradingEngineOrder.orderStatistics.actualRate.value = order.average - } else { + } else if (order.price !== undefined) { /* We use the order.price when the average is not available. */ @@ -260,8 +262,10 @@ exports.newFoundationsBotModulesOrdersCalculations = function (processIndex) { We know this field will go up until reaching the Actual Size. Once it reaches that number the order will be 100% filled. */ - tradingEngineOrder.orderStatistics.percentageFilled.value = order.filled * 100 / tradingEngineOrder.orderBaseAsset.actualSize.value - tradingEngineOrder.orderStatistics.percentageFilled.value = TS.projects.foundations.utilities.miscellaneousFunctions.truncateToThisPrecision(tradingEngineOrder.orderStatistics.percentageFilled.value, 10) + if (order.filled !== undefined) { + tradingEngineOrder.orderStatistics.percentageFilled.value = order.filled * 100 / tradingEngineOrder.orderBaseAsset.actualSize.value + tradingEngineOrder.orderStatistics.percentageFilled.value = TS.projects.foundations.utilities.miscellaneousFunctions.truncateToThisPrecision(tradingEngineOrder.orderStatistics.percentageFilled.value, 10) + } } async function feesPaidCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) { @@ -303,8 +307,10 @@ exports.newFoundationsBotModulesOrdersCalculations = function (processIndex) { take it from there for our Order Base Asset. The amount in order.filled does not reflect the Fees Paid, even if we are paying the fees in Base Asset. */ - tradingEngineOrder.orderBaseAsset.sizeFilled.value = order.filled - tradingEngineOrder.orderBaseAsset.sizeFilled.value = TS.projects.foundations.utilities.miscellaneousFunctions.truncateToThisPrecision(tradingEngineOrder.orderBaseAsset.sizeFilled.value, 10) + if (order.filled !== undefined) { + tradingEngineOrder.orderBaseAsset.sizeFilled.value = order.filled + tradingEngineOrder.orderBaseAsset.sizeFilled.value = TS.projects.foundations.utilities.miscellaneousFunctions.truncateToThisPrecision(tradingEngineOrder.orderBaseAsset.sizeFilled.value, 10) + } /* For Quoted Asset, CCXT does not return any field with the size filled, so we need to calculate that by ourselves. @@ -344,4 +350,4 @@ exports.newFoundationsBotModulesOrdersCalculations = function (processIndex) { } } } -} \ No newline at end of file +} diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js index 57939aaa6..fe9107dba 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js @@ -664,59 +664,6 @@ exports.newFoundationsBotModulesTradingOrders = function (processIndex) { return false } - if (order === null) { - /* - Some exchanges, like Coinbase Pro, deletes orders after being cancelled, and when we request information - about them, it returns null. We will interprate this as ORDER NOT FOUND. - */ - message = 'Order Not Found at the Exchange' - docs = { - project: 'Foundations', - category: 'Topic', - type: 'TS LF Trading Bot Warning - ' + message, - placeholder: {} - } - - tradingSystem.addWarning( - [ - [tradingSystemOrder.id, tradingEngineOrder.id], - message, - docs - ] - ) - - /* Close this Order */ - tradingEngineOrder.status.value = 'Closed' - /* - We must be carefull here not to overide an already defined exitType. It can happen - for instance that the order was cancellerd from the but veryfing the cancellation - was not possible because of a connection to the exchange problem. In that case - the exit type was defined but the order was kept open until the verification could be done. - */ - if (tradingEngineOrder.exitType.value === tradingEngineOrder.exitType.config.initialValue) { - tradingEngineOrder.exitType.value = 'Not Found at the Exchange' - } - /* Initialize this */ - tradingEngine.tradingCurrent.tradingEpisode.distanceToTradingEvent.closeOrder.value = 1 - - await updateEndsWithCycle(tradingEngineOrder) - - let message = "Order Closed" - let docs = { - project: 'Foundations', - category: 'Topic', - type: 'TS LF Trading Bot Info - ' + message, - placeholder: {} - } - contextInfo = { - exitType: tradingEngineOrder.exitType.value - } - TS.projects.education.utilities.docsFunctions.buildPlaceholder(docs, undefined, undefined, undefined, undefined, undefined, contextInfo) - - tradingSystem.addInfo([tradingSystemOrder.id, message, docs]) - return true - } - const AT_EXCHANGE_STATUS = { OPEN: 'open', CLOSED: 'closed', @@ -1168,6 +1115,36 @@ exports.newFoundationsBotModulesTradingOrders = function (processIndex) { ) return false } + if (order.status === 'NotFound') { + /* + Some exchanges, like Coinbase Pro, deletes orders after being cancelled, and when we request information + about them, it returns null. We will interprate this as ORDER NOT FOUND. + */ + const message = 'Order Not Found at the Exchange' + let docs = { + project: 'Superalgos', + category: 'Topic', + type: 'TS LF Trading Bot Warning - ' + message, + placeholder: {} + } + + tradingSystem.addWarning( + [ + [tradingSystemOrder.id, tradingEngineOrder.id], + message, + docs + ] + ) + /* + We must be carefull here not to overide an already defined exitType. It can happen + for instance that the order was cancellerd from the but veryfing the cancellation + was not possible because of a connection to the exchange problem. In that case + the exit type was defined but the order was kept open until the verification could be done. + */ + if (tradingEngineOrder.exitType.value === tradingEngineOrder.exitType.config.initialValue) { + tradingEngineOrder.exitType.value = 'Not Found at the Exchange' + } + } /* Close this Order. Note that we are not closing the order until we have the exchange