From bfc83b2e32d712fc79dfa275cb24032411bf65fc Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 8 Apr 2021 23:32:35 +0200 Subject: [PATCH] Coninbase Pro handling of cancelled orders --- ...rning-order-not-found-at-the-exchange.json | 49 +++++++++++++++ .../Low-Frequency-Trading/TradingOrders.js | 60 ++++++++++++++++++- 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 Projects/Superalgos/Schemas/Docs-Topics/ts-lf-trading-bot-warnings-016-ts-lf-trading-bot-warning-order-not-found-at-the-exchange.json diff --git a/Projects/Superalgos/Schemas/Docs-Topics/ts-lf-trading-bot-warnings-016-ts-lf-trading-bot-warning-order-not-found-at-the-exchange.json b/Projects/Superalgos/Schemas/Docs-Topics/ts-lf-trading-bot-warnings-016-ts-lf-trading-bot-warning-order-not-found-at-the-exchange.json new file mode 100644 index 000000000..3eea20702 --- /dev/null +++ b/Projects/Superalgos/Schemas/Docs-Topics/ts-lf-trading-bot-warnings-016-ts-lf-trading-bot-warning-order-not-found-at-the-exchange.json @@ -0,0 +1,49 @@ +{ + "topic": "TS LF Trading Bot Warnings", + "pageNumber": "15", + "type": "TS LF Trading Bot Warning - Order Not Found at the Exchange", + "definition": { + "text": "The exchange returned null while checking an order. This usually happens at exchanges like Coinbase Pro that after an order cancellation the exchange deletes the order instead of changing it's status to cancelled.", + "updated": 1611922128136 + }, + "paragraphs": [ + { + "style": "Success", + "text": "This is not an error and may be normal behavior. The trading bot created this warning to let you know that this happened so that you may keep track of all the details of the trade should you need to. This page provides information that should help you understand the context of the warning.", + "updated": 1611922148396 + }, + { + "style": "Warning", + "text": "Order Status Not Sync With Exchange" + }, + { + "style": "Title", + "text": "Context Info" + }, + { + "style": "Text", + "text": "Find below the values of variables relevant to the warning at the time the warning was created. They may prove of use to understand what happened." + }, + { + "style": "Placeholder", + "text": "contextInfo" + }, + { + "style": "Title", + "text": "Learn More" + }, + { + "style": "Text", + "text": "To learn more about this topic, please read the following page:" + }, + { + "style": "List", + "text": "Checking Exchange Events", + "updated": 1611921970650 + }, + { + "style": "List", + "text": "Syncing With the Exchange" + } + ] +} \ No newline at end of file diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js index 94393fcd9..380a27253 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js @@ -641,10 +641,13 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex) { let order = await exchangeAPIModuleObject.getOrder(tradingSystemOrder, tradingEngineOrder) + let message + let docs + if (order === undefined) { - const message = 'Order Status Not Sync With Exchange' - let docs = { + message = 'Order Status Not Sync With Exchange' + docs = { project: 'Superalgos', category: 'Topic', type: 'TS LF Trading Bot Warning - ' + message, @@ -661,6 +664,59 @@ exports.newSuperalgosBotModulesTradingOrders = 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: 'Superalgos', + 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: 'Superalgos', + category: 'Topic', + type: 'TS LF Trading Bot Info - ' + message, + placeholder: {} + } + contextInfo = { + exitType: tradingEngineOrder.exitType.value + } + TS.projects.superalgos.utilities.docsFunctions.buildPlaceholder(docs, undefined, undefined, undefined, undefined, undefined, contextInfo) + + tradingSystem.addInfo([tradingSystemOrder.id, message, docs]) + return + } + const AT_EXCHANGE_STATUS = { OPEN: 'open', CLOSED: 'closed',