From 583ef49f1dd1d44513d4d2abb74cf8ee33197338 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 30 Nov 2020 14:19:19 +0100 Subject: [PATCH] Session resume and first loop --- .../Superalgos/TS/Globals/ProcessVariables.js | 18 ++++++++++-------- TaskServer/LearningSession.js | 6 +++--- .../LowFrequencyTrading/TradingEngine.js | 2 +- .../LowFrequencyTrading/TradingOutput.js | 4 ++-- .../LowFrequencyTrading/TradingProcess.js | 10 +++++----- .../LowFrequencyTrading/TradingSimulation.js | 2 +- .../ReinforcementLearning/LearningProcess.js | 10 +++++----- .../MarkovDecisionProcess.js | 2 +- TaskServer/TradingSession.js | 6 +++--- 9 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Projects/Superalgos/TS/Globals/ProcessVariables.js b/Projects/Superalgos/TS/Globals/ProcessVariables.js index a46786095..9c456a9ff 100644 --- a/Projects/Superalgos/TS/Globals/ProcessVariables.js +++ b/Projects/Superalgos/TS/Globals/ProcessVariables.js @@ -16,14 +16,16 @@ exports.newSuperalgosGlobalsProcessVariables = function () { What it is stored here depends very much on what the process is about and what it needs to do. Following is a list of known properties of this object. - PROCESS_KEY This is the key used in events related to the Process - SESSION_KEY This is the key used in events related to the Session - SESSION_FOLDER_NAME This is the folder name for Session related data and logs. - MAIN_LOOP_COUNTER This is the counter of main loops for a bot process. - LOGS_TO_DELETE_QUEUE This stores the logs that need to be deleted, for auto mantainance. - SOCIAL_BOTS_MODULE This is the module that controls social bots. - IS_SESSION_STOPPING This tell us if a session is in the process of being stopped. - SESSION_STATUS This tell us the status of the session at any given time. + PROCESS_KEY This is the key used in events related to the Process + SESSION_KEY This is the key used in events related to the Session + SESSION_FOLDER_NAME This is the folder name for Session related data and logs. + MAIN_LOOP_COUNTER This is the counter of main loops for a bot process. + LOGS_TO_DELETE_QUEUE This stores the logs that need to be deleted, for auto mantainance. + SOCIAL_BOTS_MODULE This is the module that controls social bots. + SESSION_STATUS This tell us the status of the session at any given time. + IS_SESSION_STOPPING This tell us if a session is in the process of being stopped. + IS_SESSION_RESUMING This tell us if a session is resuming from the state at the previous execution. + IS_SESSION_FIRST_LOOP This tell us if we are at the execution of the first loop of the bot related to this session. */ return thisObject } \ No newline at end of file diff --git a/TaskServer/LearningSession.js b/TaskServer/LearningSession.js index b3e7610d1..5706eb102 100644 --- a/TaskServer/LearningSession.js +++ b/TaskServer/LearningSession.js @@ -83,8 +83,8 @@ exports.newLearningSession = function (processIndex, bot, parentLogger) { TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_NODE = JSON.parse(message.event.tradingEngine) bot.LEARNING_SESSION = JSON.parse(message.event.session) TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter) - bot.RESUME = false - bot.FIRST_EXECUTION = true + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING = false + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP = true bot.LEARNING_SESSION.stop = stopSession // stop function setUpSessionFolderName() @@ -144,7 +144,7 @@ exports.newLearningSession = function (processIndex, bot, parentLogger) { return } - bot.RESUME = true + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING = true TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING = false TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SOCIAL_BOTS_MODULE.sendMessage(bot.LEARNING_SESSION.type + " '" + TS.projects.superalgos.globals.taskConstants.TASK_NODE.bot.processes[processIndex].session.name + "' is resuming.") diff --git a/TaskServer/LowFrequencyTrading/TradingEngine.js b/TaskServer/LowFrequencyTrading/TradingEngine.js index a19be7dcb..5dcd894f5 100644 --- a/TaskServer/LowFrequencyTrading/TradingEngine.js +++ b/TaskServer/LowFrequencyTrading/TradingEngine.js @@ -29,7 +29,7 @@ exports.newTradingEngine = function (processIndex, bot, logger) { initializeNodeMap(tradingEngine) - if (bot.FIRST_EXECUTION === true) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true) { /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed diff --git a/TaskServer/LowFrequencyTrading/TradingOutput.js b/TaskServer/LowFrequencyTrading/TradingOutput.js index 7c92c6448..20b80b6e4 100644 --- a/TaskServer/LowFrequencyTrading/TradingOutput.js +++ b/TaskServer/LowFrequencyTrading/TradingOutput.js @@ -50,7 +50,7 @@ exports.newTradingOutput = function (processIndex, bot, logger, tradingEngineMod we are at the head of the market, because at the head of the market we need to append more data to the same output files that already exists. */ - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { await initializeOutputs() } else { if (bot.simulationState.tradingEngine.current.episode.headOfTheMarket.value === true) { @@ -66,7 +66,7 @@ exports.newTradingOutput = function (processIndex, bot, logger, tradingEngineMod head of the market. Remember that backtests finishes the session once the reach the final datetime. */ - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { await initializeOutputs() } else { await readFiles() diff --git a/TaskServer/LowFrequencyTrading/TradingProcess.js b/TaskServer/LowFrequencyTrading/TradingProcess.js index b01992dd5..57efc430c 100644 --- a/TaskServer/LowFrequencyTrading/TradingProcess.js +++ b/TaskServer/LowFrequencyTrading/TradingProcess.js @@ -83,7 +83,7 @@ if (getContextVariables() !== true) { return } - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { /* Here is where the Trading Engine and Trading Systems received are moved to the simulation state. */ @@ -95,7 +95,7 @@ tradingEngineModule.initialize() /* Initializing the Trading Process Date */ - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { /* This funcion is going to be called many times by the Trading Bot Loop. Only during the first execution and when the User is not resuming the execution @@ -290,7 +290,7 @@ if (bot.simulationState === undefined) { bot.simulationState = {} } // This should happen only when there is no status report if (thisReport.lastFile !== undefined) { - if (bot.RESUME !== true) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING !== true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining because bot has just started and resume execution was true."); } startFromBegining(); return true @@ -619,8 +619,8 @@ From here on, all other loops executions wont be the first execution and also we will consider that it is not resuming a previous execution as well. */ - bot.FIRST_EXECUTION = false - bot.RESUME = false + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP = false + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING = false } async function writeProcessFiles() { diff --git a/TaskServer/LowFrequencyTrading/TradingSimulation.js b/TaskServer/LowFrequencyTrading/TradingSimulation.js index bbb5dd352..71f28e1ef 100644 --- a/TaskServer/LowFrequencyTrading/TradingSimulation.js +++ b/TaskServer/LowFrequencyTrading/TradingSimulation.js @@ -57,7 +57,7 @@ exports.newTradingSimulation = function (processIndex, bot, logger, tradingEngin Estimation of the Initial Candle to Process in this Run. */ let initialCandle - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { /* Estimate Initial Candle based on the timeRage configured for the session. */ let firstEnd = candles[0].end let targetEnd = sessionParameters.timeRange.config.initialDatetime diff --git a/TaskServer/ReinforcementLearning/LearningProcess.js b/TaskServer/ReinforcementLearning/LearningProcess.js index 3e3c957d4..8acb8f5ab 100644 --- a/TaskServer/ReinforcementLearning/LearningProcess.js +++ b/TaskServer/ReinforcementLearning/LearningProcess.js @@ -83,7 +83,7 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { getContextVariables() - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { /* Here is where the Trading Engine and Trading Systems received are moved to the simulation state. */ @@ -95,7 +95,7 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { markovDecisionProcessModule.initialize() /* Initializing the Trading Process Date */ - if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) { /* This funcion is going to be called many times by the Trading Bot Loop. Only during the first execution and when the User is not resuming the execution @@ -290,7 +290,7 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { if (bot.simulationState === undefined) { bot.simulationState = {} } // This should happen only when there is no status report if (thisReport.lastFile !== undefined) { - if (bot.RESUME !== true) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING !== true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining because bot has just started and resume execution was true."); } startFromBegining(); return @@ -619,8 +619,8 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { From here on, all other loops executions wont be the first execution and also we will consider that it is not resuming a previous execution as well. */ - bot.FIRST_EXECUTION = false - bot.RESUME = false + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP = false + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING = false } async function writeProcessFiles() { diff --git a/TaskServer/ReinforcementLearning/MarkovDecisionProcess.js b/TaskServer/ReinforcementLearning/MarkovDecisionProcess.js index c39128afd..3366346c1 100644 --- a/TaskServer/ReinforcementLearning/MarkovDecisionProcess.js +++ b/TaskServer/ReinforcementLearning/MarkovDecisionProcess.js @@ -29,7 +29,7 @@ exports.newMarkovDecisionProcess = function newMarkovDecisionProcess(bot, logger initializeNodeMap(tradingEngine) - if (bot.FIRST_EXECUTION === true) { + if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true) { /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed diff --git a/TaskServer/TradingSession.js b/TaskServer/TradingSession.js index 0947026b7..0d0c8ce1d 100644 --- a/TaskServer/TradingSession.js +++ b/TaskServer/TradingSession.js @@ -87,8 +87,8 @@ TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_NODE = JSON.parse(message.event.tradingEngine) TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE = JSON.parse(message.event.session) TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter) - bot.RESUME = false - bot.FIRST_EXECUTION = true + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING = false + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP = true setUpSessionFolderName() @@ -147,7 +147,7 @@ return } - bot.RESUME = true + TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING = true TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING = false TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SOCIAL_BOTS_MODULE.sendMessage(TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.type + " '" + TS.projects.superalgos.globals.taskConstants.TASK_NODE.bot.processes[processIndex].session.name + "' is resuming.")