diff --git a/Commons.js b/Commons.js index bea993f51..f3b4cc853 100644 --- a/Commons.js +++ b/Commons.js @@ -2,8 +2,6 @@ const FULL_LOG = true; const LOG_FILE_CONTENT = false; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; - const ONE_MIN_IN_MILISECONDS = 60 * 1000; const MODULE_NAME = "Commons"; @@ -199,7 +197,7 @@ let system = { // These are the available system variables to be used in User Code and Formulas timeFrame: timeFrame, - ONE_DAY_IN_MILISECONDS: ONE_DAY_IN_MILISECONDS + ONE_DAY_IN_MILISECONDS: global.ONE_DAY_IN_MILISECONDS } let variable = {} // This is the structure where the user will define its own variables that will be shared across different code blocks and formulas. let results = [] @@ -290,8 +288,8 @@ let yesterday = {} let system = { // These are the available system variables to be used in User Code and Formulas timeFrame: timeFrame, - ONE_DAY_IN_MILISECONDS: ONE_DAY_IN_MILISECONDS, - ONE_MIN_IN_MILISECONDS: ONE_MIN_IN_MILISECONDS + ONE_DAY_IN_MILISECONDS: global.ONE_DAY_IN_MILISECONDS, + ONE_MIN_IN_MILISECONDS: global.ONE_MIN_IN_MILISECONDS } let variable = {} // This is the structure where the user will define its own variables that will be shared across different code blocks and formulas. let results = [] @@ -340,7 +338,7 @@ if (processingDailyFiles) { /* Initialization of Last Instance */ - lastInstantOfTheDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1; + lastInstantOfTheDay = currentDay.valueOf() + global.ONE_DAY_IN_MILISECONDS - 1; if (interExecutionMemory[productName] === undefined) { /* The first time the intialization variables goes to the Inter Execution Memory. */ @@ -411,7 +409,7 @@ results.push(product[variableName]); } - /* While we are positioned at Yesterday, we keey updating this data structure. */ + /* While we are positioned at Yesterday, we keep updating this data structure. */ if (processingDailyFiles) { if (positionedAtYesterday) { yesterday.variable = JSON.parse(JSON.stringify(variable)) @@ -457,7 +455,7 @@ at the next day, in whole, even if it starts in the previous day. */ - let lastInstantOdDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1; + let lastInstantOdDay = currentDay.valueOf() + global.ONE_DAY_IN_MILISECONDS - 1; if (record.end < currentDay.valueOf() - 1) { continue; } if (record.end === lastInstantOdDay) { continue; } diff --git a/DataSet.js b/DataSet.js index 1a6364b7a..bb7f789e3 100644 --- a/DataSet.js +++ b/DataSet.js @@ -204,8 +204,8 @@ exports.newDataSet = function newDataSet(BOT, logger) { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createTextFile -> pFolderPath = " + pFolderPath); } if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createTextFile -> pFileName = " + pFileName); } - let ownerId = thisObject.node.dataMine + "-" + thisObject.node.bot + "-" + thisObject.node.botVersion.major + "-" + thisObject.node.botVersion.minor + "-" + thisObject.node.dataSetVersion; - let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.dataSetVersion; + let ownerId = thisObject.node.dataMine + "-" + thisObject.node.bot + let botId = bot.dataMine + "-" + bot.codeName if (ownerId !== botId) { diff --git a/DebugLog.js b/DebugLog.js index f3372242d..e7c874e9b 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -1,6 +1,5 @@ exports.newDebugLog = function newDebugLog() { - const ROOT_DIR = './'; const MODULE_NAME = "DebugLog"; let executionDatetime = "D." + global.EXECUTION_DATETIME.getUTCFullYear() + @@ -124,7 +123,7 @@ exports.newDebugLog = function newDebugLog() { function writeLog() { - fileStorage.createTextFile(filePath + '/' + fileName, contentToPersist + '\r\n' + "]", onFileCreated); + fileStorage.createTextFile(filePath + '/' + fileName, contentToPersist + '\r\n' + "]", onFileCreated, undefined, true); function onFileCreated(err) { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> err = " + err.message); @@ -142,9 +141,7 @@ exports.newDebugLog = function newDebugLog() { function write(pModule, pMessage) { - try { - if (disableLogging === true) { return; } if (thisObject.bot === undefined) { return; } diff --git a/ExchangeAPI.js b/ExchangeAPI.js deleted file mode 100644 index 54c3c52e7..000000000 --- a/ExchangeAPI.js +++ /dev/null @@ -1,160 +0,0 @@ -exports.newExchangeAPI = function newExchangeAPI(logger, BOT) { - - let MODULE_NAME = "Exchange API"; - let LOG_INFO = true; - let apiClient; - let bot = BOT - - let thisObject = { - initialize: initialize, - getTicker: getTicker, - getOpenOrders: getOpenOrders, - getOrder: getOrder, - createOrder: createOrder, - getMaxDecimalPositions: getMaxDecimalPositions - }; - - let exchangeId - let options = {} - let exchange - - const ccxt = require('ccxt') - - return thisObject; - - function initialize(callBackFunction) { - try { - exchangeId = bot.exchange.toLowerCase() - - let key = process.env.KEY - let secret = process.env.SECRET - - if (key === "undefined") { key = undefined } - if (secret === "undefined") { secret = undefined } - - const exchangeClass = ccxt[exchangeId] - const exchangeConstructorParams = { - 'apiKey': key, - 'secret': secret, - 'timeout': 30000, - 'enableRateLimit': true, - verbose: false, - options: options - } - - exchange = new exchangeClass(exchangeConstructorParams) - - callBackFunction(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logError("initialize -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function getMaxDecimalPositions() { - return 8 - } - - async function getTicker(market, callBackFunction) { - try { - logInfo("getTicker -> Entering function."); - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - let ticker - - if (exchange.has['fetchTicker']) { - ticker = await (exchange.fetchTicker(symbol)) - callBackFunction(global.DEFAULT_OK_RESPONSE, ticker) - } else { - logError("getTicker -> Exchange does not support fetchTicker command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - } catch (err) { - logError("getTicker -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - async function getOpenOrders(market, callBackFunction) { - try { - logInfo("getOpenOrders -> Entering function. market = " + JSON.stringify(market)); - - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - let openOrders - - if (exchange.has['fetchOpenOrders']) { - openOrders = await(exchange.fetchOpenOrders (symbol)) - callBackFunction(global.DEFAULT_OK_RESPONSE, openOrders) - } else { - logError("getOpenOrders -> Exchange does not support fetchOpenOrders command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - } catch (err) { - logError("getOpenOrders -> Error = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - async function getOrder(orderId, market, callBackFunction) { - try { - logInfo("getOrder -> Entering function. orderId = " + orderId); - - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - let order - - if (exchange.has['fetchOrder']) { - order = await (exchange.fetchOrder(orderId, symbol)) - callBackFunction(global.DEFAULT_OK_RESPONSE, order) - } else { - logError("getOrder -> Exchange does not support fetchOrder command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } catch (err) { - logError("getOrder -> Error = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - async function createOrder(market, side, price, cost, amount, callBackFunction) { - try { - logInfo("createOrder -> Entering function."); - - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - - logInfo("createOrder -> market = " + market.assetA + "_" + market.assetB); - logInfo("createOrder -> side = " + side); - logInfo("createOrder -> cost = " + cost); - logInfo("createOrder -> amount = " + amount); - logInfo("createOrder -> price = " + price); - - if (side !== "buy" && side !== "sell") { - logError("createOrder -> side must be either 'buy' or 'sell'."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - let type = 'market' - let order - - if (exchange.has['createOrder']) { - order = await(exchange.createOrder(symbol, type, side, amount)) - callBackFunction(global.DEFAULT_OK_RESPONSE, order) - } else { - logError("createOrder -> Exchange does not support createOrder command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - } catch (err) { - logError("createOrder -> err = " + JSON.stringify(err.stack) || err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function logInfo(message) { - if (LOG_INFO === true) { logger.write(MODULE_NAME, '[INFO] ' + message) } - } - - function logError(message) { - logger.write(MODULE_NAME, '[ERROR] ' + message) - } -}; diff --git a/FileStorage.js b/FileStorage.js index bec9ef69a..1b55b9af7 100644 --- a/FileStorage.js +++ b/FileStorage.js @@ -191,16 +191,13 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { } } - function createTextFile(filePath, fileContent, callBackFunction, keepPrevious) { + function createTextFile(filePath, fileContent, callBackFunction, keepPrevious, noTemp) { let currentRetryWriteTextFile = 0 - recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious) - - function recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious) { - - logger.write(MODULE_NAME, '[INFO] FileStorage -> createTextFile -> Entering Function.') + recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious, noTemp) + function recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious, noTemp) { /* Choose path for either logs or data */ let fileLocation if (filePath.indexOf("/Logs/") > 0) { @@ -210,7 +207,6 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { } try { - logger.write(MODULE_NAME, '[INFO] FileStorage -> createTextFile -> fileLocation: ' + fileLocation) /* If necesary a folder or folders are created before writing the file to disk. */ @@ -222,7 +218,11 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { Then we delete the original file, if exists, and finally we rename the temporary into the original name. */ const fs = require('fs') - fs.writeFile(fileLocation + '.tmp', fileContent, onFileWritenn) + if (noTemp === true) { + fs.writeFile(fileLocation, fileContent, onFileWritenn) + } else { + fs.writeFile(fileLocation + '.tmp', fileContent, onFileWritenn) + } function onFileWritenn(err) { let retryTimeToUse = FAST_RETRY_TIME_IN_MILISECONDS @@ -235,6 +235,10 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { setTimeout(retry, retryTimeToUse) } else { + if (noTemp === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE) + return + } if (keepPrevious === true) { /* In some cases, we are going to keep a copy of the previous version of the file being written. This will be usefull to recover from crashes diff --git a/IndicatorBot.js b/IndicatorBot.js index fc28afeae..dbe4b844f 100644 --- a/IndicatorBot.js +++ b/IndicatorBot.js @@ -1,167 +1,805 @@ -exports.newIndicatorBot = function newIndicatorBot(bot, logger, UTILITIES, FILE_STORAGE) { - - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; +exports.newIndicatorBot = function newIndicatorBot(bot, parentLogger) { const MODULE_NAME = "Indicator Bot"; + const FULL_LOG = true; + + let USER_BOT_MODULE; + let COMMONS_MODULE; + + const MULTI_PERIOD_MARKET = require(global.ROOT_DIR + 'MultiPeriodMarket'); + const MULTI_PERIOD_DAILY = require(global.ROOT_DIR + 'MultiPeriodDaily'); + const FILE_STORAGE = require('./FileStorage.js'); + let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); + + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); + let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. + + let nextLoopTimeoutHandle; let thisObject = { initialize: initialize, - finalize: finalize, - start: start + run: run }; - let utilities = UTILITIES.newCloudUtilities(logger); - let fileStorage = FILE_STORAGE.newFileStorage(logger); - - const COMMONS = require('./Commons.js'); - let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE); + let processConfig; return thisObject; - function finalize() { - thisObject = undefined - utilities = undefined - fileStorage = undefined - commons = undefined - } - - function initialize(callBackFunction) { - + function initialize(pProcessConfig, callBackFunction) { try { + processConfig = pProcessConfig; - logger.fileName = MODULE_NAME; - logger.initialize(); + if (bot.definedByUI === true) { + /* The code of the bot is defined at the UI. No need to load a file with the code. */ + callBackFunction(global.DEFAULT_OK_RESPONSE); + return + } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } + /* This bot is not ready for taking its code from the UI, then we need to load it from its repo. */ + let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName + filePath += "/User.Bot.js" - callBackFunction(global.DEFAULT_OK_RESPONSE); + fileStorage.getTextFile(filePath, onBotDownloaded); + function onBotDownloaded(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> filePath = " + filePath); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + USER_BOT_MODULE = {} + USER_BOT_MODULE.newUserBot = eval(text); // TODO This needs to be changed function + + filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo; + filePath += "/Commons.js" + + fileStorage.getTextFile(filePath, onCommonsDownloaded); + + function onCommonsDownloaded(err, text) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + COMMONS_MODULE = {} + COMMONS_MODULE.newCommons = eval(text); // TODO This needs to be changed function + + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - function start(dataFiles, timeFrame, timeFrameLabel, currentDay, interExecutionMemory, callBackFunction) { - + function run(callBackFunction) { try { + let fixedTimeLoopIntervalHandle; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } + /* Errors sent to the UI */ + bot.processError = processError - let products = {} - let mainDependency = {} - let processingDailyFiles + /* Heartbeats sent to the UI */ + bot.processHeartBeat = processHeartBeat - if (currentDay !== undefined) { - processingDailyFiles = true + if (bot.runAtFixedInterval === true) { + fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); } else { - processingDailyFiles = false + loop(); } - /* The first phase here is about checking that we have everything we need at the definition level. */ - let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies - if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } + function loop() { + try { + function pad(str, max) { + str = str.toString(); + return str.length < max ? pad(" " + str, max) : str; + } - let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets - if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } + /* For each loop we want to create a new log file. */ + if (logger !== undefined) { + logger.finalize() + } + logger = DEBUG_MODULE.newDebugLog(); + global.LOGGER_MAP.set(MODULE_NAME, logger) + logger.bot = bot; + logger.initialize(); - /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + bot.loopCounter++; + bot.loopStartTime = new Date().valueOf(); - /* During the third phase, we need to generate the data of the different products this process produces an output */ - for (let i = 0; i < outputDatasets.length; i++) { + /* We tell the UI that we are running. */ + processHeartBeat() - let outputDatasetNode = outputDatasets[i] - let jsonData // Just build data as Json objects - let outputData // Data built as a result of applying user defined code and formulas at the Data Building Procedure - let singularVariableName // Name of the variable for this product - let recordDefinition // Record as defined by the user at the UI - let fileContent // Here we store the contents of the new data built just before writing it to a file. - let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. + /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ + const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(global.ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(global.ROOT_DIR + 'StatusDependencies'); + const DATA_DEPENDENCIES = require(global.ROOT_DIR + 'DataDependencies'); + const DATA_SET = require(global.ROOT_DIR + 'DataSet'); + const PROCESS_EXECUTION_EVENTS = require(global.ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(global.ROOT_DIR + 'ProcessOutput'); - /* - For each outputDatasetNode in our process output, we will build the information based on our input products. - */ + /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ + bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. - recordDefinition = outputDatasetNode.referenceParent.parentNode.record - singularVariableName = outputDatasetNode.referenceParent.parentNode.config.singularVariableName + /* High level log entry */ + console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + + " Main Loop # " + pad(Number(bot.loopCounter), 8)) - /* Check Irregular Periods */ + /* We will prepare first the infraestructure needed for the bot to run. There are 3 modules we need to sucessfullly initialize first. */ + let processExecutionEvents + let userBot; + let processFramework; + let statusDependencies; + let dataDependencies; - if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode !== undefined) { - if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { - resultsWithIrregularPeriods = true + let nextWaitTime; + + initializeProcessExecutionEvents(); + + function initializeProcessExecutionEvents() { + try { + processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) + processExecutionEvents.initialize(onInizialized); + + function onInizialized(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); + startProcessExecutionEvents() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function startProcessExecutionEvents() { + try { + processExecutionEvents.start(onStarted); + + function onStarted(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); + + if (global.STOP_TASK_GRACEFULLY === true) { + loopControl() + return + } + + initializeStatusDependencies(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessExecutionEvents -> onStarted -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function initializeStatusDependencies() { + try { + statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); + statusDependencies.initialize(onInizialized); + + function onInizialized(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); + initializeDataDependencies(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeStatusDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function initializeDataDependencies() { + try { + dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); + dataDependencies.initialize(onInizialized); + + function onInizialized(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + /* If the process is configured to run inside a framework, we continue there, otherwise we run the bot directly. */ + if (processConfig.framework === undefined) { + initializeUserBot(); + return; + } + + switch (processConfig.framework.name) { + case 'Multi-Period-Market': { + processFramework = MULTI_PERIOD_MARKET.newMultiPeriodMarket(bot, logger, UTILITIES, FILE_STORAGE); + intitializeProcessFramework(); + break; + } + case 'Multi-Period-Daily': { + processFramework = MULTI_PERIOD_DAILY.newMultiPeriodDaily(bot, logger, UTILITIES, FILE_STORAGE); + intitializeProcessFramework(); + break; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); + global.unexpectedError = err.message + processStopped() + return + } + } + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeDataDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function initializeUserBot() { + try { + usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE); + usertBot.initialize(statusDependencies, onInizialized); + + function onInizialized(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Execution finished well."); + startUserBot(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeUserBot -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function startUserBot() { + try { + usertBot.start(onFinished); + + function onFinished(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Execution finished well."); + finishProcessExecutionEvents() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + case "Dependency does not exist.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency does not exist. This Loop will go to sleep."); + nextWaitTime = 'Sleep'; + loopControl(nextWaitTime); + return; + } + case "Dependency not ready.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency not ready. Will Retry Later."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled custom response received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function intitializeProcessFramework() { + try { + processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); + + function onInizialized(err) { + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); + startProcessFramework(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> intitializeProcessFramework -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function startProcessFramework() { + try { + processFramework.start(onFinished); + + function onFinished(err) { + try { + processFramework.finalize() + processFramework = undefined + dataDependencies.finalize() + dataDependencies = undefined + statusDependencies.finalize() + statusDependencies = undefined + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Execution finished well."); + finishProcessExecutionEvents() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + case "Dependency does not exist.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency does not exist. This Loop will go to sleep."); + nextWaitTime = 'Sleep'; + loopControl(nextWaitTime); + return; + } + case "Dependency not ready.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency not ready. This Loop will go to sleep."); + nextWaitTime = 'Sleep'; + loopControl(nextWaitTime); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function finishProcessExecutionEvents() { + try { + processExecutionEvents.finish(onFinished); + + function onFinished(err) { + try { + processExecutionEvents.finalize() + processExecutionEvents = undefined + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); + nextWaitTime = 'Normal'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> finishProcessExecutionEvents -> onFinished -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); + global.unexpectedError = err.message + processStopped() + return + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function loopControl(nextWaitTime) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } + + /* We show we reached the end of the loop. */ + processHeartBeat() + + /* Here we check if we must stop the loop gracefully. */ + shallWeStop(onStop, onContinue); + + function onStop() { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } + processStopped() + return; + } + + function onContinue() { + /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ + switch (nextWaitTime) { + case 'Normal': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); + processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") + if (global.WRITE_LOGS_TO_FILES === 'true') { + logger.persist(); + } + } + break; + case 'Retry': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + logger.persist(); + } + break; + case 'Sleep': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + logger.persist(); + } + break; + case 'Coma': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + logger.persist(); + } + break; + } + } + } + + function shallWeStop(stopCallBack, continueCallBack) { + try { + /* IMPORTANT: This function is exactly the same on the 3 modules. */ + if (!global.STOP_TASK_GRACEFULLY) { + continueCallBack(); + } else { + stopCallBack(); + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + } catch (err) { + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return + } + } + + function processHeartBeat(processingDate, percentage, status) { + let event = { + seconds: (new Date()).getSeconds(), + processingDate: processingDate, + percentage: percentage, + status: status + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) + } + + function processError(node, errorMessage) { + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage } } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) + } - /* Build the data */ - jsonData = commons.dataBuildingProcedure( - products, - mainDependency, - recordDefinition, - outputDatasetNode.referenceParent.parentNode.dataBuilding, - singularVariableName, - outputDatasetNode.referenceParent.parentNode.config.codeName, - timeFrame, - timeFrameLabel, - resultsWithIrregularPeriods, - interExecutionMemory, - processingDailyFiles, - currentDay - ) - - /* Add the calculated properties */ - if (outputDatasetNode.referenceParent.parentNode.calculations !== undefined) { - outputData = commons.calculationsProcedure(jsonData, recordDefinition, outputDatasetNode.referenceParent.parentNode.calculations, singularVariableName, timeFrame) + function processStopped() { + if (global.unexpectedError !== undefined) { + processError(undefined, "An unexpected error caused the Process to stop.") } else { - outputData = jsonData + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') } - products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] = outputData - } - - /*At the fourth and last phase, we will save the new information generated into files corresponding to each output outputDatasetNode.*/ - let totalFilesWritten = 0 - for (let i = 0; i < outputDatasets.length; i++) { - let outputDatasetNode = outputDatasets[i] - let outputData = products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] - let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. - let contextSummary = {} - - /* Check Irregular Periods */ - if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { - resultsWithIrregularPeriods = true - } - - /* Simplifying the access to basic info */ - - contextSummary.dataset = outputDatasetNode.referenceParent.config.codeName - contextSummary.product = outputDatasetNode.referenceParent.parentNode.config.codeName - contextSummary.bot = outputDatasetNode.referenceParent.parentNode.parentNode.config.codeName - contextSummary.dataMine = outputDatasetNode.referenceParent.parentNode.parentNode.parentNode.config.codeName - - /* This stuff is still hardcoded and unresolved. */ - contextSummary.botVersion = { - "major": 1, - "minor": 0 - } - contextSummary.dataSetVersion = "dataSet.V1" - - let fileContent = commons.generateFileContent(outputData, outputDatasetNode.referenceParent.parentNode.record, resultsWithIrregularPeriods, processingDailyFiles, currentDay, callBackFunction) - commons.writeFile(contextSummary, fileContent, anotherFileWritten, processingDailyFiles, timeFrameLabel, currentDay, callBackFunction) - } - - - function anotherFileWritten() { - totalFilesWritten++ - if (totalFilesWritten === outputDatasets.length) { - callBackFunction(global.DEFAULT_OK_RESPONSE); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + if (global.unexpectedError !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) } } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + + } catch (err) { + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/IndicatorBotProcessMainLoop.js b/IndicatorBotProcessMainLoop.js deleted file mode 100644 index 90892dd72..000000000 --- a/IndicatorBotProcessMainLoop.js +++ /dev/null @@ -1,1121 +0,0 @@ -exports.newIndicatorBotProcessMainLoop = function newIndicatorBotProcessMainLoop(bot, parentLogger) { - - const ROOT_DIR = './'; - - const MODULE_NAME = "Indicator Process Main Loop"; - const FULL_LOG = true; - - let USER_BOT_MODULE; - let COMMONS_MODULE; - - const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket'); - const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily'); - const FILE_STORAGE = require('./FileStorage.js'); - let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); - - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); - let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. - - let nextLoopTimeoutHandle; - let checkLoopHealthHandle; - - let thisObject = { - initialize: initialize, - run: run - }; - - let processConfig; - - return thisObject; - - function initialize(pProcessConfig, callBackFunction) { - - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - processConfig = pProcessConfig; - - if (bot.definedByUI === true) { - /* The code of the bot is defined at the UI. No need to load a file with the code. */ - callBackFunction(global.DEFAULT_OK_RESPONSE); - return - } - - /* This bot is not ready for taking its code from the UI, then we need to load it from its repo. */ - - let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName - filePath += "/User.Bot.js" - - fileStorage.getTextFile(filePath, onBotDownloaded); - - function onBotDownloaded(err, text) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> filePath = " + filePath); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - USER_BOT_MODULE = {} - USER_BOT_MODULE.newUserBot = eval(text); // TODO This needs to be changed function - - filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo; - filePath += "/Commons.js" - - fileStorage.getTextFile(filePath, onCommonsDownloaded); - - function onCommonsDownloaded(err, text) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - COMMONS_MODULE = {} - COMMONS_MODULE.newCommons = eval(text); // TODO This needs to be changed function - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function run(callBackFunction) { - - try { - - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - - bot.enableCheckLoopHealth = true; - - let fixedTimeLoopIntervalHandle; - - if (bot.runAtFixedInterval === true) { - - fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); - - } else { - - loop(); - - } - - function loop() { - - try { - - function pad(str, max) { - str = str.toString(); - return str.length < max ? pad(" " + str, max) : str; - } - - /* For each loop we want to create a new log file. */ - - if (logger !== undefined) { - logger.finalize() - } - logger = DEBUG_MODULE.newDebugLog(); - global.LOGGER_MAP.set(MODULE_NAME, logger) - logger.bot = bot; - logger.initialize(); - - bot.loopCounter++; - bot.loopStartTime = new Date().valueOf(); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - - /* Loop Heartbeat sent to the UI */ - bot.processHeartBeat = processHeartBeat - processHeartBeat() - - /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); - const DATA_SET = require(ROOT_DIR + 'DataSet'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); - - /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. - - /* High level log entry */ - - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) - + " Main Loop # " + pad(Number(bot.loopCounter), 8)) - - /* We will prepare first the infraestructure needed for the bot to run. There are 3 modules we need to sucessfullly initialize first. */ - - let processExecutionEvents - let userBot; - let processFramework; - let statusDependencies; - let dataDependencies; - - let nextWaitTime; - - initializeProcessExecutionEvents(); - - function initializeProcessExecutionEvents() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - - processExecutionEvents.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); - startProcessExecutionEvents() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function startProcessExecutionEvents() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - - processExecutionEvents.start(onStarted); - - function onStarted(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); - - if (global.STOP_TASK_GRACEFULLY === true) { - loopControl() - return - } - - initializeStatusDependencies(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessExecutionEvents -> onStarted -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeStatusDependencies() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - - statusDependencies.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); - initializeDataDependencies(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeStatusDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeDataDependencies() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } - - dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); - - dataDependencies.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } - - /* If the process is configured to run inside a framework, we continue there, otherwise we run the bot directly. */ - if (processConfig.framework === undefined) { - initializeUserBot(); - return; - } - - switch (processConfig.framework.name) { - case 'Multi-Period-Market': { - processFramework = MULTI_PERIOD_MARKET.newMultiPeriodMarket(bot, logger, UTILITIES, FILE_STORAGE); - intitializeProcessFramework(); - break; - } - case 'Multi-Period-Daily': { - processFramework = MULTI_PERIOD_DAILY.newMultiPeriodDaily(bot, logger, UTILITIES, FILE_STORAGE); - intitializeProcessFramework(); - break; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeDataDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeUserBot() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> Entering function."); } - - usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE); - - usertBot.initialize(statusDependencies, onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Execution finished well."); - startUserBot(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeUserBot -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function startUserBot() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } - - usertBot.start(onFinished); - - function onFinished(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Execution finished well."); - finishProcessExecutionEvents() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - case "Dependency does not exist.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency does not exist. This Loop will go to sleep."); - nextWaitTime = 'Sleep'; - loopControl(nextWaitTime); - return; - } - case "Dependency not ready.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency not ready. Will Retry Later."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function intitializeProcessFramework() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } - - processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); - startProcessFramework(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> intitializeProcessFramework -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function startProcessFramework() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } - - processFramework.start(onFinished); - - function onFinished(err) { - - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } - processFramework.finalize() - processFramework = undefined - dataDependencies.finalize() - dataDependencies = undefined - statusDependencies.finalize() - statusDependencies = undefined - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Execution finished well."); - finishProcessExecutionEvents() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - case "Dependency does not exist.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency does not exist. This Loop will go to sleep."); - nextWaitTime = 'Sleep'; - loopControl(nextWaitTime); - return; - } - case "Dependency not ready.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency not ready. This Loop will go to sleep."); - nextWaitTime = 'Sleep'; - loopControl(nextWaitTime); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function finishProcessExecutionEvents() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - - processExecutionEvents.finish(onFinished); - - function onFinished(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - - processExecutionEvents.finalize() - processExecutionEvents = undefined - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); - nextWaitTime = 'Normal'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> finishProcessExecutionEvents -> onFinished -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } - - /* We show we reached the end of the loop. */ - - processHeartBeat() - - /* Here we check if we must stop the loop gracefully. */ - - shallWeStop(onStop, onContinue); - - function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - - if(global.WRITE_LOGS_TO_FILES === 'true'){ - logger.persist(); - } - - processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - - switch (nextWaitTime) { - case 'Normal': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") - if(global.WRITE_LOGS_TO_FILES === 'true'){ - logger.persist(); - } - } - break; - case 'Retry': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") - logger.persist(); - } - break; - case 'Sleep': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") - logger.persist(); - } - break; - case 'Coma': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") - logger.persist(); - } - break; - } - } - } - - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName,20) + " " + pad(bot.process,30) + " Loop # " + pad(Number(bot.loopCounter),5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName,20) + " " + pad(bot.process,30) + " Loop # " + pad(Number(bot.loopCounter),5) + " found delayed but still alive. No action taken.")); - - } - } - } - - function shallWeStop(stopCallBack, continueCallBack) { - - try { - - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - - if (!global.STOP_TASK_GRACEFULLY) { - continueCallBack(); - } else { - stopCallBack(); - } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = "+ err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function processHeartBeat(processingDate, percentage, status) { - let event = { - seconds: (new Date()).getSeconds(), - processingDate: processingDate, - percentage: percentage, - status: status - } - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) - } - - function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') - } - - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = "+ err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } -}; diff --git a/IndicatorOutput.js b/IndicatorOutput.js new file mode 100644 index 000000000..3faa4052e --- /dev/null +++ b/IndicatorOutput.js @@ -0,0 +1,157 @@ +exports.newIndicatorOutput = function newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) { + const FULL_LOG = true; + const MODULE_NAME = "Indicator Bot"; + + let thisObject = { + initialize: initialize, + finalize: finalize, + start: start + }; + + let utilities = UTILITIES.newCloudUtilities(logger); + let fileStorage = FILE_STORAGE.newFileStorage(logger); + + const COMMONS = require('./Commons.js'); + let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE); + + return thisObject; + + function finalize() { + thisObject = undefined + utilities = undefined + fileStorage = undefined + commons = undefined + } + + function initialize(callBackFunction) { + + try { + + logger.fileName = MODULE_NAME; + logger.initialize(); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function start(dataFiles, timeFrame, timeFrameLabel, currentDay, interExecutionMemory, callBackFunction) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } + + let products = {} + let mainDependency = {} + let processingDailyFiles + + if (currentDay !== undefined) { + processingDailyFiles = true + } else { + processingDailyFiles = false + } + + /* The first phase here is about checking that we have everything we need at the definition level. */ + let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies + if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } + + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets + if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } + + /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + + /* During the third phase, we need to generate the data of the different products this process produces an output */ + for (let i = 0; i < outputDatasets.length; i++) { + + let outputDatasetNode = outputDatasets[i] + let jsonData // Just build data as Json objects + let outputData // Data built as a result of applying user defined code and formulas at the Data Building Procedure + let singularVariableName // Name of the variable for this product + let recordDefinition // Record as defined by the user at the UI + let fileContent // Here we store the contents of the new data built just before writing it to a file. + let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. + + /* + For each outputDatasetNode in our process output, we will build the information based on our input products. + */ + + recordDefinition = outputDatasetNode.referenceParent.parentNode.record + singularVariableName = outputDatasetNode.referenceParent.parentNode.config.singularVariableName + + /* Check Irregular Periods */ + + if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode !== undefined) { + if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { + resultsWithIrregularPeriods = true + } + } + + /* Build the data */ + jsonData = commons.dataBuildingProcedure( + products, + mainDependency, + recordDefinition, + outputDatasetNode.referenceParent.parentNode.dataBuilding, + singularVariableName, + outputDatasetNode.referenceParent.parentNode.config.codeName, + timeFrame, + timeFrameLabel, + resultsWithIrregularPeriods, + interExecutionMemory, + processingDailyFiles, + currentDay + ) + + /* Add the calculated properties */ + if (outputDatasetNode.referenceParent.parentNode.calculations !== undefined) { + outputData = commons.calculationsProcedure(jsonData, recordDefinition, outputDatasetNode.referenceParent.parentNode.calculations, singularVariableName, timeFrame) + } else { + outputData = jsonData + } + products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] = outputData + } + + /*At the fourth and last phase, we will save the new information generated into files corresponding to each output outputDatasetNode.*/ + let totalFilesWritten = 0 + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let outputData = products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] + let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. + let contextSummary = {} + + /* Check Irregular Periods */ + if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { + resultsWithIrregularPeriods = true + } + + /* Simplifying the access to basic info */ + + contextSummary.dataset = outputDatasetNode.referenceParent.config.codeName + contextSummary.product = outputDatasetNode.referenceParent.parentNode.config.codeName + contextSummary.bot = outputDatasetNode.referenceParent.parentNode.parentNode.config.codeName + contextSummary.dataMine = outputDatasetNode.referenceParent.parentNode.parentNode.parentNode.config.codeName + + let fileContent = commons.generateFileContent(outputData, outputDatasetNode.referenceParent.parentNode.record, resultsWithIrregularPeriods, processingDailyFiles, currentDay, callBackFunction) + commons.writeFile(contextSummary, fileContent, anotherFileWritten, processingDailyFiles, timeFrameLabel, currentDay, callBackFunction) + } + + + function anotherFileWritten() { + totalFilesWritten++ + if (totalFilesWritten === outputDatasets.length) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } +}; diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js new file mode 100644 index 000000000..ec8c25d75 --- /dev/null +++ b/LowFrequencyTrading/Announcements.js @@ -0,0 +1,61 @@ +exports.newAnnouncements = function newAnnouncements(bot, logger) { + + const MODULE_NAME = 'Announcements' + + let thisObject = { + makeAnnoucements: makeAnnoucements, + initialize: initialize, + finalize: finalize + } + + let tradingSystem + let tradingEngine + + return thisObject + + function initialize() { + tradingSystem = bot.simulationState.tradingSystem + tradingEngine = bot.simulationState.tradingEngine + } + + function finalize() { + tradingSystem = undefined + tradingEngine = undefined + bot.SESSION.messagesSent = undefined + } + + function makeAnnoucements(node) { + if (node.announcements !== undefined) { + for (let i = 0; i < node.announcements.length; i++) { + let announcement = node.announcements[i] + let canAnnounce = true + if (announcement.announcementCondition !== undefined) { + let conditionValue + try { + conditionValue = eval(announcement.announcementCondition.code) + } catch (err) { + tradingSystem.errors.push([announcement.announcementCondition.id, err.message]) + } + if (conditionValue !== true) { canAnnounce = false } + } + if (canAnnounce === true) { + let text = announcement.config.text + if (announcement.formula !== undefined) { + try { + text = eval(announcement.formula.code) + } catch (err) { + tradingSystem.errors.push([announcement.formula.id, err.message]) + } + } + + if (bot.SESSION.socialBots !== undefined) { + bot.SESSION.socialBots.announce(text) + tradingSystem.announcements.push([announcement.id, text]) + } else { + tradingSystem.errors.push([announcement.id, 'Could not announce because session does not have Social Bots.']) + } + } + } + } + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/ExchangeAPI.js b/LowFrequencyTrading/ExchangeAPI.js new file mode 100644 index 000000000..633f9a59c --- /dev/null +++ b/LowFrequencyTrading/ExchangeAPI.js @@ -0,0 +1,162 @@ +exports.newExchangeAPI = function newExchangeAPI(bot, logger) { + + let MODULE_NAME = "Exchange API"; + + let thisObject = { + getOrder: getOrder, + createOrder: createOrder, + cancelOrder: cancelOrder, + initialize: initialize, + finalize: finalize + }; + + let tradingSystem + + let exchangeId + let options = {} + let exchange + + const ccxt = require('ccxt') + + return thisObject; + + function initialize() { + tradingSystem = bot.simulationState.tradingSystem + + exchangeId = bot.exchange.toLowerCase() + + let key = bot.KEY + let secret = bot.SECRET + + const exchangeClass = ccxt[exchangeId] + const exchangeConstructorParams = { + 'apiKey': key, + 'secret': secret, + 'timeout': 30000, + 'enableRateLimit': true, + verbose: false, + options: options + } + exchange = new exchangeClass(exchangeConstructorParams) + } + + function finalize() { + tradingSystem = undefined + exchangeId = undefined + options = undefined + exchange = undefined + } + + async function getOrder(tradingSystemOrder, tradingEngineOrder) { + + let orderId = tradingEngineOrder.exchangeId.value + + /* Basic Logging */ + logInfo("getOrder -> Entering function. orderId = " + orderId); + + const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset + + /* Basic Validations */ + if (exchange.has['fetchOrder'] === false) { + logError("getOrder -> Exchange does not support fetchOrder command."); + return + } + + try { + let order = await exchange.fetchOrder(orderId, symbol) + return order + } catch (err) { + tradingSystem.errors.push([tradingSystemOrder.id, err.message]) + logError("getOrder -> Error = " + err.message); + } + } + + async function createOrder(tradingSystemOrder, tradingEngineOrder) { + + let price = tradingEngineOrder.rate.value // CCXT: how much quote currency you are willing to pay for a trade lot of base currency (for limit orders only) + let amount = tradingEngineOrder.size.value // CCXT: how much of currency you want to trade + let type // CCXT: a string literal type of order, ccxt currently unifies market and limit orders only + let side // CCXT: a string literal for the direction of your order, buy or sell + let symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset // CCXT: a string literal symbol of the market you wish to trade on, like BTC/USD, ZEC/ETH, DOGE/DASH, etc + + switch (tradingSystemOrder.type) { + case 'Market Buy Order': { + type = 'market' + side = 'buy' + break + } + case 'Market Sell Order': { + type = 'market' + side = 'sell' + break + } + case 'Limit Buy Order': { + type = 'limit' + side = 'buy' + break + } + case 'Limit Sell Order': { + type = 'limit' + side = 'sell' + break + } + } + + /* Basic Logging */ + logInfo("createOrder -> symbol = " + symbol); + logInfo("createOrder -> side = " + side); + logInfo("createOrder -> type = " + type); + logInfo("createOrder -> amount = " + amount); + logInfo("createOrder -> price = " + price); + + /* Basic Validations */ + if (side !== "buy" && side !== "sell") { + logError("createOrder -> side must be either 'buy' or 'sell'."); + return + } + if (exchange.has['createOrder'] === false) { + logError("createOrder -> Exchange does not support createOrder command."); + return + } + + try { + let order = await (exchange.createOrder(symbol, type, side, amount, price)) + return order.id + } catch (err) { + tradingSystem.errors.push([tradingSystemOrder.id, err.message]) + logError("getOrder -> Error = " + err.message); + } + } + + async function cancelOrder(tradingSystemOrder, tradingEngineOrder) { + + let orderId = tradingEngineOrder.exchangeId.value + + /* Basic Logging */ + logInfo("getOrder -> Entering function. orderId = " + orderId); + + const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset + + /* Basic Validations */ + if (exchange.has['fetchOrder'] === false) { + logError("getOrder -> Exchange does not support fetchOrder command."); + return + } + + try { + let order = await exchange.cancelOrder(orderId, symbol) + return true + } catch (err) { + tradingSystem.errors.push([tradingSystemOrder.id, err.message]) + logError("getOrder -> Error = " + err.message); + } + } + + function logInfo(message) { + logger.write(MODULE_NAME, '[INFO] ' + message) + } + + function logError(message) { + logger.write(MODULE_NAME, '[ERROR] ' + message) + } +}; diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js new file mode 100644 index 000000000..89c10767b --- /dev/null +++ b/LowFrequencyTrading/Snapshots.js @@ -0,0 +1,336 @@ +exports.newSnapshots = function newSnapshots(bot, logger) { + /* + This module encapsulates the snapshots functionality. + */ + const MODULE_NAME = 'Snapshots' + + let thisObject = { + reset: reset, + strategyEntry: strategyEntry, + strategyExit: strategyExit, + positionEntry: positionEntry, + positionExit: positionExit, + addCodeToSnapshot: addCodeToSnapshot, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + let tradingSystem + let sessionParameters + + const SNAPSHOTS_FOLDER_NAME = 'Snapshots' + + /* Snapshots of Strategies and Positions */ + let snapshots = { + headers: [], + strategies: [], + positions: [] + } + + let positionKeys = new Map() // This is to prevent adding the same indicator / variable to the output collection. + let strategyKeys = new Map() + let createHeaders = true // This is going to be true only once + let createCloseHeaders = true // This is going to be true only once + let closeValues + let strategyValues + let addToStrategyValues + let positionValues + let addToPositionValues + + let chart + + return thisObject + + function initialize() { + tradingEngine = bot.simulationState.tradingEngine + tradingSystem = bot.simulationState.tradingSystem + sessionParameters = bot.SESSION.parameters + } + + function finalize() { + writeSnapshotFiles() + tradingEngine = undefined + tradingSystem = undefined + sessionParameters = undefined + + positionKeys = undefined + strategyKeys = undefined + + chart = undefined + } + + function reset(pChart) { + /* This function helps reset data structures at every cycle of the simulation loop/=. */ + chart = pChart // We need chat to be a local object accessible from conditions and formulas. + } + + function strategyEntry() { + /* + We will record here a snapshot of all values used in conditions and formulas within the Trading System. + If the strategy exists with a trade, this information will be added to the snapshots file. + */ + + strategyKeys = new Map() + strategyValues = [] + addToStrategyValues = true + + evalNode(tradingSystem) + + /* Turn off this */ + createHeaders = false + addToStrategyValues = false + } + + function strategyExit() { + getResults(tradingEngine.current.strategy.begin.value, tradingEngine.current.strategy.end.value) + let valuesArray = closeValues.concat(strategyValues) + snapshots.strategies.push(valuesArray) + } + + function positionEntry() { + + positionKeys = new Map() + positionValues = [] + addToPositionValues = true + + evalNode(tradingSystem) + + /* Turn off this */ + createHeaders = false + addToPositionValues = false + } + + function positionExit() { + getResults(tradingEngine.current.position.begin.value, tradingEngine.current.position.end.value) + let valuesArray = closeValues.concat(positionValues) + snapshots.positions.push(valuesArray) + } + + function evalNode(node) { + if (node === undefined) { return } + + /* Here we check if there is a codition to be evaluated */ + if (node.type === 'Condition') { + /* We will eval this condition */ + evalCondition(node) + } + + /* Here we check if there is a formula to be evaluated */ + if (node.type === 'Formula') { + if (node.code !== undefined) { + /* We will eval this formula */ + evalFormula(node) + } + } + + /* Now we go down through all this node children */ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (node[property.name] !== undefined) { + evalNode(node[property.name]) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (node[property.name] !== undefined) { + let nodePropertyArray = node[property.name] + for (let m = 0; m < nodePropertyArray.length; m++) { + evalNode(nodePropertyArray[m]) + } + } + break + } + } + } + } + } + + function evalCondition(node) { + + /* + The code can be at the condition node if it was done with the Conditions Editor, or it can also be + at a Javascript Code node. If there is a Javascript object we will give it preference and take the code + from there. Otherwise we will take the code from the Condition node. + */ + + let nodeWithCode = node + if (node.javascriptCode !== undefined) { + if (node.javascriptCode.code !== undefined) { + nodeWithCode = node.javascriptCode + } + } + addCodeToSnapshot(nodeWithCode) + } + + function evalFormula(node) { + let nodeWithCode = node + addCodeToSnapshot(nodeWithCode) + } + + function addCodeToSnapshot(nodeWithCode) { + if (nodeWithCode === undefined) { return } + if (nodeWithCode.code === undefined) { return } + + try { + let instructionsArray = nodeWithCode.code.split(' ') + for (let i = 0; i < instructionsArray.length; i++) { + let instruction = instructionsArray[i] + instruction = instruction.replace('(', '') + instruction = instruction.replace(')', '') + instruction = instruction.replace(//g, '') + instruction = instruction.replace(/<=/g, '') + instruction = instruction.replace(/>=/g, '') + instruction = instruction.replace(/!=/g, '') + instruction = instruction.replace(/!==/g, '') + instruction = instruction.replace(/==/g, '') + instruction = instruction.replace(/===/g, '') + instruction = instruction.replace(/{/g, '') + instruction = instruction.replace(/}/g, '') + if (instruction.indexOf('chart') >= 0) { + let parts = instruction.split('.') + let timeFrame = parts[1] + let product = parts[2] + let property + checkPrevious(3) + function checkPrevious(index) { + property = parts[index] + if (property === 'previous') { + product = product + '.previous' + checkPrevious(index + 1) + } + } + + // Example: chart.at01hs.popularSMA.sma200 - chart.at01hs.popularSMA.sma100 < 10 + if (timeFrame !== 'atAnyTimeFrame') { + timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) + } + let key = timeFrame + '-' + product + '-' + property + let existingKey + + if (addToStrategyValues === true) { + addValues(strategyKeys, strategyValues) + } + + if (addToPositionValues === true) { + addValues(positionKeys, positionValues) + } + + function addValues(keys, values) { + existingKey = keys.get(key) + + if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. + keys.set(key, key) + if (createHeaders === true) { + snapshots.headers.push(key) + } + + let value = eval(instruction) + values.push(value) + } + } + } + } + } catch (err) { + tradingSystem.errors.push([nodeWithCode.id, err.message]) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> nodeWithCode.code = ' + nodeWithCode.code) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) + } + } + + function getResults(openDatetime, closeDatetime) { + + let closeHeaders = ['Trade Number', 'Open Datetime', 'Close Datetime', 'Strategy Name', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] + closeValues = [ + tradingEngine.episode.episodeCounters.positions.value, // Position Number + (new Date(openDatetime)).toISOString(), // Open Datetime + (new Date(closeDatetime)).toISOString(), // Open Datetime + tradingEngine.current.strategy.strategyName.value, // Strategy Name + tradingEngine.current.strategy.situationName.value, // Trigger On Situation + tradingEngine.current.position.situationName.value, // Take Position Situation + tradingEngine.current.position.positionStatistics.hitFail.value, // Result + tradingEngine.current.position.positionStatistics.ROI.value, // ROI + tradingEngine.current.position.exitType.value // Exit Type + ] + + if (createCloseHeaders === true) { + snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshots.headers))) + createCloseHeaders = false + } + } + + function writeSnapshotFiles() { + + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + writeSnapshotFile(snapshots.strategies, 'Strategies') + } + } + + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + writeSnapshotFile(snapshots.positions, 'Positions') + } + } + } + + function writeSnapshotFile(snapshotArray, pFileName) { + try { + const FILE_STORAGE = require('../FileStorage.js'); + let fileStorage = FILE_STORAGE.newFileStorage(logger); + + let fileContent = ""; + let separator = "\r\n"; + + parseRecord(snapshots.headers) + + for (let i = 0; i < snapshotArray.length; i++) { + let record = snapshotArray[i]; + parseRecord(record) + } + + function parseRecord(record) { + for (let j = 0; j < record.length; j++) { + let property = record[j] + + fileContent = fileContent + '' + property + if (j !== record.length - 1) { + fileContent = fileContent + "," + } + } + fileContent = fileContent + separator + } + + fileContent = "" + fileContent + ""; + + let fileName = pFileName + '.csv'; + let filePath = bot.filePathRoot + "/Output/" + bot.SESSION.folderName + "/" + SNAPSHOTS_FOLDER_NAME; + filePath += '/' + fileName + + fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> onFileCreated -> market = " + market.baseAsset + "_" + market.quotedAsset); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> err = " + err.stack); + } + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js new file mode 100644 index 000000000..e10bfe927 --- /dev/null +++ b/LowFrequencyTrading/TradingEngine.js @@ -0,0 +1,207 @@ +exports.newTradingEngine = function newTradingEngine(bot, logger) { + /* + We call the Trading Engine to the data structure that is needed in order to exevute the + trading protocol with the specific rules defined at the Trading System. + */ + const MODULE_NAME = 'Trading Engine' + let thisObject = { + getNodeById: getNodeById, + cloneValues: cloneValues, + setCurrentCandle: setCurrentCandle, + updateEpisodeCountersAndStatistics: updateEpisodeCountersAndStatistics, + updateDistanceToEventsCounters: updateDistanceToEventsCounters, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + let sessionParameters + let nodesMap = new Map() + + return thisObject + + function initialize() { + /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed */ + + tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters + + if (bot.FIRST_EXECUTION === true) { + initializeNode(tradingEngine) + tradingEngine.current.balance.baseAsset.value = sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.current.balance.quotedAsset.value = sessionParameters.sessionQuotedAsset.config.initialBalance + } + } + + function finalize() { + tradingEngine = undefined + sessionParameters = undefined + nodesMap = undefined + } + + function getNodeById(NodeId) { + return nodesMap.get(NodeId) + } + + function setCurrentCandle(candle) { + cloneValues(tradingEngine.current.candle, tradingEngine.previous.candle) + + tradingEngine.current.candle.begin.value = candle.begin + tradingEngine.current.candle.end.value = candle.end + tradingEngine.current.candle.open.value = candle.open + tradingEngine.current.candle.close.value = candle.close + tradingEngine.current.candle.min.value = candle.min + tradingEngine.current.candle.max.value = candle.max + } + + function cloneValues(originNode, destinationNode) { + if (originNode === undefined) { return } + if (destinationNode === undefined) { return } + + /* Here copy the node value */ + destinationNode.value = originNode.value + + /* Now we go down through all the children of the origin node, assuming the destination node has the same children structure*/ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(originNode.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (originNode[property.name] !== undefined && destinationNode[property.name] !== undefined) { + cloneValues(originNode[property.name], destinationNode[property.name]) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (originNode[property.name] !== undefined && destinationNode[property.name] !== undefined) { + let originNodePropertyArray = originNode[property.name] + let destinationNodePropertyArray = destinationNode[property.name] + for (let m = 0; m < originNodePropertyArray.length; m++) { + cloneValues(originNodePropertyArray[m], destinationNodePropertyArray[m]) + } + } + break + } + } + } + } + } + + function initializeNode(node) { + if (node === undefined) { return } + /* Add to Node Map */ + nodesMap.set(node.id, node) + + /* Here we initialize the node value */ + if (node.config !== undefined) { + if (node.config.initialValue !== undefined) { + node.value = node.config.initialValue + } + } + node.initialize = initializeNode // This will allow anyone to initialize this node and its children. + + /* Now we go down through all this node children */ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (node[property.name] !== undefined) { + initializeNode(node[property.name]) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (node[property.name] !== undefined) { + let nodePropertyArray = node[property.name] + for (let m = 0; m < nodePropertyArray.length; m++) { + initializeNode(nodePropertyArray[m]) + } + } + break + } + } + } + } + } + + function updateEpisodeCountersAndStatistics() { + tradingEngine.episode.episodeCounters.periods.value++ + tradingEngine.episode.episodeStatistics.days.value = tradingEngine.episode.episodeCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + } + + function updateDistanceToEventsCounters() { + /* Keeping Distance Counters Up-to-date while avoinding counting before the first event happens. */ + if ( + tradingEngine.current.distanceToEvent.triggerOn.value > 0 + ) { + tradingEngine.current.distanceToEvent.triggerOn.value++ + } + + if ( + tradingEngine.current.distanceToEvent.triggerOff.value > 0 + ) { + tradingEngine.current.distanceToEvent.triggerOff.value++ + } + + if ( + tradingEngine.current.distanceToEvent.takePosition.value > 0 + ) { + tradingEngine.current.distanceToEvent.takePosition.value++ + } + + if ( + tradingEngine.current.distanceToEvent.closePosition.value > 0 + ) { + tradingEngine.current.distanceToEvent.closePosition.value++ + } + + if ( + tradingEngine.current.distanceToEvent.nextPhase.value > 0 + ) { + tradingEngine.current.distanceToEvent.nextPhase.value++ + } + + if ( + tradingEngine.current.distanceToEvent.moveToPhase.value > 0 + ) { + tradingEngine.current.distanceToEvent.moveToPhase.value++ + } + + if ( + tradingEngine.current.distanceToEvent.createOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.createOrder.value++ + } + + if ( + tradingEngine.current.distanceToEvent.cancelOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.cancelOrder.value++ + } + + if ( + tradingEngine.current.distanceToEvent.closeOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.closeOrder.value++ + } + } + +} + diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js new file mode 100644 index 000000000..b588689b7 --- /dev/null +++ b/LowFrequencyTrading/TradingExecution.js @@ -0,0 +1,586 @@ +exports.newTradingExecution = function newTradingExecution(bot, logger, tradingEngineModule) { + /* + The Trading Execution modules manages the execution of orders against the exchanges. + */ + const MODULE_NAME = 'Trading Execution' + + let thisObject = { + checkExecution: checkExecution, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + let tradingSystem + let sessionParameters + + const EXCHANGE_API_MODULE = require('./ExchangeAPI.js') + let exchangeAPIModule = EXCHANGE_API_MODULE.newExchangeAPI(bot, logger) + + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') + let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + + return thisObject + + function initialize() { + tradingSystem = bot.simulationState.tradingSystem + tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters + + exchangeAPIModule.initialize() + announcementsModule.initialize() + } + + function finalize() { + tradingSystem = undefined + tradingEngine = undefined + sessionParameters = undefined + + exchangeAPIModule.finalize() + exchangeAPIModule = undefined + + announcementsModule.finalize() + announcementsModule = undefined + } + + async function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { + + /* Enforcing Precision Limit */ + stageSizeLimit.value = global.PRECISE(stageSizeLimit.value, 10) + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + + await checkExecutionAlgorithms(executionNode) + + async function checkExecutionAlgorithms(executionNode) { + for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { + let executionAlgorithm = executionNode.executionAlgorithms[i] + await checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + } + } + + async function checkOrders(orders, executionAlgorithm, executionNode) { + for (let i = 0; i < orders.length; i++) { + + let tradingSystemOrder = orders[i] + + /* Trading System Validations */ + if (tradingSystemOrder.config.positionSizePercentage === undefined) { continue } + if (tradingSystemOrder.referenceParent === undefined) { continue } + + let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) + + /* Trading Engine Validations */ + if (tradingEngineOrder.serialNumber === undefined) { continue } + if (tradingEngineOrder.identifier === undefined) { continue } + if (tradingEngineOrder.exchangeId === undefined) { continue } + if (tradingEngineOrder.begin === undefined) { continue } + if (tradingEngineOrder.end === undefined) { continue } + if (tradingEngineOrder.rate === undefined) { continue } + if (tradingEngineOrder.size === undefined) { continue } + if (tradingEngineOrder.status === undefined) { continue } + if (tradingEngineOrder.algorithmName === undefined) { continue } + if (tradingEngineOrder.orderCounters === undefined) { continue } + if (tradingEngineOrder.orderCounters.periods === undefined) { continue } + if (tradingEngineOrder.orderStatistics.days === undefined) { continue } + if (tradingEngineOrder.orderStatistics.percentageFilled === undefined) { continue } + if (tradingEngineOrder.orderStatistics.actualRate === undefined) { continue } + if (tradingEngineOrder.orderStatistics.feesPaid === undefined) { continue } + + switch (tradingEngineOrder.status.value) { + case 'Not Open': { + { + /* Check if we need to Create this Order */ + if (stageIsClosing === true) { continue } + let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + + /* Open a new order */ + await openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) + } + } + break + } + case 'Open': { + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.orderCounters.periods.value++ + tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + + /* Simulate Events that happens at the Exchange, if needed. */ + simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) + + /* Check Events that happens at the Exchange, if needed. */ + await checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) + + /* If the stage is closing or the order is not still Open, we wont be cancelling orders based on defined events */ + if (stageIsClosing !== true && tradingEngineOrder.status.value === 'Open') { + + /* Check if we need to Cancel this Order */ + let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') + await exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') + } + } + } + } + } + } + + async function openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) { + /* Order Size Calculation */ + tradingEngineOrder.size.value = tradingSystem.formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 + if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit.value) { + /* We reduce the size to the remaining size of the position. */ + tradingEngineOrder.size.value = stageSizeLimit.value - stageOrdersSize.value + tradingEngineOrder.size.value = global.PRECISE(tradingEngineOrder.size.value, 10) + } + + /* Check Order Size */ + if (tradingEngineOrder.size.value <= 0) { return } + + /* Order Rate Calculation */ + tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. + if (tradingSystemOrder.positionRate !== undefined) { + if (tradingSystemOrder.positionRate.formula !== undefined) { + tradingEngineOrder.rate.value = tradingSystem.formulas.get(tradingSystemOrder.positionRate.formula.id) + tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) + } + } + + /* Place Order at the Exchange */ + let result = await createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) + if (result !== true) { return } + + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.orders.value++ + + /* Initialize this */ + tradingEngine.current.distanceToEvent.createOrder.value = 1 + + /* Create Order Procedure */ + tradingEngineOrder.status.value = 'Open' + tradingEngineOrder.identifier.value = global.UNIQUE_ID() + tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.serialNumber.value = tradingEngine.episode.episodeCounters.orders.value + tradingEngineOrder.orderName.value = tradingSystemOrder.name + tradingEngineOrder.algorithmName.value = executionAlgorithm.name + tradingEngineOrder.situationName.value = situationName + + /* Update Stage Orders Size */ + stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + } + + async function createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return true + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { + return true + } + } + + let orderId = await exchangeAPIModule.createOrder(tradingSystemOrder, tradingEngineOrder) + + if (orderId !== undefined) { + tradingEngineOrder.exchangeId.value = orderId + return true + } + } + + function simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + break + } + case 'Live Trading Session': { + return + } + case 'Fordward Testing Session': { + return + } + case 'Paper Trading Session': { + break + } + } + + /* Filter by what is defined at the Strategy */ + if (tradingSystemOrder.simulatedExchangeEvents === undefined) { return } + + let previousSizeFilled = tradingEngineOrder.orderStatistics.sizeFilled.value + let previousPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value + let previousAmountReceived = tradingEngineOrder.orderStatistics.amountReceived.value + let previousFeesPaid = tradingEngineOrder.orderStatistics.feesPaid.value + + actualRateSimulation() + feesPaidSimulation() + orderFillingSimulation() + sizeFilledSimulation() + amountReceivedSimulation() + + doTheAccounting(tradingSystemOrder, tradingEngineOrder, previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + + /* If the Stage is Closing and this order is still open, we need to cancel it now */ + if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { + simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') + } + + function actualRateSimulation() { + /* Actual Rate Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { + /* Calculate this only once for this order */ + if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { + tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) + } + } + } else { + switch (tradingEngineOrder.type) { + case 'Market Order': { + /* Actual Rate is simulated based on the Session Paremeters */ + let slippageAmount = tradingEngineOrder.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 + switch (tradingSystemOrder.type) { + case 'Market Sell Order': { + tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value - slippageAmount + break + } + case 'Market Buy Order': { + tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value + slippageAmount + break + } + } + break + } + case 'Limit Order': { + /* In Limit Orders the actual rate is the rate of the order, there is no slippage */ + tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value + break + } + } + } + tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) + } + + function feesPaidSimulation() { + /* Fees Paid Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { + if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 + } + } + } else { + /* Fees are simulated based on the Session Paremeters */ + switch (tradingEngineOrder.type) { + case 'Market Order': { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + break + } + case 'Limit Order': { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.maker / 100 + break + } + } + } + tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) + } + + function orderFillingSimulation() { + /* Order Filling Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { + + /* Percentage Filled */ + let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 + if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { + percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + } + tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled + tradingEngineOrder.orderStatistics.percentageFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.percentageFilled.value, 10) + + /* Check if we need to close */ + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + } + } + } + + function sizeFilledSimulation() { + /* Size Filled */ + tradingEngineOrder.orderStatistics.sizeFilled.value = tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 + tradingEngineOrder.orderStatistics.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.sizeFilled.value, 10) + } + + function amountReceivedSimulation() { + /* Amount Received */ + tradingEngineOrder.orderStatistics.amountReceived.value = tradingEngineOrder.orderStatistics.sizeFilled.value * tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate + tradingEngineOrder.orderStatistics.amountReceived.value = global.PRECISE(tradingEngineOrder.orderStatistics.amountReceived.value, 10) + } + } + + async function checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return true + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { + return true + } + } + + let order = await exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) + + if (order === undefined) { return } + + const AT_EXCHANGE_STATUS = { + OPEN: 'open', + CLOSED: 'closed', + CANCELLED: 'canceled' + } + + /* Status Checks */ + if (order.remaining === 0 && order.status === AT_EXCHANGE_STATUS.CLOSED) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + if (order.remaining > 0 && order.status === AT_EXCHANGE_STATUS.CLOSED) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Closed at the Exchange' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + if (order.status === AT_EXCHANGE_STATUS.CANCELLED) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Cancelled at the Exchange' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + + syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) + + /* Forced Cancellation Check */ + if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { + await exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') + } + } + + function syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) { + + let previousSizeFilled = tradingEngineOrder.orderStatistics.sizeFilled.value + let previousPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value + let previousAmountReceived = tradingEngineOrder.orderStatistics.amountReceived.value + let previousFeesPaid = tradingEngineOrder.orderStatistics.feesPaid.value + + /* Fees Paid */ + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value - order.amount + tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) + + /* Percentage Filled */ + tradingEngineOrder.orderStatistics.percentageFilled.value = order.filled * 100 / (order.filled + order.remaining) + tradingEngineOrder.orderStatistics.percentageFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.percentageFilled.value, 10) + + /* Size Filled */ + tradingEngineOrder.orderStatistics.sizeFilled.value = order.filled + tradingEngineOrder.orderStatistics.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.sizeFilled.value, 10) + + /* Amount Received */ + tradingEngineOrder.orderStatistics.amountReceived.value = order.cost + tradingEngineOrder.orderStatistics.amountReceived.value = global.PRECISE(tradingEngineOrder.orderStatistics.amountReceived.value, 10) + + /* Actual Rate */ + tradingEngineOrder.orderStatistics.actualRate.value = order.price + tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) + + doTheAccounting(tradingSystemOrder, tradingEngineOrder, previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + } + + function doTheAccounting(tradingSystemOrder, tradingEngineOrder, previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) { + + /* Unaccount for the previous Stage Filled Size */ + stageFilledSize.value = stageFilledSize.value - tradingEngineOrder.size.value * previousPercentageFilled / 100 + /* Account for the current Stage Filled Size */ + stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + + + /* Balances Update */ + switch (true) { + case tradingSystemOrder.type === 'Market Buy Order' || tradingSystemOrder.type === 'Limit Buy Order': { + + /* Undo the previous accounting */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - previousFeesPaid - previousSizeFilled + /* Account the current filling and fees */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngineOrder.orderStatistics.sizeFilled.value + tradingEngineOrder.orderStatistics.feesPaid.value + + /* Undo the previous accounting */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value + previousAmountReceived + /* Account the current filling and fees */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngineOrder.orderStatistics.amountReceived.value + + break + } + case tradingSystemOrder.type === 'Market Sell Order' || tradingSystemOrder.type === 'Limit Sell Order': { + + /* Undo the previous accounting */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + previousFeesPaid + previousSizeFilled + /* Account the current filling and fees */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngineOrder.orderStatistics.sizeFilled.value - tradingEngineOrder.orderStatistics.feesPaid.value + + /* Undo the previous accounting */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value - previousAmountReceived + /* Account the current filling and fees */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngineOrder.orderStatistics.amountReceived.value + + break + } + } + } + + function simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + break + } + case 'Live Trading Session': { + return + } + case 'Fordward Testing Session': { + return + } + case 'Paper Trading Session': { + break + } + } + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = exitType + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ + let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 + stageOrdersSize.value = stageOrdersSize.value - unfilledSize + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + } + + async function exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { + return + } + } + + /* Check if we can cancel the order at the Exchange. */ + let result = await exchangeAPIModule.cancelOrder(tradingSystemOrder, tradingEngineOrder) + if (result === true) { + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = exitType + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ + let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 + stageOrdersSize.value = stageOrdersSize.value - unfilledSize + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + + /* + Perhaps the order was filled a bit more between the last time we checked and when it was cancelled. + To sync our accounting, we need to check the order one last time and if it changed, fix it. + */ + + let order = await exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) + + if (order === undefined) { return } + + syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) + } + } + + function checkOrderEvent(event, order, executionAlgorithm, executionNode) { + if (event !== undefined) { + for (let k = 0; k < event.situations.length; k++) { + let situation = event.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(event.id) + tradingSystem.highlights.push(order.id) + tradingSystem.highlights.push(executionAlgorithm.id) + tradingSystem.highlights.push(executionNode.id) + + announcementsModule.makeAnnoucements(event) + return situation.name // if the event is triggered, we return the name of the situation that passed + } + } + } + } + } +} + diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js new file mode 100644 index 000000000..0abe8c40a --- /dev/null +++ b/LowFrequencyTrading/TradingOutput.js @@ -0,0 +1,276 @@ +exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FILE_STORAGE) { + /* + 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 = { + initialize: initialize, + finalize: finalize, + start: start + } + + let utilities = UTILITIES.newCloudUtilities(logger) + let fileStorage = FILE_STORAGE.newFileStorage(logger) + + return thisObject + + function finalize() { + thisObject = undefined + utilities = undefined + fileStorage = undefined + commons = undefined + } + + function initialize(callBackFunction) { + try { + logger.fileName = MODULE_NAME + logger.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) { + try { + bot.processingDailyFiles + if (timeFrame > global.dailyFilePeriods[0][0]) { + bot.processingDailyFiles = false + } else { + bot.processingDailyFiles = true + } + + /* Preparing everything for the Simulation */ + const TRADING_SIMULATION = require('./TradingSimulation.js') + let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) + + 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) { + /* + Here is where the Trading Engine and Trading Systems received are moved to the simulation state. + */ + bot.simulationState.tradingEngine = bot.TRADING_ENGINE + bot.simulationState.tradingSystem = bot.TRADING_SYSTEM + initializeOutputs() + } else { + readFiles() + } + return + + function initializeOutputs() { + if (bot.processingDailyFiles) { + initializeDailyFiles() + } else { + initializeMarketFiles() + } + } + + function initializeDailyFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Daily Files') { + outputDatasetsMap.set(dataset.parentNode.config.codeName, []) + } + } + runSimulation() + } + + function initializeMarketFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Market Files') { + outputDatasetsMap.set(dataset.parentNode.config.codeName, []) + } + } + runSimulation() + } + + 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() + } else { + readMarketFiles() + } + } + + function readMarketFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Market Files') { + + 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) + } + } + } + + function readDailyFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Daily Files') { + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + 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) + } + } + } + + function readOutputFile(fileName, filePath, productName) { + totalFilesToBeRead++ + filePath += '/' + fileName + fileStorage.getTextFile(filePath, onFileRead, true) + + function onFileRead(err, text) { + try { + + 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) + } + } + } + + function anotherFileRead() { + totalFilesRead++ + if (totalFilesRead === totalFilesToBeRead) { + runSimulation() + } + } + + function runSimulation() { + tradingSimulation.runSimulation( + chart, + outputDatasetsMap, + writeFiles, + callBackFunction) + } + + function writeFiles() { + /* + The output of files which were appended with information during the simulation execution, now needs to be saved. + */ + if (bot.processingDailyFiles) { + writeDailyFiles() + } else { + writeMarketFiles() + } + } + + function writeMarketFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Market Files') { + + 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) + } + } + } + + function writeDailyFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Daily Files') { + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + 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) + } + } + } + + function writeOutputFile(fileName, filePath, productName) { + totalFilesToBeWritten++ + filePath += '/' + fileName + let fileContent = JSON.stringify(outputDatasetsMap.get(productName)) + + fileStorage.createTextFile(filePath, fileContent, onFileCreated) + + 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) + } + } + + } catch (err) { + logger.write(MODULE_NAME, '[ERROR] start -> err = ' + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } +} + diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js new file mode 100644 index 000000000..8e38d100d --- /dev/null +++ b/LowFrequencyTrading/TradingPosition.js @@ -0,0 +1,199 @@ +exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEngineModule) { + /* + This module packages all functions related to Positions. + */ + const MODULE_NAME = 'Trading Position' + let thisObject = { + openPosition: openPosition, + closingPosition: closingPosition, + closePosition: closePosition, + applyStopLossFormula: applyStopLossFormula, + applyTakeProfitFormula: applyTakeProfitFormula, + preventStopLossDistortion: preventStopLossDistortion, + applySlippageToStopLoss: applySlippageToStopLoss, + preventTakeProfitDistortion: preventTakeProfitDistortion, + applySlippageToTakeProfit: applySlippageToTakeProfit, + updateStopLoss: updateStopLoss, + updateTakeProfit: updateTakeProfit, + updateSizeAndRate: updateSizeAndRate, + updateEnds: updateEnds, + updateStatus: updateStatus, + updateCounters: updateCounters, + updateStatistics: updateStatistics, + resetPosition: resetPosition, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + let sessionParameters + + return thisObject + + function initialize() { + tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters + } + + function finalize() { + tradingEngine = undefined + sessionParameters = undefined + } + + function openPosition(situationName) { + tradingEngine.current.position.status.value = 'Open' + tradingEngine.current.position.serialNumber.value = tradingEngine.episode.episodeCounters.positions.value + tradingEngine.current.position.identifier.value = global.UNIQUE_ID() + tradingEngine.current.position.begin.value = tradingEngine.current.candle.begin.value + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + + tradingEngine.current.position.situationName.value = situationName + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Open Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Open Stage' + tradingEngine.current.position.stopLoss.stopLossPhase.value = 0 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = 0 + } + + function closingPosition(exitType) { + tradingEngine.current.position.status.value = 'Closing' + tradingEngine.current.position.exitType.value = exitType + } + + function closePosition() { + tradingEngine.current.position.status.value = 'Closed' + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + /* + Now that the position is closed, it is the right time to move this position from current to last at the Trading Engine data structure. + */ + tradingEngineModule.cloneValues(tradingEngine.current.position, tradingEngine.last.position) + } + + function resetPosition() { + tradingEngine.current.position.initialize(tradingEngine.current.position) + } + + function applyStopLossFormula(formulas, formulaId) { + tradingEngine.current.position.stopLoss.value = formulas.get(formulaId) + } + + function applyTakeProfitFormula(formulas, formulaId) { + tradingEngine.current.position.takeProfit.value = formulas.get(formulaId) + } + + function updateStopLoss(phase, stage) { + tradingEngine.current.position.stopLoss.stopLossPhase.value = phase + tradingEngine.current.position.stopLoss.stopLossStage.value = stage + } + + function updateTakeProfit(phase, stage) { + tradingEngine.current.position.takeProfit.takeProfitPhase.value = phase + tradingEngine.current.position.takeProfit.takeProfitStage.value = stage + } + + function updateSizeAndRate(size, rate) { + tradingEngine.current.position.size.value = global.PRECISE(size, 10) + tradingEngine.current.position.rate.value = global.PRECISE(rate, 10) + + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value + } + + function preventStopLossDistortion() { + /* + Hit Point Validation + + This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + if (bot.sessionAndMarketBaseAssetsAreEqual) { + if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value + } + } else { + if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.max.value + } + } + } + + function applySlippageToStopLoss() { + let slippedStopLoss = tradingEngine.current.position.stopLoss.value + + /* Apply the Slippage */ + let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + slippedStopLoss = slippedStopLoss + slippageAmount + } else { + slippedStopLoss = slippedStopLoss - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedStopLoss + } + + function preventTakeProfitDistortion() { + /* + Hit Point Validation: + + This prevents misscalculations when a formula places the take profit in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + if (bot.sessionAndMarketBaseAssetsAreEqual) { + if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value + } + } else { + if (tradingEngine.current.position.takeProfit.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.min.value + } + } + } + + function applySlippageToTakeProfit() { + let slippedTakeProfit = tradingEngine.current.position.takeProfit.value + /* Apply the Slippage */ + let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + slippedTakeProfit = slippedTakeProfit + slippageAmount + } else { + slippedTakeProfit = slippedTakeProfit - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedTakeProfit + } + + function updateEnds() { + if (tradingEngine.current.position.status.value === 'Open') { + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + } + } + + function updateStatus() { + if (tradingEngine.current.position.status.value === 'Closed') { + resetPosition() + } + } + + function updateCounters() { + if (tradingEngine.current.position.status.value === 'Open') { + tradingEngine.current.position.positionCounters.periods.value++ + } + } + + function updateStatistics() { + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.current.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.current.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value + } else { + tradingEngine.current.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.current.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value + } + tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + if (tradingEngine.current.position.positionStatistics.ROI.value > 0) { + tradingEngine.current.position.positionStatistics.hitFail.value = 'HIT' + } else { + tradingEngine.current.position.positionStatistics.hitFail.value = 'FAIL' + } + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js new file mode 100644 index 000000000..eb23e6ae6 --- /dev/null +++ b/LowFrequencyTrading/TradingProcess.js @@ -0,0 +1,767 @@ +exports.newTradingProcess = function newTradingProcess(bot, logger, UTILITIES, TRADING_OUTPUT_MODULE, USER_BOT_COMMONS) { + /* + This Module will load all the process data dependencies from files and send them downstream. + After execution, will save the time range and status report of the process. + */ + const FULL_LOG = true; + const LOG_FILE_CONTENT = false; + const MODULE_NAME = "Trading Process"; + const GMT_SECONDS = ':00.000 GMT+0000'; + + thisObject = { + initialize: initialize, + finalize: finalize, + start: start + }; + + let utilities = UTILITIES.newCloudUtilities(logger); + + let statusDependencies; + let dataDependenciesModule; + let dataFiles = new Map(); + let multiPeriodDataFiles = new Map(); + + let tradingOutputModule; + + const FILE_STORAGE = require('../FileStorage.js'); + let fileStorage = FILE_STORAGE.newFileStorage(logger); + + let processConfig; + + return thisObject; + + function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { + try { + logger.fileName = MODULE_NAME; + logger.initialize(); + + 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); + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function finalize() { + dataFiles = undefined + multiPeriodDataFiles = undefined + statusDependencies = undefined + dataDependenciesModule = undefined + tradingOutputModule.finalize() + tradingOutputModule = undefined + fileStorage = undefined + processConfig = undefined + thisObject = undefined + } + + function start(callBackFunction) { + try { + let currentTimeFrame + let currentTimeFrameLabel + let market = bot.market; + + /* Context Variables */ + + let contextVariables = { + lastFile: undefined, // Datetime of the last file files sucessfully produced by this process. + dateBeginOfMarket: undefined, // Datetime of the first trade file in the whole market history. + dateEndOfMarket: undefined // Datetime of the last file available to be used as an input of this process. + }; + + let previousDay; // Holds the date of the previous day relative to the processing date. + + getContextVariables(); + + function getContextVariables() { + try { + let thisReport; + let reportKey; + let statusReport; + + /* We are going to use the start date as beging of market date. */ + contextVariables.dateBeginOfMarket = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime) + + /* + Here we get the status report from the bot who knows which is the end of the market. + */ + statusReport = statusDependencies.reportsByMainUtility.get("Market Ending Point") + + if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Status Report does not exist. Retrying Later. "); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + return; + } + + if (statusReport.status === "Status Report is corrupt.") { + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + return; + } + + thisReport = statusReport.file; + + if (thisReport.lastFile === undefined) { + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> thisReport = " + JSON.stringify(thisReport)); + + let customOK = { + result: global.CUSTOM_OK_RESPONSE.result, + message: "Dependency not ready." + } + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message); + callBackFunction(customOK); + return; + } + + contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()); + + /* Validation that the data is not up-to-date. */ + if (bot.tradingProcessDate.valueOf() - global.ONE_DAY_IN_MILISECONDS > contextVariables.dateEndOfMarket.valueOf() && bot.SESSION.type !== "Backtesting Session") { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> " + "Head of the market is @ " + contextVariables.dateEndOfMarket); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> You need to UPDATE your datasets in order to run a " + bot.SESSION.type) } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + + } + + /* Finally we get our own Status Report. */ + statusReport = statusDependencies.reportsByMainUtility.get("Self Reference") + + if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Status Report does not exist. Retrying Later. "); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + return; + } + + if (statusReport.status === "Status Report is corrupt.") { + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because self dependecy Status Report is corrupt. Aborting Process."); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + thisReport = statusReport.file; + bot.simulationState = thisReport.simulationState; + 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 (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; + } + contextVariables.lastFile = new Date(thisReport.lastFile); + processSingleFiles(); + return; + + } else { + /* + We are here becuse either: + 1. There is no status report + 2. There is no Last File (this happens on Market Files) + */ + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining of the market because own status report not found or lastFile was undefined."); } + startFromBegining(); + return; + } + + function startFromBegining() { + contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile); } + processSingleFiles(); + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack); + if (err.message === "Cannot read property 'file' of undefined") { + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot configuration to see if all of its statusDependencies declarations are correct. "); + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)); + } + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function processSingleFiles() { + let dependencyIndex = 0; + dataFiles = new Map(); + dependencyLoopBody(); + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + getFile(); + + function getFile() { + if (datasetModule.node.config.codeName !== "Single-File") { + dependencyControlLoop(); + return + } + + if (dataDependenciesModule.isItADepenency('atAnyTimeFrame', datasetModule.node.parentNode.config.singularVariableName) !== true) { + dependencyControlLoop(); + return + } + + let fileName = "Data.json"; + let filePath = datasetModule.node.parentNode.config.codeName + '/' + datasetModule.node.config.codeName; + datasetModule.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + try { + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + if (err.message === 'File does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + } else { + logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); + callBackFunction(err); + } + return; + } + + let dataFile = JSON.parse(text); + dataFiles.set(dependency.id, dataFile); + + dependencyControlLoop(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + } + + function dependencyControlLoop() { + dependencyIndex++ + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody() + } else { + let mapKey = "Single Files" + multiPeriodDataFiles.set(mapKey, dataFiles) + processMarketFiles() + } + } + } + + /* + We do market files first since if the simulation is run on daily files, there will be a loop to getch each daus files and we do not need + that loop to reload market files. + */ + function processMarketFiles() { + let n; + timeFramesLoop(); + + function timeFramesLoop() { + /* + We will iterate through all posible timeFrames. + */ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody(); + } + + function timeFramesLoopBody() { + const timeFrame = global.marketFilesPeriods[n][0]; + const timeFrameLabel = global.marketFilesPeriods[n][1]; + + let dependencyIndex = 0; + dataFiles = new Map(); + + if (bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel) { + currentTimeFrame = global.marketFilesPeriods[n][0]; + currentTimeFrameLabel = global.marketFilesPeriods[n][1]; + } + dependencyLoopBody(); + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + getFile(); + + function getFile() { + if (dependency.referenceParent.config.codeName !== "Multi-Period-Market") { + dependencyControlLoop(); + return + } + + if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { + if (!(bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + dependencyControlLoop(); + return + } + } + + let fileName = "Data.json"; + let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; + datasetModule.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + try { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + if (err.message === 'File does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + } else { + logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); + callBackFunction(err); + } + return; + } + + let dataFile = JSON.parse(text); + let trimmedDataFile = trimDataFile(dataFile, datasetModule.node.parentNode.record) + dataFiles.set(dependency.id, trimmedDataFile); + + dependencyControlLoop(); + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody(); + } else { + let mapKey = global.marketFilesPeriods[n][1]; + multiPeriodDataFiles.set(mapKey, dataFiles) + timeFramesControlLoop(); + } + } + } + + function timeFramesControlLoop() { + n++; + if (n < global.marketFilesPeriods.length) { + timeFramesLoopBody(); + } else { + processDailyFiles(); + } + } + } + + function processDailyFiles() { + let n = 0; + if (currentTimeFrame) { + buildCharts() + return + } + bot.tradingProcessDate = new Date(contextVariables.lastFile.valueOf() - global.ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. + advanceTime(); + + function advanceTime() { + bot.tradingProcessDate = new Date(bot.tradingProcessDate.valueOf() + global.ONE_DAY_IN_MILISECONDS); + previousDay = new Date(bot.tradingProcessDate.valueOf() - global.ONE_DAY_IN_MILISECONDS); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.tradingProcessDate = " + bot.tradingProcessDate); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } + + /* Validation that we are not going past the user defined end date. */ + if (bot.tradingProcessDate.valueOf() >= bot.SESSION.parameters.timeRange.config.finalDatetime) { + + const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } + + bot.SESSION.stop(logText) + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + /* Validation that we are not going past the head of the market. */ + if (bot.tradingProcessDate.valueOf() > contextVariables.dateEndOfMarket.valueOf() + global.ONE_DAY_IN_MILISECONDS - 1) { + + const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + checkStopTaskGracefully(); + } + + function checkStopTaskGracefully() { + /* Validation that we dont need to stop. */ + if (global.STOP_TASK_GRACEFULLY === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + checkStopProcessing(); + } + + function checkStopProcessing() { + /* Validation that we dont need to stop. */ + if (bot.STOP_SESSION === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + timeFramesLoop(); + } + + function timeFramesLoop() { + /* Telling the world we are alive and doing well */ + let processingDate = bot.tradingProcessDate.getUTCFullYear() + '-' + utilities.pad(bot.tradingProcessDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.tradingProcessDate.getUTCDate(), 2); + bot.processHeartBeat(processingDate) + /* + We will iterate through all posible timeFrames. + */ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody(); + } + + function timeFramesLoopBody() { + const timeFrame = global.dailyFilePeriods[n][0]; + const timeFrameLabel = global.dailyFilePeriods[n][1]; + + if (processConfig.framework.validtimeFrames !== undefined) { + let validPeriod = false; + for (let i = 0; i < processConfig.framework.validtimeFrames.length; i++) { + let period = processConfig.framework.validtimeFrames[i]; + if (period === timeFrameLabel) { validPeriod = true } + } + if (validPeriod === false) { + timeFramesControlLoop(); + return; + } + } + + if (bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel) { + currentTimeFrame = global.dailyFilePeriods[n][0]; + currentTimeFrameLabel = global.dailyFilePeriods[n][1]; + } + + let dependencyIndex = 0; + dataFiles = new Map(); + dependencyLoopBody(); + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + let file + getFile() + + function getFile() { + if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { + dependencyControlLoop(); + return + } + + if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { + if (!(bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + dependencyControlLoop(); + return + } + } + + let dateForPath = bot.tradingProcessDate.getUTCFullYear() + '/' + utilities.pad(bot.tradingProcessDate.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.tradingProcessDate.getUTCDate(), 2); + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + let fileName = "Data.json"; + + datasetModule.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + try { + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + return; + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Not Ok -> err = " + err.code); + callBackFunction(err); + return; + } + + file = JSON.parse(text); + dataFiles.set(dependency.id, file); + dependencyControlLoop(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody(); + } else { + let mapKey = global.dailyFilePeriods[n][1]; + multiPeriodDataFiles.set(mapKey, dataFiles) + timeFramesControlLoop(); + } + } + } + + function timeFramesControlLoop() { + n++; + if (n < global.dailyFilePeriods.length) { + timeFramesLoopBody(); + } else { + n = 0; + if (currentTimeFrame !== undefined) { + buildCharts(advanceTime); + } else { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> Time Frame not Recognized. Can not continue."); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + } + + function buildCharts(advanceTime) { + const COMMONS = require('../Commons.js') + let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) + + let chart = {} + let mainDependency = {} + + /* The first phase here is about checking that we have everything we need at the definition level. */ + let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies + if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } + + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets + if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } + + /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ + for (let j = 0; j < global.marketFilesPeriods.length; j++) { + let timeFrameLabel = marketFilesPeriods[j][1] + let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) + let products = {} + + if (dataFiles !== undefined) { + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, currentTimeFrame) + + let propertyName = 'at' + timeFrameLabel.replace('-', '') + chart[propertyName] = products + } + } + + for (let j = 0; j < global.dailyFilePeriods.length; j++) { + let timeFrameLabel = global.dailyFilePeriods[j][1] + let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) + let products = {} + + if (dataFiles !== undefined) { + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, currentTimeFrame) + + let propertyName = 'at' + timeFrameLabel.replace('-', '') + chart[propertyName] = products + } + } + + /* Single Files */ + let dataFiles = multiPeriodDataFiles.get('Single Files') + let products = {} + + if (dataFiles !== undefined) { + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, currentTimeFrame) + + let propertyName = 'atAnyTimeFrame' + chart[propertyName] = products + } + + tradingOutputModule.start( + chart, + currentTimeFrame, + currentTimeFrameLabel, + bot.tradingProcessDate, + onOutputGenerated); + + function onOutputGenerated(err) { + try { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err); + return; + } + + bot.FIRST_EXECUTION = false // From here on, all other loops executions wont be the first execution. + + if (currentTimeFrame > global.dailyFilePeriods[0][0]) { + writeMarketStatusReport(onMarketStatusReport) + } else { + writeDataRanges(currentTimeFrameLabel, onWritten); + } + + function onWritten(err) { + try { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onWritten -> err = " + err.stack); + callBackFunction(err); + return; + } + + writeDailyStatusReport(bot.tradingProcessDate, onDailyStatusReport); + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onWritten -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function onDailyStatusReport() { + advanceTime(); + } + + function onMarketStatusReport() { + /* This is where we check if we need reached the user defined end datetime. */ + const now = new Date() + if (now.valueOf() >= bot.SESSION.parameters.timeRange.config.finalDatetime) { + const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onMarketStatusReport -> " + logText); } + + bot.SESSION.stop('because it just finished.') + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + if (bot.SESSION.type === 'Backtesting Session') { + bot.SESSION.stop('Backtesting Session over Market Files Finished.') + } + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + + function writeDataRanges(currentTimeFrameLabel, callBack) { + let outputDatasetIndex = -1; + controlLoop(); + + function productLoopBody() { + let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; + writeDataRange(contextVariables.dateBeginOfMarket, bot.tradingProcessDate, productCodeName, currentTimeFrameLabel, controlLoop); + } + + function controlLoop() { + outputDatasetIndex++; + if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { + productLoopBody(); + } else { + callBack(global.DEFAULT_OK_RESPONSE); + } + } + } + + function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel, callBack) { + let dataRange = { + begin: pBegin.valueOf(), + end: pEnd.valueOf() + global.ONE_DAY_IN_MILISECONDS + }; + + let fileContent = JSON.stringify(dataRange); + let fileName = '/Data.Range.json'; + let filePath = bot.filePathRoot + "/Output/" + bot.SESSION.folderName + "/" + productCodeName + "/" + 'Multi-Period-Daily' + fileName; + + fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); + callBack(err); + return; + } + + if (LOG_FILE_CONTENT === true) { + logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent); + } + + let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset + let event = { + dateRange: dataRange + } + + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) + + callBack(global.DEFAULT_OK_RESPONSE); + } + } + + function writeDailyStatusReport(lastFileDate, callBack) { + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName + let thisReport = statusDependencies.statusReports.get(reportKey); + + thisReport.file.lastExecution = bot.currentDaytime; + thisReport.file.lastFile = lastFileDate; + thisReport.file.simulationState = bot.simulationState; + thisReport.save(callBack); + } + + function writeMarketStatusReport(callBack) { + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName + let thisReport = statusDependencies.statusReports.get(reportKey); + + thisReport.file.lastExecution = bot.processDatetime; + thisReport.file.simulationState = bot.simulationState; + + logger.newInternalLoop(bot.codeName, bot.process, bot.tradingProcessDate); + thisReport.save(callBack); + } + + function trimDataFile(dataFile, recordDefinition) { + /* + Here we will discard all the records in a file that are outside of the current time range. + */ + let beginIndex + let endIndex + let result = [] + for (let i = 0; i < recordDefinition.properties.length; i++) { + let property = recordDefinition.properties[i] + if (property.config.codeName === 'begin') { + beginIndex = i + } + if (property.config.codeName === 'end') { + endIndex = i + } + } + for (let i = 0; i < dataFile.length; i++) { + let dataRecord = dataFile[i] + let begin = dataRecord[beginIndex] + let end = dataRecord[endIndex] + if (end < bot.SESSION.parameters.timeRange.config.initialDatetime) { continue } + if (begin > bot.SESSION.parameters.timeRange.config.finalDatetime) { continue } + result.push(dataRecord) + } + return result + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function pad(str, max) { + str = str.toString(); + return str.length < max ? pad(" " + str, max) : str; + } +}; diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js new file mode 100644 index 000000000..789d67feb --- /dev/null +++ b/LowFrequencyTrading/TradingRecords.js @@ -0,0 +1,321 @@ +exports.newTradingRecords = function newTradingRecords(bot, logger) { + /* + This module facilitates the appending of records to the output of the process. + */ + const MODULE_NAME = 'Trading Records' + + let thisObject = { + appendRecords: appendRecords, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + let tradingSystem + let sessionParameters + let outputDatasetsMap + + return thisObject + + function initialize(pOutputDatasetsMap) { + tradingEngine = bot.simulationState.tradingEngine + tradingSystem = bot.simulationState.tradingSystem + sessionParameters = bot.SESSION.parameters + outputDatasetsMap = pOutputDatasetsMap // These are the files turned into arrays, stored in a Map by Product codeName. + } + + function finalize() { + tradingEngine = undefined + tradingSystem = undefined + sessionParameters = undefined + outputDatasetsMap = undefined + } + + function appendRecords() { + /* + Here we add records to the output files. At the product config property nodePath + we have a pointer to the node that have the information we need to extract. + Later, based on the product record definition we will extract each individual value. + */ + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + let product = dataset.parentNode + let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) + + if (bot.processingDailyFiles === true && dataset.config.type === 'Daily Files') { + persistRecords() + } + + if (bot.processingDailyFiles === false && dataset.config.type === 'Market Files') { + persistRecords() + } + + function persistRecords() { + + /* Clean the file from information of previous executions */ + pruneOutputFile(product, outputDatasetArray) + + /* Clean Open Records */ + if (product.config.saveAsObjects === true) { + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { + /* Remove Open Records */ + spliceOpenRecords(j, product, outputDatasetArray) + } + } + } + + /* + The product root can be a node or a node property of type array. + */ + let productRoot = eval(product.config.nodePath) + + if (product.config.nodePathType === 'array') { + /* + This means that the configured nodePath is not pointing to a node, but to a node property that is an array. + For that reason we will assume that each element of the array is a record to be outputed + */ + for (let index = 0; index < productRoot.length; index++) { + /* + The Product Root Node is the root of the node hiriarchy from where we are going to extract the record values. + */ + let productRootNode = productRoot[index] + let record = scanRecordDefinition(product, productRootNode, index) + persistIndividualRecord(record, product, outputDatasetArray) + } + } else { + /* + This means that the configured nodePath points to a single node, which is the one whose children constitutes + the record to be saved at the output file. + */ + /* + The Product Root Node is the root of the node hiriarchy from where we are going to extract the record values. + */ + let productRootNode = productRoot + let record = scanRecordDefinition(product, productRootNode) + persistIndividualRecord(record, product, outputDatasetArray) + } + } + } + + function scanRecordDefinition(product, productRootNode, index) { + + let record = [] + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + /* + The Property Root Node is the Root of the Hiriarchy branch we must find in order + to get the node where we are going to extract the value. Initially + we point it to the Product Root Node, because that is the default in case + a property does not have at its configuration a different nodePath configured + pointing to an specific Root for the property. + */ + let propertyRootNode = productRootNode + /* + If we find at the configuration a nodePath, then we take this path to find + the Root Node specifically for this property only. + */ + if (recordProperty.config.nodePath !== undefined) { + try { + propertyRootNode = eval(recordProperty.config.nodePath) + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] appendRecords -> scanRecordDefinition -> Error at Record Property Definition -> recordProperty.name = " + recordProperty.name); + logger.write(MODULE_NAME, "[ERROR] appendRecords -> scanRecordDefinition -> Error at Record Property Definition -> recordProperty.config.nodePath = " + recordProperty.config.nodePath); + logger.write(MODULE_NAME, "[ERROR] appendRecords -> scanRecordDefinition -> Error at Record Property Definition -> err.stack = " + err.stack); + throw 'Can not continue with a Definition Error like this.' + } + } + /* + The Target Node is the node from where we are going to exctract the value. + We will use the codeName of the Record Property to match it with + the any of the properties of the Root Node to get the Target Node. + */ + let targetNode = propertyRootNode[recordProperty.config.codeName] + /* + If the codeName of the Record Property can not match the name of the property at + the target node, the user can explicitly specify the property name at the configuration, + and in those cases we need to use that. This happens when there are many Record Properties + pointing to the same property at the Target Node. + */ + if (recordProperty.config.childProperty !== undefined) { + targetNode = propertyRootNode[recordProperty.config.childProperty] + } + /* + It can happen that intead of having a Node in targetNode what we have is an + array of nodes. We need to pick one of the elements of the array and for that + we use the Index value we find at the configuration of the Record Property. + */ + if (recordProperty.config.index !== undefined) { + targetNode = targetNode[recordProperty.config.index] + } + /* + By Default the value is extracted from the value property of the Target Node. + But it might happen the the Target Node does not exist for example when there is an Array + of Nodes defined in the Record Properties but not all of them exist at the Root Node. + We filter out those cases by not extracting the value from the value property. + */ + let value = 0 // This is a default value, since we do not want null in files because it breakes JSON format. + if (targetNode !== undefined) { + if (targetNode.type !== undefined) { + /* + In this case the Target Node is really a node (since it has a type), so we extract the value + from its value property. + */ + value = targetNode.value + + if (recordProperty.config.decimals !== undefined) { + value = Number(value.toFixed(recordProperty.config.decimals)) + } + } else { + /* + In this case the Target Node is not really node, but the value itself. Se we return this as the + value of the Record Property. + */ + value = targetNode + } + } + if (recordProperty.config.isString !== true && Array.isArray(value) !== true) { + value = safeNumericValue(value) + } + if (recordProperty.config.isString === true && Array.isArray(value) !== true) { + value = safeStringValue(value) + } + record.push(value) + } + return record + } + + function persistIndividualRecord(record, product, outputDatasetArray) { + + if (product.config.saveAsObjects === true) { + /* For saving objects we need to take care of a different set of rules. */ + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { + let propertyValue = record[j] + + if (bot.processingDailyFiles) { + /* + When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, + since the object will be duplicated on the next day. How do we know we are positioned at the last candle + of the day? Easy: the end of the candle must be 1 millisecod before the next day. That happens at any + time frame. + */ + let currentDay = new Date(tradingEngine.current.candle.end.value) + let nextDay = new Date(tradingEngine.current.candle.end.value + 1) + if (currentDay.getUTCDate() !== nextDay.getUTCDate()) { + /* + We will save the object only if it is closed, becasuse we are at the last candle of the day. + */ + if (propertyValue === product.config.propertyValueThatClosesObject) { + outputDatasetArray.push(record) + } + } else { + /* + When we are not at the end of the day, we will save the object normally, like in market files. + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + } + } + else { + /* + For Market Files we will add a record everytime that proeprty value does not match this + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + } + break + } + } + } else { + /* When we are not dealing with objects, we add every record to the existing file. */ + outputDatasetArray.push(record) + } + } + + function safeNumericValue(value) { + /* + The purpose of this function is to check that value variable does not have a value that + will later break the JSON format of files where this is going to be stored at. + */ + if (value === Infinity) { + value = Number.MAX_SAFE_INTEGER + } + if (value === undefined) { + value = 0 + } + if (value === null) { + value = 0 + } + if (isNaN(value)) { + value = 0 + } + return value + } + + function safeStringValue(value) { + /* + The purpose of this function is to check that value variable does not have a value that + will later break the JSON format of files where this is going to be stored at. + */ + if (value === undefined) { + value = '' + } + if (value === null) { + value = '' + } + return value + } + + function pruneOutputFile(product, outputFile) { + if (outputFile.isPrunned === true) { return } + /* + When a session is resumed, we will be potentially reading output files belonging to a previous session execution. + For that reason we need to prune all the records that are beyond the current candle. We do not delete everything + because we might be resuming a stopped session, which is fine. + */ + for (let i = 0; i < outputFile.length; i++) { + let record = outputFile[i] + + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === 'end') { + let end = record[j] + if (end >= tradingEngine.current.candle.end.value) { + outputFile.splice(i, 1) + pruneOutputFile(product, outputFile) + return + } + } + } + } + outputFile.isPrunned = true + } + + function spliceOpenRecords(j, product, outputDatasetArray) { + /* + Before adding records to the output file, we need to remove all open records, + because we will be adding the same records later with potentially + new or updated information. + */ + + for (let i = 0; i < outputDatasetArray.length; i++) { + let dataRecord = outputDatasetArray[i] + if (dataRecord !== undefined) { + let dataRecordValue = dataRecord[j] + if (dataRecordValue !== product.config.propertyValueThatClosesObject) { + outputDatasetArray.splice(i, 1) + spliceOpenRecords(j, product, outputDatasetArray) + return + } + } + } + } + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js new file mode 100644 index 000000000..58470f756 --- /dev/null +++ b/LowFrequencyTrading/TradingSimulation.js @@ -0,0 +1,397 @@ +exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILITIES) { + /* + This Module represents the trading simulacion. Escentially a loop through a set of candles and + the execution at each loop cycle of the Trading System Protocol. + */ + const FULL_LOG = true + const MODULE_NAME = 'Trading Simulation -> ' + bot.SESSION.name + + let thisObject = { + finalize: finalize, + runSimulation: runSimulation + } + + let utilities = UTILITIES.newCloudUtilities(bot, logger) + + return thisObject + + function finalize() { + thisObject = undefined + } + + function runSimulation( + chart, + outputDatasetsMap, + callback, + callBackFunction) { + try { + + let tradingSystem = bot.simulationState.tradingSystem + let tradingEngine = bot.simulationState.tradingEngine + let sessionParameters = bot.SESSION.parameters + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } + + /* Stop Loss Management TODO */ + const MIN_STOP_LOSS_VALUE = 0.0000000001 // We can not let the stop be zero to avoid division by 0 error or infinity numbers as a result. + const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER + + /* Take Profit Management TODO */ + const MIN_TAKE_PROFIT_VALUE = 0.0000000001 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. + const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER + + /* These are the Modules we will need to run the Simulation */ + const TRADING_ENGINE_MODULE = require('./TradingEngine.js') + let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) + tradingEngineModule.initialize() + + const TRADING_RECORDS_MODULE = require('./TradingRecords.js') + let tradingRecordsModule = TRADING_RECORDS_MODULE.newTradingRecords(bot, logger) + tradingRecordsModule.initialize(outputDatasetsMap) + + const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') + let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger, tradingEngineModule) + tradingSystemModule.initialize() + + /* Setting up the candles array: The whole simulation is based on the array of candles at the time-frame defined at the session parameters. */ + let propertyName = 'at' + sessionParameters.timeFrame.config.label.replace('-', '') + let candles = chart[propertyName].candles + + /* Variables needed for heartbeat functionality */ + let heartBeatDate + let previousHeartBeatDate + + /* This determines where we are standing */ + bot.sessionAndMarketBaseAssetsAreEqual = (sessionParameters.sessionBaseAsset.referenceParent.referenceParent.config.codeName === bot.market.baseAsset) + + initializeLoop() + + 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.candle.index.value = Math.trunc(amount) + 1 // Because we need to start from the next candle + if (tradingEngine.current.candle.index.value < 0 || tradingEngine.current.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.candle.index.value = ' + tradingEngine.current.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.candle.index.value = Math.trunc(amount) + if (tradingEngine.current.candle.index.value < 0) { tradingEngine.current.candle.index.value = 0 } + if (tradingEngine.current.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.candle.index.value = candles.length - 1 + } + } + + loop() + } + + async function loop() { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + tradingEngine.current.candle.index.value) } + let candle = candles[tradingEngine.current.candle.index.value] // This is the current candle the Simulation is working at. + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle Begin @ ' + (new Date(candle.begin)).toLocaleString()) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle End @ ' + (new Date(candle.end)).toLocaleString()) } + + tradingEngineModule.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas. + + if (checkIfWeAreAtTheHeadOfTheMarket() === false) { + afterLoop() + return + } + + if (checkMinimunAndMaximunBalance() === false) { + afterLoop() + return + } + + if (checkInitialAndFinalDatetime() === false) { + controlLoop() + return + } + + heartBeat() + positionChartAtCurrentCandle() + tradingSystemModule.reset(chart) + tradingSystemModule.mantainPositions() + tradingSystemModule.mantainStrategies() + tradingSystemModule.mantainOrders() + + tradingEngineModule.updateEpisodeCountersAndStatistics() + tradingEngineModule.updateDistanceToEventsCounters() + + /* Run the Trigger Stage */ + tradingSystemModule.triggerStage() + + /* Run the Open Stage */ + await tradingSystemModule.openStage() + + /* Run the Manage Stage */ + tradingSystemModule.manageStage() + + /* Run the Close Stage */ + await tradingSystemModule.closeStage() + + /* Add new records to the process output */ + tradingRecordsModule.appendRecords() + + 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.') + afterLoop() + return + } + + 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.') + afterLoop() + return + } + + tradingEngine.current.candle.index.value++ + if (tradingEngine.current.candle.index.value < candles.length) { + setImmediate(loop) // This will execute the next loop in the next iteration of the NodeJs event loop allowing for other callbacks to be executed. + } else { + afterLoop() + } + } + + function afterLoop() { + tradingEngineModule.finalize() + tradingSystemModule.finalize() + tradingRecordsModule.finalize() + + tradingEngineModule = undefined + tradingSystemModule = undefined + tradingRecordsModule = undefined + + callback() + } + + function heartBeat() { + /* We will produce a simulation level heartbeat in order to inform the user this is running. */ + heartBeatDate = new Date(Math.trunc(tradingEngine.current.candle.begin.value / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) + if (heartBeatDate.valueOf() !== previousHeartBeatDate) { + let processingDate = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + tradingEngine.current.candle.index.value + ' @ ' + processingDate) } + + /* Telling the world we are alive and doing well */ + let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime) + let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime) + + let currentDateString = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) + let currentDate = new Date(heartBeatDate) + let percentage = global.getPercentage(fromDate, currentDate, lastDate) + bot.processHeartBeat(currentDateString, percentage) + + if (global.areEqualDates(currentDate, new Date()) === false) { + logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) + } + } + previousHeartBeatDate = heartBeatDate.valueOf() + } + + function positionChartAtCurrentCandle() { + /* + In conditions and Formulas, we want users to have an easy sintax to refer to indicators. In order to achieve that, we need the user to have + easy access to the current candle for instance, or the current bollinger band, meaning the one the Simulation is currently standing at. + For that reason we do the following processing, to have at the chart data structure the current objects of each indicator / time frame. + */ + let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies + + /* Finding the Current Element on Market Files */ + if (bot.processingDailyFiles) { + for (let j = 0; j < global.dailyFilePeriods.length; j++) { + let mapKey = dailyFilePeriods[j][1] + let propertyName = 'at' + mapKey.replace('-', '') + let thisChart = chart[propertyName] + + for (let k = 0; k < dataDependencies.length; k++) { + let dataDependencyNode = dataDependencies[k] + if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Daily') { continue } + let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName + let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName + let currentElement = getElement(thisChart[pluralVariableName], 'Daily' + '-' + mapKey + '-' + pluralVariableName) + thisChart[singularVariableName] = currentElement + } + } + } + + /* Finding the Current Element on Market Files */ + for (let j = 0; j < global.marketFilesPeriods.length; j++) { + let mapKey = marketFilesPeriods[j][1] + let propertyName = 'at' + mapKey.replace('-', '') + let thisChart = chart[propertyName] + + for (let k = 0; k < dataDependencies.length; k++) { + let dataDependencyNode = dataDependencies[k] + if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Market') { continue } + let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName + let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName + let currentElement = getElement(thisChart[pluralVariableName], 'Market' + '-' + mapKey + '-' + pluralVariableName) + thisChart[singularVariableName] = currentElement + } + } + + /* Finding the Current Element At Single Files*/ + let propertyName = 'atAnyTimeFrame' + let thisChart = chart[propertyName] + + for (let k = 0; k < dataDependencies.length; k++) { + let dataDependencyNode = dataDependencies[k] + if (dataDependencyNode.referenceParent.config.codeName !== 'Single-File') { continue } + let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName + let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName + let elementArray = thisChart[pluralVariableName] + let currentElement + if (elementArray !== undefined) { + currentElement = elementArray[elementArray.length - 1] + } + thisChart[singularVariableName] = currentElement + } + } + + function getElement(pArray, datasetName) { + if (pArray === undefined) { return } + try { + let element + for (let i = 0; i < pArray.length; i++) { + element = pArray[i] + + if (tradingEngine.current.candle.end.value === element.end) { // when there is an exact match at the end we take that element + return element + } else { + if ( + i > 0 && + element.end > tradingEngine.current.candle.end.value + ) { + let previousElement = pArray[i - 1] + if (previousElement.end < tradingEngine.current.candle.end.value) { + return previousElement // If one elements goes into the future of currentCandle, then we stop and take the previous element. + } else { + return + } + } + } + } + return + } 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) + } + } + + function checkIfWeAreAtTheHeadOfTheMarket() { + /* + We skip the candle at the head of the market because it has not closed yet. The procedure to determine if we are at the head of the market is different + when we are processing Market Files than when we are processing Daily Files. TODO Check that the procedure is good for Beta 6 + */ + if (bot.processingDailyFiles) { // We are processing Daily Files + let candlesPerDay = global.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value + if (tradingEngine.current.candle.index.value === candles.length - 1) { + if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkIfWeAreAtTheHeadOfTheMarket -> Skipping Candle because it is the last one and has not been closed yet.') } + return false + /* + Note here that in the last candle of the first day or the second day it will use an incomplete candle and partially calculated indicators. + if we skip these two periods, then there will be a hole in the file since the last period will be missing. + */ + } + } + } else { // We are processing Market Files + if (tradingEngine.current.candle.index.value === candles.length - 1) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkIfWeAreAtTheHeadOfTheMarket -> Skipping Candle because it is the last one and has not been closed yet.') } + return false + } + } + return true + } + + function checkInitialAndFinalDatetime() { + /* Here we check that the current candle is not outside of user-defined time range at the session parameters.*/ + if (tradingEngine.current.candle.end.value < sessionParameters.timeRange.config.initialDatetime) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } + return false + } + if (tradingEngine.current.candle.begin.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 + } + return true + } + + function checkMinimunAndMaximunBalance() { + /* Checks for Minimun and Maximun Balance. We do the check while not inside any strategy only. */ + if ( + tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue + ) { + /* + First thing to do is to check if we are below the minimum balance, or above the maximun balance. + If we are, we are not gooing to trigger on again. + */ + let minimumBalance + let maximumBalance + let balance + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + balance = tradingEngine.current.balance.baseAsset.value + minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance + maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance + } else { + balance = tradingEngine.current.balance.quotedAsset.value + minimumBalance = sessionParameters.sessionQuotedAsset.config.minimumBalance + maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance + } + + let stopRunningDate = new Date(tradingEngine.current.candle.begin.value) + if (balance <= minimumBalance) { + tradingSystem.error = 'Min Balance @ ' + stopRunningDate.toLocaleString() + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } + return false + } + + if (balance >= maximumBalance) { + tradingSystem.error = 'Max Balance @ ' + stopRunningDate.toLocaleString() + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } + return false + } + } + return true + } + } catch (err) { + logger.write(MODULE_NAME, '[ERROR] runSimulation -> err = ' + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } +} + diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js new file mode 100644 index 000000000..adcf0ca71 --- /dev/null +++ b/LowFrequencyTrading/TradingStrategy.js @@ -0,0 +1,98 @@ +exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEngineModule) { + /* + This module packages all functions related to Strategies. + */ + const MODULE_NAME = 'Trading Strategy' + let thisObject = { + openStrategy: openStrategy, + closeStrategy: closeStrategy, + updateEnds: updateEnds, + updateStageStatus: updateStageStatus, + updateStatus: updateStatus, + updateCounters: updateCounters, + resetStrategy: resetStrategy, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + + return thisObject + + function initialize() { + tradingEngine = bot.simulationState.tradingEngine + } + + function finalize() { + tradingEngine = undefined + } + + function openStrategy(index, situationName, strategyName) { + tradingEngine.current.strategy.status.value = 'Open' + tradingEngine.current.strategy.serialNumber.value = tradingEngine.episode.episodeCounters.strategies.value + tradingEngine.current.strategy.identifier.value = global.UNIQUE_ID() + tradingEngine.current.strategy.begin.value = tradingEngine.current.candle.begin.value + tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value + + tradingEngine.current.strategy.index.value = index + tradingEngine.current.strategy.situationName.value = situationName + tradingEngine.current.strategy.strategyName.value = strategyName + } + + function closeStrategy(exitType) { + tradingEngine.current.strategy.status.value = 'Closed' + tradingEngine.current.strategy.exitType.value = exitType + tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value + /* + Now that the strategy is closed, it is the right time to move this strategy from current to last at the Trading Engine data structure. + */ + tradingEngineModule.cloneValues(tradingEngine.current.strategy, tradingEngine.last.strategy) + } + + function resetStrategy() { + tradingEngine.current.strategy.initialize(tradingEngine.current.strategy) + } + + function updateEnds() { + if (tradingEngine.current.strategy.status.value === 'Open') { + tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value + } + } + + function updateStageStatus(stage, status) { + switch (stage) { + case 'Trigger Stage': { + tradingEngine.current.strategy.triggerStageStatus.value = status + break + } + case 'Open Stage': { + tradingEngine.current.strategy.openStageStatus.value = status + break + } + case 'Manage Stage': { + tradingEngine.current.strategy.manageStageStatus.value = status + break + } + case 'Close Stage': { + tradingEngine.current.strategy.closeStageStatus.value = status + break + } + } + } + + function updateStatus() { + if (tradingEngine.current.strategy.status.value === 'Closed') { + resetStrategy() + } + } + + function updateCounters() { + if (tradingEngine.current.strategy.status.value === 'Open') { + tradingEngine.current.strategy.strategyCounters.periods.value++ + } + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js new file mode 100644 index 000000000..7aafa99d0 --- /dev/null +++ b/LowFrequencyTrading/TradingSystem.js @@ -0,0 +1,1109 @@ +exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineModule) { + /* + The Trading System is the user defined set of rules compliant with the Trading Protocol that + defines the trading logic to be applied during each cycle of the Simulation. + */ + const MODULE_NAME = 'Trading System' + let thisObject = { + reset: reset, + mantainStrategies: mantainStrategies, + mantainPositions: mantainPositions, + mantainOrders: mantainOrders, + triggerStage: triggerStage, + openStage: openStage, + manageStage: manageStage, + closeStage: closeStage, + initialize: initialize, + finalize: finalize + } + + let chart + let tradingSystem + let tradingEngine + let sessionParameters + + const TRADING_STRATEGY_MODULE = require('./TradingStrategy.js') + let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger, tradingEngineModule) + + const TRADING_POSITION_MODULE = require('./TradingPosition.js') + let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger, tradingEngineModule) + + const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') + let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger, tradingEngineModule) + + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') + let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + + const SNAPSHOTS_MODULE = require('./Snapshots.js') + let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) + + return thisObject + + function initialize() { + tradingSystem = bot.simulationState.tradingSystem + tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters + + tradingSystem.conditions = new Map() + tradingSystem.formulas = new Map() + + tradingStrategyModule.initialize() + tradingPositionModule.initialize() + announcementsModule.initialize() + snapshotsModule.initialize() + tradingExecutionModule.initialize() + + /* Adding Functions used elsewhere to Trading System Definition */ + tradingSystem.checkConditions = function (situation, passed) { + /* Given a Situation, we check if all children conditions are true or not */ + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m] + let value = false + if (tradingSystem.conditions.get(condition.id) !== undefined) { + value = tradingSystem.conditions.get(condition.id) + } + if (value === true) { + tradingSystem.highlights.push(condition.id) + } + else { + passed = false + } + tradingSystem.values.push([condition.id, value]) + } + return passed + } + } + + function finalize() { + tradingStrategyModule.finalize() + tradingStrategyModule = undefined + + tradingPositionModule.finalize() + tradingPositionModule = undefined + + announcementsModule.finalize() + announcementsModule = undefined + + snapshotsModule.finalize() + snapshotsModule = undefined + + tradingExecutionModule.finalize() + tradingExecutionModule = undefined + + chart = undefined + + tradingSystem.conditions = undefined + tradingSystem.formulas = undefined + + tradingSystem.highlights = undefined + tradingSystem.errors = undefined + tradingSystem.values = undefined + tradingSystem.status = undefined + tradingSystem.progress = undefined + tradingSystem.running = undefined + + tradingSystem = undefined + tradingEngine = undefined + sessionParameters = undefined + } + + function reset(pChart) { + /* This function helps reset data structures at every cycle of the simulation loop/=. */ + chart = pChart // We need chat to be a local object accessible from conditions and formulas. + snapshotsModule.reset(chart) + + tradingSystem.highlights = [] + tradingSystem.errors = [] + tradingSystem.values = [] + tradingSystem.status = [] + tradingSystem.progress = [] + tradingSystem.running = [] + tradingSystem.announcements = [] + } + + function mantainStrategies() { + tradingStrategyModule.updateStatus() + tradingStrategyModule.updateCounters() + tradingStrategyModule.updateEnds() + } + + function mantainPositions() { + tradingPositionModule.updateStatus() + tradingPositionModule.updateCounters() + tradingPositionModule.updateEnds() + } + + function mantainOrders() { + /* Reset All Exchange Orders that are on Closed status, to their initial value */ + if (tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue) { return } + + let stageNode + let executionNode + + stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + executionNode = stageNode.openExecution + resetExecution(executionNode) + + stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + executionNode = stageNode.closeExecution + resetExecution(executionNode) + + function resetExecution(executionNode) { + + resetExecutionAlgorithms(executionNode) + + function resetExecutionAlgorithms(executionNode) { + for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { + let executionAlgorithm = executionNode.executionAlgorithms[i] + resetOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + resetOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + resetOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + resetOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + } + } + + function resetOrders(orders, executionAlgorithm, executionNode) { + for (let i = 0; i < orders.length; i++) { + let tradingSystemOrder = orders[i] + if (tradingSystemOrder.referenceParent === undefined) { continue } + let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) + if (tradingEngineOrder.status === undefined) { continue } + if (tradingEngineOrder.status.value === 'Closed') { + /* We reset the order data structure inside the Trading Engine to its initial value */ + tradingEngineOrder.initialize(tradingEngineOrder) + } + } + } + } + } + + function evalConditions(startingNode, descendentOfNodeType) { + evalNode(startingNode, 'Conditions', descendentOfNodeType) + } + + function evalFormulas(startingNode, descendentOfNodeType) { + evalNode(startingNode, 'Formulas', descendentOfNodeType) + } + + function evalNode(node, evaluating, descendentOfNodeType, isDescendent) { + if (node === undefined) { return } + + /* Verify if this node is decendent of the specified node type */ + if (isDescendent !== true) { + if (node.type === descendentOfNodeType) { + isDescendent = true + } + } + + /* Here we check if there is a codition to be evaluated */ + if (node.type === 'Condition' && evaluating === 'Conditions') { + /* We will eval this condition */ + if (isDescendent === true) { + evalCondition(node) + } + } + + /* Here we check if there is a formula to be evaluated */ + if (node.type === 'Formula' && evaluating === 'Formulas') { + if (node.code !== undefined) { + /* We will eval this formula */ + if (isDescendent === true) { + evalFormula(node) + } + } + } + + /* Now we go down through all this node children */ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (node[property.name] !== undefined) { + evalNode(node[property.name], evaluating, descendentOfNodeType, isDescendent) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (node[property.name] !== undefined) { + let nodePropertyArray = node[property.name] + for (let m = 0; m < nodePropertyArray.length; m++) { + evalNode(nodePropertyArray[m], evaluating, descendentOfNodeType, isDescendent) + } + } + break + } + } + } + } + } + + function evalCondition(node) { + let value + let error + /* + The code can be at the condition node if it was done with the Conditions Editor, or it can also be + at a Javascript Code node. If there is a Javascript object we will give it preference and take the code + from there. Otherwise we will take the code from the Condition node. + */ + let code = node.code + if (node.javascriptCode !== undefined) { + if (node.javascriptCode.code !== undefined) { + code = node.javascriptCode.code + } + } + + try { + logger.write(MODULE_NAME, '[INFO] evalCondition -> code = ' + code) + value = eval(code) + } catch (err) { + /* + One possible error is that the conditions references a .previous that is undefined. This + will not be considered an error. + */ + value = false + + if (code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || + code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 + ) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + error = err.message + } + } + + tradingSystem.conditions.set(node.id, value) + + if (value === true) { + tradingSystem.highlights.push(node.id) + } + if (error !== undefined) { + tradingSystem.errors.push([node.id, error]) + } + if (value !== undefined) { + tradingSystem.values.push([node.id, value]) + } + + logger.write(MODULE_NAME, '[INFO] evalCondition -> value = ' + value) + logger.write(MODULE_NAME, '[INFO] evalCondition -> error = ' + error) + } + + function evalFormula(node) { + let value + let error + + try { + logger.write(MODULE_NAME, '[INFO] evalFormula -> code = ' + node.code) + value = eval(node.code) + } catch (err) { + /* + One possible error is that the formula references a .previous that is undefined. This + will not be considered an error. + */ + value = 0 + + if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || + node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 + ) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + error = err.message + } + } + + tradingSystem.formulas.set(node.id, value) + + if (error !== undefined) { + tradingSystem.errors.push([node.id, error]) + } + if (value !== undefined) { + tradingSystem.values.push([node.id, value]) + } + + logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) + logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) + } + + function triggerStage() { + /* + We check if we will be triggering on, off or taking position. + */ + checkTriggerOn() + checkTriggerOff() + checkTakePosition() + + function checkTriggerOn() { + if ( + tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue + ) { + /* + To pick a new strategy we will check that any of the situations of the trigger on is true. + Once we enter into one strategy, we will ignore market conditions for others. However there is also + a strategy trigger off which can be hit before taking a position. If hit, we would + be outside a strategy again and looking for the conditions to enter all over again. + */ + evalConditions(tradingSystem, 'Trigger On Event') + + for (let j = 0; j < tradingSystem.tradingStrategies.length; j++) { + if ( // If a strategy was already picked during the loop, we exit the loop + tradingEngine.current.strategy.index.value !== tradingEngine.current.strategy.index.config.initialValue + ) { continue } + + let strategy = tradingSystem.tradingStrategies[j] + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + if (triggerStage.triggerOn !== undefined) { + for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { + let situation = triggerStage.triggerOn.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(triggerStage.triggerOn.id) + tradingSystem.highlights.push(triggerStage.id) + + tradingStrategyModule.openStrategy(j, situation.name, strategy.name) + + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.strategies.value++ + + /* Initialize this */ + tradingEngine.current.distanceToEvent.triggerOn.value = 1 + + announcementsModule.makeAnnoucements(triggerStage.triggerOn) + announcementsModule.makeAnnoucements(triggerStage) + + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyEntry() + } + } + } + + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Open') + } + } + } + } + } + } + } + + function checkTriggerOff() { + if (tradingEngine.current.strategy.triggerStageStatus.value === 'Open') { + + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let triggerStage = strategy.triggerStage + + evalConditions(strategy, 'Trigger Off Event') + + if (triggerStage !== undefined) { + if (triggerStage.triggerOff !== undefined) { + for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { + let situation = triggerStage.triggerOff.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(triggerStage.triggerOff.id) + tradingSystem.highlights.push(triggerStage.id) + + tradingEngine.current.distanceToEvent.triggerOff.value = 1 + announcementsModule.makeAnnoucements(triggerStage.triggerOff) + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Closed') + tradingStrategyModule.closeStrategy('Trigger Off') + } + } + } + } + } + } + + function checkTakePosition() { + if ( + tradingEngine.current.strategy.triggerStageStatus.value === 'Open' + ) { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let triggerStage = strategy.triggerStage + + evalConditions(strategy, 'Take Position Event') + evalFormulas(strategy, 'Take Position Event') + + if (triggerStage !== undefined) { + if (triggerStage.takePosition !== undefined) { + for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { + let situation = triggerStage.takePosition.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(triggerStage.takePosition.id) + tradingSystem.highlights.push(triggerStage.id) + + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.positions.value++ + + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.takePosition.value = 1 + + tradingPositionModule.openPosition(situation.name) + + announcementsModule.makeAnnoucements(triggerStage.takePosition) + announcementsModule.makeAnnoucements(strategy.openStage) + + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionEntry() + } + } + } + + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Closed') + tradingStrategyModule.updateStageStatus('Open Stage', 'Opening') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Opening') + } + } + } + } + } + } + } + + async function openStage() { + /* Abort Open Stage Check */ + if (tradingEngine.current.strategy.openStageStatus.value === 'Open' && tradingEngine.current.strategy.closeStageStatus.value === 'Open') { + /* + if the Close stage is opened while the open stage is still open that means that + we need to stop placing orders, check what happened to the orders already placed, + and cancel all unfilled limit orders. We call this the Closing status of the Open Stage. + */ + tradingStrategyModule.updateStageStatus('Open Stage', 'Closing') + } + + /* Opening Status Procedure */ + if (tradingEngine.current.strategy.openStageStatus.value === 'Opening') { + + /* This procedure is intended to run only once */ + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = stageNode.openExecution + + /* Reset the Exchange Orders data structure to its initial value */ + tradingEngine.exchangeOrders.initialize(tradingEngine.exchangeOrders) + + evalConditions(stageNode, 'Initial Definition') + evalFormulas(stageNode, 'Initial Definition') + + getReadyToTakePosition() + + calculateTakeProfit() + calculateStopLoss() + + /* Remember the balance we had before taking the position to later calculate profit or loss */ + tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + + /* Check Execution in opening stage node */ + evalConditions(stageNode, 'Open Execution') + evalFormulas(stageNode, 'Open Execution') + + await tradingExecutionModule.checkExecution(executionNode, true, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + + /* From here on, the state is officially Open */ + tradingStrategyModule.updateStageStatus('Open Stage', 'Open') + return + } + + /* Open Status Procedure */ + if (tradingEngine.current.strategy.openStageStatus.value === 'Open') { + /* + While the Open Stage is Open, we do our regular stuff: place orders and check + what happened to the orders already placed. + */ + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = stageNode.openExecution + + /* Every Loop Cycle Activity */ + evalConditions(stageNode, 'Open Execution') + evalFormulas(stageNode, 'Open Execution') + + await tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + + /* + The Open is finished when the fillSize reaches the Position Size. + This can happens at any time when we update the filledSize value when we see + at the exchange that orders were filled. + */ + if (tradingEngine.current.position.openStageFilledSize.value === tradingEngine.current.position.size.value) { + tradingStrategyModule.updateStageStatus('Open Stage', 'Closed') + } + } + + /* Closing Status Procedure */ + if (tradingEngine.current.strategy.openStageStatus.value === 'Closing') { + /* + During the closing stage status, we do not place new orders, just check + if the ones placed were filled, and cancel the ones not filled. + */ + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = stageNode.openExecution + + /* Check if there are unfilled orders */ + if (tradingEngine.current.position.openStageOrdersSize.value !== tradingEngine.current.position.openStageFilledSize.value) { + /* There are unfilled orders, we will check if they were executed, and cancel the ones that were not. */ + + evalConditions(stageNode, 'Open Execution') + evalFormulas(stageNode, 'Open Execution') + + await tradingExecutionModule.checkExecution(executionNode, false, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + } + + /* + The Closing is finished when the fillSize reaches the ordersSize. + This can happens either because we update the filledSize value when we see + at the exchange that orders were filled, or we reduce the ordersSize when + we cancel not yet filled orders. + */ + if (tradingEngine.current.position.openStageFilledSize.value === tradingEngine.current.position.openStageOrdersSize.value) { + tradingStrategyModule.updateStageStatus('Open Stage', 'Closed') + } + } + + function getReadyToTakePosition() { + + /* Position size and rate */ + tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) + + function getPositionSize() { + let balance + if (bot.sessionAndMarketBaseAssetsAreEqual) { + balance = tradingEngine.current.balance.baseAsset.value + } else { + balance = tradingEngine.current.balance.quotedAsset.value + } + const DEFAULT_VALUE = balance + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + + if (strategy.openStage === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionSize === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return DEFAULT_VALUE + + let value = tradingSystem.formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) + if (value === undefined) return DEFAULT_VALUE + return value + } + + function getPositionRate() { + const DEFAULT_VALUE = tradingEngine.current.candle.close.value + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + + if (strategy.openStage === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionRate === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return DEFAULT_VALUE + + let value = tradingSystem.formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) + if (value === undefined) return DEFAULT_VALUE + return value + } + } + } + + function manageStage() { + /* Opening Status Procedure */ + if (tradingEngine.current.strategy.manageStageStatus.value === 'Opening') { + /* We jump the management at the loop cycle where the position is being taken. */ + tradingStrategyModule.updateStageStatus('Manage Stage', 'Open') + return + } + + /* Open Status Procedure */ + if (tradingEngine.current.strategy.manageStageStatus.value === 'Open') { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let manageStage = strategy.manageStage + evalConditions(manageStage, 'Manage Stage') + evalFormulas(manageStage, 'Manage Stage') + + /* Stop Loss Management */ + checkStopPhasesEvents() + calculateStopLoss() + + /* Take Profit Management */ + checkTakeProfitPhaseEvents() + calculateTakeProfit() + + /* Checking if Stop or Take Profit were hit */ + checkStopLossOrTakeProfitWasHit() + } + + function checkStopPhasesEvents() { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let phaseIndex + let phase + let stopLoss + let stage + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + stage = openStage + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.initialStopLoss !== undefined) { + parentNode = openStage.initialDefinition + phaseIndex = tradingEngine.current.position.stopLoss.stopLossPhase.value + stopLoss = openStage.initialDefinition.initialStopLoss + phase = stopLoss.phases[phaseIndex] + } + } + } + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + stage = manageStage + if (manageStage.managedStopLoss !== undefined) { + parentNode = manageStage + phaseIndex = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 + stopLoss = manageStage.managedStopLoss + phase = stopLoss.phases[phaseIndex] + } + } + + if (parentNode !== undefined) { + if (phase === undefined) { return } // trying to jump to a phase that does not exists. + + /* Check the next Phase Event. */ + let nextPhaseEvent = phase.nextPhaseEvent + if (nextPhaseEvent !== undefined) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + let situation = nextPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(nextPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(stopLoss.id) + tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) + + tradingPositionModule.updateStopLoss(tradingEngine.current.position.stopLoss.stopLossPhase.value + 1, 'Manage Stage') + + announcementsModule.makeAnnoucements(nextPhaseEvent) + return // only one event can pass at the time + } + } + } + + /* Check the Move to Phase Events. */ + for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { + let moveToPhaseEvent = phase.moveToPhaseEvents[n] + if (moveToPhaseEvent !== undefined) { + for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { + let situation = moveToPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(moveToPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(stopLoss.id) + tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) + + let moveToPhase = moveToPhaseEvent.referenceParent + if (moveToPhase !== undefined) { + for (let q = 0; q < stopLoss.phases.length; q++) { + if (stopLoss.phases[q].id === moveToPhase.id) { + tradingPositionModule.updateStopLoss(q + 1, 'Manage Stage') + } + } + } else { + tradingSystem.errors.push([moveToPhaseEvent.id, 'This Node needs to reference a Phase.']) + continue + } + + announcementsModule.makeAnnoucements(moveToPhaseEvent) + return // only one event can pass at the time + } + } + } + } + } + } + + function checkTakeProfitPhaseEvents() { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let phaseIndex + let phase + let takeProfit + let stage + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + stage = openStage + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.initialTakeProfit !== undefined) { + parentNode = openStage.initialDefinition + phaseIndex = tradingEngine.current.position.takeProfit.takeProfitPhase.value + takeProfit = openStage.initialDefinition.initialTakeProfit + phase = takeProfit.phases[phaseIndex] + } + } + } + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + stage = manageStage + if (manageStage.managedTakeProfit !== undefined) { + parentNode = manageStage + phaseIndex = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 + takeProfit = manageStage.managedTakeProfit + phase = takeProfit.phases[phaseIndex] + } + } + + if (parentNode !== undefined) { + if (phase === undefined) { return } // trying to jump to a phase that does not exists. + + /* Check the next Phase Event. */ + let nextPhaseEvent = phase.nextPhaseEvent + if (nextPhaseEvent !== undefined) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + let situation = nextPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(nextPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(takeProfit.id) + tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) + + tradingPositionModule.updateTakeProfit(tradingEngine.current.position.takeProfit.takeProfitPhase.value + 1, 'Manage Stage') + + announcementsModule.makeAnnoucements(nextPhaseEvent) + return // only one event can pass at the time + } + } + } + + /* Check the Move to Phase Events. */ + for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { + let moveToPhaseEvent = phase.moveToPhaseEvents[n] + if (moveToPhaseEvent !== undefined) { + for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { + let situation = moveToPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = tradingSystem.checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(moveToPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(takeProfit.id) + tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) + + let moveToPhase = moveToPhaseEvent.referenceParent + if (moveToPhase !== undefined) { + for (let q = 0; q < takeProfit.phases.length; q++) { + if (takeProfit.phases[q].id === moveToPhase.id) { + tradingPositionModule.updateTakeProfit(q + 1, 'Manage Stage') + } + } + } else { + tradingSystem.errors.push([moveToPhaseEvent.id, 'This Node needs to reference a Phase.']) + continue + } + + announcementsModule.makeAnnoucements(moveToPhaseEvent) + return // only one event can pass at the time + } + } + } + } + } + } + + function checkStopLossOrTakeProfitWasHit() { + { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + /* + Checking what happened since the last execution. We need to know if the Stop Loss + or our Take Profit were hit. + */ + + /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ + if ( + (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || + (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) + ) { + logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') + + tradingPositionModule.preventStopLossDistortion() + tradingPositionModule.applySlippageToStopLoss() + tradingPositionModule.closingPosition('Stop Loss') + tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Closed') + announcementsModule.makeAnnoucements(strategy.closeStage) + return + } + + /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ + if ( + (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || + (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) + ) { + logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') + + tradingPositionModule.preventTakeProfitDistortion() + tradingPositionModule.applySlippageToTakeProfit() + tradingPositionModule.closingPosition('Take Profit') + tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Closed') + announcementsModule.makeAnnoucements(strategy.closeStage) + return + } + return + } + } + } + + async function closeStage() { + /* Opening Status Procedure */ + if (tradingEngine.current.strategy.closeStageStatus.value === 'Opening') { + /* + This will happen only once, as soon as the Take Profit or Stop was hit. + We wil not be placing orders at this time because we do not know + the total filled size of the Open Stage. By skiping execution now + we allow the open stage to get a final value for filledSize that we can use. + */ + tradingStrategyModule.updateStageStatus('Close Stage', 'Open') + return + } + + /* Open Status Procedure */ + if (tradingEngine.current.strategy.closeStageStatus.value === 'Open') { + /* + This will happen as long as the Close Stage is Open. + */ + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + let executionNode = stageNode.closeExecution + + evalConditions(stageNode, 'Close Execution') + evalFormulas(stageNode, 'Close Execution') + + await tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + + /* Close the Stage Validation */ + if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.openStageFilledSize.value) { + /* + The Close Stage is closed when the fillSize reaches the filledSize of the Open Stage. + */ + tradingStrategyModule.updateStageStatus('Close Stage', 'Closed') + } + } + + /* Exit Position Validation */ + if ( + tradingEngine.current.strategy.triggerStageStatus.value === 'Closed' && + tradingEngine.current.strategy.openStageStatus.value === 'Closed' && + tradingEngine.current.strategy.manageStageStatus.value === 'Closed' && + tradingEngine.current.strategy.closeStageStatus.value === 'Closed' + ) { + + /* Exiting Everything now. */ + exitPositionAndStrategy() + } + + function exitPositionAndStrategy() { + + /* Needed for Episode Statistics */ + tradingPositionModule.updateStatistics() + + /* Recalculating Episode Counters */ + if (tradingEngine.current.position.positionStatistics.profitLoss.value > 0) { + tradingEngine.episode.episodeCounters.hits.value++ + } else { + tradingEngine.episode.episodeCounters.fails.value++ + } + + /* Recalculating Episode Statistics */ + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + } else { + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + } + + /* Taking Position Snapshot */ + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionExit() + } + } + } + + /* Taking Strategy Snapshot */ + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyExit() + } + } + } + + /* Close the Position */ + tradingPositionModule.closePosition() + + /* Close the Strategy */ + tradingStrategyModule.closeStrategy('Position Closed') + + /* Distance to Events Updates */ + tradingEngine.current.distanceToEvent.closePosition.value = 1 + tradingEngine.current.distanceToEvent.triggerOff.value = 1 + } + } + + function calculateStopLoss() { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.initialStopLoss !== undefined) { + phase = openStage.initialDefinition.initialStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] + } + } + } + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.managedStopLoss !== undefined) { + phase = manageStage.managedStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] + } + } + + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.stopLoss.value + tradingPositionModule.applyStopLossFormula(tradingSystem.formulas, phase.formula.id) + + if (tradingEngine.current.position.stopLoss.value !== previousValue) { + announcementsModule.makeAnnoucements(phase) + } + } + } + } + + function calculateTakeProfit() { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.initialTakeProfit !== undefined) { + phase = openStage.initialDefinition.initialTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] + } + } + } + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.managedTakeProfit !== undefined) { + phase = manageStage.managedTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] + } + } + + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.takeProfit.value + tradingPositionModule.applyTakeProfitFormula(tradingSystem.formulas, phase.formula.id) + + if (tradingEngine.current.position.takeProfit.value !== previousValue) { + announcementsModule.makeAnnoucements(phase) + } + } + } + } + + function isItInside(elementWithTimestamp, elementWithBeginEnd) { + /* + The function is to allow in Conditions and Formulas to easily know when an element with timestamp (like the ones of single files) are inside the time range + of an element with a time range, like a candle. + */ + if (elementWithTimestamp.timestamp >= elementWithBeginEnd.begin && elementWithTimestamp.timestamp <= elementWithBeginEnd.end) { + return true + } else { + return false + } + } +} + diff --git a/MultiPeriod.js b/MultiPeriod.js deleted file mode 100644 index c414630a3..000000000 --- a/MultiPeriod.js +++ /dev/null @@ -1,1041 +0,0 @@ -exports.newMultiPeriod = function newMultiPeriod(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS) { - - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; - const MODULE_NAME = "Multi Period"; - const GMT_SECONDS = ':00.000 GMT+0000'; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; - - thisObject = { - initialize: initialize, - finalize: finalize, - start: start - }; - - let utilities = UTILITIES.newCloudUtilities(logger); - - let statusDependencies; - let dataDependenciesModule; - let dataFiles = new Map(); - let multiPeriodDataFiles = new Map(); - - let botInstance; - - const FILE_STORAGE = require('./FileStorage.js'); - let fileStorage = FILE_STORAGE.newFileStorage(logger); - - let processConfig; - - return thisObject; - - function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, exchangeAPI, callBackFunction) { - - try { - - logger.fileName = MODULE_NAME; - logger.initialize(); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - statusDependencies = pStatusDependencies; - dataDependenciesModule = pDataDependencies; - processConfig = pProcessConfig; - - let USER_BOT_MODULE = require("./Trading/TradingBot") - - botInstance = USER_BOT_MODULE.newTradingBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(exchangeAPI, callBackFunction); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function finalize() { - dataFiles = undefined - multiPeriodDataFiles = undefined - statusDependencies = undefined - dataDependenciesModule = undefined - botInstance.finalize() - botInstance = undefined - fileStorage = undefined - processConfig = undefined - thisObject = undefined - } - - function start(callBackFunction) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } - - let currentTimeFrame - let currentTimeFrameLabel - - let market = bot.market; - let botNeverRan = true; - - /* Context Variables */ - - let contextVariables = { - lastFile: undefined, // Datetime of the last file files sucessfully produced by this process. - dateBeginOfMarket: undefined, // Datetime of the first trade file in the whole market history. - dateEndOfMarket: undefined // Datetime of the last file available to be used as an input of this process. - }; - - let previousDay; // Holds the date of the previous day relative to the processing date. - - let variable - - getContextVariables(); - - function getContextVariables() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Entering function."); } - - let thisReport; - let reportKey; - let statusReport; - - /* We are going to use the start date as beging of market date. */ - contextVariables.dateBeginOfMarket = bot.VALUES_TO_USE.timeRange.initialDatetime - - /* - Here we get the status report from the bot who knows which is the end of the market. - */ - statusReport = statusDependencies.reportsByMainUtility.get("Market Ending Point") - - if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Status Report does not exist. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - thisReport = statusReport.file; - - if (thisReport.lastFile === undefined) { - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> thisReport = " + JSON.stringify(thisReport)); - - let customOK = { - result: global.CUSTOM_OK_RESPONSE.result, - message: "Dependency not ready." - } - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message); - callBackFunction(customOK); - return; - } - - contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()); - - /* Validation that the data is not up-to-date. */ - if (bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS > contextVariables.dateEndOfMarket.valueOf() && bot.SESSION.type !== "Backtesting Session") { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> " + "Head of the market is @ " + contextVariables.dateEndOfMarket); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> You need to UPDATE your datasets in order to run a " + bot.SESSION.type) } - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - - } - - /* Finally we get our own Status Report. */ - statusReport = statusDependencies.reportsByMainUtility.get("Self Reference") - - if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Status Report does not exist. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because self dependecy Status Report is corrupt. Aborting Process."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - thisReport = statusReport.file; - variable = thisReport.variable; - if (variable === undefined) { variable = {} } // This should happen only when there is no status report - - if (thisReport.lastFile !== undefined) { - if (bot.hasTheBotJustStarted === true && bot.resumeExecution === false) { - - 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; - } - contextVariables.lastFile = new Date(thisReport.lastFile); - processSingleFiles(); - return; - - } else { - /* - We are here becuse either: - 1. There is no status report - 2. There is no Last File (this happens on Market Files) - */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining of the market because own status report not found or lastFile was undefined."); } - startFromBegining(); - return; - } - - function startFromBegining() { - - contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile); } - processSingleFiles(); - - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack); - if (err.message === "Cannot read property 'file' of undefined") { - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot configuration to see if all of its statusDependencies declarations are correct. "); - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)); - } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function processSingleFiles() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> Entering function."); } - - try { - let dependencyIndex = 0; - dataFiles = new Map(); - - dependencyLoopBody(); - - function dependencyLoopBody() { - - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - getFile(); - - function getFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> Entering function."); } - - if (datasetModule.node.config.codeName !== "Single-File") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency('atAnyTimeFrame', datasetModule.node.parentNode.config.singularVariableName) !== true) { - dependencyControlLoop(); - return - } - - let fileName = "Data.json"; - let filePath = datasetModule.node.parentNode.config.codeName + '/' + datasetModule.node.config.codeName; - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - if (err.message === 'File does not exist.') { - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); - callBackFunction(err); - } - return; - } - - let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); - - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyControlLoop -> Entering function."); } - - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - dependencyLoopBody(); - } else { - let mapKey = "Single Files" - multiPeriodDataFiles.set(mapKey, dataFiles) - processMarketFiles(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function processMarketFiles() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> Entering function."); } - - try { - let n; - timeFramesLoop(); - - function timeFramesLoop() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoop -> Entering function."); } - /* - We will iterate through all posible timeFrames. - */ - n = 0 // loop Variable representing each possible period as defined at the timeFrames array. - timeFramesLoopBody(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesLoopBody() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> Entering function."); } - const timeFrame = global.marketFilesPeriods[n][0]; - const timeFrameLabel = global.marketFilesPeriods[n][1]; - - let dependencyIndex = 0; - dataFiles = new Map(); - - if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { - currentTimeFrame = global.marketFilesPeriods[n][0]; - currentTimeFrameLabel = global.marketFilesPeriods[n][1]; - } - dependencyLoopBody(); - - function dependencyLoopBody() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - getFile(); - - function getFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> Entering function."); } - - if (dependency.referenceParent.config.codeName !== "Multi-Period-Market") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let fileName = "Data.json"; - let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - if (err.message === 'File does not exist.') { - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); - callBackFunction(err); - } - return; - } - - let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); - - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> dependencyControlLoop -> Entering function."); } - - dependencyIndex++; - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - dependencyLoopBody(); - } else { - let mapKey = global.marketFilesPeriods[n][1]; - multiPeriodDataFiles.set(mapKey, dataFiles) - timeFramesControlLoop(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesControlLoop() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesControlLoop -> Entering function."); } - n++; - if (n < global.marketFilesPeriods.length) { - timeFramesLoopBody(); - } else { - processDailyFiles(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function processDailyFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> Entering function."); } - try { - let n = 0; - if (currentTimeFrame) { - callTheBot() - return - } - bot.multiPeriodProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. - advanceTime(); - - function advanceTime() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> Entering function."); } - - bot.multiPeriodProcessDatetime = new Date(bot.multiPeriodProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); - previousDay = new Date(bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.multiPeriodProcessDatetime = " + bot.multiPeriodProcessDatetime); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } - - /* Validation that we are not going past the user defined end date. */ - if (bot.multiPeriodProcessDatetime.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { - - const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } - - bot.STOP_SESSION = true - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - - /* Validation that we are not going past the head of the market. */ - if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf() + ONE_DAY_IN_MILISECONDS - 1) { - - const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - - checkStopTaskGracefully(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> advanceTime -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function checkStopTaskGracefully() { - - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> checkStopTaskGracefully -> Entering function."); } - /* Validation that we dont need to stop. */ - if (global.STOP_TASK_GRACEFULLY === true) { - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - checkStopProcessing(); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> checkStopTaskGracefully -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function checkStopProcessing() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> checkStopProcessing -> Entering function."); } - - /* Validation that we dont need to stop. */ - if (bot.STOP_SESSION === true) { - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - - timeFramesLoop(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> checkStopProcessing -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesLoop() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoop -> Entering function."); } - - /* Telling the world we are alive and doing well */ - let processingDate = bot.multiPeriodProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); - bot.processHeartBeat(processingDate) - /* - We will iterate through all posible timeFrames. - */ - n = 0 // loop Variable representing each possible period as defined at the timeFrames array. - timeFramesLoopBody(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesLoopBody() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> Entering function."); } - - const timeFrame = global.dailyFilePeriods[n][0]; - const timeFrameLabel = global.dailyFilePeriods[n][1]; - - if (processConfig.framework.validtimeFrames !== undefined) { - let validPeriod = false; - for (let i = 0; i < processConfig.framework.validtimeFrames.length; i++) { - let period = processConfig.framework.validtimeFrames[i]; - if (period === timeFrameLabel) { validPeriod = true } - } - if (validPeriod === false) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> Discarding period for not being listed as a valid period. -> timeFrameLabel = " + timeFrameLabel); } - timeFramesControlLoop(); - return; - } - } - - if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { - currentTimeFrame = global.dailyFilePeriods[n][0]; - currentTimeFrameLabel = global.dailyFilePeriods[n][1]; - } - - let dependencyIndex = 0; - dataFiles = new Map(); - dependencyLoopBody(); - - function dependencyLoopBody() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - let previousFile; - let currentFile; - - if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { - getPreviousFile(); - } else { - previousFile = []; - getCurrentFile() - } - - function getPreviousFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> Entering function."); } - - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); - let filePath - - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> text = " + text); } - - if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { - - /* - Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. - When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find - all the dependencies and the first run of the bot is successful. - - After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. - */ - logger.write(MODULE_NAME, "[WARN] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found."); - - advanceTime(); - return; - } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - previousFile = JSON.parse(text); - getCurrentFile(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function getCurrentFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> Entering function."); } - - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let dateForPath = bot.multiPeriodProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> text = " + text); } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Not Ok -> err = " + err.code); - callBackFunction(err); - return; - } - - currentFile = JSON.parse(text); - let dataFile = previousFile.concat(currentFile); - dataFiles.set(dependency.id, dataFile); - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> dependencyControlLoop -> Entering function."); } - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - dependencyLoopBody(); - } else { - let mapKey = global.dailyFilePeriods[n][1]; - multiPeriodDataFiles.set(mapKey, dataFiles) - timeFramesControlLoop(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesControlLoop() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesControlLoop -> Entering function."); } - n++; - if (n < global.dailyFilePeriods.length) { - timeFramesLoopBody(); - } else { - n = 0; - if (currentTimeFrame !== undefined) { - callTheBot(); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> Time Frame not Recognized. Can not continue."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function callTheBot() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> Entering function."); } - botInstance.start( - multiPeriodDataFiles, - currentTimeFrame, - currentTimeFrameLabel, - bot.multiPeriodProcessDatetime, - variable, - onBotFinished); - - function onBotFinished(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> Entering function."); } - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - callBackFunction(err); - return; - } - - botNeverRan = false - bot.RESUME = true // From here on, all other loops executions must resume from where we left at this current run. - - if (currentTimeFrame > global.dailyFilePeriods[0][0]) { - writeMarketStatusReport(onMarketStatusReport) - - } else { - writeDataRanges(currentTimeFrameLabel, onWritten); - } - - function onWritten(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); - callBackFunction(err); - return; - } - - writeDailyStatusReport(bot.multiPeriodProcessDatetime, onDailyStatusReport); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - - function onDailyStatusReport() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onDailyStatusReport -> Entering function."); } - - /* The next run we need the process to continue at the date it finished. */ - processConfig.framework.startDate.resumeExecution = true; - - advanceTime(); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onDailyStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function onMarketStatusReport() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> Entering function."); } - - /* This is where we check if we need reached the user defined end datetime. */ - const now = new Date() - if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { - - const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> " + logText); } - - bot.STOP_SESSION = true - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function writeDataRanges(currentTimeFrameLabel, callBack) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRanges -> Entering function."); } - - let outputDatasetIndex = -1; - controlLoop(); - - function productLoopBody() { - let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; - writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodProcessDatetime, productCodeName, currentTimeFrameLabel, controlLoop); - } - - function controlLoop() { - outputDatasetIndex++; - if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { - productLoopBody(); - } else { - callBack(global.DEFAULT_OK_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRanges -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); - } - - } - - function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel, callBack) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> Entering function."); } - let dataRange = { - begin: pBegin.valueOf(), - end: pEnd.valueOf() + ONE_DAY_IN_MILISECONDS - }; - - let fileContent = JSON.stringify(dataRange); - let fileName = '/Data.Range.json'; - let filePath = bot.filePathRoot + "/Output/" + bot.SESSION.folderName + "/" + productCodeName + "/" + 'Multi-Period-Daily' + fileName; - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); - - function onFileCreated(err) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); - callBack(err); - return; - } - - if (LOG_FILE_CONTENT === true) { - logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent); - } - - let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset - let event = { - dateRange: dataRange - } - - global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) - - callBack(global.DEFAULT_OK_RESPONSE); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); - } - } - - function writeDailyStatusReport(lastFileDate, callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDailyStatusReport -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDailyStatusReport -> lastFileDate = " + lastFileDate); } - - try { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; - let thisReport = statusDependencies.statusReports.get(reportKey); - - thisReport.file.lastExecution = bot.currentDaytime; - thisReport.file.lastFile = lastFileDate; - thisReport.file.variable = variable; - thisReport.save(callBack); - - bot.hasTheBotJustStarted = false; - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDailyStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function writeMarketStatusReport(callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeMarketStatusReport -> Entering function."); } - try { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; - let thisReport = statusDependencies.statusReports.get(reportKey); - - thisReport.file.lastExecution = bot.processDatetime; - thisReport.file.variable = variable; - thisReport.save(callBack); - - logger.newInternalLoop(bot.codeName, bot.process, bot.multiPeriodProcessDatetime); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeMarketStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - function pad(str, max) { - str = str.toString(); - return str.length < max ? pad(" " + str, max) : str; - } -}; diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index 096df4c62..2d5e9741d 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -1,10 +1,7 @@ exports.newMultiPeriodDaily = function newMultiPeriodDaily(bot, logger, UTILITIES, FILE_STORAGE) { - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; const MODULE_NAME = "Multi Period Daily"; const GMT_SECONDS = ':00.000 GMT+0000'; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; thisObject = { initialize: initialize, @@ -12,15 +9,13 @@ start: start }; - let utilities = UTILITIES.newCloudUtilities(logger); + let utilities = UTILITIES.newCloudUtilities(logger) + let fileStorage = FILE_STORAGE.newFileStorage(logger) let statusDependencies; let dataDependenciesModule; let dataFiles = new Map; - - let botInstance; - - let fileStorage = FILE_STORAGE.newFileStorage(logger); + let indicatorOutputModule; let bootstrappingTheProcess = false @@ -30,49 +25,34 @@ return thisObject; function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { + logger.fileName = MODULE_NAME; + logger.initialize() - try { + statusDependencies = pStatusDependencies; + dataDependenciesModule = pDataDependencies; + processConfig = pProcessConfig; - logger.fileName = MODULE_NAME; - logger.initialize(); + let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - statusDependencies = pStatusDependencies; - dataDependenciesModule = pDataDependencies; - processConfig = pProcessConfig; - - let USER_BOT_MODULE = require("./IndicatorBot") - - botInstance = USER_BOT_MODULE.newIndicatorBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(callBackFunction); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) + indicatorOutputModule.initialize(callBackFunction) } function finalize() { dataFiles = undefined statusDependencies = undefined dataDependenciesModule = undefined - botInstance = undefined + indicatorOutputModule = undefined fileStorage = undefined processConfig = undefined thisObject = undefined } function start(callBackFunction) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } - let market = bot.market; /* Context Variables */ - let contextVariables = { lastFile: undefined, // Datetime of the last file files sucessfully produced by this process. dateBeginOfMarket: undefined, // Datetime of the first trade file in the whole market history. @@ -80,726 +60,468 @@ }; let previousDay; // Holds the date of the previous day relative to the processing date. - let interExecutionMemoryArray; - getContextVariables(); + getContextVariables() function getContextVariables() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Entering function."); } - let thisReport; let reportKey; let statusReport; - /* - We look first for the bot who knows the begining of the market in order to get when the market starts. + We look first for the bot who knows the begining of the market in order to get when the market starts. */ - statusReport = statusDependencies.reportsByMainUtility.get("Market Starting Point") if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Starting Point -> Status Report does not exist or Market Starting Point not defined. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Starting Point -> Status Report does not exist or Market Starting Point not defined. Retrying Later. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } thisReport = statusReport.file; if (thisReport.beginingOfMarket === undefined) { - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey); - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> It is too early too run this process since the trade history of the market is not there yet."); + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey) + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> It is too early too run this process since the trade history of the market is not there yet.") let customOK = { result: global.CUSTOM_OK_RESPONSE.result, message: "Dependency does not exist." } - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message); - callBackFunction(customOK); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message) + callBackFunction(customOK) + return } - contextVariables.dateBeginOfMarket = new Date(thisReport.beginingOfMarket.year + "-" + thisReport.beginingOfMarket.month + "-" + thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + GMT_SECONDS); - + contextVariables.dateBeginOfMarket = new Date(thisReport.beginingOfMarket.year + "-" + thisReport.beginingOfMarket.month + "-" + thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + GMT_SECONDS) /* - Here we get the status report from the bot who knows which is the end of the market. + Here we get the status report from the bot who knows which is the end of the market. */ - statusReport = statusDependencies.reportsByMainUtility.get("Market Ending Point") if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Ending Point -> Status Report does not exist or Market Ending Point not defined. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Ending Point -> Status Report does not exist or Market Ending Point not defined. Retrying Later. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } thisReport = statusReport.file; if (thisReport.lastFile === undefined) { - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey); + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey) let customOK = { result: global.CUSTOM_OK_RESPONSE.result, message: "Dependency not ready." } - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message); - callBackFunction(customOK); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message) + callBackFunction(customOK) + return } - contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()); + contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()) /* Finally we get our own Status Report. */ - statusReport = statusDependencies.reportsByMainUtility.get("Self Reference") if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Self Reference -> Status Report does not exist or Self Reference not defined. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Self Reference -> Status Report does not exist or Self Reference not defined. Retrying Later. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because self dependecy Status Report is corrupt. Aborting Process."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because self dependecy Status Report is corrupt. Aborting Process.") + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return } thisReport = statusReport.file; if (thisReport.lastFile !== undefined) { - beginingOfMarket = new Date(thisReport.beginingOfMarket); + beginingOfMarket = new Date(thisReport.beginingOfMarket) if (beginingOfMarket.valueOf() !== contextVariables.dateBeginOfMarket.valueOf()) { // Reset Mechanism for Begining of the Market - logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Reset Mechanism for Begining of the Market Activated. -> reportKey = " + reportKey); + logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Reset Mechanism for Begining of the Market Activated. -> reportKey = " + reportKey) - beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()); - startFromBegining(); - return; + beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()) + startFromBegining() + return } if (bot.hasTheBotJustStarted === true && processConfig.framework.startDate !== undefined && processConfig.framework.startDate.resumeExecution === false) { - 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; + 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 } - contextVariables.lastFile = new Date(thisReport.lastFile); + contextVariables.lastFile = new Date(thisReport.lastFile) interExecutionMemoryArray = thisReport.interExecutionMemoryArray; - processTimeFrames(); - return; - + processTimeFrames() } else { /* In the case when there is no status report, we assume like the last processed file is the one on the date of Begining of Market. */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining of the market because own status report not found or lastFile was undefined."); } - beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()); - startFromBegining(); - return; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining of the market because own status report not found or lastFile was undefined.") } + beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()) + startFromBegining() } function startFromBegining() { + contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS) - contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile); } - + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile) } /* The first time the process is running is the right time to create the data structure that is going to be shared across different executions. This data structure has one object per each timeFrame. */ - - interExecutionMemoryArray = []; + interExecutionMemoryArray = [] /* Also, we will remember that we are bootstrapping the process, this will allow us, to advanceTime if necesary. It can happen that a process depends on data from another process that does not produce a file during the first day, or even more days, in that situation we want the process not to just wait for that data that will never arrive, but to advance time until it finds valid data. */ - bootstrappingTheProcess = true for (let i = 0; i < global.dailyFilePeriods.length; i++) { let emptyObject = {}; - interExecutionMemoryArray.push(emptyObject); + interExecutionMemoryArray.push(emptyObject) } - processTimeFrames(); - + processTimeFrames() } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack) if (err.message === "Cannot read property 'file' of undefined") { - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot configuration to see if all of its statusDependencies declarations are correct. "); - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)); + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot configuration to see if all of its statusDependencies declarations are correct. ") + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)) } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } function processTimeFrames() { + let n; + let botNeverRan = true; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> Entering function."); } + bot.multiPeriodDailyProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS) // Go back one day to start well when we advance time at the begining of the loop. + let fromDate = new Date(bot.multiPeriodDailyProcessDatetime.valueOf()) + let lastDate = new Date() - try { + advanceTime() - let n; - let botNeverRan = true; + function advanceTime() { + bot.multiPeriodDailyProcessDatetime = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS) + previousDay = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS) - bot.multiPeriodDailyProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. - let fromDate = new Date(bot.multiPeriodDailyProcessDatetime.valueOf()) - let lastDate = new Date() + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> bot.multiPeriodDailyProcessDatetime = " + bot.multiPeriodDailyProcessDatetime) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> previousDay = " + previousDay) } - advanceTime(); + /* Validation that we are not going past the head of the market. */ + if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf()) { - function advanceTime() { + const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> " + logText) } - try { + callBackFunction(global.DEFAULT_OK_RESPONSE) + return - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> Entering function."); } - - bot.multiPeriodDailyProcessDatetime = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); - previousDay = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> bot.multiPeriodDailyProcessDatetime = " + bot.multiPeriodDailyProcessDatetime); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> previousDay = " + previousDay); } - - /* Validation that we are not going past the head of the market. */ - - if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf()) { - - const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> " + logText); } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - /* Telling the world we are alive and doing well */ - let currentDateString = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2); - let currentDate = new Date(bot.multiPeriodDailyProcessDatetime) - let percentage = global.getPercentage(fromDate, currentDate, lastDate) - bot.processHeartBeat(currentDateString, percentage) - - if (global.areEqualDates(currentDate, new Date()) === false) { - logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage); - } - - checkStopTaskGracefully(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> advanceTime -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } } - function checkStopTaskGracefully() { + /* Telling the world we are alive and doing well */ + let currentDateString = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2) + let currentDate = new Date(bot.multiPeriodDailyProcessDatetime) + let percentage = global.getPercentage(fromDate, currentDate, lastDate) + bot.processHeartBeat(currentDateString, percentage) - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFramesDailyFiles -> checkStopTaskGracefully -> Entering function."); } - - /* Validation that we dont need to stop. */ - - if (global.STOP_TASK_GRACEFULLY === true) { - - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - periodsLoop(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFramesDailyFiles -> checkStopTaskGracefully -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + if (global.areEqualDates(currentDate, new Date()) === false) { + logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) } - - function periodsLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoop -> Entering function."); } - - /* - - We will iterate through all posible periods. - - */ - - n = 0 // loop Variable representing each possible period as defined at the periods array. - - periodsLoopBody(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> Entering function."); } - - const timeFrame = global.dailyFilePeriods[n][0]; - const timeFrameLabel = global.dailyFilePeriods[n][1]; - - if (processConfig.framework.validPeriods !== undefined) { - let validPeriod = false; - for (let i = 0; i < processConfig.framework.validPeriods.length; i++) { - let period = processConfig.framework.validPeriods[i]; - if (period === timeFrameLabel) { validPeriod = true } - } - if (validPeriod === false) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> Discarding period for not being listed as a valid period. -> timeFrameLabel = " + timeFrameLabel); } - periodsControlLoop(); - return; - } - } - - let dependencyIndex = 0; - dataFiles = new Map(); - - dependencyLoopBody(); - - function dependencyLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - - if (dependency === undefined) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> You need to add at least one Data Dependency to the process Multi Period Daily. Aborting process."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - let previousFile; - let currentFile; - - if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { - getPreviousFile(); - } else { - previousFile = []; - getCurrentFile() - } - - function getPreviousFile() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> Entering function."); } - - let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> text = " + text); } - - if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { - - /* - Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. - When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find - all the dependencies and the first run of the bot is successful. - - After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. - */ - logger.write(MODULE_NAME, "[WARN] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found."); - - advanceTime(); - return; - } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - previousFile = JSON.parse(text); - - getCurrentFile(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function getCurrentFile() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Entering function."); } - - let dateForPath = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2); - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> text = " + text); } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - if (bootstrappingTheProcess === true) { - /* - In the special situation where we are running this process for the first time (no status report found), - we will consider the possibility that a depanant process does not produce a file during the first day of the market, - or even more than one day. That is why we are going to advance time here. - */ - advanceTime() - return - } - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.code); - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> filePath = " + filePath); - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> fileName = " + fileName); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Not OK -> onFileReceived -> err = " + err.code); - callBackFunction(err); - return; - } - - currentFile = JSON.parse(text); - - let dataFile = previousFile.concat(currentFile); - - dataFiles.set(dependency.id, dataFile); - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Catch Error -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> dependencyControlLoop -> Entering function."); } - - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - - dependencyLoopBody(); - - } else { - - callTheBot(); - - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function callTheBot() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> Entering function."); } - - const timeFrame = global.dailyFilePeriods[n][0]; - const timeFrameLabel = global.dailyFilePeriods[n][1]; - - botInstance.start( - dataFiles, - timeFrame, - timeFrameLabel, - bot.multiPeriodDailyProcessDatetime, - interExecutionMemoryArray[n], - onBotFinished); - - function onBotFinished(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - callBackFunction(err); - return; - } - - botNeverRan = false; - periodsControlLoop(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsControlLoop -> Entering function."); } - - n++; - - if (n < global.dailyFilePeriods.length) { - - periodsLoopBody(); - - } else { - - n = 0; - - writeDataRanges(onWritten); - - function onWritten(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> controlLoop -> onWritten -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> controlLoop -> onWritten -> err = " + err.stack); - callBackFunction(err); - return; - } - - writeStatusReport(bot.multiPeriodDailyProcessDatetime, advanceTime); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> controlLoop -> onWritten -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - + checkStopTaskGracefully() } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + function checkStopTaskGracefully() { + /* Validation that we dont need to stop. */ + if (global.STOP_TASK_GRACEFULLY === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE) + return + } + timeFramesLoop() + } + + function timeFramesLoop() { + /* + We will iterate through all posible timeFrames. + */ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody() + } + + function timeFramesLoopBody() { + const timeFrame = global.dailyFilePeriods[n][0] + const timeFrameLabel = global.dailyFilePeriods[n][1] + + if (processConfig.framework.validPeriods !== undefined) { + let validPeriod = false; + for (let i = 0; i < processConfig.framework.validPeriods.length; i++) { + let period = processConfig.framework.validPeriods[i] + if (period === timeFrameLabel) { validPeriod = true } + } + if (validPeriod === false) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> timeFramesLoopBody -> Discarding period for not being listed as a valid period. -> timeFrameLabel = " + timeFrameLabel) } + timeFramesControlLoop() + return + } + } + let dependencyIndex = 0 + dataFiles = new Map() + + dependencyLoopBody() + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex] + if (dependency === undefined) { + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> You need to add at least one Data Dependency to the process Multi Period Daily. Aborting process.") + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return + } + + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex] + let previousFile + let currentFile + + if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { + getPreviousFile() + } else { + previousFile = [] + getCurrentFile() + } + + function getPreviousFile() { + let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2) + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + let fileName = "Data.json" + + datasetModule.getTextFile(filePath, fileName, onFileReceived) + + function onFileReceived(err, text) { + if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { + /* + Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. + When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find + all the dependencies and the first run of the bot is successful. + + After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. + */ + logger.write(MODULE_NAME, "[WARN] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found.") + + advanceTime() + return + } + + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack) + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack) + callBackFunction(err) + return + } + + previousFile = JSON.parse(text) + getCurrentFile() + } + + } + + function getCurrentFile() { + let dateForPath = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2) + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + let fileName = "Data.json"; + + datasetModule.getTextFile(filePath, fileName, onFileReceived) + + function onFileReceived(err, text) { + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + if (bootstrappingTheProcess === true) { + /* + In the special situation where we are running this process for the first time (no status report found), + we will consider the possibility that a depanant process does not produce a file during the first day of the market, + or even more than one day. That is why we are going to advance time here. + */ + advanceTime() + return + } + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.code) + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> filePath = " + filePath) + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> fileName = " + fileName) + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> Not OK -> onFileReceived -> err = " + err.code) + callBackFunction(err) + return + } + + currentFile = JSON.parse(text) + let dataFile = previousFile.concat(currentFile) + dataFiles.set(dependency.id, dataFile) + dependencyControlLoop() + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody() + } else { + generateOutput() + } + + function generateOutput() { + const timeFrame = global.dailyFilePeriods[n][0] + const timeFrameLabel = global.dailyFilePeriods[n][1] + + indicatorOutputModule.start( + dataFiles, + timeFrame, + timeFrameLabel, + bot.multiPeriodDailyProcessDatetime, + interExecutionMemoryArray[n], + onOutputGenerated) + + function onOutputGenerated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err) + return + } + botNeverRan = false; + timeFramesControlLoop() + } + } + } + } + + function timeFramesControlLoop() { + n++ + if (n < global.dailyFilePeriods.length) { + timeFramesLoopBody() + } else { + n = 0; + writeDataRanges(onWritten) + + function onWritten(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> controlLoop -> onWritten -> err = " + err.stack) + callBackFunction(err) + return + } + writeStatusReport(bot.multiPeriodDailyProcessDatetime, advanceTime) + } + } } } function writeDataRanges(callBack) { + let outputDatasetIndex = -1; + controlLoop() - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRanges -> Entering function."); } - - let outputDatasetIndex = -1; - controlLoop(); - - function productLoopBody() { - let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; - writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodDailyProcessDatetime, productCodeName, controlLoop); - } - - function controlLoop() { - - outputDatasetIndex++; - - if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { - productLoopBody(); - } else { - callBack(global.DEFAULT_OK_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRanges -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); + function productLoopBody() { + let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; + writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodDailyProcessDatetime, productCodeName, controlLoop) } + function controlLoop() { + outputDatasetIndex++ + if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { + productLoopBody() + } else { + callBack(global.DEFAULT_OK_RESPONSE) + } + } } function writeDataRange(pBegin, pEnd, productCodeName, callBack) { + let dataRange = { + begin: pBegin.valueOf(), + end: pEnd.valueOf() + }; + let fileContent = JSON.stringify(dataRange) + let fileName = '/Data.Range.json'; + let filePath = bot.filePathRoot + "/Output/" + productCodeName + "/" + bot.process + fileName; - try { + fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> Entering function."); } - - let dataRange = { - begin: pBegin.valueOf(), - end: pEnd.valueOf() - }; - - let fileContent = JSON.stringify(dataRange); - - let fileName = '/Data.Range.json'; - let filePath = bot.filePathRoot + "/Output/" + productCodeName + "/" + bot.process + fileName; - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); - - function onFileCreated(err) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); - callBack(err); - return; - } - - if (LOG_FILE_CONTENT === true) { - logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent); - } - - let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset - let event = { - dateRange: dataRange - } - - global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) - - callBack(global.DEFAULT_OK_RESPONSE); + function onFileCreated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack) + callBack(err) + return } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); + let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset + let event = { + dateRange: dataRange + } + + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) + callBack(global.DEFAULT_OK_RESPONSE) } } function writeStatusReport(lastFileDate, callBack) { + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Daily" + let thisReport = statusDependencies.statusReports.get(reportKey) - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> lastFileDate = " + lastFileDate); } + thisReport.file.lastExecution = bot.currentDaytime; + thisReport.file.lastFile = lastFileDate; + thisReport.file.interExecutionMemoryArray = interExecutionMemoryArray; + thisReport.file.beginingOfMarket = beginingOfMarket.toUTCString() + thisReport.save(callBack) - try { - - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Daily" + "-" + "dataSet.V1"; - let thisReport = statusDependencies.statusReports.get(reportKey); - - thisReport.file.lastExecution = bot.currentDaytime; - thisReport.file.lastFile = lastFileDate; - thisReport.file.interExecutionMemoryArray = interExecutionMemoryArray; - thisReport.file.beginingOfMarket = beginingOfMarket.toUTCString() - thisReport.save(callBack); - - bot.hasTheBotJustStarted = false; - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + bot.hasTheBotJustStarted = false; } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } - - function pad(str, max) { - str = str.toString(); - return str.length < max ? pad(" " + str, max) : str; - } }; diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js index 11bc7ae40..3e29f55f7 100644 --- a/MultiPeriodMarket.js +++ b/MultiPeriodMarket.js @@ -1,327 +1,164 @@ exports.newMultiPeriodMarket = function newMultiPeriodMarket(bot, logger, UTILITIES, FILE_STORAGE) { - - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; const MODULE_NAME = "Multi Period Market"; - thisObject = { initialize: initialize, finalize: finalize, start: start }; - let utilities = UTILITIES.newCloudUtilities(logger); + let utilities = UTILITIES.newCloudUtilities(logger) let statusDependencies; let dataDependenciesModule; - let dataFiles = new Map(); - - let botInstance; - + let dataFiles = new Map() + let indicatorOutputModule; let processConfig; return thisObject; function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { + logger.fileName = MODULE_NAME; + logger.initialize() - try { + statusDependencies = pStatusDependencies; + dataDependenciesModule = pDataDependencies; + processConfig = pProcessConfig; - logger.fileName = MODULE_NAME; - logger.initialize(); + let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - statusDependencies = pStatusDependencies; - dataDependenciesModule = pDataDependencies; - processConfig = pProcessConfig; - - let USER_BOT_MODULE = require("./IndicatorBot") - - botInstance = USER_BOT_MODULE.newIndicatorBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(callBackFunction); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) + indicatorOutputModule.initialize(callBackFunction) } function finalize() { dataFiles = undefined statusDependencies = undefined dataDependenciesModule = undefined - botInstance = undefined + indicatorOutputModule = undefined processConfig = undefined thisObject = undefined } function start(callBackFunction) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } - - let market = bot.market; - - /* Context Variables */ - - let contextVariables = { - dateBeginOfMarket: undefined, // Datetime of the first trade file in the whole market history. - dateEndOfMarket: undefined // Datetime of the last file available to be used as an input of this process. - }; - - processTimeFrames(); + processTimeFrames() function processTimeFrames() { + let n; + timeFramesLoop() - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> Entering function."); } - - try { - - let n; - - periodsLoop(); - - function periodsLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoop -> Entering function."); } - - /* - - We will iterate through all posible periods. - - */ - - n = 0 // loop Variable representing each possible period as defined at the periods array. - - periodsLoopBody(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> Entering function."); } - - const timeFrame = global.marketFilesPeriods[n][0]; - const timeFrameLabel = global.marketFilesPeriods[n][1]; - - let dependencyIndex = 0; - dataFiles = new Map; - - dependencyLoopBody(); - - function dependencyLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - getFile(); - - function getFile() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> Entering function."); } - - let dateForPath = bot.processDatetime.getUTCFullYear() + '/' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.processDatetime.getUTCDate(), 2); - let fileName = "Data.json"; - - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Market") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); - - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> dependencyControlLoop -> Entering function."); } - - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - - dependencyLoopBody(); - - } else { - - callTheBot(); - - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function callTheBot() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> Entering function."); } - - const timeFrame = global.marketFilesPeriods[n][0]; - const timeFrameLabel = global.marketFilesPeriods[n][1]; - - botInstance.start( - dataFiles, - timeFrame, - timeFrameLabel, - undefined, - undefined, - onBotFinished); - - function onBotFinished(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - callBackFunction(err); - return; - } - - periodsControlLoop(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsControlLoop -> Entering function."); } - - n++; - - if (n < global.marketFilesPeriods.length) { - - periodsLoopBody(); - - } else { - - writeStatusReport(callBackFunction); - - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - + function timeFramesLoop() { + /* We will iterate through all posible timeFrames.*/ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody() } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + function timeFramesLoopBody() { + const timeFrame = global.marketFilesPeriods[n][0] + const timeFrameLabel = global.marketFilesPeriods[n][1] + + /* Check Time Frames Filter */ + if (bot.marketTimeFrames !== undefined) { + if (bot.marketTimeFrames.includes(timeFrameLabel) === false) { + /* We are not going to process this Time Frame */ + timeFramesControlLoop() + return + } + } + + let dependencyIndex = 0; + dataFiles = new Map; + + dependencyLoopBody() + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex] + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex] + + getFile() + + function getFile() { + let fileName = "Data.json"; + let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; + datasetModule.getTextFile(filePath, fileName, onFileReceived) + + function onFileReceived(err, text) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack) + callBackFunction(err) + return; + } + + let dataFile = JSON.parse(text) + dataFiles.set(dependency.id, dataFile) + + dependencyControlLoop() + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody() + } else { + generateOutput() + } + + function generateOutput() { + indicatorOutputModule.start( + dataFiles, + timeFrame, + timeFrameLabel, + undefined, + undefined, + onOutputGenerated) + + function onOutputGenerated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err) + return; + } + timeFramesControlLoop() + } + } + } + } + + function timeFramesControlLoop() { + n++; + if (n < global.marketFilesPeriods.length) { + timeFramesLoopBody() + } else { + writeStatusReport(callBackFunction) + } } } function writeStatusReport(callBack) { + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Market" + let thisReport = statusDependencies.statusReports.get(reportKey) - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> Entering function."); } - - try { - - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Market" + "-" + "dataSet.V1"; - let thisReport = statusDependencies.statusReports.get(reportKey); - - thisReport.file.lastExecution = bot.processDatetime; - thisReport.save(callBack); - - if (global.areEqualDates(bot.processDatetime, new Date()) === false) { - logger.newInternalLoop(bot.codeName, bot.process, bot.processDatetime); - } - - /* Telling the world we are alive and doing well */ - let currentDateString = bot.processDatetime.getUTCFullYear() + '-' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.processDatetime.getUTCDate(), 2); - let currentDate = new Date(bot.processDatetime) - let lastDate = new Date() - bot.processHeartBeat(currentDateString, global.getPercentage(currentDate, currentDate, lastDate)) + thisReport.file.lastExecution = bot.processDatetime; + if (bot.marketTimeFrames !== undefined) { + thisReport.file.timeFrames = bot.marketTimeFrames } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + thisReport.save(callBack) + + if (global.areEqualDates(bot.processDatetime, new Date()) === false) { + logger.newInternalLoop(bot.codeName, bot.process, bot.processDatetime) } + + /* Telling the world we are alive and doing well */ + let currentDateString = bot.processDatetime.getUTCFullYear() + '-' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.processDatetime.getUTCDate(), 2) + let currentDate = new Date(bot.processDatetime) + let lastDate = new Date() + bot.processHeartBeat(currentDateString, global.getPercentage(currentDate, currentDate, lastDate)) } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } -}; +} diff --git a/ProcessOutput.js b/ProcessOutput.js index 2130a7cf6..f9cb330d1 100644 --- a/ProcessOutput.js +++ b/ProcessOutput.js @@ -14,7 +14,7 @@ return thisObject; - function raiseEvents(lastFile, callBackFunction) { + function raiseEvents(lastFile, timeFrames, callBackFunction) { try { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] raiseEvents -> Entering function."); } @@ -83,7 +83,8 @@ key = dataMine + "-" + botName + "-" + product + "-" + dataset + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset let event = { - lastFile: lastFile + lastFile: lastFile, + timeFrames: timeFrames } global.EVENT_SERVER_CLIENT.createEventHandler(key, 'Dataset Updated') global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Dataset Updated', event) diff --git a/Root.js b/Root.js index f4f3910f3..06b1fe77b 100644 --- a/Root.js +++ b/Root.js @@ -35,7 +35,6 @@ global.dailyFilePeriods = JSON.parse(global.dailyFilePeriods); - const ROOT_DIR = './'; const MODULE_NAME = "Root"; const WAIT_TIME_FOR_ALL_PROCESS_INSTANCES_TO_START = 10000 // This avoid a race condition that could happen if one process finished before all the other even started. @@ -132,9 +131,9 @@ /* Process Loops Declarations. */ - const INDICATOR_BOT_MAIN_LOOP_MODULE = require('./IndicatorBotProcessMainLoop'); - const SENSOR_BOT_MAIN_LOOP_MODULE = require('./SensorBotProcessMainLoop'); - const TRADING_ENGINE_MAIN_LOOP_MODULE = require('./TradingBotProcessMainLoop'); + const INDICATOR_BOT_MODULE = require('./IndicatorBot'); + const SENSOR_BOT = require('./SensorBot'); + const TRADING_BOT_MODULE = require('./TradingBot'); let botConfig; let processInstance = global.TASK_NODE.bot.processes[processIndex] @@ -144,7 +143,7 @@ processInstance.referenceParent.config.framework !== undefined && (processInstance.referenceParent.config.framework.name === 'Multi-Period-Market' || processInstance.referenceParent.config.framework.name === 'Multi-Period-Daily' || - processInstance.referenceParent.config.framework.name === 'Multi-Period') + processInstance.referenceParent.config.framework.name === 'Low-Frequency-Trading-Process') ) { botConfig = processInstance.referenceParent.parentNode.config botConfig.definedByUI = true @@ -180,7 +179,7 @@ botConfig.repo = global.TASK_NODE.bot.config.repo; bootingBot(processIndex); } catch (err) { - console.log(logDisplace + "Root : [ERROR] start -> getBotConfigFromFile -> onInizialized -> onFileReceived -> err = " + JSON.stringify(err)); + console.log(logDisplace + "Root : [ERROR] start -> getBotConfigFromFile -> onInizialized -> onFileReceived -> err = " + err.stack); return; } } @@ -214,13 +213,6 @@ botConfig.uiStartDate = global.TASK_NODE.bot.config.startDate botConfig.config = global.TASK_NODE.bot.config - /* This stuff is still hardcoded and unresolved. */ - botConfig.version = { - "major": 1, - "minor": 0 - } - botConfig.dataSetVersion = "dataSet.V1" - /* Loop Counter */ botConfig.loopCounter = 0; @@ -233,18 +225,20 @@ /* Bot Type */ botConfig.type = global.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.type - /* Setting the Key to use */ - process.env.KEY = undefined - process.env.SECRET = undefined + /* Time Frame Filter */ + if (global.TASK_NODE.bot.timeFramesFilter !== undefined) { + botConfig.dailyTimeFrames = global.TASK_NODE.bot.timeFramesFilter.config.dailyTimeFrames + botConfig.marketTimeFrames = global.TASK_NODE.bot.timeFramesFilter.config.marketTimeFrames + } if (botConfig.processNode) { if (botConfig.processNode.marketReference) { - if (botConfig.processNode.marketReference.keyInstance !== undefined) { - if (botConfig.processNode.marketReference.keyInstance.referenceParent !== undefined) { - let key = botConfig.processNode.marketReference.keyInstance.referenceParent + if (botConfig.processNode.marketReference.keyReference !== undefined) { + if (botConfig.processNode.marketReference.keyReference.referenceParent !== undefined) { + let key = botConfig.processNode.marketReference.keyReference.referenceParent - process.env.KEY = key.config.codeName - process.env.SECRET = key.config.secret + botConfig.KEY = key.config.codeName + botConfig.SECRET = key.config.secret } } } @@ -253,7 +247,7 @@ let processConfig = global.TASK_NODE.bot.processes[processIndex].referenceParent.config if (processConfig.framework !== undefined) { - if (processConfig.framework.name === "Multi-Period-Daily" || processConfig.framework.name === "Multi-Period-Market" || processConfig.framework.name === "Multi-Period") { + if (processConfig.framework.name === "Multi-Period-Daily" || processConfig.framework.name === "Multi-Period-Market" || processConfig.framework.name === "Low-Frequency-Trading-Process") { if (processConfig.framework.startDate !== undefined) { processConfig.framework.startDate.resumeExecution = true; if (processConfig.startMode.noTime !== undefined) { @@ -387,7 +381,7 @@ try { global.TOTAL_PROCESS_INSTANCES_CREATED++ - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; logger = DEBUG_MODULE.newDebugLog(); @@ -396,14 +390,14 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> Entering function."); } - let extractionBotMainLoop = SENSOR_BOT_MAIN_LOOP_MODULE.newSensorBotProcessMainLoop(pBotConfig, logger); - extractionBotMainLoop.initialize(pProcessConfig, onInitializeReady); + let sensorBot = SENSOR_BOT.newSensorBot(pBotConfig, logger); + sensorBot.initialize(pProcessConfig, onInitializeReady); function onInitializeReady(err) { if (err.result === global.DEFAULT_OK_RESPONSE.result) { - extractionBotMainLoop.run(whenRunFinishes); + sensorBot.run(whenRunFinishes); function whenRunFinishes(err) { @@ -412,14 +406,11 @@ let botId = pBotConfig.dataMine + "." + pBotConfig.codeName + "." + pBotConfig.process; if (err.result === global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Bot execution finished sucessfully."); logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Bot Id = " + botId); logger.persist(); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> err = " + err.message); logger.write(MODULE_NAME, "[ERROR] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Execution will be stopped. "); logger.write(MODULE_NAME, "[ERROR] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Bye."); @@ -452,7 +443,7 @@ try { global.TOTAL_PROCESS_INSTANCES_CREATED++ - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; logger = DEBUG_MODULE.newDebugLog(); @@ -461,14 +452,14 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runIndicatorBot -> Entering function."); } - let indicatorBotMainLoop = INDICATOR_BOT_MAIN_LOOP_MODULE.newIndicatorBotProcessMainLoop(pBotConfig, logger); - indicatorBotMainLoop.initialize(pProcessConfig, onInitializeReady); + let indicatorBot = INDICATOR_BOT_MODULE.newIndicatorBot(pBotConfig, logger); + indicatorBot.initialize(pProcessConfig, onInitializeReady); function onInitializeReady(err) { if (err.result === global.DEFAULT_OK_RESPONSE.result) { - indicatorBotMainLoop.run(whenRunFinishes); + indicatorBot.run(whenRunFinishes); function whenRunFinishes(err) { @@ -515,7 +506,7 @@ global.TOTAL_PROCESS_INSTANCES_CREATED++ try { - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; logger = DEBUG_MODULE.newDebugLog(); @@ -524,14 +515,14 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runTradingBot -> Entering function."); } - let tradingBotMainLoop = TRADING_ENGINE_MAIN_LOOP_MODULE.newTradingBotProcessMainLoop(pBotConfig, logger); - tradingBotMainLoop.initialize(pProcessConfig, onInitializeReady); + let tradingBot = TRADING_BOT_MODULE.newTradingBot(pBotConfig, logger); + tradingBot.initialize(pProcessConfig, onInitializeReady); function onInitializeReady(err) { if (err.result === global.DEFAULT_OK_RESPONSE.result) { - tradingBotMainLoop.run(whenRunFinishes); + tradingBot.run(whenRunFinishes); function whenRunFinishes(err) { diff --git a/SensorBotProcessMainLoop.js b/SensorBot.js similarity index 59% rename from SensorBotProcessMainLoop.js rename to SensorBot.js index 558d9ffa4..d8f8aaa32 100644 --- a/SensorBotProcessMainLoop.js +++ b/SensorBot.js @@ -1,8 +1,6 @@ -exports.newSensorBotProcessMainLoop = function newSensorBotProcessMainLoop(bot, parentLogger) { +exports.newSensorBot = function newSensorBot(bot, parentLogger) { - const ROOT_DIR = './'; - - const MODULE_NAME = "Sensor Process Main Loop"; + const MODULE_NAME = "Sensor Bot"; const FULL_LOG = true; let USER_BOT_MODULE; @@ -11,11 +9,10 @@ const FILE_STORAGE = require('./FileStorage.js'); let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; - let checkLoopHealthHandle; let thisObject = { initialize: initialize, @@ -27,12 +24,8 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - /* This function is exactly the same in the 3 modules representing the 2 different bot types loops. */ - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - processConfig = pProcessConfig; if (bot.definedByUI === true) { @@ -47,7 +40,6 @@ fileStorage.getTextFile(filePath, onBotDownloaded); function onBotDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); @@ -70,7 +62,6 @@ fileStorage.getTextFile(filePath, onCommonsDownloaded); function onCommonsDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -85,42 +76,37 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } function run(callBackFunction) { - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - - bot.enableCheckLoopHealth = true; - let fixedTimeLoopIntervalHandle; - if (bot.runAtFixedInterval === true) { + /* Errors sent to the UI */ + bot.processError = processError + /* Heartbeats sent to the UI */ + bot.processHeartBeat = processHeartBeat + + if (bot.runAtFixedInterval === true) { fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); loop(); // First run. - } else { - loop(); - } function loop() { - try { - processHeartBeat(undefined, undefined, "Running...") + processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { str = str.toString(); return str.length < max ? pad(" " + str, max) : str; } /* For each loop we want to create a new log file. */ - if (logger !== undefined) { logger.finalize() } @@ -132,31 +118,24 @@ bot.loopCounter++; bot.loopStartTime = new Date().valueOf(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - - /* Loop Heartbeat sent to the UI */ - bot.processHeartBeat = processHeartBeat - processHeartBeat() + /* We tell the UI that we are running. */ + processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(global.ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(global.ROOT_DIR + 'StatusDependencies'); + const PROCESS_EXECUTION_EVENTS = require(global.ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(global.ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. - /* High level log entry */ - - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) - + " Main Loop # " + pad(Number(bot.loopCounter), 8) ) + /* High level log entry */ + console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + + " Main Loop # " + pad(Number(bot.loopCounter), 8)) /* We will prepare first the infraestructure needed for the bot to run. There are 4 modules we need to sucessfullly initialize first. */ - let processExecutionEvents let userBot; let statusDependencies; @@ -167,21 +146,12 @@ initializeProcessExecutionEvents(); function initializeProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - processExecutionEvents.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); @@ -195,66 +165,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - processExecutionEvents.start(onStarted); function onStarted(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); @@ -274,68 +219,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeStatusDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - statusDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); @@ -349,68 +267,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function initializeUserBot() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> Entering function."); } - usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE, STATUS_REPORT, exchangeAPI); - usertBot.initialize(statusDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized > Execution finished well."); @@ -424,81 +315,51 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { switch (err.message) { default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function startUserBot() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } - usertBot.start(onFinished); function onFinished(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished > Execution finished well."); @@ -512,15 +373,10 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { @@ -539,66 +395,43 @@ } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function finishProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - processExecutionEvents.finish(onFinished); function onFinished(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - processExecutionEvents.finalize() - processExecutionEvents = undefined + processExecutionEvents = undefined switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { @@ -614,108 +447,63 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ /* Here we check if we must stop the loop gracefully. */ - shallWeStop(onStop, onContinue); function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - - if(global.WRITE_LOGS_TO_FILES === 'true'){ - logger.persist(); - } - - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); return; - } function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - /* Sensor bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - switch (nextWaitTime) { case 'Normal': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") - if(global.WRITE_LOGS_TO_FILES === 'true'){ + processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") + if (global.WRITE_LOGS_TO_FILES === 'true') { logger.persist(); } } break; case 'Retry': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.retryWaitTime * 5, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); } break; @@ -727,7 +515,7 @@ } else { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") logger.persist(); } } @@ -740,7 +528,7 @@ } else { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") logger.persist(); } } @@ -749,44 +537,9 @@ } } - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); - - } - } - } - function shallWeStop(stopCallBack, continueCallBack) { - try { - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - if (!global.STOP_TASK_GRACEFULLY) { continueCallBack(); } else { @@ -794,23 +547,17 @@ } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = "+ err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } @@ -824,17 +571,44 @@ global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) } + function processError(node, errorMessage) { + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) + } + function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + if (global.unexpectedError !== undefined) { + processError(undefined, "An unexpected error caused the Process to stop.") + } else { + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + } + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + if (global.unexpectedError !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/Session.js b/Session.js index 3e84808ed..056d01a56 100644 --- a/Session.js +++ b/Session.js @@ -2,22 +2,19 @@ const MODULE_NAME = "Session" const FULL_LOG = true; - const ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000 let thisObject = { initialize: initialize } + const SOCIAL_BOTS_MODULE = require('./SocialBots.js') + let socialBotsModule = SOCIAL_BOTS_MODULE.newSocialBots(bot, parentLogger) + return thisObject; function initialize(pProcessConfig, callBackFunction) { try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - let telegramAPI - /* Initialize this info so that everything is logged propeerly */ - bot.SESSION = { name: bot.processNode.session.name, id: bot.processNode.session.id @@ -44,11 +41,16 @@ 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) + /* Errors sent to the UI */ + bot.sessionError = sessionError + + /* Heartbeats sent to the UI */ + bot.sessionHeartBeat = sessionHeartBeat + callBackFunction(global.DEFAULT_OK_RESPONSE) return function runSession(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. @@ -59,31 +61,15 @@ bot.SESSION = JSON.parse(message.event.session) bot.DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter) bot.RESUME = message.event.resume + bot.FIRST_EXECUTION = true + bot.SESSION.stop = stopSession // stop function - /* Setup the APP_SCHEMA_MAP based on the APP_SCHEMA_ARRAY */ - bot.APP_SCHEMA_MAP = new Map() - for (let i = 0; i < bot.APP_SCHEMA_ARRAY.length; i++) { - let nodeDefinition = bot.APP_SCHEMA_ARRAY[i] - let key = nodeDefinition.type - bot.APP_SCHEMA_MAP.set(key, nodeDefinition) - } + setUpAppSchema() + setUpSessionFolderName() - /* Set the folderName for logging, reports, context and data output */ - let config - if (bot.SESSION.config !== undefined) { - config = bot.SESSION.config - if (config.folderName === undefined) { - bot.SESSION.folderName = bot.SESSION.id - } else { - bot.SESSION.folderName = config.folderName + "-" + bot.SESSION.id - } - } + if (checkParemeters() === false) { return } - /* Extract values from different sources and consolidate them under one structure that is going to be used later on. */ - setValuesToUse(message) - - /* Set up Social Bots */ - initializeSocialBots() + socialBotsModule.initialize() let allGood switch (bot.SESSION.type) { @@ -110,105 +96,258 @@ } else { bot.STOP_SESSION = true } + + socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is starting.") } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> runSession -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - function stopSession(message) { + 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 - finalizeSocialBots() } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> stopSession -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - function checkDatetimes() { - if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding initialDatetime with present datetime because " + bot.VALUES_TO_USE.timeRange.initialDatetime + " is in the past."); } - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() + function setUpAppSchema() { + /* Setup the APP_SCHEMA_MAP based on the APP_SCHEMA_ARRAY */ + bot.APP_SCHEMA_MAP = new Map() + for (let i = 0; i < bot.APP_SCHEMA_ARRAY.length; i++) { + let nodeDefinition = bot.APP_SCHEMA_ARRAY[i] + let key = nodeDefinition.type + bot.APP_SCHEMA_MAP.set(key, nodeDefinition) } - if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() < (new Date()).valueOf()) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding finalDatetime with present datetime plus one year because " + bot.VALUES_TO_USE.timeRange.finalDatetime + " is in the past."); } - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() + ONE_YEAR_IN_MILISECONDS + } + + function setUpSessionFolderName() { + /* Set the folderName for logging, reports, context and data output */ + let config + if (bot.SESSION.config !== undefined) { + config = bot.SESSION.config + if (config.folderName === undefined) { + bot.SESSION.folderName = bot.SESSION.id + } else { + bot.SESSION.folderName = config.folderName + "-" + bot.SESSION.id + } } } + function checkParemeters() { + /* + Here we check all the Session Parameters received. If something critical is missing we abort returning false. If something + non critical is missing, we complete it with a default value. + */ + + if (bot.SESSION.parameters === undefined) { + let errorMessage = "Session Node with no Parameters." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION, errorMessage) + return false + } + + /* Time Range */ + if (bot.SESSION.parameters.timeRange === undefined) { // if the Time Range is missing we create a default one. + bot.SESSION.parameters.timeRange = { + name: 'Missing Time Range', + type: 'Time Range', + config: { + initialDatetime: (new Date()).valueOf(), + finalDatetime: (new Date()).valueOf() + } + } + } + + /* Session Type Forced Values */ + let today = new Date((new Date()).valueOf()) + let aYearAgo = today - global.ONE_YEAR_IN_MILISECONDS + let aYearFromNow = today + global.ONE_YEAR_IN_MILISECONDS + switch (bot.SESSION.type) { + case 'Backtesting Session': { + useDefaultDatetimes(aYearAgo, today) + break + } + case 'Live Trading Session': { + useDefaultDatetimes(today, aYearFromNow) + break + } + case 'Fordward Testing Session': { + useDefaultDatetimes(today, aYearFromNow) + break + } + case 'Paper Trading Session': { + useDefaultDatetimes(today, aYearFromNow) + break + } + } + + function useDefaultDatetimes(initialDefault, finalDefault) { + /* Initial Datetime */ + if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) { + bot.SESSION.parameters.timeRange.config.initialDatetime = initialDefault + } else { + bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf() + } + + /* Final Datetime */ + if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { + bot.SESSION.parameters.timeRange.config.finalDatetime = finalDefault + } else { + bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf() + } + } + + /* Time Frame */ + if (bot.SESSION.parameters.timeFrame === undefined) { + let errorMessage = "Session Parameters Node with no Time Frame." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters, errorMessage) + return false + } + if (bot.SESSION.parameters.timeFrame.config.label === undefined) { + let errorMessage = "Session Parameters Node with no Time Frame Label configuration." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters.timeFrame, errorMessage) + return false + } + bot.SESSION.parameters.timeFrame.config.value = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) + + /* Session Base Asset */ + if (bot.SESSION.parameters.sessionBaseAsset === undefined) { + let errorMessage = "Session Parameters Node with no Session Base Asset." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters, errorMessage) + return false + } + if (bot.SESSION.parameters.sessionBaseAsset.config.initialBalance === undefined) { + let errorMessage = "Session Parameters Session Base Asset with no initialBalance configuration." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters.sessionBaseAsset, errorMessage) + return false + } + if (bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance === undefined) { + bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = 0 + } + if (bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance === undefined) { + bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER + } + + /* Session Quoted Asset */ + if (bot.SESSION.parameters.sessionQuotedAsset === undefined) { + let errorMessage = "Session Parameters Node with no Session Quoted Asset." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters, errorMessage) + return false + } + if (bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance === undefined) { + let errorMessage = "Session Parameters Session Quoted Asset with no initialBalance configuration." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters.sessionQuotedAsset, errorMessage) + return false + } + if (bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance === undefined) { + bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = 0 + } + if (bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance === undefined) { + bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER + } + + /* Slippage */ + if (bot.SESSION.parameters.slippage === undefined) { // if the Slippage is missing we create a default one. + bot.SESSION.parameters.timeRange = { + name: 'Missing Slippage', + type: 'Slippage', + config: { + positionRate: 0, + stopLoss: 0, + takeProfit: 0 + } + } + } + if (bot.SESSION.parameters.slippage.config.positionRate === undefined) { // if the positionRate is missing we create a default one. + bot.SESSION.parameters.slippage.config.positionRate = 0 + } + if (bot.SESSION.parameters.slippage.config.stopLoss === undefined) { // if the stopLoss is missing we create a default one. + bot.SESSION.parameters.slippage.config.stopLoss = 0 + } + if (bot.SESSION.parameters.slippage.config.takeProfit === undefined) { // if the takeProfit is missing we create a default one. + bot.SESSION.parameters.slippage.config.takeProfit = 0 + } + + /* Fee Structure */ + if (bot.SESSION.parameters.feeStructure === undefined) { // if the Fee Structure is missing we create a default one. + bot.SESSION.parameters.feeStructure = { + name: 'Missing Fee Structure', + type: 'Fee Structure', + config: { + maker: 0, + taker: 0 + } + } + } + if (bot.SESSION.parameters.feeStructure.config.maker === undefined) { // if the maker is missing we create a default one. + bot.SESSION.parameters.feeStructure.config.maker = 0 + } + if (bot.SESSION.parameters.feeStructure.config.taker === undefined) { // if the taker is missing we create a default one. + bot.SESSION.parameters.feeStructure.config.taker = 0 + } + + return true + } + function startBackTesting(message) { - bot.startMode = "Backtest" - if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() > (new Date()).valueOf()) { - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() - } - bot.resumeExecution = false; - bot.hasTheBotJustStarted = true - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // Here we initialize the date of the process. return true } function startLiveTrading(message) { - - if (process.env.KEY === undefined || process.env.SECRET === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startLiveTrading -> Key name or Secret not provided, not possible to run the process in Live mode."); } - console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.") - return + if (bot.KEY === undefined || bot.SECRET === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> Key 'codeName' or 'secret' not provided. Plese check that and try again."); } + return false } - bot.startMode = "Live" - checkDatetimes() - bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) - bot.hasTheBotJustStarted = true - pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + pProcessConfig.liveWaitTime = bot.SESSION.parameters.timeFrame.config.value return true } function startFordwardTesting(message) { - - if (process.env.KEY === undefined || process.env.SECRET === undefined) { + if (bot.KEY === undefined || bot.SECRET === undefined) { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startFordwardTesting -> Key name or Secret not provided, not possible to run the process in Forward Testing mode."); } console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.") - return + return false } - bot.startMode = "Live" - checkDatetimes() - bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) - bot.hasTheBotJustStarted = true + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) /* Reduce the balance */ - let balancePercentage = 1 // This is the default value if (bot.SESSION.config.balancePercentage !== undefined) { balancePercentage = bot.SESSION.config.balancePercentage } - bot.VALUES_TO_USE.initialBalanceA = bot.VALUES_TO_USE.initialBalanceA * balancePercentage / 100 - bot.VALUES_TO_USE.initialBalanceB = bot.VALUES_TO_USE.initialBalanceB * balancePercentage / 100 + bot.SESSION.parameters.sessionBaseAsset.config.initialBalance = bot.SESSION.parameters.sessionBaseAsset.config.initialBalance * balancePercentage / 100 + bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance = bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance * balancePercentage / 100 - bot.VALUES_TO_USE.minimumBalanceA = bot.VALUES_TO_USE.minimumBalanceA * balancePercentage / 100 - bot.VALUES_TO_USE.minimumBalanceB = bot.VALUES_TO_USE.minimumBalanceB * balancePercentage / 100 + bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance * balancePercentage / 100 + bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance * balancePercentage / 100 - bot.VALUES_TO_USE.maximumBalanceA = bot.VALUES_TO_USE.maximumBalanceA * balancePercentage / 100 - bot.VALUES_TO_USE.maximumBalanceB = bot.VALUES_TO_USE.maximumBalanceB * balancePercentage / 100 + bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100 + bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100 - pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) + pProcessConfig.normalWaitTime = bot.SESSION.parameters.timeFrame.config.value return true } function startPaperTrading(message) { - bot.startMode = "Backtest" - - checkDatetimes() - bot.resumeExecution = false; - bot.hasTheBotJustStarted = true - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) - pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + pProcessConfig.normalWaitTime = bot.SESSION.parameters.timeFrame.config.value return true } @@ -233,290 +372,37 @@ } } - function setValuesToUse(message) { - /* - Base on the information received we will determined which values ultimatelly are going to be used, - and once we do, they will become constants across the multiple loops executions. - */ - - /* Set all default values */ - bot.VALUES_TO_USE = { - slippage: { - positionRate: 0, - stopLoss: 0, - takeProfit: 0 - }, - feeStructure: { - maker: 0, - taker: 0 - }, - timeRange: { - initialDatetime: new Date(), - finalDatetime: new Date() - } + function sessionHeartBeat(processingDate) { + let event = { + seconds: (new Date()).getSeconds(), + processingDate: processingDate } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Heartbeat', event) - /* Session Type Dependant Default Values */ - - switch (bot.SESSION.type) { - case 'Backtesting Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date((new Date()).valueOf() - ONE_YEAR_IN_MILISECONDS) - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() - break - } - case 'Live Trading Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) - break - } - case 'Fordward Testing Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) - break - } - case 'Paper Trading Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) - break - } - } - - let tradingSystem = bot.TRADING_SYSTEM - - if (tradingSystem !== undefined) { - - /* Applying Session Level Parameters */ - if (bot.SESSION !== undefined) { - if (bot.SESSION.parameters !== undefined) { - - /* Base Asset and Initial Balances. */ - { - if (bot.SESSION.parameters.baseAsset !== undefined) { - if (bot.SESSION.parameters.baseAsset.referenceParent !== undefined) { - if (bot.SESSION.parameters.baseAsset.referenceParent.referenceParent !== undefined) { - - let config = bot.SESSION.parameters.baseAsset.referenceParent.referenceParent.config - - if (config.codeName !== undefined) { - bot.VALUES_TO_USE.baseAsset = config.codeName; - } - - config = bot.SESSION.parameters.baseAsset.config - - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { - if (config.initialBalance !== undefined) { - bot.VALUES_TO_USE.initialBalanceA = config.initialBalance; - bot.VALUES_TO_USE.initialBalanceB = 0 - } - if (config.minimumBalance !== undefined) { - bot.VALUES_TO_USE.minimumBalanceA = config.minimumBalance; - bot.VALUES_TO_USE.minimumBalanceB = 0 - } - if (config.maximumBalance !== undefined) { - bot.VALUES_TO_USE.maximumBalanceA = config.maximumBalance; - bot.VALUES_TO_USE.maximumBalanceB = 0 - } - } else { - if (config.initialBalance !== undefined) { - bot.VALUES_TO_USE.initialBalanceB = config.initialBalance; - bot.VALUES_TO_USE.initialBalanceA = 0 - } - if (config.minimumBalance !== undefined) { - bot.VALUES_TO_USE.minimumBalanceB = config.minimumBalance; - bot.VALUES_TO_USE.minimumBalanceA = 0 - } - if (config.maximumBalance !== undefined) { - bot.VALUES_TO_USE.maximumBalanceB = config.maximumBalance; - bot.VALUES_TO_USE.maximumBalanceA = 0 - } - } - - /* Solve when values are missing. */ - if (bot.VALUES_TO_USE.minimumBalanceA === undefined) { - bot.VALUES_TO_USE.minimumBalanceA = 0 - } - - if (bot.VALUES_TO_USE.minimumBalanceB === undefined) { - bot.VALUES_TO_USE.minimumBalanceB = 0 - } - - if (bot.VALUES_TO_USE.maximumBalanceA === undefined) { - bot.VALUES_TO_USE.maximumBalanceA = 10000000000000000 - } - - if (bot.VALUES_TO_USE.maximumBalanceB === undefined) { - bot.VALUES_TO_USE.maximumBalanceB = 10000000000000000 - } - } - } - } - } - - /* Quoted Asset. */ - { - if (bot.SESSION.parameters.quotedAsset !== undefined) { - if (bot.SESSION.parameters.quotedAsset.referenceParent !== undefined) { - if (bot.SESSION.parameters.quotedAsset.referenceParent.referenceParent !== undefined) { - - let config = bot.SESSION.parameters.quotedAsset.referenceParent.referenceParent.config - - if (config.codeName !== undefined) { - bot.VALUES_TO_USE.quotedAsset = config.codeName; - } - } - } - } - } - - /* Time Frame */ - if (bot.SESSION.parameters.timeFrame !== undefined) { - bot.VALUES_TO_USE.timeFrame = bot.SESSION.parameters.timeFrame.config.value - } - - /* Slippage */ - if (bot.SESSION.parameters.slippage !== undefined) { - if (bot.SESSION.parameters.slippage.config !== undefined) { - - let config = bot.SESSION.parameters.slippage.config - - if (config.positionRate !== undefined) { - bot.VALUES_TO_USE.slippage.positionRate = config.positionRate - } - if (config.stopLoss !== undefined) { - bot.VALUES_TO_USE.slippage.stopLoss = config.stopLoss - } - if (config.takeProfit !== undefined) { - bot.VALUES_TO_USE.slippage.takeProfit = config.takeProfit - } - } - } - - /* Fee Structure */ - if (bot.SESSION.parameters.feeStructure !== undefined) { - if (bot.SESSION.parameters.feeStructure.config !== undefined) { - - let config = bot.SESSION.parameters.feeStructure.config - - if (config.maker !== undefined) { - bot.VALUES_TO_USE.feeStructure.maker = config.maker - } - if (config.taker !== undefined) { - bot.VALUES_TO_USE.feeStructure.taker = config.taker - } - } - } - - /* Time Range */ - if (bot.SESSION.parameters.timeRange !== undefined) { - if (bot.SESSION.parameters.timeRange.config !== undefined) { - - let config = bot.SESSION.parameters.timeRange.config - if (config.initialDatetime !== undefined) { - if (isNaN(Date.parse(config.initialDatetime)) === true) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use initialDatatime provided at Session Parameters because it is not a valid Date."); - } else { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date(config.initialDatetime) - } - } - if (config.finalDatetime !== undefined) { - if (isNaN(Date.parse(config.finalDatetime)) === true) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use finalDatetime provided at Session Parameters because it is not a valid Date."); - } else { - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date(config.finalDatetime) - } - } - } - } - } - } + if (global.STOP_TASK_GRACEFULLY === true) { + bot.STOP_SESSION = true } } - function initializeSocialBots() { - - if (bot.SESSION.socialBots !== undefined) { - if (bot.SESSION.socialBots.bots !== undefined) { - for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { - let socialBot = bot.SESSION.socialBots.bots[i] - if (socialBot.type === "Telegram Bot") { - let config = socialBot.config - socialBot.botInstance = initializeTelegramBot(config.botToken, config.chatId) - } - } + function sessionError(node, errorMessage) { + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage } - - function announce(announcement) { - if (bot.SESSION.socialBots.bots !== undefined) { - for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { - let socialBot = bot.SESSION.socialBots.bots[i] - try { - let config = announcement.config - - if (socialBot.type === "Telegram Bot") { - if (announcement.formulaValue !== undefined) { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } else { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, config.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } - } - - } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> err = " + err.stack); - } - } - } - } - - bot.SESSION.socialBots.announce = announce - } - } - - function finalizeSocialBots() { - if (bot.SESSION.socialBots === undefined) { return } - if (bot.SESSION.socialBots.bots !== undefined) { - for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { - let socialBot = bot.SESSION.socialBots.bots[i] - if (socialBot.type === "Telegram Bot") { - let config = socialBot.config - socialBot.botInstance = finalizeTelegramBot(config.chatId) - } + } else { + event = { + errorMessage: errorMessage } } - } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Error', event) - function initializeTelegramBot(botToken, chatId) { - /* Telegram Bot Initialization */ - try { - const Telegraf = require('telegraf') - let telegramBot - telegramBot = new Telegraf(botToken) - telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.')) - telegramBot.help((ctx) => ctx.reply('I can stop the session if you say STOP.')) - telegramBot.hears('STOP', (ctx) => stopSession()) - telegramBot.launch() - - const Telegram = require('telegraf/telegram') - - telegramAPI = new Telegram(botToken) - - const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started with an initial balance of " + " " + bot.VALUES_TO_USE.initialBalanceA + " " + bot.VALUES_TO_USE.baseAsset + "." - telegramAPI.sendMessage(chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> Telegram API error -> err = " + err)) - - let botInstance = { - telegramBot: telegramBot, - telegramAPI: telegramAPI, - chatId: chatId - } - return botInstance - } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> err = " + err.stack); + if (global.STOP_TASK_GRACEFULLY === true) { + bot.STOP_SESSION = true } - - } - - function finalizeTelegramBot(chatId) { - const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was stopped." - telegramAPI.sendMessage(chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> Telegram API error -> err = " + err)) } } catch (err) { diff --git a/SocialBots.js b/SocialBots.js new file mode 100644 index 000000000..9ae190727 --- /dev/null +++ b/SocialBots.js @@ -0,0 +1,71 @@ +exports.newSocialBots = function newSocialBots(bot, logger) { + + const MODULE_NAME = 'Social Bots' + + let thisObject = { + sendMessage: sendMessage, + initialize: initialize, + finalize: finalize + } + + const TELEGRAM_BOT_MODULE = require('./TelegramBot.js') + + return thisObject + + function initialize() { + if (bot.SESSION.socialBots !== undefined) { + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + if (socialBot.type === "Telegram Bot") { + let config = socialBot.config + socialBot.botInstance = TELEGRAM_BOT_MODULE.newTelegramBot(bot, logger) + socialBot.botInstance.initialize(config.botToken, config.chatId) + } + } + } + + function announce(text) { + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + try { + if (socialBot.type === "Telegram Bot") { + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, text).catch(err => logger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) + } + } catch (err) { + logger.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); + } + } + } + } + + bot.SESSION.socialBots.announce = announce + } + } + + function finalize() { + if (bot.SESSION.socialBots === undefined) { return } + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + if (socialBot.type === "Telegram Bot") { + socialBot.botInstance.finalize() + } + } + } + } + + function sendMessage(message) { + if (bot.SESSION.socialBots !== undefined) { + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + if (socialBot.type === "Telegram Bot") { + socialBot.botInstance.sendMessage(message) + } + } + } + } + } +} \ No newline at end of file diff --git a/StatusDependencies.js b/StatusDependencies.js index 48b5659a9..21314c4c8 100644 --- a/StatusDependencies.js +++ b/StatusDependencies.js @@ -4,7 +4,7 @@ const LOG_FILE_CONTENT = false; const MODULE_NAME = "Status Dependencies"; - let bot = BOT + let bot = BOT let thisObject = { nodeArray: undefined, @@ -47,7 +47,7 @@ let statusDependency = { dependency: thisObject.nodeArray[i] } - + dependenciesToProcess.push(statusDependency) } @@ -124,7 +124,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> onLoad -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] initialize -> onLoad -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -137,14 +137,14 @@ loadCount++; logger.write(MODULE_NAME, "[INFO] initialize -> addReport -> Total Added = " + loadCount); - let key = statusDependency.dependency.dataMine + "-" + statusDependency.dependency.bot + "-" + statusDependency.dependency.process + "-" + statusDependency.dependency.dataSetVersion; + let key = statusDependency.dependency.dataMine + "-" + statusDependency.dependency.bot + "-" + statusDependency.dependency.process thisObject.keys.push(key); thisObject.statusReports.set(key, statusReportModule); if (statusReportModule.mainUtility !== undefined) { thisObject.reportsByMainUtility.set(statusReportModule.mainUtility, statusReportModule) - } + } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> addReport -> Report added to Map. -> key = " + key); } @@ -161,7 +161,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/StatusReport.js b/StatusReport.js index b9865452e..4da076767 100644 --- a/StatusReport.js +++ b/StatusReport.js @@ -48,47 +48,47 @@ /* Some very basic validations that we have all the information needed. */ if (statusDependencyNode.referenceParent === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Status Dependency without Reference Parent. Status Dependency = " + JSON.stringify(statusDependencyNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode, "Status Dependency without Reference Parent.") return } if (statusDependencyNode.referenceParent.parentNode === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Status Report not attached to a Process Definition. Status Report = " + JSON.stringify(statusDependencyNode.referenceParent)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent, "Status Report not attached to a Process Definition.") return } if (statusDependencyNode.referenceParent.parentNode.config.codeName === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Process Definition witn no codeName defined. Process Definition = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition witn no codeName defined.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Process Definition not attached to a Bot. Process Definition = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition not attached to a Bot.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Bot witn no codeName defined. Bot = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot with no codeName defined.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Bot not attached to a Data Mine. Bot = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot not attached to a Data Mine.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.config.codeName === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Data Mine witn no codeName defined. Data Mine = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode.parentNode.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode.parentNode, "Data Mine witn no codeName defined.") return } + function validationFailed(errorInNode, errorMessage) { + let nodeString = JSON.stringify(errorInNode) + logger.write(MODULE_NAME, "[ERROR] initialize -> " + errorMessage + ' -> nodeString = ' + nodeString) + bot.processError(errorInNode, errorMessage) + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + /* Simplifying the access to basic info */ statusDependencyNode.bot = statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName statusDependencyNode.process = statusDependencyNode.referenceParent.parentNode.config.codeName @@ -102,13 +102,6 @@ } } - /* This stuff is still hardcoded and unresolved. */ - statusDependencyNode.botVersion = { - "major": 1, - "minor": 0 - } - statusDependencyNode.dataSetVersion = "dataSet.V1" - if (bot.SESSION !== undefined && statusDependencyNode.bottype === "Trading Bot") { sessionPath = bot.SESSION.folderName + "/" } @@ -204,8 +197,8 @@ let fileName = "Status.Report.json"; let filePath; - let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.botVersion.major + "-" + statusDependencyNode.botVersion.minor + "-" + statusDependencyNode.process + "-" + statusDependencyNode.dataSetVersion; - let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.process + "-" + bot.dataSetVersion; + let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process + let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.process if (ownerId !== botId) { @@ -301,8 +294,8 @@ if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] save -> Entering function."); } - let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.botVersion.major + "-" + statusDependencyNode.botVersion.minor + "-" + statusDependencyNode.process + "-" + statusDependencyNode.dataSetVersion; - let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.process + "-" + bot.dataSetVersion; + let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process + let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.process if (ownerId !== botId && statusDependencyNode.process !== "Context") { // Context is a special case where the report is created by the Context.js module itself. @@ -341,7 +334,7 @@ let processOutput = PROCESS_OUTPUT.newProcessOutput(bot, logger) - processOutput.raiseEvents(thisObject.file.lastFile, callBackFunction); + processOutput.raiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames, callBackFunction); return; } diff --git a/TelegramBot.js b/TelegramBot.js new file mode 100644 index 000000000..9e2383ed1 --- /dev/null +++ b/TelegramBot.js @@ -0,0 +1,51 @@ +exports.newTelegramBot = function newTelegramBot(bot, logger) { + + const MODULE_NAME = 'Telegram Bot' + + let thisObject = { + telegramBot: undefined, + telegramAPI: undefined, + chatId: undefined, + sendMessage: sendMessage, + initialize: initialize, + finalize: finalize + } + + return thisObject + + function initialize(botToken, chatId) { + /* Telegram Bot Initialization */ + try { + thisObject.chatId = chatId + + const Telegraf = require('telegraf') + + thisObject.telegramBot = new Telegraf(botToken) + thisObject.telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.')) + thisObject.telegramBot.help((ctx) => ctx.reply('I can stop the session if you type STOP.')) + thisObject.telegramBot.hears('STOP', (ctx) => bot.SESSION.stop(' by STOP command at Telegram.')) + thisObject.telegramBot.launch() + + const Telegram = require('telegraf/telegram') + + thisObject.telegramAPI = new Telegram(botToken) + + const message = "Telegram bot is starting." + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeTelegramBot -> Telegram API error -> err = " + err)) + + } catch (err) { + parentLogger.write(MODULE_NAME, "[WARN] initialize -> err = " + err.stack); + } + + } + + function finalize() { + const message = "Telegram bot is signing off." + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] finalize -> Telegram API error -> err = " + err)) + } + + function sendMessage(message) { + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] announce -> Telegram API error -> err = " + err)) + } + +} \ No newline at end of file diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js deleted file mode 100644 index 37c791fc4..000000000 --- a/Trading/TradingBot.js +++ /dev/null @@ -1,998 +0,0 @@ -exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STORAGE) { - const FULL_LOG = true - const LOG_FILE_CONTENT = false - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 - - const MODULE_NAME = 'Trading Bot' - - let thisObject = { - initialize: initialize, - finalize: finalize, - start: start - } - - let utilities = UTILITIES.newCloudUtilities(logger) - let fileStorage = FILE_STORAGE.newFileStorage(logger) - - const COMMONS = require('../Commons.js') - let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) - - let exchangeAPI - - return thisObject - - function finalize() { - thisObject = undefined - utilities = undefined - fileStorage = undefined - commons = undefined - } - - function initialize(pExchangeAPI, callBackFunction) { - try { - logger.fileName = MODULE_NAME - logger.initialize() - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> Entering function.') } - - exchangeAPI = pExchangeAPI - callBackFunction(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] initialize -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, variable, callBackFunction) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Entering function.') } - - let chart = {} - let mainDependency = {} - - /* The first phase here is about checking that we have everything we need at the definition level. */ - let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies - if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } - - let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets - if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } - - /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ - for (let j = 0; j < global.marketFilesPeriods.length; j++) { - let timeFrameLabel = marketFilesPeriods[j][1] - let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) - let products = {} - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Inflating Data Files for timeFrame = ' + timeFrameLabel) } - - if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - let propertyName = 'at' + timeFrameLabel.replace('-', '') - chart[propertyName] = products - } - } - - for (let j = 0; j < global.dailyFilePeriods.length; j++) { - let timeFrameLabel = global.dailyFilePeriods[j][1] - let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) - let products = {} - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Inflating Data Files for timeFrame = ' + timeFrameLabel) } - - if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - let propertyName = 'at' + timeFrameLabel.replace('-', '') - chart[propertyName] = products - } - } - - /* Single Files */ - - let dataFiles = multiPeriodDataFiles.get('Single Files') - let products = {} - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Inflating Data Files from Single Files.') } - - if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - let propertyName = 'atAnyTimeFrame' - chart[propertyName] = products - } - - /* Simulation */ - - const TRADING_SIMULATION = require('./TradingSimulation.js') - let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) - - let market = bot.market - - const SIMULATED_RECORDS_FOLDER_NAME = 'Trading-Simulation' - const CONDITIONS_FOLDER_NAME = 'Simulation-Conditions' - const STRATEGIES_FOLDER_NAME = 'Simulation-Strategies' - const TRADES_FOLDER_NAME = 'Simulation-Trades' - const SNAPSHOTS_FOLDER_NAME = 'Snapshots' - - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 - - let recordsArray = [] - let conditionsArray = [] - let strategiesArray = [] - let positionsArray = [] - - let snapshotHeaders - let triggerOnSnapshot - let takePositionSnapshot - - let tradingSystem = {} - - if (bot.RESUME === true) { - readFiles() - } else { - runSimulation() - } - - function readFiles() { - - if (timeFrame > global.dailyFilePeriods[0][0]) { - readMarketFiles(runSimulation) - } else { - readDailyFiles(runSimulation) - } - } - - function readMarketFiles(callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> Entering function.') } - - readRecordsFile() - - function readRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readRecordsFile -> Entering function.') } - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readRecordsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - recordsArray = [] - readConditionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - recordsArray = JSON.parse(text) - readConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readConditionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readConditionsFile -> Entering function.') } - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + CONDITIONS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readConditionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - conditionsArray = [] - readStrategiesFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - conditionsArray = JSON.parse(text) - conditionsArray = conditionsArray[1] // conditionsArray[0] = a snapshot of the trading system. - readStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readStrategiesFile -> Entering function.') } - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - strategiesArray = [] - readPositionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - strategiesArray = JSON.parse(text) - readPositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readPositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readPositionsFile -> Entering function.') } - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readPositionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - positionsArray = [] - callBack() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - positionsArray = JSON.parse(text) - callBack() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } - - function readDailyFiles(callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> Entering function.') } - - readRecordsFile() - - function readRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readRecordsFile -> Entering function.') } - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readRecordsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - recordsArray = [] - readConditionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - recordsArray = JSON.parse(text) - readConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readConditionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readConditionsFile -> Entering function.') } - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readConditionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - conditionsArray = [] - readStrategiesFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - conditionsArray = JSON.parse(text) - conditionsArray = conditionsArray[1] // conditionsArray[0] = a snapshot of the trading system. - readStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readStrategiesFile -> Entering function.') } - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - strategiesArray = [] - readPositionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - strategiesArray = JSON.parse(text) - readPositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readPositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readPositionsFile -> Entering function.') } - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readPositionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - positionsArray = [] - callBack() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - positionsArray = JSON.parse(text) - callBack() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } - - function runSimulation() { - tradingSimulation.runSimulation( - chart, - dataDependencies, - timeFrame, - timeFrameLabel, - currentDay, - variable, - exchangeAPI, - recordsArray, - conditionsArray, - strategiesArray, - positionsArray, - writeFiles, - callBackFunction) - } - - function writeFiles(pTradingSystem, pSnapshotHeaders, pTriggerOnSnapshot, pTakePositionSnapshot) { - tradingSystem = pTradingSystem - - snapshotHeaders = pSnapshotHeaders - triggerOnSnapshot = pTriggerOnSnapshot - takePositionSnapshot = pTakePositionSnapshot - - if (timeFrame > global.dailyFilePeriods[0][0]) { - writeMarketFiles() - } else { - writeDailyFiles() - } - } - - function writeMarketFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> Entering function.') } - - writeRecordsFile() - - function writeRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeRecordsFile -> Entering function.') } - - let fileContent = JSON.stringify(recordsArray) - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeConditionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeConditionsFile -> Entering function.') } - - let fileContent = JSON.stringify(conditionsArray) - - fileContent = '[' + JSON.stringify(tradingSystem) + ',' + fileContent + ']' - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + CONDITIONS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeStrategiesFile -> Entering function.') } - - let fileContent = JSON.stringify(strategiesArray) - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writePositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writePositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writePositionsFile -> Entering function.') } - - let fileContent = JSON.stringify(positionsArray) - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeSnapshotFiles() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeSnapshotFiles() { - writeSnapshotFile(triggerOnSnapshot, 'Trigger-On', onFinish) - - function onFinish() { - writeSnapshotFile(takePositionSnapshot, 'Take-Position', callBackFunction) - } - } - - function writeSnapshotFile(snapshotArray, pFileName, callBack) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeSnapshotFile -> Entering function.') } - - let fileRecordCounter = 0 - - let fileContent = '' - let separator = '\r\n' - - parseRecord(snapshotHeaders) - - for (let i = 0; i < snapshotArray.length; i++) { - let record = snapshotArray[i] - parseRecord(record) - fileRecordCounter++ - } - - function parseRecord(record) { - for (let j = 0; j < record.length; j++) { - let property = record[j] - - fileContent = fileContent + '' + property - if (j !== record.length - 1) { - fileContent = fileContent + ',' - } - } - fileContent = fileContent + separator - } - - fileContent = '' + fileContent + '' - - let fileName = pFileName + '.csv' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SNAPSHOTS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - callBack(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } - - function writeDailyFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> Entering function.') } - - writeRecordsFile() - - function writeRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeRecordsFile -> Entering function.') } - - let fileContent = JSON.stringify(recordsArray) - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeConditionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeConditionsFile -> Entering function.') } - - let fileContent = JSON.stringify(conditionsArray) - - fileContent = '[' + JSON.stringify(tradingSystem) + ',' + fileContent + ']' - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + CONDITIONS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeStrategiesFile -> Entering function.') } - - let fileContent = JSON.stringify(strategiesArray) - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writePositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writePositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writePositionsFile -> Entering function.') } - - let fileContent = JSON.stringify(positionsArray) - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeSnapshotFiles() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeSnapshotFiles() { - writeSnapshotFile(triggerOnSnapshot, 'Trigger-On', onFinish) - - function onFinish() { - writeSnapshotFile(takePositionSnapshot, 'Take-Position', callBackFunction) - } - } - - function writeSnapshotFile(snapshotArray, pFileName, callBack) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeSnapshotFile -> Entering function.') } - - let fileRecordCounter = 0 - - let fileContent = '' - let separator = '\r\n' - - parseRecord(snapshotHeaders) - - for (let i = 0; i < snapshotArray.length; i++) { - let record = snapshotArray[i] - parseRecord(record) - fileRecordCounter++ - } - - function parseRecord(record) { - for (let j = 0; j < record.length; j++) { - let property = record[j] - - fileContent = fileContent + '' + property - if (j !== record.length - 1) { - fileContent = fileContent + ',' - } - } - fileContent = fileContent + separator - } - - fileContent = '' + fileContent + '' - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = pFileName + '.csv' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SNAPSHOTS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - callBack(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } -} - diff --git a/Trading/TradingEngine.js b/Trading/TradingEngine.js deleted file mode 100644 index 30207da99..000000000 --- a/Trading/TradingEngine.js +++ /dev/null @@ -1,69 +0,0 @@ -exports.newTradingEngine = function newTradingEngine(bot, logger) { - - const MODULE_NAME = 'Trading Engine' - - let thisObject = { - initialize: initialize, - finalize: finalize - } - - return thisObject - - function initialize() { - /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed */ - - if (bot.TRADING_ENGINE.isInitialized !== true || bot.RESUME === false) { - bot.TRADING_ENGINE.isInitialized = true - initializeNode(bot.TRADING_ENGINE) - } - } - - function finalize() { - - } - - function initializeNode(node) { - if (node === undefined) { return } - - /* Here we initialize the node value */ - if (node.config !== undefined) { - if (node.config.initialValue !== undefined) { - node.value = node.config.initialValue - } - } - - /* Now we go down through all this node children */ - let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) - if (nodeDefinition === undefined) { return } - - if (nodeDefinition.properties !== undefined) { - let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. - for (let i = 0; i < nodeDefinition.properties.length; i++) { - let property = nodeDefinition.properties[i] - - switch (property.type) { - case 'node': { - if (property.name !== previousPropertyName) { - if (node[property.name] !== undefined) { - initializeNode(node[property.name]) - } - previousPropertyName = property.name - } - break - } - case 'array': { - if (node[property.name] !== undefined) { - let nodePropertyArray = node[property.name] - object[property.name] = [] - for (let m = 0; m < nodePropertyArray.length; m++) { - initializeNode(nodePropertyArray[m]) - } - } - break - } - } - } - } - } -} - diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js deleted file mode 100644 index 0f78d4c25..000000000 --- a/Trading/TradingSimulation.js +++ /dev/null @@ -1,2546 +0,0 @@ -exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILITIES) { - const FULL_LOG = true - const LOG_FILE_CONTENT = false - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 - const MODULE_NAME = 'Trading Simulation -> ' + bot.SESSION.name - - const GMT_SECONDS = ':00.000 GMT+0000' - - let thisObject = { - finalize: finalize, - runSimulation: runSimulation - } - - let utilities = UTILITIES.newCloudUtilities(bot, logger) - - return thisObject - - function finalize() { - thisObject = undefined - } - - function runSimulation( - chart, - dataDependencies, - timeFrame, - timeFrameLabel, - currentDay, - variable, - exchangeAPI, - recordsArray, - conditionsArray, - strategiesArray, - positionsArray, - callback, - callBackFunction) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> Entering function.') } - let processingDailyFiles - if (timeFrame > global.dailyFilePeriods[0][0]) { - processingDailyFiles = false - } else { - processingDailyFiles = true - } - - /* Snapshots of Trigger On and Take Positions */ - let snapshots = { - headers: undefined, - triggerOn: [], - takePosition: [], - lastTriggerOn: undefined, - lastTakePosition: undefined - } - - let tradingSystem = bot.TRADING_SYSTEM - let tradingEngine = bot.TRADING_ENGINE - let sessionParameters = bot.SESSION.parameters - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } - - let timerToCloseStage = 0 - - /* Stop Loss Management */ - - const MIN_STOP_LOSS_VALUE = 0.0000000001 // We can not let the stop be zero to avoid division by 0 error or infinity numbers as a result. - const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER - - /* Take Profit Management */ - - const MIN_TAKE_PROFIT_VALUE = 0.0000000001 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. - const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER - - /* Variables for this executioin only */ - let takePositionNow = false - let closePositionNow = false - - /* In some cases we need to know if we are positioned at the last candle of the calendar day, for that we need these variables. */ - let lastInstantOfTheDay = 0 - if (currentDay) { - lastInstantOfTheDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1 - } - - const TRADING_ENGINE_MODULE = require('./TradingEngine.js') - let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) - tradingEngineModule.initialize() - - if (variable.isInitialized !== true || bot.RESUME === false) { - variable.isInitialized = true - - variable.episode = {} // An Episode represents each execution of the Simulation - variable.episode.count = {} // Everything in here means the total count since the begining of the episode. - variable.episode.stat = {} // Everything in here means the statistics calculated since the begining of the episode. - - variable.current = {} // Everything in here means that is happening right now. - variable.current.balance = {} // Balances at the current candle of the Simulation. - variable.current.distance = {} // Everything in here means the distance, measured in periods to whatever happened in the past, usually events. - variable.current.distance.toEvent = {} // Distance to Events that already happened. - variable.current.strategy = {} // Everything in here are variables related to the current strategy being executed. - variable.current.position = {} // Everything in here are variables related to the current position being executed. - - variable.current.balance.baseAsset = bot.VALUES_TO_USE.initialBalanceA - variable.current.balance.quotedAsset = bot.VALUES_TO_USE.initialBalanceB - variable.current.distance.toEvent.triggerOn = 0 - variable.current.distance.toEvent.triggerOff = 0 - variable.current.distance.toEvent.takePosition = 0 - variable.current.distance.toEvent.closePosition = 0 - - variable.last = {} // Everything in here means that it already happened. - variable.last.position = {} // Las values of variables at the last position. - - variable.last.position.profitLoss = 0 - variable.last.position.ROI = 0 - - variable.previous = {} // Everything in here means the the value that was current before a new current value was set. - variable.previous.balance = {} // Balances values previous to the current ones. - - variable.previous.balance.baseAsset = 0 - variable.previous.balance.quotedAsset = 0 - - variable.episode.parameters = {} // Parameters give you access to information received by the Simulation as parameters. - variable.episode.parameters.initial = {} // Everything in here means the status at the begining of the episode received as a parameter of the Sumulation. - variable.episode.parameters.minimum = {} // Everything in here means the minimum in the whole episode received as a parameter of the Sumulation. - variable.episode.parameters.maximum = {} // Everything in here means the maximum in the whole episode received as a parameter of the Sumulation. - variable.episode.parameters.initial.balance = {} - variable.episode.parameters.minimum.balance = {} - variable.episode.parameters.maximum.balance = {} - - variable.episode.parameters.baseAsset = bot.VALUES_TO_USE.baseAsset - variable.episode.parameters.quotedAsset = bot.VALUES_TO_USE.quotedAsset - variable.episode.parameters.marketBaseAsset = bot.market.baseAsset - variable.episode.parameters.marketQuotedAsset = bot.market.quotedAsset - variable.episode.parameters.timeFrame = timeFrame // The numeric value of the time frame, in milliseconds - variable.episode.parameters.timeFrameLabel = timeFrameLabel // The alpha-numeric value of the time frame - - variable.episode.parameters.initial.balance.baseAsset = bot.VALUES_TO_USE.initialBalanceA - variable.episode.parameters.minimum.balance.baseAsset = bot.VALUES_TO_USE.minimumBalanceA - variable.episode.parameters.maximum.balance.baseAsset = bot.VALUES_TO_USE.maximumBalanceA - - variable.episode.parameters.initial.balance.quotedAsset = bot.VALUES_TO_USE.initialBalanceB - variable.episode.parameters.minimum.balance.quotedAsset = bot.VALUES_TO_USE.minimumBalanceB - variable.episode.parameters.maximum.balance.quotedAsset = bot.VALUES_TO_USE.maximumBalanceB - - variable.episode.count.positions = 0 - variable.episode.count.fails = 0 - variable.episode.count.hits = 0 - variable.episode.count.periods = 0 - - variable.episode.stat.profitLoss = 0 - variable.episode.stat.hitRatio = 0 - variable.episode.stat.days = 0 - variable.episode.stat.ROI = 0 - variable.episode.stat.anualizedRateOfReturn = 0 - - inializeCurrentStrategy() - initializeCurrentPosition() - - variable.announcements = [] - } - - function inializeCurrentStrategy() { - - variable.current.strategy.name = '' - variable.current.strategy.index = -1 - variable.current.strategy.stage = 'No Stage' - variable.current.strategy.begin = 0 - variable.current.strategy.end = 0 - variable.current.strategy.status = 0 - variable.current.strategy.number = 0 - variable.current.strategy.beginRate = 0 - variable.current.strategy.endRate = 0 - variable.current.strategy.situationName = '' - - } - - function initializeCurrentPosition() { - - variable.current.position.begin = 0 - variable.current.position.end = 0 - variable.current.position.rate = 0 - variable.current.position.size = 0 - variable.current.position.status = 0 - variable.current.position.exitType = 0 - variable.current.position.beginRate = 0 - variable.current.position.endRate = 0 - variable.current.position.situationName = '' - - variable.current.position.stat = {} - variable.current.position.stat.ROI = 0 - variable.current.position.stat.days = 0 - - variable.current.position.count = {} - variable.current.position.count.periods = 0 - - variable.current.position.stopLoss = {} - variable.current.position.stopLoss.value = 0 - variable.current.position.stopLoss.phase = -1 - variable.current.position.stopLoss.stage = 'No Stage' - - variable.current.position.takeProfit = {} - variable.current.position.takeProfit.value = 0 - variable.current.position.takeProfit.phase = -1 - variable.current.position.takeProfit.stage = 'No Stage' - - } - - /* Main Array and Maps */ - let propertyName = 'at' + variable.episode.parameters.timeFrameLabel.replace('-', '') - let candles = chart[propertyName].candles - let currentChart = chart[propertyName] - - /* Last Candle */ - let lastCandle = candles[candles.length - 1] - - /* Main Simulation Loop: We go thourgh all the candles at this time period. */ - let currentCandleIndex - - /* For Loop Level heartbeat */ - let loopingDay - let previousLoopingDay - - initializeLoop() - - function initializeLoop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initializeLoop -> Entering function.') } - - if (bot.RESUME === true && variable.last.candle !== undefined) { - /* Estimate the Initial Candle based on the last candle saved at variable */ - let firstBegin = candles[0].begin - let lastBegin = variable.last.candle.begin - let diff = lastBegin - firstBegin - let amount = diff / variable.episode.parameters.timeFrame - - currentCandleIndex = Math.trunc(amount) + 1 // Because we need to start from the next candle - if (currentCandleIndex < 0 || currentCandleIndex > candles.length - 1) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> Cannot resume simulation.') - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> currentCandleIndex = ' + currentCandleIndex) - 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.valueOf() - let diff = targetEnd - firstEnd - let amount = diff / variable.episode.parameters.timeFrame - - currentCandleIndex = Math.trunc(amount) - if (currentCandleIndex < 0) { currentCandleIndex = 0 } - if (currentCandleIndex > 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. - */ - currentCandleIndex = candles.length - 1 - } - } - - loop() - } - - function loop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering function.') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + currentCandleIndex) } - - /* We are going to take snapshots of the values of indicators at key moments of the sumulation. */ - - let snapshotKeys = new Map() - snapshotLoopHeaders = [] - snapshotDataRecord = [] - saveAsLastTriggerOnSnapshot = false - saveAsLastTakePositionSnapshot = false - addToSnapshots = false - - let announcementsToBeMade = [] - let candle = candles[currentCandleIndex] - - /* Not processing while out of user-defined time range */ - if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } - controlLoop() - return - } - if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle after the sessionParameters.timeRange.config.finalDatetime.') } - afterLoop() - return - } - - /* Here we update at the chart data structure the objects for each product representing where we are currently standing at the simulation loop */ - if (processingDailyFiles) { - for (let j = 0; j < global.dailyFilePeriods.length; j++) { - let mapKey = dailyFilePeriods[j][1] - let propertyName = 'at' + mapKey.replace('-', '') - let thisChart = chart[propertyName] - - for (let k = 0; k < dataDependencies.length; k++) { - let dataDependencyNode = dataDependencies[k] - if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Daily') { continue } - let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName - let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let currentElement = getElement(thisChart[pluralVariableName], candle, 'Daily' + '-' + mapKey + '-' + pluralVariableName) - thisChart[singularVariableName] = currentElement - } - } - } - - /* Finding the Current Element on Market Files */ - for (let j = 0; j < global.marketFilesPeriods.length; j++) { - let mapKey = marketFilesPeriods[j][1] - let propertyName = 'at' + mapKey.replace('-', '') - let thisChart = chart[propertyName] - - for (let k = 0; k < dataDependencies.length; k++) { - let dataDependencyNode = dataDependencies[k] - if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Market') { continue } - let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName - let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let currentElement = getElement(thisChart[pluralVariableName], candle, 'Market' + '-' + mapKey + '-' + pluralVariableName) - thisChart[singularVariableName] = currentElement - } - } - - /* Finding the Current Element on Single Files */ - function isItInside(elementWithTimestamp, elementWithBeginEnd) { - if (elementWithTimestamp.timestamp >= elementWithBeginEnd.begin && elementWithTimestamp.timestamp <= elementWithBeginEnd.end) { - return true - } else { - return false - } - } - - let propertyName = 'atAnyTimeFrame' - let thisChart = chart[propertyName] - - for (let k = 0; k < dataDependencies.length; k++) { - let dataDependencyNode = dataDependencies[k] - if (dataDependencyNode.referenceParent.config.codeName !== 'Single-File') { continue } - let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName - let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let elementArray = thisChart[pluralVariableName] - let currentElement - if (elementArray !== undefined) { - currentElement = elementArray[elementArray.length - 1] - } - thisChart[singularVariableName] = currentElement - } - - /* While we are processing the previous day. */ - let positionedAtYesterday = false - if (currentDay) { - positionedAtYesterday = (candle.end < currentDay.valueOf()) - } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle Begin @ ' + (new Date(candle.begin)).toLocaleString()) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle End @ ' + (new Date(candle.end)).toLocaleString()) } - - let ticker = { - bid: candle.close, - ask: candle.close, - last: candle.close - } - - /* We will produce a simulation level heartbeat in order to inform the user this is running. */ - loopingDay = new Date(Math.trunc(candle.begin / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - if (loopingDay.valueOf() !== previousLoopingDay) { - let processingDate = loopingDay.getUTCFullYear() + '-' + utilities.pad(loopingDay.getUTCMonth() + 1, 2) + '-' + utilities.pad(loopingDay.getUTCDate(), 2) - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + currentCandleIndex + ' @ ' + processingDate) } - - /* Telling the world we are alive and doing well */ - let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime.valueOf()) - let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime.valueOf()) - - let currentDateString = loopingDay.getUTCFullYear() + '-' + utilities.pad(loopingDay.getUTCMonth() + 1, 2) + '-' + utilities.pad(loopingDay.getUTCDate(), 2) - let currentDate = new Date(loopingDay) - let percentage = global.getPercentage(fromDate, currentDate, lastDate) - bot.processHeartBeat(currentDateString, percentage) - - if (global.areEqualDates(currentDate, new Date()) === false) { - logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) - } - } - previousLoopingDay = loopingDay.valueOf() - - variable.episode.count.periods++ - variable.episode.stat.days = variable.episode.count.periods * variable.episode.parameters.timeFrame / ONE_DAY_IN_MILISECONDS - - if (processingDailyFiles) { - /* We skip the candle at the head of the market because currentCandleIndex has not closed yet. */ - let candlesPerDay = ONE_DAY_IN_MILISECONDS / variable.episode.parameters.timeFrame - if (currentCandleIndex === candles.length - 1) { - if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { - /* We are at the head of the market, thus we skip the last candle because it has not close yet. */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle because it is the last one and has not been closed yet.') } - controlLoop() - return - /* Note here that in the last candle of the first day or the second day it will use an incomplete candle and partially calculated indicators. - if we skip these two variable.episode.count.periods, then there will be a hole in the file since the last period will be missing. */ - } - } - } else { // We are processing Market Files - if (currentCandleIndex === candles.length - 1) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle because it is the last one and has not been closed yet.') } - controlLoop() - return - } - } - - let conditions = new Map() // Here we store the conditions values that will be use in the simulator for decision making. - let formulas = new Map() - let conditionsValues = [] // Here we store the conditions values that will be written on file for the plotter. - let formulasErrors = [] // Here we store the errors produced by all phase formulas. - let formulasValues = [] // Here we store the values produced by all phase formulas. - - /* We define and evaluate all conditions to be used later during the simulation loop. */ - for (let j = 0; j < tradingSystem.strategies.length; j++) { - let strategy = tradingSystem.strategies[j] - - let positionSize = 0 - let positionRate = 0 - - /* Continue with the rest of the formulas and conditions */ - - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.triggerOn !== undefined) { - for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { - let situation = triggerStage.triggerOn.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - if (triggerStage.triggerOff !== undefined) { - for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { - let situation = triggerStage.triggerOff.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - if (triggerStage.takePosition !== undefined) { - for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { - let situation = triggerStage.takePosition.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - - let openStage = strategy.openStage - - if (openStage !== undefined) { - /* Default Values */ - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - positionSize = variable.current.balance.baseAsset - positionRate = candle.close - } else { - positionSize = variable.current.balance.quotedAsset - positionRate = candle.close - } - - let initialDefinition = openStage.initialDefinition - - if (initialDefinition !== undefined) { - if (variable.current.position.size !== 0) { - positionSize = variable.current.position.size - } else { - if (initialDefinition.positionSize !== undefined) { - if (initialDefinition.positionSize.formula !== undefined) { - try { - let code = initialDefinition.positionSize.formula.code - positionSize = eval(code) - addCodeToSnapshot(code) - } catch (err) { - initialDefinition.positionSize.formula.error = err.message - } - if (isNaN(positionSize)) { - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - positionSize = variable.current.balance.baseAsset - } else { - positionSize = variable.current.balance.quotedAsset - } - } else { - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - if (positionSize > variable.current.balance.baseAsset) { positionSize = variable.current.balance.baseAsset } - } else { - if (positionSize > variable.current.balance.quotedAsset) { positionSize = variable.current.balance.quotedAsset } - } - } - } - } - } - - if (variable.current.position.rate !== 0) { - positionRate = variable.current.position.rate - } else { - if (initialDefinition.positionRate !== undefined) { - if (initialDefinition.positionRate.formula !== undefined) { - try { - let code = initialDefinition.positionRate.formula.code - positionRate = eval(initialDefinition.positionRate.formula.code) - addCodeToSnapshot(code) - } catch (err) { - initialDefinition.positionRate.formula.error = err.message - } - if (isNaN(positionRate)) { - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - positionRate = candle.close - } else { - positionRate = candle.close - } - } - } - } - } - - if (initialDefinition.stopLoss !== undefined) { - for (let p = 0; p < initialDefinition.stopLoss.phases.length; p++) { - let phase = initialDefinition.stopLoss.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaError = 'Formula evaluates to Infinity.' - formulaValue = MAX_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Open Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.stopLoss -> MAX_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_STOP_LOSS_VALUE) { - formulaValue = MIN_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Open Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_STOP_LOSS_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.stopLoss -> MIN_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - - if (initialDefinition.takeProfit !== undefined) { - for (let p = 0; p < initialDefinition.takeProfit.phases.length; p++) { - let phase = initialDefinition.takeProfit.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaValue = MAX_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Open Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.takeProfit -> MAX_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_TAKE_PROFIT_VALUE) { - formulaValue = MIN_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Open Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_TAKE_PROFIT_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.takeProfit -> MIN_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - } - - strategy.positionSize = positionSize - strategy.positionRate = positionRate - } - - let manageStage = strategy.manageStage - - if (manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - for (let p = 0; p < manageStage.stopLoss.phases.length; p++) { - let phase = manageStage.stopLoss.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaError = '' - formulaValue = MAX_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.stopLoss -> MAX_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_STOP_LOSS_VALUE) { - formulaValue = MIN_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_STOP_LOSS_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.stopLoss -> MIN_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - - if (manageStage.takeProfit !== undefined) { - for (let p = 0; p < manageStage.takeProfit.phases.length; p++) { - let phase = manageStage.takeProfit.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaError = 'Formula evaluates to Infinity.' - formulaValue = MAX_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.takeProfit -> MAX_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_TAKE_PROFIT_VALUE) { - formulaValue = MIN_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_TAKE_PROFIT_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.takeProfit -> MIN_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - } - - function newCondition(key, node, chart) { - let condition - let value - - try { - let code = node.code - value = eval(code) - addCodeToSnapshot(code) - } catch (err) { - /* - One possible error is that the conditions references a .previous that is undefined. For this - reason and others, we will simply set the value to false. - */ - value = false - - if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || - node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 - ) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - node.error = err.message + ' @ ' + (new Date(candle.begin)).toLocaleString() - } - } - - condition = { - key: key, - value: value - } - - conditions.set(condition.key, condition) - - if (condition.value) { - conditionsValues.push(1) - } else { - conditionsValues.push(0) - } - } - } - - /* Trigger On Conditions */ - if ( - variable.current.strategy.stage === 'No Stage' && - variable.current.strategy.index === -1 - ) { - let minimumBalance - let maximumBalance - let balance - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - balance = variable.current.balance.baseAsset - minimumBalance = variable.episode.parameters.minimum.balance.baseAsset - maximumBalance = variable.episode.parameters.maximum.balance.baseAsset - } else { - balance = variable.current.balance.quotedAsset - minimumBalance = variable.episode.parameters.minimum.balance.quotedAsset - maximumBalance = variable.episode.parameters.maximum.balance.quotedAsset - } - - let stopRunningDate = new Date(candle.begin) - if (balance <= minimumBalance) { - tradingSystem.error = 'Min Balance @ ' + stopRunningDate.toLocaleString() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> ' + tradingSystem.error) } - afterLoop() - return - } - - if (balance >= maximumBalance) { - tradingSystem.error = 'Max Balance @ ' + stopRunningDate.toLocaleString() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> ' + tradingSystem.error) } - afterLoop() - return - } - - /* - Here we need to pick a strategy, or if there is not suitable strategy for the current - market conditions, we pass until the next period. - - To pick a new strategy we will evaluate what we call the trigger on. Once we enter - into one strategy, we will ignore market conditions for others. However there is also - a strategy trigger off which can be hit before taking a position. If hit, we would - be outside a strategy again and looking for the condition to enter all over again. - - */ - - for (let j = 0; j < tradingSystem.strategies.length; j++) { - if ( - variable.current.strategy.stage !== 'No Stage' || - variable.current.strategy.index !== -1 - ) { continue } - - let strategy = tradingSystem.strategies[j] - - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.triggerOn !== undefined) { - for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { - let situation = triggerStage.triggerOn.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - - variable.current.strategy.stage = 'Trigger Stage' - checkAnnouncements(triggerStage) - - variable.current.strategy.index = j - variable.current.strategy.begin = candle.begin - variable.current.strategy.beginRate = candle.min - variable.current.strategy.endRate = candle.min // In case the strategy does not get exited - variable.current.strategy.situationName = situation.name - variable.current.strategy.name = strategy.name - - variable.current.distance.toEvent.triggerOn = 1 - - checkAnnouncements(triggerStage.triggerOn) - saveAsLastTriggerOnSnapshot = true - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Switching to Trigger Stage because conditions at Trigger On Event were met.') } - break - } - } - } - } - } - } - - /* Trigger Off Condition */ - if (variable.current.strategy.stage === 'Trigger Stage') { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.triggerOff !== undefined) { - for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { - let situation = triggerStage.triggerOff.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = variable.current.strategy.index + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - variable.current.strategy.number = variable.current.strategy.index - variable.current.strategy.end = candle.end - variable.current.strategy.endRate = candle.min - variable.current.strategy.status = 1 // This means the strategy is closed, i.e. that has a begin and end. - variable.current.strategy.stage = 'No Stage' - variable.current.strategy.index = -1 - - variable.current.distance.toEvent.triggerOff = 1 - - checkAnnouncements(triggerStage.triggerOff) - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Switching to No Stage because conditions at the Trigger Off Event were met.') } - break - } - } - } - } - } - - /* Take Position Condition */ - if (variable.current.strategy.stage === 'Trigger Stage') { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.takePosition !== undefined) { - for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { - let situation = triggerStage.takePosition.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = variable.current.strategy.index + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - variable.current.strategy.stage = 'Open Stage' - checkAnnouncements(strategy.openStage) - - variable.current.position.stopLoss.stage = 'Open Stage' - variable.current.position.takeProfit.stage = 'Open Stage' - variable.current.position.stopLoss.phase = 0 - variable.current.position.takeProfit.phase = 0 - - takePositionNow = true - variable.current.position.situationName = situation.name - - checkAnnouncements(triggerStage.takePosition) - saveAsLastTakePositionSnapshot = true - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Conditions at the Take Position Event were met.') } - break - } - } - } - } - } - - /* Stop Loss Management */ - if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') && - takePositionNow !== true - ) { - checkStopPhases() - calculateStopLoss() - } - - function checkStopPhases() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let parentNode - let j = variable.current.strategy.index - let stageKey - let initialDefinitionKey = '' - let p - - if (variable.current.position.stopLoss.stage === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - parentNode = openStage.initialDefinition - initialDefinitionKey = '-' + 'initialDefinition' - stageKey = 'openStage' - p = variable.current.position.stopLoss.phase - } - } - } - - if (variable.current.position.stopLoss.stage === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - parentNode = manageStage - stageKey = 'manageStage' - p = variable.current.position.stopLoss.phase - 1 - } - } - - if (parentNode !== undefined) { - let phase = parentNode.stopLoss.phases[p] - - /* Check the next Phase Event. */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let situation = nextPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - variable.current.position.stopLoss.phase++ - variable.current.position.stopLoss.stage = 'Manage Stage' - if (variable.current.position.takeProfit.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' - checkAnnouncements(manageStage, 'Take Profit') - } - - checkAnnouncements(nextPhaseEvent) - return - } - } - } - - /* Check the Move to Phase Events. */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - let situation = moveToPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + n + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - - let moveToPhase = moveToPhaseEvent.referenceParent - if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { - if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { - variable.current.position.stopLoss.phase = q + 1 - } - } - } else { - moveToPhaseEvent.error = 'This Node needs to reference a Phase.' - continue - } - - variable.current.position.stopLoss.stage = 'Manage Stage' - if (variable.current.position.takeProfit.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' - checkAnnouncements(manageStage, 'Take Profit') - } - - checkAnnouncements(moveToPhaseEvent) - return - } - } - } - } - } - } - - function calculateStopLoss() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - let key - - if (variable.current.position.stopLoss.stage === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - phase = openStage.initialDefinition.stopLoss.phases[variable.current.position.stopLoss.phase] - key = variable.current.strategy.index + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase) - } - } - } - - if (variable.current.position.stopLoss.stage === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - phase = manageStage.stopLoss.phases[variable.current.position.stopLoss.phase - 1] - key = variable.current.strategy.index + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase - 1) - } - } - - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = variable.current.position.stopLoss.value - - variable.current.position.stopLoss.value = formulas.get(key) - - if (variable.current.position.stopLoss.value !== previousValue) { - checkAnnouncements(phase, variable.current.position.stopLoss.value) - } - } - } - } - - /* Take Profit Management */ - if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') && - takePositionNow !== true - ) { - checkTakeProfitPhases() - calculateTakeProfit() - } - - function checkTakeProfitPhases() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let parentNode - let j = variable.current.strategy.index - let stageKey - let initialDefinitionKey = '' - let p - - if (variable.current.position.takeProfit.stage === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - parentNode = openStage.initialDefinition - initialDefinitionKey = '-' + 'initialDefinition' - stageKey = 'openStage' - p = variable.current.position.takeProfit.phase - } - } - } - - if (variable.current.position.takeProfit.stage === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - parentNode = manageStage - stageKey = 'manageStage' - p = variable.current.position.takeProfit.phase - 1 - } - } - - if (parentNode !== undefined) { - let phase = parentNode.takeProfit.phases[p] - if (phase === undefined) { return } // trying to jump to a phase that does not exists. - - /* Check the next Phase Event. */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let situation = nextPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - variable.current.position.takeProfit.phase++ - variable.current.position.takeProfit.stage = 'Manage Stage' - if (variable.current.position.stopLoss.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' - checkAnnouncements(manageStage, 'Stop') - } - - checkAnnouncements(nextPhaseEvent) - return - } - } - } - - /* Check the Move to Phase Events. */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - let situation = moveToPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + n + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - - let moveToPhase = moveToPhaseEvent.referenceParent - if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { - if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { - variable.current.position.takeProfit.phase = q + 1 - } - } - } else { - moveToPhaseEvent.error = 'This Node needs to reference a Phase.' - continue - } - - variable.current.position.takeProfit.stage = 'Manage Stage' - if (variable.current.position.stopLoss.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' - checkAnnouncements(manageStage, 'Stop') - } - - checkAnnouncements(moveToPhaseEvent) - return - } - } - } - } - } - } - - function calculateTakeProfit() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - let key - - if (variable.current.position.takeProfit.stage === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - phase = openStage.initialDefinition.takeProfit.phases[variable.current.position.takeProfit.phase] - key = variable.current.strategy.index + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase) - } - } - } - - if (variable.current.position.takeProfit.stage === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - phase = manageStage.takeProfit.phases[variable.current.position.takeProfit.phase - 1] - key = variable.current.strategy.index + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase - 1) - } - } - - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = variable.current.position.stopLoss.value - - variable.current.position.takeProfit.value = formulas.get(key) - - if (variable.current.position.takeProfit.value !== previousValue) { - checkAnnouncements(phase, variable.current.position.takeProfit.value) - } - } - } - } - - /* Keeping Position Counters Up-to-date */ - if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') - ) { - if (takePositionNow === true) { - variable.current.position.count.periods = 0 - } - - variable.current.position.count.periods++ - variable.current.position.stat.days = variable.current.position.count.periods * variable.episode.parameters.timeFrame / ONE_DAY_IN_MILISECONDS - } else { - variable.current.position.count.periods = 0 - variable.current.position.stat.days = 0 - } - - /* Keeping Distance Counters Up-to-date */ - if ( - variable.current.distance.toEvent.triggerOn > 0 // with this we avoind counting before the first event happens. - ) { - variable.current.distance.toEvent.triggerOn++ - } - - if ( - variable.current.distance.toEvent.triggerOff > 0 // with this we avoind counting before the first event happens. - ) { - variable.current.distance.toEvent.triggerOff++ - } - - if ( - variable.current.distance.toEvent.takePosition > 0 // with this we avoind counting before the first event happens. - ) { - variable.current.distance.toEvent.takePosition++ - } - - if ( - variable.current.distance.toEvent.closePosition > 0 // with this we avoind counting before the first event happens. - ) { - variable.current.distance.toEvent.closePosition++ - } - - /* Checking if Stop or Take Profit were hit */ - if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') && - takePositionNow !== true - ) { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - /* Checking what happened since the last execution. We need to know if the Stop Loss - or our Take Profit were hit. */ - - /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - - if ((variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset && candle.max >= variable.current.position.stopLoss.value) || (variable.episode.parameters.baseAsset !== variable.episode.parameters.marketBaseAsset && candle.min <= variable.current.position.stopLoss.value)) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } - /* - Hit Point Validation - - This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - if (variable.current.position.stopLoss.value < candle.min) { - variable.current.position.stopLoss.value = candle.min - } - } else { - if (variable.current.position.stopLoss.value > candle.max) { - variable.current.position.stopLoss.value = candle.max - } - } - - let slippedStopLoss = variable.current.position.stopLoss.value - - /* Apply the Slippage */ - let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - slippedStopLoss = slippedStopLoss + slippageAmount - } else { - slippedStopLoss = slippedStopLoss - slippageAmount - } - - closeRate = slippedStopLoss - - variable.current.strategy.stage = 'Close Stage' - checkAnnouncements(strategy.closeStage, 'Stop') - - variable.current.position.stopLoss.stage = 'No Stage' - variable.current.position.takeProfit.stage = 'No Stage' - variable.current.position.end = candle.end - variable.current.position.status = 1 - variable.current.position.exitType = 1 - variable.current.position.endRate = closeRate - - closePositionNow = true - } - - /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ - - if ((variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset && candle.min <= variable.current.position.takeProfit.value) || (variable.episode.parameters.baseAsset !== variable.episode.parameters.marketBaseAsset && candle.max >= variable.current.position.takeProfit.value)) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } - /* - Hit Point Validation: - - This prevents misscalculations when a formula places the take profit in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - if (variable.current.position.takeProfit.value > candle.max) { - variable.current.position.takeProfit.value = candle.max - } - } else { - if (variable.current.position.takeProfit.value < candle.min) { - variable.current.position.takeProfit.value = candle.min - } - } - - let slippedTakeProfit = variable.current.position.takeProfit.value - /* Apply the Slippage */ - let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - slippedTakeProfit = slippedTakeProfit + slippageAmount - } else { - slippedTakeProfit = slippedTakeProfit - slippageAmount - } - - closeRate = slippedTakeProfit - - variable.current.strategy.stage = 'Close Stage' - checkAnnouncements(strategy.closeStage, 'Take Profit') - - variable.current.position.stopLoss.stage = 'No Stage' - variable.current.position.takeProfit.stage = 'No Stage' - - variable.current.position.end = candle.end - variable.current.position.status = 1 - variable.current.position.exitType = 2 - variable.current.position.endRate = closeRate - - closePositionNow = true - addToSnapshots = true - } - } - - /* Taking a Position */ - if ( - takePositionNow === true - ) { - takePositionNow = false - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Entering code block.') } - - /* Inicializing this counter */ - variable.current.distance.toEvent.takePosition = 1 - - /* Position size and rate */ - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - variable.current.position.size = strategy.positionSize - variable.current.position.rate = strategy.positionRate - - /* We take what was calculated at the formula and apply the slippage. */ - let slippageAmount = variable.current.position.rate * bot.VALUES_TO_USE.slippage.positionRate / 100 - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - variable.current.position.rate = variable.current.position.rate - slippageAmount - } else { - variable.current.position.rate = variable.current.position.rate + slippageAmount - } - - if (bot.startMode === 'Live') { - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> Taking a Position in Live Mode.') - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> strategy.positionSize = ' + strategy.positionSize) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> strategy.positionRate = ' + strategy.positionRate) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> variable.current.position.rate = ' + variable.current.position.rate) - } - - /* Update the trade record information. */ - variable.current.position.begin = candle.begin - variable.current.position.beginRate = variable.current.position.rate - - /* Check if we need to execute. */ - if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ - /* Check that we are in LIVE MODE */ - if (bot.startMode === 'Live') { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // We need to put the order because It was not put yet. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - break - } - case 'Position Closed': { // Waiting for a confirmation that the position was closed. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - break - } - case 'Taking Position': { // Waiting for a confirmation that the position was taken. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is Taking Position.') } - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is In a Position.') } - break - } - } - } else { // The context does not exist so it means we are not in a position. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not trading live.') } - } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not the last closed candle.') } - } - - takePositionAtSimulation() - - return - - function putOpeningOrder() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Entering function.') } - - /* We wont take a position unless we are withing the sessionParameters.timeRange.config.initialDatetime and the sessionParameters.timeRange.config.finalDatetime range */ - if (sessionParameters.timeRange.config.initialDatetime !== undefined) { - if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } - takePositionAtSimulation() - return - } - } - - /* We wont take a position if we are past the final datetime */ - if (sessionParameters.timeRange.config.finalDatetime !== undefined) { - if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } - takePositionAtSimulation() - return - } - } - - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (variable.executionContext !== undefined) { - if (variable.executionContext.periods !== undefined) { - if (variable.episode.count.periods <= variable.executionContext.periods) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') } - takePositionAtSimulation() - return - } - } - } - - /* We are not going to place orders based on outdated information. The next filter prevents firing orders when backtesting. */ - if (currentDay) { - let today = new Date(Math.trunc((new Date().valueOf()) / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - let processDay = new Date(Math.trunc(currentDay.valueOf() / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - if (today.valueOf() !== processDay.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> processDay = ' + processDay) } - takePositionAtSimulation() - return - } - } - - let orderPrice - let amountA - let amountB - let orderSide - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - orderSide = 'sell' - - orderPrice = variable.current.position.rate - - amountA = variable.current.position.size * orderPrice - amountB = variable.current.position.size - } else { - orderSide = 'buy' - - orderPrice = variable.current.position.rate - - amountA = variable.current.position.size - amountB = variable.current.position.size / orderPrice - } - - variable.executionContext = { - status: 'Taking Position', - periods: variable.episode.count.periods - } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') } - exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) - - function onOrderCreated(err, order) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Entering function.') } - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } - variable.executionContext = { - status: 'In a Position', - periods: variable.episode.count.periods, - amountA: amountA, - amountB: amountB, - orderId: order.id - } - takePositionAtSimulation() - return - } - case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } - strategy.openStage.openExecution.error = err.message - afterLoop() - return - } - case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } - strategy.openStage.openExecution.error = err.message - afterLoop() - return - } - } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } - } - } - - function takePositionAtSimulation() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionAtSimulation -> Entering function.') } - - /* Continue with the simulation */ - calculateTakeProfit() - calculateStopLoss() - - variable.previous.balance.baseAsset = variable.current.balance.baseAsset - variable.previous.balance.quotedAsset = variable.current.balance.quotedAsset - - variable.last.position.profitLoss = 0 - variable.last.position.ROI = 0 - - let feePaid = 0 - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - feePaid = variable.current.position.size * variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 - - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + variable.current.position.size * variable.current.position.rate - feePaid - variable.current.balance.baseAsset = variable.current.balance.baseAsset - variable.current.position.size - } else { - feePaid = variable.current.position.size / variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 - - variable.current.balance.baseAsset = variable.current.balance.baseAsset + variable.current.position.size / variable.current.position.rate - feePaid - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset - variable.current.position.size - } - - addRecords() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionAtSimulation -> Exiting Loop Body after taking position at simulation.') } - controlLoop() - return - } - } - - if (closePositionNow === true) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Entering code block.') } - - closePositionNow = false - - /* Inicializing this counter */ - variable.current.distance.toEvent.closePosition = 1 - - /* Position size and rate */ - let strategy = tradingSystem.strategies[variable.current.strategy.index] - - if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ - /* Check that we are in LIVE MODE */ - if (bot.startMode === 'Live') { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // No way to close anything at the exchange. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Without a Position.') } - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } - } - break - } - case 'Closing Position': { // Waiting for a confirmation that the position was taken. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } - } - break - } - - case 'Position Closed': { // - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Position Closed.') } - break - } - } - } - } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Not within the last 10 candles.') } - } - - closePositionAtSimulation() - return - - function putClosingOrder() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putClosingOrder -> Entering function.') } - - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (variable.executionContext !== undefined) { - if (variable.executionContext.periods !== undefined) { - if (variable.episode.count.periods <= variable.executionContext.periods) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') } - closePositionAtSimulation() - return - } - } - } - - let orderPrice - let amountA - let amountB - let orderSide - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - orderSide = 'buy' - - orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - - amountA = variable.current.balance.quotedAsset - amountB = variable.current.balance.quotedAsset / orderPrice - } else { - orderSide = 'sell' - - orderPrice = ticker.last - 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - - amountA = variable.current.balance.baseAsset * orderPrice - amountB = variable.current.balance.baseAsset - } - - variable.executionContext = { - status: 'Closing Position', - periods: variable.episode.count.periods - } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') } - exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) - - function onOrderCreated(err, order) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Entering function.') } - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } - variable.executionContext = { - status: 'Position Closed', - periods: variable.episode.count.periods, - amountA: amountA, - amountB: amountB, - orderId: order.id - } - closePositionAtSimulation() - return - } - case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } - /* We will assume that the problem is temporary, and expect that it will work at the next execution. */ - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return - } - case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return - } - } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } - } - } - - function closePositionAtSimulation() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> closePositionAtSimulation -> Entering function.') } - - variable.episode.count.positions++ - - let feePaid = 0 - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - strategy.positionSize = variable.current.balance.quotedAsset / closeRate - strategy.positionRate = closeRate - - feePaid = variable.current.balance.quotedAsset / closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 - - variable.current.balance.baseAsset = variable.current.balance.baseAsset + variable.current.balance.quotedAsset / closeRate - feePaid - variable.current.balance.quotedAsset = 0 - } else { - strategy.positionSize = variable.current.balance.baseAsset * closeRate - strategy.positionRate = closeRate - - feePaid = variable.current.balance.baseAsset * closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 - - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + variable.current.balance.baseAsset * closeRate - feePaid - variable.current.balance.baseAsset = 0 - } - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - variable.last.position.profitLoss = variable.current.balance.baseAsset - variable.previous.balance.baseAsset - variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size - if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } - variable.episode.stat.profitLoss = variable.current.balance.baseAsset - variable.episode.parameters.initial.balance.baseAsset - } else { - variable.last.position.profitLoss = variable.current.balance.quotedAsset - variable.previous.balance.quotedAsset - variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size - if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } - variable.episode.stat.profitLoss = variable.current.balance.quotedAsset - variable.episode.parameters.initial.balance.quotedAsset - } - - variable.current.position.stat.ROI = variable.last.position.ROI - - if (variable.last.position.profitLoss > 0) { - variable.episode.count.hits++ - } else { - variable.episode.count.fails++ - } - - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.baseAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.baseAsset - 1 - variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions - variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / variable.episode.stat.days * 365 - } else { - variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.quotedAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.quotedAsset - 1 - variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions - variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / variable.episode.stat.days * 365 - } - - addRecords() - - variable.current.position.stopLoss.value = 0 - variable.current.position.takeProfit.value = 0 - - variable.current.position.rate = 0 - variable.current.position.size = 0 - - timerToCloseStage = candle.begin - variable.current.position.stopLoss.stage = 'No Stage' - variable.current.position.takeProfit.stage = 'No Stage' - variable.current.position.stopLoss.phase = -1 - variable.current.position.takeProfit.phase = -1 - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> closePositionAtSimulation -> Exiting Loop Body after closing position at simulation.') } - controlLoop() - return - } - } - - /* Closing the Closing Stage */ - if (variable.current.strategy.stage === 'Close Stage') { - if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { - variable.current.strategy.number = variable.current.strategy.index - variable.current.strategy.end = candle.end - variable.current.strategy.endRate = candle.min - variable.current.strategy.status = 1 // This means the strategy is closed, i.e. that has a begin and end. - - variable.current.strategy.index = -1 - variable.current.strategy.stage = 'No Stage' - - timerToCloseStage = 0 - variable.current.distance.toEvent.triggerOff = 1 - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing the Closing Stage -> Exiting Close Stage.') } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing the Closing Stage -> Waiting for timer.') } - } - } - - /* Not a buy or sell condition */ - - addRecords() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Exiting Loop Body after adding a record.') } - controlLoop() - return - - function addRecords() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> addRecords -> Entering function.') } - - manageSnapshots() - addSimulationRecord() - addConditionsRecord() - addStrategyRecord() - addPositionRecord() - - /* We will remmember the last candle processed, so that if it is the last one of this run we can use it to continue from there next time. */ - variable.last.candle = { - begin: candle.begin, - end: candle.end - } - - /* After everything at the simulation level was done, we will do the annoucements that are pending. */ - makeAnnoucements() - - function manageSnapshots() { - /* Snapshots Management (before we generate the trade record and delete that info) */ - if (saveAsLastTriggerOnSnapshot === true) { - snapshots.lastTriggerOn = snapshotDataRecord - saveAsLastTriggerOnSnapshot = false - } - - if (saveAsLastTakePositionSnapshot === true) { - snapshots.lastTakePosition = snapshotDataRecord - saveAsLastTakePositionSnapshot = false - } - - if (addToSnapshots === true) { - let closeValues = [ - variable.episode.count.positions, // Position Number - (new Date(candle.begin)).toISOString(), // Datetime - tradingSystem.strategies[variable.current.strategy.index].name, // Strategy Name - variable.current.strategy.situationName, // Trigger On Situation - variable.current.position.situationName, // Take Position Situation - hitOrFial(), // Result - variable.last.position.ROI, // ROI - exitType() // Exit Type - ] - - function hitOrFial() { - if (variable.last.position.ROI > 0) { return 'HIT' } else { return 'FAIL' } - } - - function exitType() { - switch (variable.current.position.exitType) { - case 1: return 'Stop' - case 2: return 'Take Profit' - } - } - - - if (positionedAtYesterday === false) { - snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) - snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) - } - snapshots.lastTriggerOn = undefined - snapshots.lastTakePosition = undefined - addToSnapshots = false - } - - let closeHeaders = ['Trade Number', 'Close Datetime', 'Strategy', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] - if (snapshots.headers === undefined) { - snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshotLoopHeaders))) - } - } - - function addSimulationRecord() { - /* Simulation Record */ - let simulationRecord - - if (variable.current.balance.baseAsset === Infinity) { - variable.current.balance.baseAsset = Number.MAX_SAFE_INTEGER - } - - if (variable.current.balance.quotedAsset === Infinity) { - variable.current.balance.quotedAsset = Number.MAX_SAFE_INTEGER - } - - simulationRecord = [ - candle.begin, - candle.end, - variable.current.balance.baseAsset, - variable.current.balance.quotedAsset, - variable.episode.stat.profitLoss, - variable.last.position.profitLoss, - variable.current.position.stopLoss.value, - variable.episode.count.positions, - variable.episode.count.hits, - variable.episode.count.fails, - variable.episode.stat.hitRatio, - variable.episode.stat.ROI, - variable.episode.count.periods, - variable.episode.stat.days, - variable.episode.stat.anualizedRateOfReturn, - variable.current.position.rate, - variable.last.position.ROI, - variable.current.position.takeProfit.value, - variable.current.position.stopLoss.phase, - variable.current.position.takeProfit.phase, - variable.current.position.size, - variable.episode.parameters.initial.balance.baseAsset, - variable.episode.parameters.minimum.balance.baseAsset, - variable.episode.parameters.maximum.balance.baseAsset, - variable.episode.parameters.initial.balance.quotedAsset, - variable.episode.parameters.minimum.balance.quotedAsset, - variable.episode.parameters.maximum.balance.quotedAsset, - '"' + variable.episode.parameters.baseAsset + '"', - '"' + variable.episode.parameters.quotedAsset + '"', - '"' + variable.episode.parameters.marketBaseAsset + '"', - '"' + variable.episode.parameters.marketQuotedAsset + '"', - variable.current.position.count.periods, - variable.current.position.stat.days - ] - - recordsArray.push(simulationRecord) - } - - function addConditionsRecord() { - /* Prepare the information for the Conditions File */ - let conditionsRecord = [ - candle.begin, - candle.end, - variable.current.strategy.index, - variable.current.position.stopLoss.phase, - variable.current.position.takeProfit.phase, - conditionsValues, - formulasErrors, - formulasValues - ] - - conditionsArray.push(conditionsRecord) - } - - function addStrategyRecord() { - /* - Lets see if there will be an open strategy ... - Except if we are at the head of the market (remember we skipped the last candle for not being closed.) - */ - if (variable.current.strategy.begin !== 0 && variable.current.strategy.end === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { - variable.current.strategy.status = 2 // This means the strategy is open, i.e. that has a begin but no end. - variable.current.strategy.end = candle.end - } - - /* Prepare the information for the Strategies File */ - if (variable.current.strategy.begin !== 0 && variable.current.strategy.end !== 0) { - let strategyRecord = [ - variable.current.strategy.begin, - variable.current.strategy.end, - variable.current.strategy.status, - variable.current.strategy.number, - variable.current.strategy.beginRate, - variable.current.strategy.endRate, - '"' + variable.current.strategy.situationName + '"', - '"' + variable.current.strategy.name + '"' - ] - - strategiesArray.push(strategyRecord) - - inializeCurrentStrategy() - } - } - - function addPositionRecord() { - /* - Lets see if there will be an open trade ... - Except if we are at the head of the market (remember we skipped the last candle for not being closed.) - */ - if (variable.current.position.begin !== 0 && - variable.current.position.end === 0 && - currentCandleIndex === candles.length - 2 && - lastCandle.end !== lastInstantOfTheDay) { - - /* This means the trade is open */ - variable.current.position.status = 2 - variable.current.position.end = candle.end - variable.current.position.endRate = candle.close - - /* Here we will calculate the ongoing ROI */ - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - variable.current.position.stat.ROI = (variable.current.position.rate - candle.close) / variable.current.position.rate * 100 - } else { - variable.current.position.stat.ROI = (candle.close - variable.current.position.rate) / variable.current.position.rate * 100 - } - } - - /* Prepare the information for the Positions File */ - if (variable.current.position.begin !== 0 && variable.current.position.end !== 0) { - let positionRecord = [ - variable.current.position.begin, - variable.current.position.end, - variable.current.position.status, - variable.current.position.stat.ROI, - variable.current.position.beginRate, - variable.current.position.endRate, - variable.current.position.exitType, - '"' + variable.current.position.situationName + '"' - ] - - positionsArray.push(positionRecord) - - initializeCurrentPosition() - } - } - } - - function checkAnnouncements(node, value) { - /* - Value is an optional parameter that represents the value that the announcement is monitoring for change (for numeric values only). - If we do receive this value, we will only make the annoucement if the variance is grater than the user pre-defined value - for this variance. - */ - - if (node.announcements !== undefined) { - for (let i = 0; i < node.announcements.length; i++) { - let announcement = node.announcements[i] - let key = node.type + '-' + announcement.name + '-' + announcement.id - - let lastPeriodAnnounced = -1 - let newAnnouncementRecord = {} - - for (let j = 0; j < variable.announcements.length; j++) { - let announcementRecord = variable.announcements[j] - if (announcementRecord.key === key) { - lastPeriodAnnounced = announcementRecord.periods - newAnnouncementRecord = announcementRecord - break - } - } - - if (variable.episode.count.periods > lastPeriodAnnounced) { - if (isNaN(value) === false) { - /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ - let valueVariation - - let config = announcement.config - valueVariation = config.valueVariation - - if (newAnnouncementRecord.value !== undefined && valueVariation !== undefined) { - let upperLimit = newAnnouncementRecord.value + newAnnouncementRecord.value * valueVariation / 100 - let lowerLimit = newAnnouncementRecord.value - newAnnouncementRecord.value * valueVariation / 100 - if (value > lowerLimit && value < upperLimit) { - /* There is not enough variation to announce this again. */ - return - } - } - } - - /* - We store the announcement temporarily at an Array to differ its execution, becasue we need to evaulate its formula - and at this point in time the potential variables used at the formula are still not set. - */ - announcement.value = value - announcementsToBeMade.push(announcement) - - /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ - if (newAnnouncementRecord.periods !== undefined) { - newAnnouncementRecord.periods = variable.episode.count.periods - newAnnouncementRecord.value = value - } else { - newAnnouncementRecord = { - key: key, - periods: variable.episode.count.periods, - value: value - } - variable.announcements.push(newAnnouncementRecord) - } - } - } - } - } - - function makeAnnoucements() { - /* Here we go through all the annoucements that need to be done during this loop, and we just do them. */ - for (let i = 0; i < announcementsToBeMade.length; i++) { - announcement = announcementsToBeMade[i] - /* Here we check if there is a formula attached to the annoucement, we evaluate it to get the annoucement text. */ - let formulaValue - if (announcement.formula !== undefined) { - try { - let value = announcement.value - formulaValue = eval(announcement.formula.code) - } catch (err) { - announcement.formula.error = err.message - } - } - announcement.formulaValue = formulaValue - - if (bot.SESSION.socialBots !== undefined) { - bot.SESSION.socialBots.announce(announcement) - } - } - } - - function addCodeToSnapshot(code) { - if (code === undefined) { return } - - try { - let instructionsArray = code.split(' ') - for (let i = 0; i < instructionsArray.length; i++) { - let instruction = instructionsArray[i] - instruction = instruction.replace('(', '') - instruction = instruction.replace(')', '') - instruction = instruction.replace(//g, '') - instruction = instruction.replace(/<=/g, '') - instruction = instruction.replace(/>=/g, '') - instruction = instruction.replace(/!=/g, '') - instruction = instruction.replace(/!==/g, '') - instruction = instruction.replace(/==/g, '') - instruction = instruction.replace(/===/g, '') - instruction = instruction.replace(/{/g, '') - instruction = instruction.replace(/}/g, '') - if (instruction.indexOf('chart') >= 0) { - let parts = instruction.split('.') - let timeFrame = parts[1] - let product = parts[2] - let property - checkPrevious(3) - function checkPrevious(index) { - property = parts[index] - if (property === 'previous') { - product = product + '.previous' - checkPrevious(index + 1) - } - } - - // Example: chart.at01hs.popularSMA.sma200 - chart.at01hs.popularSMA.sma100 < 10 - if (timeFrame !== 'atAnyvariable.timeFrame') { - timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) - } - let key = timeFrame + '-' + product + '-' + property - let existingKey = snapshotKeys.get(key) - - if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. - snapshotKeys.set(key, key) - snapshotLoopHeaders.push(key) - - let value = eval(instruction) - snapshotDataRecord.push(value) - } - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> code = ' + code) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } - - function controlLoop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> controlLoop -> Entering function.') } - - /* 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.') - afterLoop() - return - } - - 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.') - afterLoop() - return - } - - currentCandleIndex++ - if (currentCandleIndex < candles.length) { - setImmediate(loop) // This will execute the next loop in the next iteration of the NodeJs event loop allowing for other callbacks to be executed. - } else { - afterLoop() - } - } - - function afterLoop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> afterLoop -> Entering function.') } - - /* - Before returning we need to see if we have to record some of our counters at the variable. - To do that, the condition to be met is that this execution must include all candles of the current day. - */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> callback -> recordsArray.length = ' + recordsArray.length) } - - callback(tradingSystem, snapshots.headers, snapshots.triggerOn, snapshots.takePosition) - } - - function getElement(pArray, currentCandle, datasetName) { - if (pArray === undefined) { return } - try { - let element - for (let i = 0; i < pArray.length; i++) { - element = pArray[i] - - if (currentCandle.end === element.end) { // when there is an exact match at the end we take that element - return element - } else { - if ( - i > 0 && - element.end > currentCandle.end - ) { - let previousElement = pArray[i - 1] - if (previousElement.end < currentCandle.end) { - return previousElement // If one elements goes into the future of currentCandle, then we stop and take the previous element. - } else { - return - } - } - if ( - i === pArray.length - 1 // If we reach the end of the array, then we return the last element. - && - element.end < currentCandle.end - ) { - return element - } - } - } - return - } 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) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } -} - diff --git a/TradingBotProcessMainLoop.js b/TradingBot.js similarity index 54% rename from TradingBotProcessMainLoop.js rename to TradingBot.js index 579de4f33..8b16f955e 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBot.js @@ -1,27 +1,22 @@ -exports.newTradingBotProcessMainLoop = function newTradingBotProcessMainLoop(bot, parentLogger) { +exports.newTradingBot = function newTradingBot(bot, parentLogger) { - const ROOT_DIR = './'; - - const MODULE_NAME = "Trading Process Main Loop"; + const MODULE_NAME = "Trading Bot"; const FULL_LOG = true; let USER_BOT_MODULE; let USER_BOT_COMMONS; - const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket'); - const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily'); - const MULTI_PERIOD = require(ROOT_DIR + 'MultiPeriod'); + const TRADING_PROCESS_MODULE = require(global.ROOT_DIR + '/LowFrequencyTrading/TradingProcess.js'); const FILE_STORAGE = require('./FileStorage.js'); - const SESSION = require(ROOT_DIR + 'Session'); - + const SESSION = require(global.ROOT_DIR + 'Session'); + let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); let session = SESSION.newSession(bot, parentLogger) - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; - let checkLoopHealthHandle; let thisObject = { initialize: initialize, @@ -33,14 +28,9 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - /* This function is exactly the same in the 3 modules representing the 2 different bot types loops. */ - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - processConfig = pProcessConfig; - if (bot.definedByUI === true) { /* The code of the bot is defined at the UI. No need to load a file with the code. */ session.initialize(processConfig, onSessionInitialized) @@ -51,15 +41,12 @@ return } - bot.resumeExecution = processConfig.framework.startDate.resumeExecution // We are inherating this from root, but from here we need it at bot - let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName filePath += "/User.Bot.js" fileStorage.getTextFile(filePath, onBotDownloaded); function onBotDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); @@ -77,7 +64,6 @@ fileStorage.getTextFile(filePath, onCommonsDownloaded); function onCommonsDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -87,7 +73,7 @@ USER_BOT_COMMONS = {} USER_BOT_COMMONS.newCommons = eval(text); // Use this for production - session.initialize(onSessionInitialized) + session.initialize(onSessionInitialized) function onSessionInitialized(err) { callBackFunction(err); @@ -95,29 +81,28 @@ } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } function run(callBackFunction) { - try { - - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - /* Some initial values*/ - bot.enableCheckLoopHealth = true; let fixedTimeLoopIntervalHandle; bot.STOP_SESSION = true; + /* Errors sent to the UI */ + bot.processError = processError + + /* Heartbeats sent to the UI */ + bot.processHeartBeat = processHeartBeat + loop(); function loop() { - try { - - processHeartBeat(undefined, undefined, "Running...") + processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { str = str.toString(); return str.length < max ? pad(" " + str, max) : str; @@ -135,37 +120,28 @@ bot.loopCounter++; bot.loopStartTime = new Date().valueOf(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - let nextWaitTime; - /* Heartbeats sent to the UI */ - bot.sessionHeartBeat = sessionHeartBeat - bot.processHeartBeat = processHeartBeat - processHeartBeat() + /* We tell the UI that we are running. */ + processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const EXCHANGE_API = require(ROOT_DIR + 'ExchangeAPI'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); - const DATA_SET = require(ROOT_DIR + 'DataSet'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(global.ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(global.ROOT_DIR + 'StatusDependencies'); + const DATA_DEPENDENCIES = require(global.ROOT_DIR + 'DataDependencies'); + const DATA_SET = require(global.ROOT_DIR + 'DataSet'); + const PROCESS_EXECUTION_EVENTS = require(global.ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(global.ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. /* High level log entry */ - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + " Main Loop # " + pad(Number(bot.loopCounter), 8) + " " + bot.processNode.session.type + " " + bot.processNode.session.name) /* Checking if we need to need to emit any event */ - if (bot.SESSION_STATUS === 'Idle' && bot.STOP_SESSION === false) { global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Running') bot.SESSION_STATUS = 'Running' @@ -178,13 +154,13 @@ /* Checking if we should process this loop or not.*/ if (bot.STOP_SESSION === true) { - + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Waiting for " + bot.processNode.session.type + " " + bot.processNode.session.name + " to be ran."); } console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Waiting for " + bot.processNode.session.type + " " + bot.processNode.session.name + " to be ran. "); - nextWaitTime = 'Normal'; + nextWaitTime = 'Waiting for Session'; loopControl(nextWaitTime); return } @@ -200,21 +176,12 @@ initializeProcessExecutionEvents(); function initializeProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - processExecutionEvents.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); @@ -228,66 +195,40 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - processExecutionEvents.start(onStarted); function onStarted(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); @@ -307,68 +248,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeStatusDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - statusDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); @@ -382,73 +296,59 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return return; } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function initializeDataDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } - dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); - dataDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } - initializeExchangeAPI(); + switch (processConfig.framework.name) { + case 'Low-Frequency-Trading-Process': { + processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); + intitializeProcessFramework(); + break; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); + global.unexpectedError = err.message + processStopped() + return + } + } return; } case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. @@ -458,161 +358,40 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeExchangeAPI() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> Entering function."); } - - exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); - - exchangeAPI.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeContext -> onInizialized -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> onInizialized -> Execution finished well."); } - - switch (processConfig.framework.name) { - case 'Multi-Period': { - processFramework = MULTI_PERIOD.newMultiPeriod(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); - intitializeProcessFramework(); - break; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Process Framework not Supported."); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeExchangeAPI -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() + global.unexpectedError = err.message processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeContext -> onInizialized -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function intitializeProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } - - processFramework.initialize(processConfig, statusDependencies, dataDependencies, exchangeAPI, onInizialized); + processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); @@ -626,81 +405,46 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function startProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } - processFramework.start(onFinished); function onFinished(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } processFramework.finalize() processFramework = undefined dataDependencies.finalize() @@ -722,16 +466,10 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { @@ -750,58 +488,37 @@ } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = "+ err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } function finishProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - processExecutionEvents.finish(onFinished); function onFinished(err) { @@ -809,8 +526,6 @@ processExecutionEvents.finalize() processExecutionEvents = undefined - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); @@ -825,69 +540,44 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ - processHeartBeat() /* Here we check if we must stop the loop gracefully. */ - shallWeStop(onStop, onContinue); function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } if (global.WRITE_LOGS_TO_FILES === 'true') { @@ -896,30 +586,47 @@ global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Stopped') processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); return; - } function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - switch (nextWaitTime) { + case 'Waiting for Session': { + let waitTime = processConfig.sessionRunWaitTime + nextLoopTimeoutHandle = setTimeout(loop, waitTime); + let waitingTime = waitTime / 1000 / 60 + let label = 'minute/s' + if (waitingTime < 1) { + waitingTime = waitTime / 1000 + label = 'seconds' + } + processHeartBeat(undefined, undefined, "Waiting " + waitingTime + " " + label + " for " + bot.processNode.session.type + " " + bot.processNode.session.name + " to be ran. ") + logger.persist(); + } + break case 'Normal': { let waitTime - if (bot.startMode === "Live") { - waitTime = processConfig.liveWaitTime - } else { - waitTime = processConfig.normalWaitTime + switch (bot.SESSION.type) { + case 'Live Trading Session': { + waitTime = processConfig.liveTradingWaitTime + } + break + case 'Fordward Tessting Session': { + waitTime = processConfig.fordwardTestingWaitTime + } + break + case 'Paper Trading Session': { + waitTime = processConfig.paperTradingWaitTime + } + break + case 'Backtesting Session': { + waitTime = processConfig.backtestingWaitTime + } + break } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (waitTime / 1000 / 60) + " minute/s."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, waitTime); let waitingTime = waitTime / 1000 / 60 let label = 'minute/s' @@ -928,32 +635,27 @@ label = 'seconds' } processHeartBeat(undefined, undefined, "Waiting " + waitingTime + " " + label + " for next execution.") - if (global.WRITE_LOGS_TO_FILES === 'true') { - logger.persist(); - } + logger.persist(); } break; case 'Retry': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); } break; case 'Sleep': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") logger.persist(); } break; case 'Coma': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") logger.persist(); } break; @@ -961,44 +663,9 @@ } } - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); - - } - } - } - function shallWeStop(stopCallBack, continueCallBack) { - try { - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - if (!global.STOP_TASK_GRACEFULLY) { continueCallBack(); } else { @@ -1006,23 +673,17 @@ } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = "+ err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); + global.unexpectedError = err.message + processStopped() + return } } @@ -1036,20 +697,38 @@ global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) } - function sessionHeartBeat(processingDate) { - let event = { - seconds: (new Date()).getSeconds(), - processingDate: processingDate - } - global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Heartbeat', event) - - if (global.STOP_TASK_GRACEFULLY === true) { - bot.STOP_SESSION = true + function processError(node, errorMessage) { + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) } function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + if (global.unexpectedError !== undefined) { + processError(undefined, "An unexpected error caused the Process to stop.") + } else { + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + } + sessionStopped() + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + if (global.unexpectedError !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } } function sessionStopped() { @@ -1060,11 +739,9 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/server.js b/server.js index e2840cc6d..b9e30080f 100644 --- a/server.js +++ b/server.js @@ -25,19 +25,36 @@ global.CUSTOM_FAIL_RESPONSE = { message: "Custom Message" }; -const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; +global.ROOT_DIR = './'; + +global.ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000 +global.ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 +global.ONE_MIN_IN_MILISECONDS = 60 * 1000 global.LOGGER_MAP = new Map() global.SESSION_MAP = new Map() +global.UNIQUE_ID = function () { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { + var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8) + return v.toString(16) + }) +} + +global.PRECISE = function (floatNumber, precision) { + return parseFloat(Number.parseFloat(floatNumber).toPrecision(precision)) +} + process.on('uncaughtException', function (err) { console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack) + console.log(err.stack) global.EXIT_NODE_PROCESS() }) process.on('unhandledRejection', (reason, p) => { console.log('[ERROR] Task Server -> server -> unhandledRejection -> reason = ' + JSON.stringify(reason)) console.log('[ERROR] Task Server -> server -> unhandledRejection -> p = ' + JSON.stringify(p)) + console.log(reason.stack) global.EXIT_NODE_PROCESS() }) @@ -91,6 +108,10 @@ process.on('message', message => { let shuttingDownProcess = false global.EXIT_NODE_PROCESS = function exitProcess() { + if (global.unexpectedError !== undefined) { + global.taskError(undefined, "An unexpected error caused the Task to stop.") + } + if (shuttingDownProcess === true) { return } shuttingDownProcess = true @@ -197,6 +218,24 @@ function bootLoader() { global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Heartbeat', event) } + global.taskError = function taskError(node, errorMessage) { + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } + } + + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Error', event) + } + for (let processIndex = 0; processIndex < global.TASK_NODE.bot.processes.length; processIndex++) { let config = global.TASK_NODE.bot.processes[processIndex].config @@ -292,9 +331,9 @@ function startRoot(processIndex) { } global.getPercentage = function (fromDate, currentDate, lastDate) { - let fromDays = Math.trunc(fromDate.valueOf() / ONE_DAY_IN_MILISECONDS) - let currentDays = Math.trunc(currentDate.valueOf() / ONE_DAY_IN_MILISECONDS) - let lastDays = Math.trunc(lastDate.valueOf() / ONE_DAY_IN_MILISECONDS) + let fromDays = Math.trunc(fromDate.valueOf() / global.ONE_DAY_IN_MILISECONDS) + let currentDays = Math.trunc(currentDate.valueOf() / global.ONE_DAY_IN_MILISECONDS) + let lastDays = Math.trunc(lastDate.valueOf() / global.ONE_DAY_IN_MILISECONDS) let percentage = (currentDays - fromDays) * 100 / (lastDays - fromDays) if ((lastDays - fromDays) === 0) { percentage = 100 @@ -303,8 +342,8 @@ global.getPercentage = function (fromDate, currentDate, lastDate) { } global.areEqualDates = function (date1, date2) { - let day1Days = Math.trunc(date1.valueOf() / ONE_DAY_IN_MILISECONDS) - let day2Days = Math.trunc(date2.valueOf() / ONE_DAY_IN_MILISECONDS) + let day1Days = Math.trunc(date1.valueOf() / global.ONE_DAY_IN_MILISECONDS) + let day2Days = Math.trunc(date2.valueOf() / global.ONE_DAY_IN_MILISECONDS) if (day1Days === day2Days) { return true