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 ad7848639..ca532c749 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 @@ -1,7 +1,7 @@ -exports.newFoundationsBotModulesExchangeAPI = function (processIndex) { +exports.newSuperalgosBotModulesExchangeAPI = function (processIndex) { let MODULE_NAME = "Exchange API"; - + let thisObject = { getOrder: getOrder, createOrder: createOrder, @@ -9,33 +9,34 @@ initialize: initialize, finalize: finalize }; - + let tradingSystem - + let exchangeId let options = {} let exchange - + const ccxt = require('ccxt') - + return thisObject; - + function initialize() { - tradingSystem = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingSystem - - exchangeId = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName - + tradingSystem = TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingSystem + + exchangeId = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + let key let secret let uid let password - - if (TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference !== undefined) { - if (TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference.referenceParent !== undefined) { - key = TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.codeName - secret = TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.secret - uid = TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.uid - password = TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.password + let sandBox = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.sandbox || false; + + if (TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference !== undefined) { + if (TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference.referenceParent !== undefined) { + key = TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.codeName + secret = TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.secret + uid = TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.uid + password = TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference.referenceParent.config.password } } const exchangeClass = ccxt[exchangeId] @@ -56,42 +57,41 @@ exchange.setSandboxMode(sandBox); } console.log('Sandbox mode = ' + sandBox); - // uncomment the following line if you want to log the exchange api being used // console.log(exchange.urls.api); } - + function finalize() { tradingSystem = undefined exchangeId = undefined options = undefined exchange = undefined } - + async function getOrder(tradingSystemOrder, tradingEngineOrder) { - + let orderId = tradingEngineOrder.exchangeId.value - const symbol = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName - + const symbol = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName + /* Basic Logging */ logInfo("getOrder -> orderId = " + orderId) logInfo("getOrder -> symbol = " + symbol) - + /* Basic Validations */ if (exchange.has['fetchOrder'] === false) { logError("getOrder -> Exchange does not support fetchOrder command."); return } - + try { let order = await exchange.fetchOrder(orderId, symbol) - + logInfo("getOrder -> Response from the Exchange: " + JSON.stringify(order)); return order } catch (err) { const message = 'Get Order Unexpected Error' let docs = { - project: 'Foundations', + project: 'Superalgos', category: 'Topic', type: 'TS LF Trading Bot Error - ' + message, placeholder: {} @@ -100,11 +100,11 @@ symbol: symbol, orderId: orderId } - - TS.projects.education.utilities.docsFunctions.buildPlaceholder(docs, err, tradingSystemOrder.name, undefined, undefined, undefined, contextInfo) - + + TS.projects.superalgos.utilities.docsFunctions.buildPlaceholder(docs, err, tradingSystemOrder.name, undefined, undefined, undefined, contextInfo) + 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.") @@ -115,15 +115,15 @@ } } } - + async function createOrder(tradingSystemOrder, tradingEngineOrder) { - + let price = tradingEngineOrder.rate.value // CCXT: how much quote currency you are willing to pay for a trade lot of base currency (for limit orders only) let type // CCXT: a string literal type of order, ccxt currently unifies market and limit orders only let side // CCXT: a string literal for the direction of your order, buy or sell - let symbol = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName // CCXT: a string literal symbol of the market you wish to trade on, like BTC/USD, ZEC/ETH, DOGE/DASH, etc + let symbol = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName // CCXT: a string literal symbol of the market you wish to trade on, like BTC/USD, ZEC/ETH, DOGE/DASH, etc let amount = tradingEngineOrder.orderBaseAsset.size.value // CCXT: how much of currency you want to trade, in Base Asset. - + switch (tradingSystemOrder.type) { case 'Market Buy Order': { type = 'market' @@ -146,14 +146,14 @@ break } } - + /* Basic Logging */ logInfo("createOrder -> symbol = " + symbol); logInfo("createOrder -> side = " + side); logInfo("createOrder -> type = " + type); logInfo("createOrder -> amount = " + amount); logInfo("createOrder -> price = " + price); - + /* Basic Validations */ if (side !== "buy" && side !== "sell") { logError("createOrder -> side must be either 'buy' or 'sell'."); @@ -163,17 +163,17 @@ logError("createOrder -> Exchange does not support createOrder command."); return } - + try { let order = await (exchange.createOrder(symbol, type, side, amount, price)) - + logInfo("createOrder -> Response from the Exchange: " + JSON.stringify(order)); return order.id } catch (err) { - - let exchangeApiKey = TS.projects.foundations.globals.taskConstants.TASK_NODE.keyReference.referenceParent + + let exchangeApiKey = TS.projects.superalgos.globals.taskConstants.TASK_NODE.keyReference.referenceParent let message = 'Create Order Unexpected Error' - + logError("createOrder -> Error = " + err.message) logError("createOrder -> symbol = " + symbol); logError("createOrder -> side = " + side); @@ -187,7 +187,7 @@ ) { logError('The exchange says the API key provided is not good or it is not in the correct format. This is what you are using:') errorFooter() - + message = 'Invalid Exchange API Key' } if ( @@ -196,17 +196,17 @@ ) { logError('The exchange says the secret provided is not good, incorrect or not in the correct format. This is what you are using:') errorFooter() - + message = 'Invalid Exchange API Secret' } - + let docs = { - project: 'Foundations', + project: 'Superalgos', category: 'Topic', type: 'TS LF Trading Bot Error - ' + message, placeholder: {} } - + let contextInfo = { symbol: symbol, side: side, @@ -218,10 +218,10 @@ keyLength: exchangeApiKey.config.codeName.length, secretLength: exchangeApiKey.config.secret.length } - TS.projects.education.utilities.docsFunctions.buildPlaceholder(docs, err, exchangeApiKey.name, undefined, exchangeApiKey.config, undefined, contextInfo) - + TS.projects.superalgos.utilities.docsFunctions.buildPlaceholder(docs, err, exchangeApiKey.name, undefined, exchangeApiKey.config, undefined, contextInfo) + tradingSystem.addError([exchangeApiKey.id, message, docs]) - + function errorFooter() { logError('key: ->' + exchangeApiKey.config.codeName + '<-') logError('secret: ->' + exchangeApiKey.config.secret + '<-') @@ -232,31 +232,31 @@ } } } - + async function cancelOrder(tradingSystemOrder, tradingEngineOrder) { - + let orderId = tradingEngineOrder.exchangeId.value - + /* Basic Logging */ logInfo("cancelOrder -> Entering function. orderId = " + orderId); - - const symbol = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName - + + const symbol = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName + /* Basic Validations */ if (exchange.has['fetchOrder'] === false) { logError("cancelOrder -> Exchange does not support fetchOrder command."); return } - + try { let order = await exchange.cancelOrder(orderId, symbol) - + logInfo("cancelOrder -> Response from the Exchange: " + JSON.stringify(order)); return true } catch (err) { const message = 'Cancel Order Unexpected Error' let docs = { - project: 'Foundations', + project: 'Superalgos', category: 'Topic', type: 'TS LF Trading Bot Error - ' + message, placeholder: {} @@ -265,19 +265,20 @@ symbol: symbol, orderId: orderId } - - TS.projects.education.utilities.docsFunctions.buildPlaceholder(docs, err, tradingSystemOrder.name, undefined, undefined, undefined, contextInfo) - + + TS.projects.superalgos.utilities.docsFunctions.buildPlaceholder(docs, err, tradingSystemOrder.name, undefined, undefined, undefined, contextInfo) + tradingSystem.addError([tradingSystemOrder.id, message, docs]) logError("cancelOrder -> Error = " + err.message); } } - + function logInfo(message) { - TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[INFO] ' + message) + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[INFO] ' + message) } - + function logError(message) { - TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[ERROR] ' + message) + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[ERROR] ' + message) } -}; + }; + \ No newline at end of file