From 0bb9208d66ed65b5e2c8bf1f03096d88901b18be Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 30 Nov 2020 15:57:27 +0100 Subject: [PATCH] Session Heart Beat --- .../TS/Function-Libraries/SessionFunctions.js | 18 ++++++++++++++ .../LowFrequencyTrading/TradingProcess.js | 24 +++++++++---------- .../ReinforcementLearning/LearningProcess.js | 24 +++++++++---------- TaskServer/TradingSession.js | 18 -------------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Projects/Superalgos/TS/Function-Libraries/SessionFunctions.js b/Projects/Superalgos/TS/Function-Libraries/SessionFunctions.js index 0b0527987..3351d938c 100644 --- a/Projects/Superalgos/TS/Function-Libraries/SessionFunctions.js +++ b/Projects/Superalgos/TS/Function-Libraries/SessionFunctions.js @@ -1,6 +1,7 @@ exports.newSuperalgosFunctionLibrariesSessionFunctions = function () { let thisObject = { + sessionHeartBeat: sessionHeartBeat, stopSession: stopSession, emitSessionStatus: emitSessionStatus, finalizeSessions: finalizeSessions, @@ -11,6 +12,23 @@ exports.newSuperalgosFunctionLibrariesSessionFunctions = function () { return thisObject + function sessionHeartBeat(processIndex, logger, processingDate, percentage, status) { + let event = { + seconds: (new Date()).getSeconds(), + processingDate: processingDate, + percentage: percentage, + status: status + } + global.EVENT_SERVER_CLIENT_MODULE.raiseEvent(TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_KEY, 'Heartbeat', event) + + if (TS.projects.superalgos.globals.taskVariables.IS_TASK_STOPPING === true) { + TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING = true + if (logger !== undefined) { + logger.write(MODULE_NAME, '[IMPORTANT] sessionHeartBeat -> Stopping the Session now. ') + } + } + } + function stopSession(processIndex, commandOrigin) { 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 stopping " + commandOrigin) diff --git a/TaskServer/LowFrequencyTrading/TradingProcess.js b/TaskServer/LowFrequencyTrading/TradingProcess.js index 1f1c6a187..a3afb56ad 100644 --- a/TaskServer/LowFrequencyTrading/TradingProcess.js +++ b/TaskServer/LowFrequencyTrading/TradingProcess.js @@ -119,7 +119,7 @@ await processSingleFiles() if (await processMarketFiles() === false) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be run') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be run') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } @@ -141,17 +141,17 @@ With all the indicators data files loaded, we will build the chart object data structure that will be used in user-defied conditions and formulas. */ - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waking up') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waking up') buildCharts(chart) if (checkThereAreCandles(chart) === true) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Running') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Running') await generateOutput(chart) - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Saving') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Saving') await writeProcessFiles() - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Sleeping') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Sleeping') } else { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be up to date. No candles found at.') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be up to date. No candles found at.') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } @@ -171,9 +171,9 @@ if (checkStopTaskGracefully() === false) { break } if (checkStopProcessing() === false) { break } - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waking up') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waking up') if (await processDailyFiles() === false) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be run') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be run') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } @@ -186,13 +186,13 @@ The process of generating the output includes the trading simulation. */ if (checkThereAreCandles(chart) === true) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Running') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Running') await generateOutput(chart) - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Saving') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Saving') await writeProcessFiles() - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Sleeping') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Sleeping') } else { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be up to date') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be up to date') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } diff --git a/TaskServer/ReinforcementLearning/LearningProcess.js b/TaskServer/ReinforcementLearning/LearningProcess.js index baf4f7f47..841692fd3 100644 --- a/TaskServer/ReinforcementLearning/LearningProcess.js +++ b/TaskServer/ReinforcementLearning/LearningProcess.js @@ -119,7 +119,7 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { await processSingleFiles() if (await processMarketFiles() === false) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be run') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be run') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } @@ -141,17 +141,17 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { With all the indicators data files loaded, we will build the chart object data structure that will be used in user-defied conditions and formulas. */ - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waking up') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waking up') buildCharts(chart) if (checkThereAreCandles(chart) === true) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Running') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Running') await generateOutput(chart) - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Saving') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Saving') await writeProcessFiles() - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Sleeping') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Sleeping') } else { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be up to date. No candles found at.') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be up to date. No candles found at.') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } @@ -171,9 +171,9 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { if (checkStopTaskGracefully() === false) { break } if (checkStopProcessing() === false) { break } - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waking up') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waking up') if (await processDailyFiles() === false) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be run') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be run') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } @@ -186,13 +186,13 @@ exports.newLearningProcess = function (processIndex, bot, logger, UTILITIES) { The process of generating the output includes the trading simulation. */ if (checkThereAreCandles(chart) === true) { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Running') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Running') await generateOutput(chart) - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Saving') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Saving') await writeProcessFiles() - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Sleeping') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Sleeping') } else { - bot.TRADING_SESSIONHeartBeat(undefined, undefined, 'Waiting for Data Mining to be up to date') + TS.projects.superalgos.functionLibraries.sessionFunctions.sessionHeartBeat(processIndex, logger, undefined, undefined, 'Waiting for Data Mining to be up to date') callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) return } diff --git a/TaskServer/TradingSession.js b/TaskServer/TradingSession.js index 2b9133ff6..539dc1069 100644 --- a/TaskServer/TradingSession.js +++ b/TaskServer/TradingSession.js @@ -51,9 +51,6 @@ global.EVENT_SERVER_CLIENT_MODULE.listenToEvent(TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_KEY, 'Stop Trading Session', undefined, TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_KEY, undefined, onSessionStop) global.EVENT_SERVER_CLIENT_MODULE.listenToEvent(TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_KEY, 'Resume Trading Session', undefined, TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_KEY, undefined, onSessionResume) - /* Heartbeats sent to the UI */ - bot.TRADING_SESSIONHeartBeat = sessionHeartBeat - callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE) return @@ -453,21 +450,6 @@ } } - function sessionHeartBeat(processingDate, percentage, status) { - let event = { - seconds: (new Date()).getSeconds(), - processingDate: processingDate, - percentage: percentage, - status: status - } - global.EVENT_SERVER_CLIENT_MODULE.raiseEvent(TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_KEY, 'Heartbeat', event) - - if (TS.projects.superalgos.globals.taskVariables.IS_TASK_STOPPING === true) { - TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING = true - parentLogger.write(MODULE_NAME, '[IMPORTANT] sessionHeartBeat -> Stopping the Session now. ') - } - } - } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE);