diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index 5973fcb96..25312ea36 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -54,7 +54,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL } else { await readFiles() } - + await tradingSimulation.runSimulation( chart, outputDatasetsMap, @@ -148,7 +148,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL if (response.err.result !== global.DEFAULT_OK_RESPONSE.result) { throw(response.err) } - outputDatasetsMap.set(productName, JSON.parse(text)) + outputDatasetsMap.set(productName, JSON.parse(response.text)) } async function writeFiles() { diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 274a60380..277c8e565 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -63,7 +63,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } /* Clean the file from information of previous executions */ - pruneOutputFile(product, outputDatasetArray) + pruneOutputFile(product, outputDatasetArray, tradingEngine.current.episode.candle.end.value) /* Clean Open Records */ if (product.config.saveAsObjects === true) { @@ -321,7 +321,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { return value } - function pruneOutputFile(product, outputFile) { + function pruneOutputFile(product, outputFile, currentEnd) { if (outputFile.isPrunned === true) { return } /* When a session is resumed, we will be potentially reading output files belonging to a previous session execution. @@ -335,14 +335,14 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let recordProperty = product.record.properties[j] if (recordProperty.config.codeName === 'end') { let end = record[j] - if (end >= tradingEngine.current.episode.candle.end.value) { + if (end >= currentEnd) { outputFile.splice(i, 1) /* This will execute the next prune call in the next iteration of the NodeJs event loop allowing for other callbacks to be executed. It also prevents the error 'Maximum call stack size exceeded', since the call is not placed at the call stack. */ - setImmediate(pruneOutputFile, product, outputFile) + setImmediate(pruneOutputFile, product, outputFile, currentEnd) return } } diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 65cbf0e61..4be2c96af 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -95,7 +95,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT This will happen when the sessionParameters.timeRange.config.initialDatetime is beyond the last candle available, meaning that the dataSet needs to be updated with more up-to-date data. */ - tradingEngine.current.episode.candle.index.value = candles.length - 1 + bot.SESSION.stop('Data is not up-to-date enough. Please start the Masters Data Mining Operation.') + return } } diff --git a/Session.js b/Session.js index 17f9cb6ec..daed3a114 100644 --- a/Session.js +++ b/Session.js @@ -38,8 +38,8 @@ bot.sessionKey = bot.processNode.session.name + '-' + bot.processNode.session.type + '-' + bot.processNode.session.id global.SESSION_MAP.set(bot.sessionKey, bot.sessionKey) - global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Run Session', undefined, bot.sessionKey, undefined, runSession) - global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, stopSession) + global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Run Session', undefined, bot.sessionKey, undefined, onSessionRan) + global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, onSessionStopped) /* Errors sent to the UI */ bot.sessionError = sessionError @@ -50,7 +50,7 @@ callBackFunction(global.DEFAULT_OK_RESPONSE) return - function runSession(message) { + function onSessionRan(message) { try { if (bot.SESSION_STATUS === 'Idle' || bot.SESSION_STATUS === 'Running') { return } // This happens when the UI is reloaded, the session was running and tries to run it again. @@ -99,19 +99,21 @@ socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is starting.") } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> runSession -> err = " + err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onSessionRan -> err = " + err.stack); } } + function onSessionStopped() { + stopSession('Session Stopped From the User Interface.') + } + function stopSession(commandOrigin) { - try { - if (commandOrigin === undefined) { commandOrigin = ' from the User Interface.' } - socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is stopping " + commandOrigin) - socialBotsModule.finalize() - bot.STOP_SESSION = true - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> stopSession -> err = " + err.stack); - } + + socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is stopping " + commandOrigin) + socialBotsModule.finalize() + bot.STOP_SESSION = true + sessionError(bot.SESSION, commandOrigin) + } function setUpAppSchema() {