From a0745fe1b955d87d4c00b3b96399fcb90f96fe09 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 9 Jun 2020 11:36:27 +0200 Subject: [PATCH] Code Cleaning --- ExchangeAPI.js | 24 ++--- Trading/Snapshots.js | 5 + Trading/TradingBot.js | 17 +-- Trading/TradingProcess.js | 6 +- Trading/TradingSimulation.js | 6 +- TradingBotProcessMainLoop.js | 204 ++++------------------------------- 6 files changed, 41 insertions(+), 221 deletions(-) diff --git a/ExchangeAPI.js b/ExchangeAPI.js index 54c3c52e7..617b5a5a8 100644 --- a/ExchangeAPI.js +++ b/ExchangeAPI.js @@ -1,5 +1,5 @@ exports.newExchangeAPI = function newExchangeAPI(logger, BOT) { - + let MODULE_NAME = "Exchange API"; let LOG_INFO = true; let apiClient; @@ -22,8 +22,7 @@ return thisObject; - function initialize(callBackFunction) { - try { + function initialize() { exchangeId = bot.exchange.toLowerCase() let key = process.env.KEY @@ -41,18 +40,11 @@ verbose: false, options: options } - exchange = new exchangeClass(exchangeConstructorParams) - - callBackFunction(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logError("initialize -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } } function getMaxDecimalPositions() { - return 8 + return 8 } async function getTicker(market, callBackFunction) { @@ -62,13 +54,13 @@ let ticker if (exchange.has['fetchTicker']) { - ticker = await (exchange.fetchTicker(symbol)) + ticker = await (exchange.fetchTicker(symbol)) callBackFunction(global.DEFAULT_OK_RESPONSE, ticker) } else { logError("getTicker -> Exchange does not support fetchTicker command."); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } - + } catch (err) { logError("getTicker -> err = " + err.message); callBackFunction(global.DEFAULT_FAIL_RESPONSE); @@ -83,7 +75,7 @@ let openOrders if (exchange.has['fetchOpenOrders']) { - openOrders = await(exchange.fetchOpenOrders (symbol)) + openOrders = await (exchange.fetchOpenOrders(symbol)) callBackFunction(global.DEFAULT_OK_RESPONSE, openOrders) } else { logError("getOpenOrders -> Exchange does not support fetchOpenOrders command."); @@ -116,7 +108,7 @@ } } - async function createOrder(market, side, price, cost, amount, callBackFunction) { + async function createOrder(market, side, price, cost, amount, callBackFunction) { try { logInfo("createOrder -> Entering function."); @@ -137,7 +129,7 @@ let order if (exchange.has['createOrder']) { - order = await(exchange.createOrder(symbol, type, side, amount)) + order = await (exchange.createOrder(symbol, type, side, amount)) callBackFunction(global.DEFAULT_OK_RESPONSE, order) } else { logError("createOrder -> Exchange does not support createOrder command."); diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js index 49faa2995..a8aa133f8 100644 --- a/Trading/Snapshots.js +++ b/Trading/Snapshots.js @@ -11,6 +11,11 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let tradingEngine + const SNAPSHOTS_FOLDER_NAME = 'Snapshots' + + let snapshotHeaders + let triggerOnSnapshot + let takePositionSnapshot /* Snapshots of Trigger On and Take Positions */ let snapshots = { diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js index 6a2539469..b3eacbdef 100644 --- a/Trading/TradingBot.js +++ b/Trading/TradingBot.js @@ -14,8 +14,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR const COMMONS = require('../Commons.js') let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) - let exchangeAPI - return thisObject function finalize() { @@ -25,14 +23,11 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR commons = undefined } - function initialize(pExchangeAPI, callBackFunction) { + function initialize(callBackFunction) { try { logger.fileName = MODULE_NAME logger.initialize() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> Entering function.') } - - exchangeAPI = pExchangeAPI callBackFunction(global.DEFAULT_OK_RESPONSE) } catch (err) { logger.write(MODULE_NAME, '[ERROR] initialize -> err = ' + err.stack) @@ -42,7 +37,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { try { - bot.processingDailyFiles if (timeFrame > global.dailyFilePeriods[0][0]) { bot.processingDailyFiles = false @@ -104,20 +98,14 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } /* Simulation */ - const SNAPSHOTS_FOLDER_NAME = 'Snapshots' const TRADING_SIMULATION = require('./TradingSimulation.js') let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) + let market = bot.market let totalFilesRead = 0 let totalFilesWritten = 0 let outputDatasetsMap = new Map() - let snapshotHeaders - let triggerOnSnapshot - let takePositionSnapshot - - let tradingSystem = {} - if (bot.RESUME !== true) { simulationState.tradingEngine = bot.SESSION.tradingEngine simulationState.tradingSystem = bot.SESSION.tradingSystem @@ -214,7 +202,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR chart, dataDependencies, simulationState, - exchangeAPI, outputDatasets, outputDatasetsMap, writeFiles, diff --git a/Trading/TradingProcess.js b/Trading/TradingProcess.js index 725d66510..1adfa6288 100644 --- a/Trading/TradingProcess.js +++ b/Trading/TradingProcess.js @@ -32,9 +32,8 @@ return thisObject; - function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, exchangeAPI, callBackFunction) { + function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { try { - logger.fileName = MODULE_NAME; logger.initialize(); @@ -43,9 +42,8 @@ processConfig = pProcessConfig; let USER_BOT_MODULE = require("./TradingBot") - botInstance = USER_BOT_MODULE.newTradingBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(exchangeAPI, callBackFunction); + botInstance.initialize(callBackFunction); } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 3161270a9..feaf5451e 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -20,7 +20,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT chart, dataDependencies, simulationState, - exchangeAPI, outputDatasets, outputDatasetsMap, callback, @@ -49,6 +48,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let closePositionNow = false /* These are the Modules we will need to run the Simulation */ + + const EXCHANGE_API = require(ROOT_DIR + 'ExchangeAPI'); + exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); + exchangeAPI.initialize(); + const TRADING_ENGINE_MODULE = require('./TradingEngine.js') let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index ec15efa4d..db448c00e 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -31,14 +31,9 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - /* This function is exactly the same in the 3 modules representing the 2 different bot types loops. */ - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - processConfig = pProcessConfig; - if (bot.definedByUI === true) { /* The code of the bot is defined at the UI. No need to load a file with the code. */ session.initialize(processConfig, onSessionInitialized) @@ -57,7 +52,6 @@ fileStorage.getTextFile(filePath, onBotDownloaded); function onBotDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); @@ -75,7 +69,6 @@ fileStorage.getTextFile(filePath, onCommonsDownloaded); function onCommonsDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -99,11 +92,7 @@ } function run(callBackFunction) { - try { - - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - /* Some initial values*/ bot.enableCheckLoopHealth = true; let fixedTimeLoopIntervalHandle; @@ -112,9 +101,7 @@ loop(); function loop() { - try { - processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { str = str.toString(); @@ -133,8 +120,6 @@ bot.loopCounter++; bot.loopStartTime = new Date().valueOf(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - let nextWaitTime; /* Heartbeats sent to the UI */ @@ -145,7 +130,6 @@ /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const EXCHANGE_API = require(ROOT_DIR + 'ExchangeAPI'); const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); @@ -198,21 +182,12 @@ initializeProcessExecutionEvents(); function initializeProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - processExecutionEvents.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); @@ -273,19 +248,11 @@ } function startProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - processExecutionEvents.start(onStarted); function onStarted(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); @@ -352,21 +319,12 @@ } function initializeStatusDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - statusDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); @@ -428,25 +386,33 @@ } function initializeDataDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } - dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); - dataDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } - initializeExchangeAPI(); + switch (processConfig.framework.name) { + case 'Trading-Process': { + processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); + intitializeProcessFramework(); + break; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } return; } case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. @@ -503,114 +469,12 @@ } } - function initializeExchangeAPI() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> Entering function."); } - - exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); - - exchangeAPI.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeContext -> onInizialized -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> onInizialized -> Execution finished well."); } - - switch (processConfig.framework.name) { - case 'Trading-Process': { - processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); - intitializeProcessFramework(); - break; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Process Framework not Supported."); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeExchangeAPI -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeContext -> onInizialized -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - function intitializeProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } - - processFramework.initialize(processConfig, statusDependencies, dataDependencies, exchangeAPI, onInizialized); + processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); @@ -687,18 +551,11 @@ } function startProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } - processFramework.start(onFinished); function onFinished(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } processFramework.finalize() processFramework = undefined dataDependencies.finalize() @@ -795,11 +652,7 @@ } function finishProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - processExecutionEvents.finish(onFinished); function onFinished(err) { @@ -807,8 +660,6 @@ processExecutionEvents.finalize() processExecutionEvents = undefined - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); @@ -870,22 +721,15 @@ } function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ - processHeartBeat() /* Here we check if we must stop the loop gracefully. */ - shallWeStop(onStop, onContinue); function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } if (global.WRITE_LOGS_TO_FILES === 'true') { @@ -896,15 +740,10 @@ processStopped() callBackFunction(global.DEFAULT_OK_RESPONSE); return; - } function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - switch (nextWaitTime) { case 'Normal': { let waitTime @@ -990,13 +829,8 @@ } function shallWeStop(stopCallBack, continueCallBack) { - try { - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - if (!global.STOP_TASK_GRACEFULLY) { continueCallBack(); } else {