diff --git a/TradingEngineProcessMainLoop.js b/TradingEngineProcessMainLoop.js index de45d16b1..22bc0b3f2 100644 --- a/TradingEngineProcessMainLoop.js +++ b/TradingEngineProcessMainLoop.js @@ -94,6 +94,7 @@ bot.enableCheckLoopHealth = true; let fixedTimeLoopIntervalHandle; + let skipProcessing = true; if (bot.runAtFixedInterval === true) { @@ -101,10 +102,25 @@ } else { - loop(); + backtestingEventListenerSubscriptionId = global.SYSTEM_EVENT_HANDLER.listenToEvent('Cockpit-Restart-Button', 'Backstesting Started', undefined, undefined, undefined, startBackTesting) + liveTradingEventListenerSubscriptionId = global.SYSTEM_EVENT_HANDLER.listenToEvent('Cockpit-Restart-Button', 'Live-Trading Started', undefined, undefined, undefined, startLiveTrading) + stopRequestedEventListenerSubscriptionId = global.SYSTEM_EVENT_HANDLER.listenToEvent('Cockpit-Restart-Button', 'Stop Requested', undefined, undefined, undefined, stopRequested) + function startBackTesting() { + skipProcessing = false + } + + function startLiveTrading() { + skipProcessing = false + } + + function stopRequested() { + skipProcessing = true + } } + loop(); + function loop() { try { @@ -125,6 +141,17 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } + let nextWaitTime; + + /* Checking if we should process this loop or not.*/ + if (skipProcessing === true) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> We are going to skip this Loop bacause we were requested to stop or never asked to start."); } + console.log("[INFO] run -> loop -> We are going to skip this Loop bacause we were requested to stop or never asked to start.") + nextWaitTime = 'Normal'; + loopControl(nextWaitTime); + return + } + /* Loop Heartbeat sent to the UI */ hearBeat() @@ -155,8 +182,6 @@ let statusDependencies; let dataDependencies; - let nextWaitTime; - initializeStatusDependencies(); function initializeStatusDependencies() { diff --git a/server.js b/server.js index 65deed103..35e80281a 100644 --- a/server.js +++ b/server.js @@ -105,23 +105,6 @@ global.SYSTEM_EVENT_HANDLER.initialize('Task Server', bootLoader) function bootLoader() { - - //global.SYSTEM_EVENT_HANDLER.createEventHandler(global.USER_DEFINITION.name) - - /* - tryToListenToCockpit() - - function tryToListenToCockpit() { - - global.SYSTEM_EVENT_HANDLER.listenToEvent('Cockpit-Restart-Simulation', 'Simulation Started', undefined, 'Task Server Boot Loader', onResponse, startSequence) - - function onResponse(message) { - if (message.result !== global.DEFAULT_OK_RESPONSE.result) { - setTimeout(tryToListenToCockpit, 3000) - } - } - } - */ for (let i = 0; i < global.USER_DEFINITION.bot.processes.length; i++) { let code = global.USER_DEFINITION.bot.processes[i].code @@ -136,12 +119,15 @@ function bootLoader() { } /* Heartbeat sent to the UI */ + + let key = global.USER_DEFINITION.name + '-' + global.USER_DEFINITION.type + '-' + global.USER_DEFINITION.id + + global.SYSTEM_EVENT_HANDLER.createEventHandler(key) global.HEARTBEAT_INTERVAL_HANDLER = setInterval(hearBeat, 1000) function hearBeat() { /* The heartbeat event is raised at the event handler of the instance of this task, created at the UI. */ - let key = global.USER_DEFINITION.name + '-' + global.USER_DEFINITION.type + '-' + global.USER_DEFINITION.id let event = { seconds: (new Date()).getSeconds() }