From d9b06694e29836803bebfb39faf6e43ac2f1a8c1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 2 Aug 2020 14:57:51 +0200 Subject: [PATCH] All coded --- LowFrequencyTrading/TradingSimulation.js | 62 ++++++++++++++---------- LowFrequencyTrading/TradingSystem.js | 54 +++++++++++++++++++-- Session.js | 12 ----- 3 files changed, 86 insertions(+), 42 deletions(-) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 4e8f655b2..5c2d65067 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -62,9 +62,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let heartBeatDate let previousHeartBeatDate - /* This determines where we are standing */ - bot.sessionAndMarketBaseAssetsAreEqual = (sessionParameters.sessionBaseAsset.referenceParent.referenceParent.config.codeName === bot.market.baseAsset) - initializeLoop() function initializeLoop() { @@ -363,36 +360,49 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if ( tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue ) { - /* - First thing to do is to check if we are below the minimum balance, or above the maximun balance. - If we are, we are not gooing to trigger on again. + /* + We will perform this check only when we are not inside a position, + because there the balances have shifted from their resting position. */ - let minimumBalance - let maximumBalance - let balance - if (bot.sessionAndMarketBaseAssetsAreEqual) { - balance = tradingEngine.current.balance.baseAsset.value - minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance - maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance - } else { - balance = tradingEngine.current.balance.quotedAsset.value - minimumBalance = sessionParameters.sessionQuotedAsset.config.minimumBalance - maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance + let stopRunningDate = (new Date(tradingEngine.current.candle.begin.value)).toLocaleString() + + if (sessionParameters.sessionBaseAsset.config.minimumBalance !== undefined) { + if (tradingEngine.current.balance.baseAsset.value <= sessionParameters.sessionBaseAsset.config.minimumBalance) { + const errorMessage = 'Min Balance reached @ ' + stopRunningDate + tradingSystem.errors.push([tradingSystem.id, errorMessage]) + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> checkMinimunAndMaximunBalance -> ' + errorMessage) } + return false + } } - let stopRunningDate = new Date(tradingEngine.current.candle.begin.value) - if (balance <= minimumBalance) { - tradingSystem.error = 'Min Balance @ ' + stopRunningDate.toLocaleString() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } - return false + if (sessionParameters.sessionBaseAsset.config.maximumBalance !== undefined) { + if (tradingEngine.current.balance.baseAsset.value >= sessionParameters.sessionBaseAsset.config.maximumBalance) { + const errorMessage = 'Max Balance reached @ ' + stopRunningDate + tradingSystem.errors.push([tradingSystem.id, errorMessage]) + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> checkMinimunAndMaximunBalance -> ' + errorMessage) } + return false + } } - if (balance >= maximumBalance) { - tradingSystem.error = 'Max Balance @ ' + stopRunningDate.toLocaleString() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } - return false + if (sessionParameters.sessionQuotedAsset.config.minimumBalance !== undefined) { + if (tradingEngine.current.balance.quotedAsset.value <= sessionParameters.sessionQuotedAsset.config.minimumBalance) { + const errorMessage = 'Min Balance reached @ ' + stopRunningDate + tradingSystem.errors.push([tradingSystem.id, errorMessage]) + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> checkMinimunAndMaximunBalance -> ' + errorMessage) } + return false + } } + + if (sessionParameters.sessionQuotedAsset.config.maximumBalance !== undefined) { + if (tradingEngine.current.balance.quotedAsset.value >= sessionParameters.sessionQuotedAsset.config.maximumBalance) { + const errorMessage = 'Max Balance reached @ ' + stopRunningDate + tradingSystem.errors.push([tradingSystem.id, errorMessage]) + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> checkMinimunAndMaximunBalance -> ' + errorMessage) } + return false + } + } + } return true } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 45c6a0d11..b28a7e40c 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -664,15 +664,49 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Stop Loss Management */ checkStopPhasesEvents() calculateStopLoss() + calculateStopLossPosition() /* Take Profit Management */ checkTakeProfitPhaseEvents() calculateTakeProfit() + calculateTakeProfitPosition() /* Checking if Stop or Take Profit were hit */ checkStopLossOrTakeProfitWasHit() } + function calculateTakeProfitPosition() { + /* + The position of the Stop Loss (above or below the price) is needed in order to know + later if the price hit the Stop Loss or not at every Simulation cycle. When we get + the first values of the simulation of the Stop Loss we check if it is above or below + the Position Rate, and we assign the values Above or Below to it. + */ + if (tradingEngine.current.position.stopLoss.stopLossPosition.value === tradingEngine.current.position.stopLoss.stopLossPosition.config.initialValue) { + if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.position.rate) { + tradingEngine.current.position.stopLoss.stopLossPosition.value = 'Above' + } else { + tradingEngine.current.position.stopLoss.stopLossPosition.value = 'Below' + } + } + } + + function calculateTakeProfitPosition() { + /* + The position of the Take Profit (above or below the price) is needed in order to know + later if the price hit the Take Profit or not at every Simulation cycle. When we get + the first values of the simulation of the Take Profits we check if it is above or below + the Position Rates, and we assign the values Above or Below to it. + */ + if (tradingEngine.current.position.takeProfit.takeProfitPosition.value === tradingEngine.current.position.takeProfit.takeProfitPosition.config.initialValue) { + if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.position.rate) { + tradingEngine.current.position.takeProfit.takeProfitPosition.value = 'Above' + } else { + tradingEngine.current.position.takeProfit.takeProfitPosition.value = 'Below' + } + } + } + function checkStopPhasesEvents() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage @@ -897,8 +931,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ if ( - (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || - (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) + ( + tradingEngine.current.position.stopLoss.stopLossPosition.value === 'Above' && + tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value + ) || + ( + tradingEngine.current.position.stopLoss.stopLossPosition.value === 'Below' && + tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value + ) ) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') @@ -911,8 +951,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ if ( - (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || - (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) + ( + tradingEngine.current.position.takeProfit.takeProfitPosition.value === 'Below' && + tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value + ) || + ( + tradingEngine.current.position.takeProfit.takeProfitPosition.value === 'Above' && + tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value + ) ) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') diff --git a/Session.js b/Session.js index 056d01a56..6f627e326 100644 --- a/Session.js +++ b/Session.js @@ -229,12 +229,6 @@ bot.sessionError(bot.SESSION.parameters.sessionBaseAsset, errorMessage) return false } - if (bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance === undefined) { - bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = 0 - } - if (bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance === undefined) { - bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER - } /* Session Quoted Asset */ if (bot.SESSION.parameters.sessionQuotedAsset === undefined) { @@ -249,12 +243,6 @@ bot.sessionError(bot.SESSION.parameters.sessionQuotedAsset, errorMessage) return false } - if (bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance === undefined) { - bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = 0 - } - if (bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance === undefined) { - bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER - } /* Slippage */ if (bot.SESSION.parameters.slippage === undefined) { // if the Slippage is missing we create a default one.