From d4c840cfcf9a5adff8ecf461228e172260a4f65c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 30 May 2018 12:25:56 +0200 Subject: [PATCH] Backtest at the middle of each candle. --- TradingBotProcessMainLoop.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index d6a8bb631..73a9ea139 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -156,6 +156,18 @@ if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> Backtesting Mode detected."); } + let timePeriod; + + if (UI_COMMANDS.timePeriod !== undefined) { + + timePeriod = UI_COMMANDS.timePeriod; + + } else { + + timePeriod = processConfig.timePeriod; + + } + if (bot.hasTheBotJustStarted === true) { if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> Setting initial datetime."); } @@ -170,20 +182,17 @@ } + /* + We will standarize the backtesting execution time, setting the exact time at the middle of the candle or timePeriod. + */ + + let totalMiliseconds = bot.processDatetime.valueOf(); + totalMiliseconds = Math.trunc(totalMiliseconds / timePeriod) * timePeriod + timePeriod / 2; + + bot.processDatetime = new Date(totalMiliseconds); + } else { - let timePeriod; - - if (UI_COMMANDS.timePeriod !== undefined) { - - timePeriod = UI_COMMANDS.timePeriod; - - } else { - - timePeriod = processConfig.normalWaitTime; - - } - bot.processDatetime = new Date(bot.processDatetime.valueOf() + timePeriod); // We advance time here.