From 231183802351538f101c7d4cee9b4bef1ddc4aa3 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 17 Oct 2019 17:28:25 +0200 Subject: [PATCH] Forward testing and paper trading --- MultiPeriod.js | 11 +++++++ Session.js | 80 +++++++++++++++++++++++++++++++++++++++----------- server.js | 2 +- 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index f3c183383..d4e8325bc 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -160,6 +160,17 @@ 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. */ diff --git a/Session.js b/Session.js index f91607445..cf857dcbe 100644 --- a/Session.js +++ b/Session.js @@ -36,12 +36,12 @@ /* Listen to event to start or stop the session. */ bot.sessionKey = bot.processNode.session.name + '-' + bot.processNode.session.type + '-' + bot.processNode.session.id - + global.SYSTEM_EVENT_HANDLER.listenToEvent(bot.sessionKey, 'Run Session', undefined, undefined, undefined, runSession) global.SYSTEM_EVENT_HANDLER.listenToEvent(bot.sessionKey, 'Stop Session', undefined, undefined, undefined, stopSession) function runSession(message) { - + console.log("runSession") /* We are going to run the Definition comming at the event. */ bot.DEFINITION = JSON.parse(message.event.definition) bot.SESSION = JSON.parse(message.event.session) @@ -73,6 +73,14 @@ startLiveTrading(message) break } + case 'Fordward Testing Session': { + startFordwardTesting(message) + break + } + case 'Paper Trading Session': { + startPaperTrading(message) + break + } } bot.SESSION_STATUS = 'Idle' bot.STOP_SESSION = false @@ -84,13 +92,65 @@ 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 = bot.VALUES_TO_USE.timeRange.initialDatetime + bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) } function startLiveTrading(message) { + setKeyToUse() + + 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."); } + return + } + + bot.startMode = "Live" + if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { + bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() + } + bot.resumeExecution = false; + bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.hasTheBotJustStarted = true + + } + + function startFordwardTesting(message) { + + setKeyToUse() + + 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 Forward Testing mode."); } + return + } + + bot.startMode = "Live" + if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { + bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() + } + bot.resumeExecution = false; + bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.hasTheBotJustStarted = true + + } + + function startPaperTrading(message) { + bot.startMode = "Backtest" + console.log("startPaperTrading") + if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { + bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() + } + bot.resumeExecution = false; + bot.hasTheBotJustStarted = true + bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + + } + + function setKeyToUse() { /* The last place where we can find a key to use is the key pool at the exchange account. */ if (bot.DEFINITION.personalData) { if (bot.DEFINITION.personalData.exchangeAccounts) { @@ -130,20 +190,6 @@ process.env.SECRET = key.code } } - - 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."); } - return - } - - bot.startMode = "Live" - if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf) { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - } - bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = bot.VALUES_TO_USE.timeRange.initialDatetime - bot.hasTheBotJustStarted = true - } function setValuesToUse(message) { diff --git a/server.js b/server.js index 6490aef23..184f140c5 100644 --- a/server.js +++ b/server.js @@ -212,7 +212,7 @@ function bootLoader() { } if (global.TASK_NODE.bot.processes[processIndex].code.process === undefined) { - console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'process' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[processIndex].name); + console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'process' at object Process is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[processIndex].name); continue }