From 23903024d19328e385b310e523f10e1556362a61 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 27 Nov 2020 17:38:57 +0100 Subject: [PATCH] date time utilitiy functions --- Projects/ProjectsSchema.json | 6 +++ .../Multi-Period-Daily/User.Bot.js | 4 +- .../Multi-Period-Market/User.Bot.js | 4 +- .../Multi-Period-Daily/User.Bot.js | 4 +- .../Multi-Period-Market/User.Bot.js | 4 +- .../Historic-OHLCVs/User.Bot.js | 4 +- .../TS/Utilities/DateTimeFunctions.js | 38 +++++++++++++++++++ TaskServer/Globals.js | 26 ------------- .../LowFrequencyTrading/TradingProcess.js | 8 ++-- .../LowFrequencyTrading/TradingSimulation.js | 4 +- TaskServer/MultiPeriodDaily.js | 4 +- TaskServer/MultiPeriodMarket.js | 4 +- .../ReinforcementLearning/LearningProcess.js | 8 ++-- 13 files changed, 68 insertions(+), 50 deletions(-) create mode 100644 Projects/Superalgos/TS/Utilities/DateTimeFunctions.js diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index a591ad01b..7b86369ed 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -371,6 +371,12 @@ "propertyName": "nodeFunctions", "functionName": "newSuperalgosUtilitiesNodeFunctions", "fileName": "NodeFunctions.js" + }, + { + "name": "Date Time Functions", + "propertyName": "dateTimeFunctions", + "functionName": "newSuperalgosUtilitiesDateTimeFunctions", + "fileName": "DateTimeFunctions.js" } ], "botClasses": [] diff --git a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Daily/User.Bot.js b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Daily/User.Bot.js index 0c6919b2f..1d53c2675 100644 --- a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Daily/User.Bot.js +++ b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Daily/User.Bot.js @@ -281,10 +281,10 @@ /* Telling the world we are alive and doing well */ let currentDateString = processDate.getUTCFullYear() + '-' + utilities.pad(processDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(processDate.getUTCDate(), 2); let currentDate = new Date(processDate) - let percentage = global.GET_PERCENTAGE(fromDate, currentDate, lastDate) + let percentage = TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(fromDate, currentDate, lastDate) bot.processHeartBeat(currentDateString, percentage) - if (global.GET_EQUAL_DATES(currentDate, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(currentDate, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage); } periodsLoop(); diff --git a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Market/User.Bot.js b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Market/User.Bot.js index 930462866..b8de69056 100644 --- a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Market/User.Bot.js +++ b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Bollinger-Bands-Indicator-Bot/Multi-Period-Market/User.Bot.js @@ -471,7 +471,7 @@ thisReport.file.lastExecution = bot.processDatetime; thisReport.save(callBack); - if (global.GET_EQUAL_DATES(bot.processDatetime, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(bot.processDatetime, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, bot.processDatetime); } @@ -479,7 +479,7 @@ 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.GET_PERCENTAGE(currentDate, currentDate, lastDate)) + bot.processHeartBeat(currentDateString, TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(currentDate, currentDate, lastDate)) } catch (err) { logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); diff --git a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Daily/User.Bot.js b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Daily/User.Bot.js index c03521d77..3eda0af20 100644 --- a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Daily/User.Bot.js +++ b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Daily/User.Bot.js @@ -259,10 +259,10 @@ Read the candles and volumes from Exchange Raw Data and produce a file for each /* Telling the world we are alive and doing well */ let currentDateString = contextVariables.lastCandleFile.getUTCFullYear() + '-' + utilities.pad(contextVariables.lastCandleFile.getUTCMonth() + 1, 2) + '-' + utilities.pad(contextVariables.lastCandleFile.getUTCDate(), 2); let currentDate = new Date(contextVariables.lastCandleFile) - let percentage = global.GET_PERCENTAGE(fromDate, currentDate, lastDate) + let percentage = TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(fromDate, currentDate, lastDate) bot.processHeartBeat(currentDateString, percentage) - if (global.GET_EQUAL_DATES(currentDate, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(currentDate, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage); } /* diff --git a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Market/User.Bot.js b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Market/User.Bot.js index cd4a18117..4fa3a1944 100644 --- a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Market/User.Bot.js +++ b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Candles-Volumes-Indicator-Bot/Multi-Period-Market/User.Bot.js @@ -425,10 +425,10 @@ /* Telling the world we are alive and doing well */ let currentDateString = contextVariables.lastCandleFile.getUTCFullYear() + '-' + utilities.pad(contextVariables.lastCandleFile.getUTCMonth() + 1, 2) + '-' + utilities.pad(contextVariables.lastCandleFile.getUTCDate(), 2); let currentDate = new Date(contextVariables.lastCandleFile) - let percentage = global.GET_PERCENTAGE(fromDate, currentDate, lastDate) + let percentage = TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(fromDate, currentDate, lastDate) bot.processHeartBeat(currentDateString, percentage) - if (global.GET_EQUAL_DATES(currentDate, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(currentDate, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage); } diff --git a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Exchange-Raw-Data-Sensor-Bot/Historic-OHLCVs/User.Bot.js b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Exchange-Raw-Data-Sensor-Bot/Historic-OHLCVs/User.Bot.js index caabec2a7..02e8317ee 100644 --- a/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Exchange-Raw-Data-Sensor-Bot/Historic-OHLCVs/User.Bot.js +++ b/Projects/Superalgos/Bots-Plotters-Code/Masters/bots/Exchange-Raw-Data-Sensor-Bot/Historic-OHLCVs/User.Bot.js @@ -262,9 +262,9 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, FILE_S if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getOHLCVs -> Fetching OHLCVs @ " + processingDate + "-> exchange = " + bot.exchange + " -> symbol = " + symbol + " -> since = " + since + " -> limit = " + limit) } let heartBeatText = "Fetching " + allOHLCVs.length.toFixed(0) + " OHLCVs from " + bot.exchange + " " + symbol + " @ " + processingDate let currentDate = new Date(since) - let percentage = global.GET_PERCENTAGE(fromDate, currentDate, lastDate) + let percentage = TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(fromDate, currentDate, lastDate) bot.processHeartBeat(heartBeatText, percentage) // tell the world we are alive and doing well - if (global.GET_EQUAL_DATES(currentDate, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(currentDate, new Date()) === false) { if (noNewInternalLoop !== true) { logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage); } diff --git a/Projects/Superalgos/TS/Utilities/DateTimeFunctions.js b/Projects/Superalgos/TS/Utilities/DateTimeFunctions.js new file mode 100644 index 000000000..2bbc3e264 --- /dev/null +++ b/Projects/Superalgos/TS/Utilities/DateTimeFunctions.js @@ -0,0 +1,38 @@ +exports.newSuperalgosUtilitiesDateTimeFunctions = function () { + + let thisObject = { + removeTime: removeTime, + getPercentage: getPercentage, + areTheseDatesEqual: areTheseDatesEqual + } + + return thisObject + + function removeTime (datetime) { + const GMT_SECONDS = ':00.000 GMT+0000'; + let date = new Date(datetime) + return new Date(date.getUTCFullYear() + "-" + (date.getUTCMonth() + 1) + "-" + date.getUTCDate() + " " + "00:00" + GMT_SECONDS); + } + + function getPercentage(fromDate, currentDate, lastDate) { + let fromDays = Math.trunc(fromDate.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let currentDays = Math.trunc(currentDate.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let lastDays = Math.trunc(lastDate.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let percentage = (currentDays - fromDays) * 100 / (lastDays - fromDays) + if ((lastDays - fromDays) === 0) { + percentage = 100 + } + return percentage + } + + function areTheseDatesEqual(date1, date2) { + let day1Days = Math.trunc(date1.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let day2Days = Math.trunc(date2.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + + if (day1Days === day2Days) { + return true + } else { + return false + } + } +} \ No newline at end of file diff --git a/TaskServer/Globals.js b/TaskServer/Globals.js index 9470e09c6..944c67dff 100644 --- a/TaskServer/Globals.js +++ b/TaskServer/Globals.js @@ -27,12 +27,6 @@ exports.newGlobals = function newGlobals() { return this.parseFloat(floatNumber.toFixed(precision)) } - global.REMOVE_TIME = function (datetime) { - const GMT_SECONDS = ':00.000 GMT+0000'; - let date = new Date(datetime) - return new Date(date.getUTCFullYear() + "-" + (date.getUTCMonth() + 1) + "-" + date.getUTCDate() + " " + "00:00" + GMT_SECONDS); - } - global.FINALIZE_LOGGERS = function () { global.LOGGER_MAP.forEach(forEachLogger) @@ -91,26 +85,6 @@ exports.newGlobals = function newGlobals() { global.STOP_TASK_GRACEFULLY = false; - global.GET_PERCENTAGE = function (fromDate, currentDate, lastDate) { - let fromDays = Math.trunc(fromDate.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) - let currentDays = Math.trunc(currentDate.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) - let lastDays = Math.trunc(lastDate.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) - let percentage = (currentDays - fromDays) * 100 / (lastDays - fromDays) - if ((lastDays - fromDays) === 0) { - percentage = 100 - } - return percentage - } - global.GET_EQUAL_DATES = function (date1, date2) { - let day1Days = Math.trunc(date1.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) - let day2Days = Math.trunc(date2.valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_DAY_IN_MILISECONDS) - - if (day1Days === day2Days) { - return true - } else { - return false - } - } } } \ No newline at end of file diff --git a/TaskServer/LowFrequencyTrading/TradingProcess.js b/TaskServer/LowFrequencyTrading/TradingProcess.js index f29b0cc04..526d31616 100644 --- a/TaskServer/LowFrequencyTrading/TradingProcess.js +++ b/TaskServer/LowFrequencyTrading/TradingProcess.js @@ -103,7 +103,7 @@ This variable tell us which day we are standing at, specially while working with Daily Files. From this Date is that we are going to load the Daily Files. */ - bot.simulationState.tradingEngine.current.episode.processDate.value = global.REMOVE_TIME(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime).valueOf() + bot.simulationState.tradingEngine.current.episode.processDate.value = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime).valueOf() } /* @@ -115,7 +115,7 @@ the simulation loop, once we discover that all candles from a certain date have benn processed. Here is the point where we sync one and the other. */ - let tradingProcessDate = global.REMOVE_TIME(bot.simulationState.tradingEngine.current.episode.processDate.value) + let tradingProcessDate = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.simulationState.tradingEngine.current.episode.processDate.value) await processSingleFiles() if (await processMarketFiles() === false) { @@ -166,7 +166,7 @@ use it to save Output Files and later the Data Ranges. This is the point where the date calculated by the Simulation is applied at the Trading Process Level. */ - tradingProcessDate = global.REMOVE_TIME(bot.simulationState.tradingEngine.current.episode.processDate.value) + tradingProcessDate = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.simulationState.tradingEngine.current.episode.processDate.value) if (checkStopTaskGracefully() === false) { break } if (checkStopProcessing() === false) { break } @@ -236,7 +236,7 @@ let statusReport; /* We are going to use the start date as beging of market date. */ - contextVariables.dateBeginOfMarket = global.REMOVE_TIME(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime) + contextVariables.dateBeginOfMarket = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime) /* Here we get the status report from the bot who knows which is the end of the market. */ diff --git a/TaskServer/LowFrequencyTrading/TradingSimulation.js b/TaskServer/LowFrequencyTrading/TradingSimulation.js index 369f766b3..9b572867d 100644 --- a/TaskServer/LowFrequencyTrading/TradingSimulation.js +++ b/TaskServer/LowFrequencyTrading/TradingSimulation.js @@ -265,7 +265,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, tradin let currentDateString = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) let currentDate = new Date(heartBeatDate) - let percentage = global.GET_PERCENTAGE(fromDate, currentDate, lastDate) + let percentage = TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(fromDate, currentDate, lastDate) /* Theere are a few tasks that we need to do only when the date changes, @@ -279,7 +279,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, tradin if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.TRADING_SESSIONKey + ' Loop # ' + tradingEngine.current.episode.candle.index.value + ' @ ' + processingDate) } /* Logging to console and disk */ - if (global.GET_EQUAL_DATES(currentDate, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(currentDate, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) } diff --git a/TaskServer/MultiPeriodDaily.js b/TaskServer/MultiPeriodDaily.js index 461f73ef2..24824e889 100644 --- a/TaskServer/MultiPeriodDaily.js +++ b/TaskServer/MultiPeriodDaily.js @@ -248,10 +248,10 @@ /* 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.GET_PERCENTAGE(fromDate, currentDate, lastDate) + let percentage = TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(fromDate, currentDate, lastDate) bot.processHeartBeat(currentDateString, percentage) - if (global.GET_EQUAL_DATES(currentDate, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(currentDate, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) } checkStopTaskGracefully() diff --git a/TaskServer/MultiPeriodMarket.js b/TaskServer/MultiPeriodMarket.js index c016764cb..b64a35468 100644 --- a/TaskServer/MultiPeriodMarket.js +++ b/TaskServer/MultiPeriodMarket.js @@ -202,7 +202,7 @@ } thisReport.save(callBack) - if (global.GET_EQUAL_DATES(bot.processDatetime, new Date()) === false) { + if (TS.projects.superalgos.utilities.dateTimeFunctions.areTheseDatesEqual(bot.processDatetime, new Date()) === false) { logger.newInternalLoop(bot.codeName, bot.process, bot.processDatetime) } @@ -210,7 +210,7 @@ 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.GET_PERCENTAGE(currentDate, currentDate, lastDate)) + bot.processHeartBeat(currentDateString, TS.projects.superalgos.utilities.dateTimeFunctions.getPercentage(currentDate, currentDate, lastDate)) } } catch (err) { diff --git a/TaskServer/ReinforcementLearning/LearningProcess.js b/TaskServer/ReinforcementLearning/LearningProcess.js index 1bb5aad9b..fbeaf3146 100644 --- a/TaskServer/ReinforcementLearning/LearningProcess.js +++ b/TaskServer/ReinforcementLearning/LearningProcess.js @@ -103,7 +103,7 @@ exports.newLearningProcess = function newLearningProcess(bot, logger, UTILITIES) This variable tell us which day we are standing at, specially while working with Daily Files. From this Date is that we are going to load the Daily Files. */ - bot.simulationState.tradingEngine.current.episode.processDate.value = global.REMOVE_TIME(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime).valueOf() + bot.simulationState.tradingEngine.current.episode.processDate.value = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime).valueOf() } /* @@ -115,7 +115,7 @@ exports.newLearningProcess = function newLearningProcess(bot, logger, UTILITIES) the simulation loop, once we discover that all candles from a certain date have benn processed. Here is the point where we sync one and the other. */ - let LearningProcessDate = global.REMOVE_TIME(bot.simulationState.tradingEngine.current.episode.processDate.value) + let LearningProcessDate = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.simulationState.tradingEngine.current.episode.processDate.value) await processSingleFiles() if (await processMarketFiles() === false) { @@ -166,7 +166,7 @@ exports.newLearningProcess = function newLearningProcess(bot, logger, UTILITIES) use it to save Output Files and later the Data Ranges. This is the point where the date calculated by the Simulation is applied at the Trading Process Level. */ - LearningProcessDate = global.REMOVE_TIME(bot.simulationState.tradingEngine.current.episode.processDate.value) + LearningProcessDate = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.simulationState.tradingEngine.current.episode.processDate.value) if (checkStopTaskGracefully() === false) { break } if (checkStopProcessing() === false) { break } @@ -236,7 +236,7 @@ exports.newLearningProcess = function newLearningProcess(bot, logger, UTILITIES) let statusReport; /* We are going to use the start date as beging of market date. */ - contextVariables.dateBeginOfMarket = global.REMOVE_TIME(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime) + contextVariables.dateBeginOfMarket = TS.projects.superalgos.utilities.dateTimeFunctions.removeTime(bot.TRADING_SESSION.tradingParameters.timeRange.config.initialDatetime) /* Here we get the status report from the bot who knows which is the end of the market. */