diff --git a/DataDependencies.js b/DataDependencies.js
index db08c5a77..df98108cc 100644
--- a/DataDependencies.js
+++ b/DataDependencies.js
@@ -9,6 +9,7 @@
let ownerBot; // This is the bot owner of the Data Set.
let thisObject = {
+ config: undefined,
dataSets: new Map(),
initialize: initialize,
keys: []
@@ -23,6 +24,15 @@
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
+ thisObject.config = pDataDependenciesConfig;
+
+ if (thisObject.config === undefined) {
+
+ // We allow old indicators not to declare their data dependencies.
+
+ callBackFunction(global.DEFAULT_OK_RESPONSE);
+ return;
+ }
/*
For each dependency declared at the bot config, we will initialize a DataSet as part of this initialization process.
@@ -31,11 +41,11 @@
let alreadyCalledBack = false;
let addCount = 0;
- for (let i = 0; i < pDataDependenciesConfig.length; i++) {
+ for (let i = 0; i < thisObject.config.length; i++) {
let dataSetModule = DATA_SET.newDataSet(BOT, logger, BLOB_STORAGE, UTILITIES);
- dataSetModule.initialize(pDataDependenciesConfig[i], onInitilized);
+ dataSetModule.initialize(thisObject.config[i], onInitilized);
function onInitilized(err) {
@@ -58,14 +68,14 @@
let key;
- key = pDataDependenciesConfig[i].devTeam + "-" + pDataDependenciesConfig[i].bot + "-" + pDataDependenciesConfig[i].product + "-" + pDataDependenciesConfig[i].dataSet + "-" + pDataDependenciesConfig[i].dataSetVersion;
+ key = thisObject.config[i].devTeam + "-" + thisObject.config[i].bot + "-" + thisObject.config[i].product + "-" + thisObject.config[i].dataSet + "-" + thisObject.config[i].dataSetVersion;
thisObject.keys.push(key);
thisObject.dataSets.set(key, dataSetModule);
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> addDataSet -> DataSet added to Map. -> key = " + key); }
- if (addCount === pDataDependenciesConfig.length) {
+ if (addCount === thisObject.config.length) {
if (alreadyCalledBack === false) {
callBackFunction(global.DEFAULT_OK_RESPONSE);
return;
diff --git a/DataSet.js b/DataSet.js
index 074135d0a..215137939 100644
--- a/DataSet.js
+++ b/DataSet.js
@@ -5,7 +5,6 @@
let bot = BOT;
let thisObject = {
- file: undefined, // Here we have the JSON object representing the file content.
initialize: initialize,
getTextFile: getTextFile,
createTextFile: createTextFile
diff --git a/IndicatorBotProcessMainLoop.js b/IndicatorBotProcessMainLoop.js
index 5ed0969df..45e6cf024 100644
--- a/IndicatorBotProcessMainLoop.js
+++ b/IndicatorBotProcessMainLoop.js
@@ -9,6 +9,8 @@
let COMMONS_MODULE;
const BLOB_STORAGE = require(ROOT_DIR + 'BlobStorage');
+ const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket');
+ const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily');
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.
@@ -175,6 +177,8 @@
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');
/* 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. */
@@ -188,7 +192,9 @@
/* We will prepare first the infraestructure needed for the bot to run. There are 3 modules we need to sucessfullly initialize first. */
let userBot;
+ let processFramework;
let statusDependencies;
+ let dataDependencies;
let nextWaitTime;
@@ -213,7 +219,7 @@
switch (err.result) {
case global.DEFAULT_OK_RESPONSE.result: {
logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well.");
- initializeUserBot();
+ 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.
@@ -269,6 +275,95 @@
}
}
+ 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, BLOB_STORAGE, UTILITIES);
+
+ dataDependencies.initialize(processConfig.dataDependencies, 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. */
+
+ switch (processConfig.framework) {
+ case 'Multi-Period-Market': {
+ processFramework = MULTI_PERIOD_MARKET.newMultiPeriodMarket(bot, logger, COMMONS_MODULE, UTILITIES, BLOB_STORAGE, USER_BOT_MODULE, COMMONS_MODULE);
+ intitializeProcessFramework();
+ break;
+ }
+ case 'Multi-Period-Daily': {
+ processFramework = MULTI_PERIOD_DAILY.newMultiPeriodDaily(bot, logger, COMMONS_MODULE, UTILITIES, BLOB_STORAGE, USER_BOT_MODULE, COMMONS_MODULE);
+ intitializeProcessFramework();
+ break;
+ }
+ default: { initializeUserBot();}
+ }
+ 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, "[ERROR] 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.
+ 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.message = " + 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.message);
+ 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.message);
+ logger.persist();
+ clearInterval(fixedTimeLoopIntervalHandle);
+ clearTimeout(nextLoopTimeoutHandle);
+ clearTimeout(checkLoopHealthHandle);
+ bot.enableCheckLoopHealth = false;
+ callBackFunction(err);
+ }
+ }
+
function initializeUserBot() {
try {
@@ -500,6 +595,235 @@
}
}
+ function intitializeProcessFramework() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); }
+
+ processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized, dataDependencies);
+
+ 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, "[ERROR] 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.");
+ logger.persist();
+ clearInterval(fixedTimeLoopIntervalHandle);
+ clearTimeout(nextLoopTimeoutHandle);
+ clearTimeout(checkLoopHealthHandle);
+ bot.enableCheckLoopHealth = false;
+ callBackFunction(err);
+ return;
+ }
+ case global.CUSTOM_OK_RESPONSE.result: {
+
+ switch (err.message) {
+ case "Too far in the future.": {
+ logger.write(MODULE_NAME, "[WARN] run -> loop -> intitializeProcessFramework -> onInizialized > Too far in the future. This Loop will enter in coma.");
+ nextWaitTime = 'Coma';
+ loopControl(nextWaitTime);
+ return;
+ }
+ case "Not needed now, but soon.": {
+ logger.write(MODULE_NAME, "[WARN] run -> loop -> intitializeProcessFramework -> onInizialized > Not needed now, but soon. This Loop will continue with Normal wait time.");
+ nextWaitTime = 'Normal';
+ loopControl(nextWaitTime);
+ return;
+ }
+ default: {
+ logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err.message = " + 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.message = " + 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.message);
+ 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.message);
+ 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."); }
+
+ switch (err.result) {
+ case global.DEFAULT_OK_RESPONSE.result: {
+ logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> 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, "[ERROR] 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.");
+ 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;
+ }
+ case "Month before it is needed.": {
+ logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Month before it is needed. This Loop will be terminated.");
+ logger.persist();
+ clearInterval(fixedTimeLoopIntervalHandle);
+ clearTimeout(nextLoopTimeoutHandle);
+ clearTimeout(checkLoopHealthHandle);
+ bot.enableCheckLoopHealth = false;
+ callBackFunction(global.DEFAULT_OK_RESPONSE);
+ return;
+ }
+ case "Month fully processed.": {
+ logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Month fully processed. This Loop will be terminated.");
+ logger.persist();
+ clearInterval(fixedTimeLoopIntervalHandle);
+ clearTimeout(nextLoopTimeoutHandle);
+ clearTimeout(checkLoopHealthHandle);
+ bot.enableCheckLoopHealth = false;
+ callBackFunction(global.DEFAULT_OK_RESPONSE);
+ return;
+ }
+ case "End of the month reached.": {
+ logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> End of the month reached. This Loop will be terminated.");
+ logger.persist();
+ clearInterval(fixedTimeLoopIntervalHandle);
+ clearTimeout(nextLoopTimeoutHandle);
+ clearTimeout(checkLoopHealthHandle);
+ bot.enableCheckLoopHealth = false;
+ callBackFunction(global.DEFAULT_OK_RESPONSE);
+ return;
+ }
+ default: {
+ logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err.message = " + 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.message = " + 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.message);
+ 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.message);
+ 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."); }
diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js
new file mode 100644
index 000000000..c9c8fe1cf
--- /dev/null
+++ b/MultiPeriodDaily.js
@@ -0,0 +1,687 @@
+exports.newMultiPeriodDaily = function newMultiPeriodDaily(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, USER_BOT_MODULE, COMMONS_MODULE) {
+
+ const FULL_LOG = true;
+ const LOG_FILE_CONTENT = false;
+ const MODULE_NAME = "Multi Period Daily";
+ const EXCHANGE_NAME = "Poloniex";
+ const commons = COMMONS.newCommons(bot, logger, UTILITIES);
+ const GMT_SECONDS = ':00.000 GMT+0000';
+ const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000;
+
+ thisObject = {
+ initialize: initialize,
+ start: start
+ };
+
+ let utilities = UTILITIES.newCloudUtilities(bot, logger);
+
+ let statusDependencies;
+ let dataDependencies;
+ let storages = [];
+ let dataFiles = [];
+
+ let usertBot;
+ let currentBotStorage = BLOB_STORAGE.newBlobStorage(bot, logger);
+
+ let pProcessConfig;
+
+ return thisObject;
+
+ function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) {
+
+ try {
+
+ logger.fileName = MODULE_NAME;
+ logger.initialize();
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
+
+ statusDependencies = pStatusDependencies;
+ dataDependencies = pDataDependencies;
+ processConfig = pProcessConfig;
+
+ for (let i = 0; i < dataDependencies.config.length; i++) {
+
+ let key;
+ let storage;
+ let dependency = dataDependencies.config[i];
+
+ key = dependency.devTeam + "-" +
+ dependency.bot + "-" +
+ dependency.product + "-" +
+ dependency.dataSet + "-" +
+ dependency.dataSetVersion
+
+ storage = dataDependencies.dataSets.get(key);
+
+ storages.push(storage);
+
+ }
+
+ currentBotStorage.initialize(bot.devTeam, onStorageInizialized);
+
+ function onStorageInizialized(err) {
+
+ if (err.result === global.DEFAULT_OK_RESPONSE.result) {
+
+ usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, BLOB_STORAGE);
+ usertBot.initialize(dataDependencies, callBackFunction);
+
+ } else {
+ logger.write(MODULE_NAME, "[ERROR] initialize -> onStorageInizialized -> err = " + err.message);
+ callBackFunction(err);
+ }
+ }
+
+ } catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function start(callBackFunction) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); }
+
+ let market = global.MARKET;
+
+ /* Context Variables */
+
+ let contextVariables = {
+ lastFile: undefined, // Datetime of the last file files sucessfully produced by this process.
+ dateBeginingOfMarket: 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 currentDay; // Holds the processing date.
+
+ 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 marke in order to get when the market starts.
+ */
+
+ let botWhoKnowsTheBeginingOfTheMarket = statusDependencies.config[processConfig.statusDependenciesIndexBegingOfMarket];
+ let botWhoKnowsTheEndOfTheMarket = statusDependencies.config[processConfig.statusDependenciesIndexEndOfMarket];
+
+ reportKey = botWhoKnowsTheBeginingOfTheMarket.devTeam + "-" + botWhoKnowsTheBeginingOfTheMarket.bot + "-" + botWhoKnowsTheBeginingOfTheMarket.process + "-" + "dataSet.V1";
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> reportKey = " + reportKey); }
+
+ statusReport = statusDependencies.statusReports.get(reportKey);
+
+ 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 = statusDependencies.statusReports.get(reportKey).file;
+
+ if (thisReport.lastFile === 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.");
+
+ 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;
+ }
+
+ contextVariables.dateBeginingOfMarket = new Date(thisReport.lastFile.year + "-" + thisReport.lastFile.month + "-" + thisReport.lastFile.days + " " + thisReport.lastFile.hours + ":" + thisReport.lastFile.minutes + GMT_SECONDS);
+ //contextVariables.dateBeginingOfMarket = new Date("2018-01-01"); // TODO: Remove this temporaty patch.
+
+ /*
+ Here we get the status report from the bot who knows which is the end of the market.
+ */
+
+ reportKey = botWhoKnowsTheEndOfTheMarket.devTeam + "-" + botWhoKnowsTheEndOfTheMarket.bot + "-" + botWhoKnowsTheEndOfTheMarket.process + "-" + "dataSet.V1";
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> reportKey = " + reportKey); }
+
+ statusReport = statusDependencies.statusReports.get(reportKey);
+
+ 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 = statusDependencies.statusReports.get(reportKey).file;
+
+ if (thisReport.lastFile === undefined) {
+ 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;
+ }
+
+ contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf());
+
+ /* Finally we get our own Status Report. */
+
+ reportKey = bot.devTeam + "-" + bot.codeName + "-" + "Multi-Period-Daily" + "-" + "dataSet.V1";
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> reportKey = " + reportKey); }
+
+ statusReport = statusDependencies.statusReports.get(reportKey);
+
+ 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 = statusDependencies.statusReports.get(reportKey).file;
+
+ if (thisReport.lastFile !== undefined) {
+
+ contextVariables.lastFile = new Date(thisReport.lastFile);
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> thisReport.lastFile !== undefined"); }
+
+ processTimePeriods();
+ return;
+
+ } else {
+
+ /*
+ In the case when there is no status report, we take the date of the file with the first trades as the begining of the market. Then we will
+ go one day further in time, so that the previous day does fine a file at the begining of the market.
+ */
+
+ contextVariables.lastFile = new Date(contextVariables.dateBeginingOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginingOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginingOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS);
+ contextVariables.lastFile = new Date(contextVariables.lastFile.valueOf() + ONE_DAY_IN_MILISECONDS);
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> thisReport.lastFile === undefined"); }
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> contextVariables.lastFile = " + contextVariables.lastFile); }
+
+ processTimePeriods();
+ return;
+ }
+
+ } catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.message);
+ 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 processTimePeriods() {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> Entering function."); }
+
+ try {
+
+ let n;
+ let botNeverRan = true;
+
+ currentDay = 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 {
+
+ logger.newInternalLoop(bot.codeName, bot.process);
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> advanceTime -> Entering function."); }
+
+ currentDay = new Date(currentDay.valueOf() + ONE_DAY_IN_MILISECONDS);
+ previousDay = new Date(currentDay.valueOf() - ONE_DAY_IN_MILISECONDS);
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> advanceTime -> currentDay = " + currentDay); }
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> advanceTime -> previousDay = " + previousDay); }
+
+ /* Validation that we are not going past the head of the market. */
+
+ if (currentDay.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 -> processTimePeriods -> advanceTime -> " + logText); }
+
+ callBackFunction(global.DEFAULT_OK_RESPONSE);
+ return;
+
+ }
+
+ periodsLoop();
+
+ } catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> advanceTime -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function periodsLoop() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 -> processTimePeriods -> periodsLoop -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function periodsLoopBody() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> Entering function."); }
+
+ const timePeriod = global.dailyFilePeriods[n][0];
+ const outputPeriodLabel = global.dailyFilePeriods[n][1];
+
+ let dependencyIndex = 0;
+ dataFiles = [];
+
+ dependencyLoopBody();
+
+ function dependencyLoopBody() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> Entering function."); }
+
+ let dependency = dataDependencies.config[dependencyIndex];
+ let storage = storages[dependencyIndex];
+
+ let previousFile;
+ let currentFile;
+
+ getPreviousFile();
+
+ function getPreviousFile() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> Entering function."); }
+
+ let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2);
+ let filePath = dependency.product + '/' + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath;
+ let fileName = market.assetA + '_' + market.assetB + ".json";
+
+ storage.getTextFile(filePath, fileName, onFileReceived, true);
+
+ function onFileReceived(err, text) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Entering function."); }
+ if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> text = " + text); }
+
+ if (err.message === "File does not exist." && botNeverRan === true) {
+
+ /*
+ 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 -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found.");
+
+ advanceTime();
+ return;
+ }
+
+
+ if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
+
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.message);
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> filePath = " + filePath);
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> fileName = " + fileName);
+ callBackFunction(err);
+ return;
+ }
+
+ previousFile = JSON.parse(text);
+
+ getCurrentFile();
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function getCurrentFile() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Entering function."); }
+
+ let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2);
+ let filePath = dependency.product + '/' + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath;
+ let fileName = market.assetA + '_' + market.assetB + ".json";
+
+ storage.getTextFile(filePath, fileName, onFileReceived, true);
+
+ function onFileReceived(err, text) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Entering function."); }
+ if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> text = " + text); }
+
+ if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
+
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.message);
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> filePath = " + filePath);
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> fileName = " + fileName);
+ callBackFunction(err);
+ return;
+ }
+
+ currentFile = JSON.parse(text);
+
+ let dataFile = previousFile.concat(currentFile);
+
+ dataFiles.push(dataFile);
+ dependencyControlLoop();
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoop -> dependencyLoopBody -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function dependencyControlLoop() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> dependencyControlLoop -> Entering function."); }
+
+ dependencyIndex++;
+
+ if (dependencyIndex < dataDependencies.config.length) {
+
+ dependencyLoopBody();
+
+ } else {
+
+ callTheBot();
+
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> dependencyControlLoop -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function callTheBot() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> callTheBot -> Entering function."); }
+
+ const timePeriod = global.dailyFilePeriods[n][0];
+ const outputPeriodLabel = global.dailyFilePeriods[n][1];
+
+ usertBot.start(dataFiles, timePeriod, outputPeriodLabel, currentDay, onBotFinished);
+
+ function onBotFinished(err) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 -> processTimePeriods -> periodsLoopBody -> callTheBot -> onBotFinished -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> callTheBot -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function periodsControlLoop() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 -> processTimePeriods -> controlLoop -> onWritten -> Entering function."); }
+
+ if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> controlLoop -> onWritten -> err = " + err.message);
+ callBackFunction(err);
+ return;
+ }
+
+ writeStatusReport(currentDay, advanceTime);
+
+ } catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> controlLoop -> onWritten -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsControlLoop -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function writeDataRanges(callBack) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRanges -> Entering function."); }
+
+ let productIndex = 0;
+ productLoopBody();
+
+ function productLoopBody() {
+
+ let folderName = bot.products[productIndex].codeName;
+
+ writeDataRange(contextVariables.dateBeginingOfMarket, currentDay, folderName, controlLoop);
+ }
+
+ function controlLoop() {
+
+ productIndex++;
+
+ if (productIndex < bot.products.length) {
+ productLoopBody();
+ } else {
+ callBack(global.DEFAULT_OK_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> writeDataRanges -> err = " + err.message);
+ callBack(global.DEFAULT_FAIL_RESPONSE);
+ }
+
+ }
+
+ function writeDataRange(pBegin, pEnd, pProductFolder, callBack) {
+
+ try {
+
+ 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.' + market.assetA + '_' + market.assetB + '.json';
+ let filePath = bot.filePathRoot + "/Output/" + pProductFolder + "/" + bot.process;
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> fileName = " + fileName); }
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> filePath = " + filePath); }
+
+ currentBotStorage.createTextFile(filePath, fileName, 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.message);
+ callBack(err);
+ return;
+ }
+
+ if (LOG_FILE_CONTENT === true) {
+ logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent);
+ }
+
+ callBack(global.DEFAULT_OK_RESPONSE);
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> err = " + err.message);
+ callBack(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function writeStatusReport(lastFileDate, callBack) {
+
+ 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); }
+
+ try {
+
+ let reportKey = bot.devTeam + "-" + bot.codeName + "-" + "Multi-Period-Daily" + "-" + "dataSet.V1";
+ let thisReport = statusDependencies.statusReports.get(reportKey);
+
+ thisReport.file.lastExecution = bot.currentDaytime;
+ thisReport.file.lastFile = lastFileDate;
+ thisReport.save(callBack);
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+};
diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js
new file mode 100644
index 000000000..f0e3ffccb
--- /dev/null
+++ b/MultiPeriodMarket.js
@@ -0,0 +1,311 @@
+exports.newMultiPeriodMarket = function newMultiPeriodMarket(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, USER_BOT_MODULE, COMMONS_MODULE) {
+
+ const FULL_LOG = true;
+ const LOG_FILE_CONTENT = false;
+
+ const MODULE_NAME = "Multi Period Market";
+
+ const EXCHANGE_NAME = "Poloniex";
+
+ const commons = COMMONS.newCommons(bot, logger, UTILITIES);
+
+ thisObject = {
+ initialize: initialize,
+ start: start
+ };
+
+ let utilities = UTILITIES.newCloudUtilities(bot, logger);
+
+ let statusDependencies;
+ let dataDependencies;
+ let storages = [];
+ let dataFiles = [];
+
+ let usertBot;
+
+ return thisObject;
+
+ function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) {
+
+ try {
+
+ logger.fileName = MODULE_NAME;
+ logger.initialize();
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
+
+ statusDependencies = pStatusDependencies;
+ dataDependencies = pDataDependencies;
+
+ for (let i = 0; i < dataDependencies.config.length; i++) {
+
+ let key;
+ let storage;
+ let dependency = dataDependencies.config[i];
+
+ key = dependency.devTeam + "-" +
+ dependency.bot + "-" +
+ dependency.product + "-" +
+ dependency.dataSet + "-" +
+ dependency.dataSetVersion
+
+ storage = dataDependencies.dataSets.get(key);
+
+ storages.push(storage);
+
+ }
+
+ usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, BLOB_STORAGE);
+ usertBot.initialize(dataDependencies, callBackFunction);
+
+ } catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function start(callBackFunction) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); }
+
+ let market = global.MARKET;
+
+ processTimePeriods();
+
+ function processTimePeriods() {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> Entering function."); }
+
+ try {
+
+ let n;
+
+ periodsLoop();
+
+ function periodsLoop() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 -> processTimePeriods -> periodsLoop -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function periodsLoopBody() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> Entering function."); }
+
+ const timePeriod = global.marketFilesPeriods[n][0];
+ const outputPeriodLabel = global.marketFilesPeriods[n][1];
+
+ let dependencyIndex = 0;
+ dataFiles = [];
+
+ dependencyLoopBody();
+
+ function dependencyLoopBody() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> Entering function."); }
+
+ let dependency = dataDependencies.config[dependencyIndex];
+ let storage = storages[dependencyIndex];
+
+ getFile();
+
+ function getFile() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> Entering function."); }
+
+ let fileName = market.assetA + '_' + market.assetB + ".json";
+
+ let filePath = dependency.product + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel;
+
+ storage.getTextFile(filePath, fileName, onFileReceived, true);
+
+ function onFileReceived(err, text) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); }
+ if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); }
+
+ if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
+
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message);
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> filePath = " + filePath);
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> fileName = " + fileName);
+ callBackFunction(err);
+ return;
+ }
+
+ let dataFile = JSON.parse(text);
+ dataFiles.push(dataFile);
+
+ dependencyControlLoop();
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getFile -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoop -> dependencyLoopBody -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function dependencyControlLoop() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> dependencyControlLoop -> Entering function."); }
+
+ dependencyIndex++;
+
+ if (dependencyIndex < dataDependencies.config.length) {
+
+ dependencyLoopBody();
+
+ } else {
+
+ callTheBot();
+
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> dependencyControlLoop -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function callTheBot() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> callTheBot -> Entering function."); }
+
+ const timePeriod = global.marketFilesPeriods[n][0];
+ const outputPeriodLabel = global.marketFilesPeriods[n][1];
+
+ usertBot.start(dataFiles, timePeriod, outputPeriodLabel, onBotFinished);
+
+ function onBotFinished(err) {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 -> processTimePeriods -> periodsLoopBody -> callTheBot -> onBotFinished -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch(err){
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> callTheBot -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsLoopBody -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function periodsControlLoop() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsControlLoop -> Entering function."); }
+
+ n++;
+
+ if (n < global.marketFilesPeriods.length) {
+
+ periodsLoopBody();
+
+ } else {
+
+ writeStatusReport(callBackFunction);
+
+ }
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> periodsControlLoop -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> processTimePeriods -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+
+ function writeStatusReport(callBack) {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> Entering function."); }
+
+ try {
+
+ let reportKey = bot.devTeam + "-" + bot.codeName + "-" + "Multi-Period-Market" + "-" + "dataSet.V1";
+ let thisReport = statusDependencies.statusReports.get(reportKey);
+
+ thisReport.file.lastExecution = bot.processDatetime;
+ thisReport.save(callBack);
+
+ }
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+ }
+
+ catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.message);
+ callBackFunction(global.DEFAULT_FAIL_RESPONSE);
+ }
+ }
+};
diff --git a/Project.njsproj b/Project.njsproj
index 9edd19db5..c9cb27126 100644
--- a/Project.njsproj
+++ b/Project.njsproj
@@ -33,6 +33,12 @@
Code
+
+ Code
+
+
+ Code
+
Code
diff --git a/StatusDependencies.js b/StatusDependencies.js
index 635efdf37..2ce421ef8 100644
--- a/StatusDependencies.js
+++ b/StatusDependencies.js
@@ -6,6 +6,7 @@
const MODULE_NAME = "Status Dependencies";
let thisObject = {
+ config: undefined,
statusReports: new Map(),
initialize: initialize,
keys: []
@@ -22,6 +23,8 @@
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
+ thisObject.config = pStatusDependenciesConfig;
+
/*
For each dependency declared at the bot config, we will initialize the status report, and load it as part of this initialization process.
diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js
index 85ac5cab1..9d6e6cab5 100644
--- a/TradingBotProcessMainLoop.js
+++ b/TradingBotProcessMainLoop.js
@@ -904,28 +904,12 @@
}
}
- async function saveContext() {
+ function saveContext() {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> saveContext -> Entering function."); }
- if (global.CURRENT_EXECUTION_AT === "Cloud") {
- // Update operations module with latest context
- let lastExecution = context.newHistoryRecord
- let date = lastExecution.date / 1000 | 0
- let buyAvgRate = lastExecution.buyAvgRate
- let sellAvgRate = lastExecution.sellAvgRate
- let marketRate = lastExecution.marketRate
- let combinedProfitsA = lastExecution.combinedProfitsA
- let combinedProfitsB = lastExecution.combinedProfitsB
-
- await operationsIntegration.updateExecutionResults(date, buyAvgRate, sellAvgRate,
- marketRate, combinedProfitsA, combinedProfitsB)
-
- if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> saveContext -> Execution Results updated."); }
- }
-
context.saveThemAll(onFinished);
function onFinished(err) {
@@ -938,7 +922,7 @@
case global.DEFAULT_OK_RESPONSE.result: {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> saveContext -> onFinished -> Execution finished well."); }
nextWaitTime = 'Normal';
- loopControl(nextWaitTime);
+ updateClonesModule();
return;
}
case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time.
@@ -997,6 +981,41 @@
}
}
+ async function updateClonesModule() {
+
+ try {
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> updateClonesModule -> Entering function."); }
+
+ if (global.CURRENT_EXECUTION_AT === "Cloud") {
+
+ // Update clones module with latest context informations
+
+ let lastExecution = context.newHistoryRecord
+ let date = lastExecution.date / 1000 | 0
+ let buyAvgRate = lastExecution.buyAvgRate
+ let sellAvgRate = lastExecution.sellAvgRate
+ let marketRate = lastExecution.marketRate
+ let combinedProfitsA = lastExecution.combinedProfitsA
+ let combinedProfitsB = lastExecution.combinedProfitsB
+
+ await operationsIntegration.updateExecutionResults(date, buyAvgRate, sellAvgRate,
+ marketRate, combinedProfitsA, combinedProfitsB)
+
+ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> updateClonesModule -> Clones Module Updated."); }
+ }
+
+ nextWaitTime = 'Normal';
+ loopControl(nextWaitTime);
+
+ } catch (err) {
+ logger.write(MODULE_NAME, "[ERROR] run -> loop -> updateClonesModule -> err = " + err.message);
+ logger.write(MODULE_NAME, "[ERROR] run -> loop -> updateClonesModule -> Execution will continue anyways. ");
+ nextWaitTime = 'Normal';
+ loopControl(nextWaitTime);
+ }
+ }
+
function loopControl(nextWaitTime) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); }