From 860bcfc39eee81f69ce4cbcc2536c9ec2b38eba4 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 3 Apr 2020 11:10:33 +0200 Subject: [PATCH] Bug Fixed with max balance --- Session.js | 24 +++++--- TradingSimulation.js | 137 +++++++++++++++++++++---------------------- 2 files changed, 84 insertions(+), 77 deletions(-) diff --git a/Session.js b/Session.js index 8f341ca3c..0d6685f89 100644 --- a/Session.js +++ b/Session.js @@ -187,13 +187,6 @@ /* Set all default values */ bot.VALUES_TO_USE = { - baseAsset: "BTC", - initialBalanceA: 0.001, - initialBalanceB: 0, - minimumBalanceA: 0.0005, - minimumBalanceB: 0, - maximumBalanceA: 0.002, - maximumBalanceB: 0, slippage: { positionRate: 0, stopLoss: 0, @@ -283,6 +276,23 @@ 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 + } } } } diff --git a/TradingSimulation.js b/TradingSimulation.js index 2356e2d0b..471ae162d 100644 --- a/TradingSimulation.js +++ b/TradingSimulation.js @@ -1114,104 +1114,101 @@ minimumBalance = bot.VALUES_TO_USE.minimumBalanceB maximumBalance = bot.VALUES_TO_USE.maximumBalanceB } - - if (minimumBalance === undefined) { - minimumBalance = 0 + + 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 (maximumBalance === undefined) { - maximumBalance = 10000000000000000 + if (balance >= maximumBalance) { + tradingSystem.error = "Max Balance @ " + stopRunningDate.toLocaleString() + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> " + tradingSystem.error); } + afterLoop() + return } - if (balance > minimumBalance && balance < maximumBalance) { - - /* - 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. + /* + 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. + 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++) { + for (let j = 0; j < tradingSystem.strategies.length; j++) { - let strategy = tradingSystem.strategies[j]; + let strategy = tradingSystem.strategies[j]; - let triggerStage = strategy.triggerStage + let triggerStage = strategy.triggerStage - if (triggerStage !== undefined) { + if (triggerStage !== undefined) { - if (triggerStage.triggerOn !== undefined) { + if (triggerStage.triggerOn !== undefined) { - for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { + for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { - let situation = triggerStage.triggerOn.situations[k]; - let passed = true; + let situation = triggerStage.triggerOn.situations[k]; + let passed = true; - for (let m = 0; m < situation.conditions.length; m++) { + for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m]; - let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + 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; } + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; } - if (passed) { + if (value === false) { passed = false; } + } - strategyStage = 'Trigger Stage'; - checkAnnouncements(triggerStage) + if (passed) { - currentStrategyIndex = j; - currentStrategy.begin = candle.begin; - currentStrategy.beginRate = candle.min; - currentStrategy.endRate = candle.min; // In case the strategy does not get exited - currentStrategy.triggerOnSituation = situation.name + strategyStage = 'Trigger Stage'; + checkAnnouncements(triggerStage) - if (processingDailyFiles) { - if (positionedAtYesterday) { - yesterday.strategyStage = strategyStage; - yesterday.currentStrategyIndex = currentStrategyIndex; - yesterday.currentStrategy.begin = currentStrategy.begin; - yesterday.currentStrategy.beginRate = currentStrategy.beginRate; - yesterday.currentStrategy.endRate = currentStrategy.endRate; - yesterday.currentStrategy.triggerOnSituation = currentStrategy.triggerOnSituation; - } + currentStrategyIndex = j; + currentStrategy.begin = candle.begin; + currentStrategy.beginRate = candle.min; + currentStrategy.endRate = candle.min; // In case the strategy does not get exited + currentStrategy.triggerOnSituation = situation.name + + if (processingDailyFiles) { + if (positionedAtYesterday) { + yesterday.strategyStage = strategyStage; + yesterday.currentStrategyIndex = currentStrategyIndex; + yesterday.currentStrategy.begin = currentStrategy.begin; + yesterday.currentStrategy.beginRate = currentStrategy.beginRate; + yesterday.currentStrategy.endRate = currentStrategy.endRate; + yesterday.currentStrategy.triggerOnSituation = currentStrategy.triggerOnSituation; } - - distanceToLast.triggerOn = 1; - - if (processingDailyFiles) { - if (positionedAtYesterday) { - yesterday.distanceToLast.triggerOn = distanceToLast.triggerOn - } - } - - checkAnnouncements(triggerStage.triggerOn) - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> loop -> Switching to Trigger Stage because conditions at Trigger On Event were met."); } - break; } + + distanceToLast.triggerOn = 1; + + if (processingDailyFiles) { + if (positionedAtYesterday) { + yesterday.distanceToLast.triggerOn = distanceToLast.triggerOn + } + } + + checkAnnouncements(triggerStage.triggerOn) + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> loop -> Switching to Trigger Stage because conditions at Trigger On Event were met."); } + break; } } } } - } else { - let stopRunningDate = new Date(candle.begin) - if (balance <= minimumBalance) { - tradingSystem.error = "Min Balance @ " + stopRunningDate.toLocaleString() - } - if (balance >= maximumBalance) { - tradingSystem.error = "Max Balance @ " + stopRunningDate.toLocaleString() - } } + } /* Trigger Off Condition */