From 68f767e15a358e0b0bf39d547195ba4d20f3c29e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 18 Aug 2020 14:12:15 +0200 Subject: [PATCH] Async Await Implementation finished --- LowFrequencyTrading/TradingOutput.js | 149 +++++------------- LowFrequencyTrading/TradingPosition.js | 8 +- LowFrequencyTrading/TradingProcess.js | 20 +-- LowFrequencyTrading/TradingSimulation.js | 191 ++++++++++------------- MultiPeriodDaily.js | 2 +- ProcessOutput.js | 69 +++++++- StatusReport.js | 3 +- 7 files changed, 210 insertions(+), 232 deletions(-) diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index 0abe8c40a..5973fcb96 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -3,7 +3,6 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL This module will load if necesary all the data outputs so that they can be appended with new records if needed. After running the simulation, it will save all the data outputs. */ - const FULL_LOG = true const MODULE_NAME = 'Trading Bot' let thisObject = { @@ -24,21 +23,13 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL commons = undefined } - function initialize(callBackFunction) { - try { - logger.fileName = MODULE_NAME - logger.initialize() + function initialize() { - callBackFunction(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] initialize -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } } - function start(chart, timeFrame, timeFrameLabel, currentDay, callBackFunction) { + async function start(chart, timeFrame, timeFrameLabel, currentDay) { try { - bot.processingDailyFiles + if (timeFrame > global.dailyFilePeriods[0][0]) { bot.processingDailyFiles = false } else { @@ -51,10 +42,6 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets let market = bot.market - let totalFilesToBeRead = 0 - let totalFilesRead = 0 - let totalFilesToBeWritten = 0 - let totalFilesWritten = 0 let outputDatasetsMap = new Map() if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { @@ -63,21 +50,27 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL */ bot.simulationState.tradingEngine = bot.TRADING_ENGINE bot.simulationState.tradingSystem = bot.TRADING_SYSTEM - initializeOutputs() + await initializeOutputs() } else { - readFiles() + await readFiles() } + + await tradingSimulation.runSimulation( + chart, + outputDatasetsMap, + writeFiles + ) return - function initializeOutputs() { + async function initializeOutputs() { if (bot.processingDailyFiles) { - initializeDailyFiles() + await initializeDailyFiles() } else { - initializeMarketFiles() + await initializeMarketFiles() } } - function initializeDailyFiles() { + async function initializeDailyFiles() { for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent @@ -86,10 +79,9 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL outputDatasetsMap.set(dataset.parentNode.config.codeName, []) } } - runSimulation() } - function initializeMarketFiles() { + async function initializeMarketFiles() { for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent @@ -98,23 +90,22 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL outputDatasetsMap.set(dataset.parentNode.config.codeName, []) } } - runSimulation() } - function readFiles() { + async function readFiles() { /* This bot have an output of files that it generates. At every call to the bot, it needs to read the previously generated files in order to later append more information after the execution is over. Here in this function we are going to read those output files and get them ready for appending content during the simulation. */ if (bot.processingDailyFiles) { - readDailyFiles() + await readDailyFiles() } else { - readMarketFiles() + await readMarketFiles() } } - function readMarketFiles() { + async function readMarketFiles() { for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent @@ -124,12 +115,12 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel - readOutputFile(fileName, filePath, dataset.parentNode.config.codeName) + await readOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } - function readDailyFiles() { + async function readDailyFiles() { for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent @@ -140,70 +131,38 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel + "/" + dateForPath - readOutputFile(fileName, filePath, dataset.parentNode.config.codeName) + await readOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } - function readOutputFile(fileName, filePath, productName) { - totalFilesToBeRead++ + async function readOutputFile(fileName, filePath, productName) { filePath += '/' + fileName - fileStorage.getTextFile(filePath, onFileRead, true) - function onFileRead(err, text) { - try { + let response = await fileStorage.asyncGetTextFile(filePath, true) - if (err.message === 'File does not exist.') { - outputDatasetsMap.set(productName, []) - anotherFileRead() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - outputDatasetsMap.set(productName, JSON.parse(text)) - anotherFileRead() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } + if (response.err.message === 'File does not exist.') { + outputDatasetsMap.set(productName, []) + return } - } - - function anotherFileRead() { - totalFilesRead++ - if (totalFilesRead === totalFilesToBeRead) { - runSimulation() + if (response.err.result !== global.DEFAULT_OK_RESPONSE.result) { + throw(response.err) } + outputDatasetsMap.set(productName, JSON.parse(text)) } - function runSimulation() { - tradingSimulation.runSimulation( - chart, - outputDatasetsMap, - writeFiles, - callBackFunction) - } - - function writeFiles() { + async function writeFiles() { /* The output of files which were appended with information during the simulation execution, now needs to be saved. */ if (bot.processingDailyFiles) { - writeDailyFiles() + await writeDailyFiles() } else { - writeMarketFiles() + await writeMarketFiles() } } - function writeMarketFiles() { + async function writeMarketFiles() { for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent @@ -213,12 +172,12 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel - writeOutputFile(fileName, filePath, dataset.parentNode.config.codeName) + await writeOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } - function writeDailyFiles() { + async function writeDailyFiles() { for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent @@ -229,47 +188,25 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel + "/" + dateForPath - writeOutputFile(fileName, filePath, dataset.parentNode.config.codeName) + await writeOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } - function writeOutputFile(fileName, filePath, productName) { - totalFilesToBeWritten++ + async function writeOutputFile(fileName, filePath, productName) { filePath += '/' + fileName let fileContent = JSON.stringify(outputDatasetsMap.get(productName)) - fileStorage.createTextFile(filePath, fileContent, onFileCreated) + let response = await fileStorage.asyncCreateTextFile(filePath, fileContent) - function onFileCreated(err) { - try { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - anotherFileWritten() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } - - function anotherFileWritten() { - totalFilesWritten++ - if (totalFilesWritten === totalFilesToBeWritten) { - callBackFunction(global.DEFAULT_OK_RESPONSE) + if (response.err.result !== global.DEFAULT_OK_RESPONSE.result) { + throw(response.err) } } } catch (err) { logger.write(MODULE_NAME, '[ERROR] start -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) + throw(global.DEFAULT_FAIL_RESPONSE) } } } diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index 98c131fa2..a147149fb 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -256,7 +256,7 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng } } - function calculateStatistics() { + function calculateStatistics(tradingEngineStageNode) { calculateAssetsStatistics() calculatePositionStatistics() @@ -277,11 +277,11 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng /* ROI Calculation */ tradingEngine.current.position.positionBaseAsset.ROI.value = tradingEngine.current.position.positionBaseAsset.profitLoss.value * 100 / - tradingEngine.current.position.positionBaseAsset.sizeFilled.value + tradingEngine.current.strategyOpenStage.stageBaseAsset.sizeFilled.value tradingEngine.current.position.positionQuotedAsset.ROI.value = tradingEngine.current.position.positionQuotedAsset.profitLoss.value * 100 / - tradingEngine.current.position.positionQuotedAsset.sizeFilled.value + tradingEngine.current.strategyOpenStage.stageQuotedAsset.sizeFilled.value tradingEngine.current.position.positionBaseAsset.ROI.value = global.PRECISE(tradingEngine.current.position.positionBaseAsset.ROI.value, 10) tradingEngine.current.position.positionQuotedAsset.ROI.value = global.PRECISE(tradingEngine.current.position.positionQuotedAsset.ROI.value, 10) @@ -306,7 +306,7 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng tradingEngine.current.episode.episodeBaseAsset.profitLoss.value * tradingEngine.current.position.endRate.value + tradingEngine.current.episode.episodeQuotedAsset.profitLoss.value ) - + tradingEngine.current.position.positionStatistics.profitLoss.value = global.PRECISE(tradingEngine.current.position.positionStatistics.profitLoss.value, 10) /* ROI Calculation */ diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 48ca5e79b..68ee99d0c 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -31,16 +31,18 @@ function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { try { - logger.fileName = MODULE_NAME; - logger.initialize(); + logger.fileName = MODULE_NAME + logger.initialize() - statusDependencies = pStatusDependencies; - dataDependenciesModule = pDataDependencies; - processConfig = pProcessConfig; + statusDependencies = pStatusDependencies + dataDependenciesModule = pDataDependencies + processConfig = pProcessConfig let TRADING_OUTPUT_MODULE = require("./TradingOutput") - tradingOutputModule = TRADING_OUTPUT_MODULE.newTradingOutput(bot, logger, UTILITIES, FILE_STORAGE); - tradingOutputModule.initialize(callBackFunction); + tradingOutputModule = TRADING_OUTPUT_MODULE.newTradingOutput(bot, logger, UTILITIES, FILE_STORAGE) + tradingOutputModule.initialize() + + callBackFunction(global.DEFAULT_OK_RESPONSE) } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); @@ -560,7 +562,7 @@ } async function generateOutput(chart) { - tradingOutputModule.start( + await tradingOutputModule.start( chart, currentTimeFrame, currentTimeFrameLabel, @@ -584,7 +586,7 @@ async function writeDataRanges(currentTimeFrameLabel) { for (let outputDatasetIndex = 0; outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length; outputDatasetIndex++) { let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; - await writeDataRange(contextVariables.dateBeginOfMarket, bot.tradingProcessDate, productCodeName, currentTimeFrameLabel, controlLoop); + await writeDataRange(contextVariables.dateBeginOfMarket, bot.tradingProcessDate, productCodeName, currentTimeFrameLabel); } async function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel) { diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index cd3ef2ae6..65cbf0e61 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -19,11 +19,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT thisObject = undefined } - function runSimulation( + async function runSimulation( chart, outputDatasetsMap, - callback, - callBackFunction) { + writeFiles, + ) { try { let tradingSystem = bot.simulationState.tradingSystem @@ -58,51 +58,54 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let heartBeatDate let previousHeartBeatDate let firstLoopExecution = true + + /* + Loop Initialization + */ + if (bot.FIRST_EXECUTION === false && tradingEngine.last.candle !== undefined) { + /* Estimate the Initial Candle based on the last candle saved at tradingEngine */ + let firstBegin = candles[0].begin + let lastBegin = tradingEngine.last.candle.begin + let diff = lastBegin - firstBegin + let amount = diff / sessionParameters.timeFrame.config.value - initializeLoop() + tradingEngine.current.episode.candle.index.value = Math.trunc(amount) + 1 // Because we need to start from the next candle + if (tradingEngine.current.episode.candle.index.value < 0 || tradingEngine.current.episode.candle.index.value > candles.length - 1) { + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> Cannot resume simulation.') + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> tradingEngine.current.episode.candle.index.value = ' + tradingEngine.current.episode.candle.index.value) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> firstBegin = ' + firstBegin) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> lastBegin = ' + lastBegin) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> diff = ' + diff) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> amount = ' + amount) - function initializeLoop() { - if (bot.FIRST_EXECUTION === false && tradingEngine.last.candle !== undefined) { - /* Estimate the Initial Candle based on the last candle saved at tradingEngine */ - let firstBegin = candles[0].begin - let lastBegin = tradingEngine.last.candle.begin - let diff = lastBegin - firstBegin - let amount = diff / sessionParameters.timeFrame.config.value - - tradingEngine.current.episode.candle.index.value = Math.trunc(amount) + 1 // Because we need to start from the next candle - if (tradingEngine.current.episode.candle.index.value < 0 || tradingEngine.current.episode.candle.index.value > candles.length - 1) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> Cannot resume simulation.') - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> tradingEngine.current.episode.candle.index.value = ' + tradingEngine.current.episode.candle.index.value) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> firstBegin = ' + firstBegin) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> lastBegin = ' + lastBegin) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> diff = ' + diff) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> amount = ' + amount) - - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } - } else { - /* Estimate Initial Candle based on the timeRage configured for the session. */ - let firstEnd = candles[0].end - let targetEnd = sessionParameters.timeRange.config.initialDatetime - let diff = targetEnd - firstEnd - let amount = diff / sessionParameters.timeFrame.config.value - - tradingEngine.current.episode.candle.index.value = Math.trunc(amount) - if (tradingEngine.current.episode.candle.index.value < 0) { tradingEngine.current.episode.candle.index.value = 0 } - if (tradingEngine.current.episode.candle.index.value > candles.length - 1) { - /* - 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 - } + throw(global.DEFAULT_FAIL_RESPONSE) + return } + } else { + /* Estimate Initial Candle based on the timeRage configured for the session. */ + let firstEnd = candles[0].end + let targetEnd = sessionParameters.timeRange.config.initialDatetime + let diff = targetEnd - firstEnd + let amount = diff / sessionParameters.timeFrame.config.value - loop() + tradingEngine.current.episode.candle.index.value = Math.trunc(amount) + if (tradingEngine.current.episode.candle.index.value < 0) { tradingEngine.current.episode.candle.index.value = 0 } + if (tradingEngine.current.episode.candle.index.value > candles.length - 1) { + /* + 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 + } } - async function loop() { + /* + Main Simulation Loop + */ + let breakLoop = false + for( let i = tradingEngine.current.episode.candle.index.value; i < candles.length; i++) { + + tradingEngine.current.episode.candle.index.value = i if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + tradingEngine.current.episode.candle.index.value) } let candle = candles[tradingEngine.current.episode.candle.index.value] // This is the current candle the Simulation is working at. @@ -120,23 +123,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } if (checkInitialDatetime() === false) { - controlLoop() - return - } - - if (checkFinalDatetime() === false) { - closeEpisode('Final Datetime Reached') - return - } - - if (checkLastCandle() === false) { - controlLoop() - return - } - - if (checkMinimunAndMaximunBalance() === false) { - closeEpisode('Min or Max Balance Reached') - return + continue } positionChartAtCurrentCandle() @@ -183,75 +170,61 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngineModule.setCurrentCycle('Second') await tradingSystemModule.run() - controlLoop() - } - - function controlLoop() { /* Checking if we should continue processing this loop or not. */ if (bot.STOP_SESSION === true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> controlLoop -> We are going to stop here bacause we were requested to stop processing this session.') } console.log('[INFO] runSimulation -> controlLoop -> We are going to stop here bacause we were requested to stop processing this session.') updateEpisode('Session Stopped') - return + breakLoop = true } if (global.STOP_TASK_GRACEFULLY === true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> controlLoop -> We are going to stop here bacause we were requested to stop processing this task.') } console.log('[INFO] runSimulation -> controlLoop -> We are going to stop here bacause we were requested to stop processing this task.') updateEpisode('Task Stopped') - return + breakLoop = true } - if (tradingEngine.current.episode.candle.index.value + 1 < candles.length) { + if (checkFinalDatetime() === false) { + closeEpisode('Final Datetime Reached') + breakLoop = true + } - /* Add new records to the process output */ - tradingRecordsModule.appendRecords('Second') - - tradingEngine.current.episode.candle.index.value++ - - /* - This will execute the next loop 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(loop) - } else { + if (checkLastCandle() === false) { updateEpisode('All Available Candles Processed') + breakLoop = true } + + if (checkMinimunAndMaximunBalance() === false) { + closeEpisode('Min or Max Balance Reached') + breakLoop = true + } + + /* Add new records to the process output */ + tradingRecordsModule.appendRecords('Second') + + if (breakLoop === true) {break} } + tradingEngineModule.finalize() + tradingSystemModule.finalize() + tradingRecordsModule.finalize() + tradingEpisodeModule.finalize() + + tradingEngineModule = undefined + tradingSystemModule = undefined + tradingRecordsModule = undefined + tradingEpisodeModule = undefined + + await writeFiles() + function closeEpisode(exitType) { tradingEpisodeModule.updateExitType(exitType) tradingEpisodeModule.closeEpisode() - - /* Add new records to the process output */ - tradingRecordsModule.appendRecords('Second') - - afterLoop() } function updateEpisode(exitType) { tradingEpisodeModule.updateExitType(exitType) - - /* Add new records to the process output */ - tradingRecordsModule.appendRecords('Second') - - afterLoop() - } - - function afterLoop() { - - tradingEngineModule.finalize() - tradingSystemModule.finalize() - tradingRecordsModule.finalize() - tradingEpisodeModule.finalize() - - tradingEngineModule = undefined - tradingSystemModule = undefined - tradingRecordsModule = undefined - tradingEpisodeModule = undefined - - callback() } function heartBeat() { @@ -373,7 +346,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } catch (err) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> getElement -> datasetName = ' + datasetName) logger.write(MODULE_NAME, '[ERROR] runSimulation -> getElement -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) + throw(global.DEFAULT_FAIL_RESPONSE) } } @@ -384,7 +357,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT */ if (bot.processingDailyFiles) { // We are processing Daily Files let candlesPerDay = global.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value - if (tradingEngine.current.episode.candle.index.value === candles.length - 1) { + if (tradingEngine.current.episode.candle.index.value + 1 === candles.length - 1) { if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkLastCandle -> Skipping Candle because it is the last one and has not been closed yet.') } return false @@ -395,7 +368,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } } else { // We are processing Market Files - if (tradingEngine.current.episode.candle.index.value === candles.length - 1) { + if (tradingEngine.current.episode.candle.index.value + 1 === candles.length - 1) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkLastCandle -> Skipping Candle because it is the last one and has not been closed yet.') } return false } @@ -413,8 +386,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function checkFinalDatetime() { - /* Here we check that the current candle is not after of the user-defined final datetime at the session parameters.*/ - if (tradingEngine.current.episode.candle.begin.value > sessionParameters.timeRange.config.finalDatetime) { + /* Here we check that the next candle is not after of the user-defined final datetime at the session parameters.*/ + if (tradingEngine.current.episode.candle.begin.value + sessionParameters.timeFrame.config.value > sessionParameters.timeRange.config.finalDatetime) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle after the sessionParameters.timeRange.config.finalDatetime.') } return false } @@ -474,7 +447,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } catch (err) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) + throw(global.DEFAULT_FAIL_RESPONSE) } } } diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index b3d0899ce..7ae3cc3b5 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -490,7 +490,7 @@ function writeDataRange(pBegin, pEnd, productCodeName, callBack) { let dataRange = { begin: pBegin.valueOf(), - end: pEnd.valueOf() + end: pEnd.valueOf() + ONE_DAY_IN_MILISECONDS }; let fileContent = JSON.stringify(dataRange) let fileName = '/Data.Range.json'; diff --git a/ProcessOutput.js b/ProcessOutput.js index d9ee5a6ba..71d7cdc81 100644 --- a/ProcessOutput.js +++ b/ProcessOutput.js @@ -9,7 +9,8 @@ let bot = BOT; let thisObject = { - raiseEvents, raiseEvents + raiseEvents: raiseEvents, + asyncRaiseEvents: asyncRaiseEvents }; return thisObject; @@ -99,4 +100,70 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } + + function asyncRaiseEvents(lastFile, timeFrames) { + + if (bot.processNode.referenceParent.processOutput !== undefined) { + if (bot.processNode.referenceParent.processOutput.outputDatasets !== undefined) { + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets + + for (let j = 0; j < outputDatasets.length; j++) { + let outputDataset = outputDatasets[j] + + /* Some validations to verify that everything is in place. */ + if (outputDataset.referenceParent !== undefined) { + if (outputDataset.referenceParent.config.codeName === undefined) { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Dataset Definition not defined. Dataset Definition = " + JSON.stringify(outputDataset.referenceParent)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + if (outputDataset.referenceParent.parentNode !== undefined) { + if (outputDataset.referenceParent.parentNode.config.codeName === undefined) { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Product not defined. Product = " + JSON.stringify(outputDataset.referenceParent.parentNode)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + if (outputDataset.referenceParent.parentNode.parentNode !== undefined) { + if (outputDataset.referenceParent.parentNode.parentNode.config.codeName === undefined) { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Bot not defined. Bot = " + JSON.stringify(outputDataset.referenceParent.parentNode.parentNode)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + if (outputDataset.referenceParent.parentNode.parentNode.parentNode !== undefined) { + if (outputDataset.referenceParent.parentNode.parentNode.parentNode.config.codeName === undefined) { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Data Mine not defined. Data Mine = " + JSON.stringify(outputDataset.referenceParent.parentNode.parentNode.parentNode)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + } else { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> Bot not attached to a Data Mine. Bot = " + JSON.stringify(outputDataset.referenceParent.parentNode.parentNode)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + } else { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> Product not attached to a Bot. Product = " + JSON.stringify(outputDataset.referenceParent.parentNode)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + } else { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> Dataset Definition not attached to a Product. Dataset Definition = " + JSON.stringify(outputDataset.referenceParent)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + } else { + logger.write(MODULE_NAME, "[ERROR] raiseEvents -> Output Dataset with no reference to a Dataset Definition. Output Dataset = " + JSON.stringify(outputDataset)); + throw(global.DEFAULT_FAIL_RESPONSE); + } + + /* All good, lets emit the event. */ + + let dataMine = outputDataset.referenceParent.parentNode.parentNode.parentNode.config.codeName + let botName = outputDataset.referenceParent.parentNode.parentNode.config.codeName + let product = outputDataset.referenceParent.parentNode.config.codeName + let dataset = outputDataset.referenceParent.config.codeName + + key = dataMine + "-" + botName + "-" + product + "-" + dataset + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset + let event = { + lastFile: lastFile, + timeFrames: timeFrames + } + global.EVENT_SERVER_CLIENT.createEventHandler(key, 'Dataset Updated') + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Dataset Updated', event) + } + } + } + } }; diff --git a/StatusReport.js b/StatusReport.js index 2600fca26..b442e3aa9 100644 --- a/StatusReport.js +++ b/StatusReport.js @@ -352,7 +352,6 @@ filePath += '/' + fileName let fileContent = JSON.stringify(thisObject.file); - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated, true); let response = await fileStorage.asyncCreateTextFile(filePath, fileContent + '\n', true) if (response.err.result !== global.DEFAULT_OK_RESPONSE.result) { @@ -361,6 +360,6 @@ /* All good, lets emit the event that means data has been updated. */ let processOutput = PROCESS_OUTPUT.newProcessOutput(bot, logger) - processOutput.raiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames, callBackFunction); + processOutput.asyncRaiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames) } }