From bfdb411601f255a886cbe58b35a40044ca2c3a5c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 22 Oct 2020 14:51:24 +0200 Subject: [PATCH] Update TradingPosition.js --- LowFrequencyTrading/TradingPosition.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index 327b0478c..1dcf49727 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -123,15 +123,38 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng } function applyStopLossFormula(formulas, formulaId) { + updateStopLossTakeProfitFinalValue(tradingEngine.current.position.stopLoss) tradingEngine.current.position.stopLoss.value = formulas.get(formulaId) + updateStopLossTakeProfitInitialValue(tradingEngine.current.position.stopLoss) updateStopLossTakeProfitBeginEnd(tradingEngine.current.position.stopLoss) } function applyTakeProfitFormula(formulas, formulaId) { + updateStopLossTakeProfitFinalValue(tradingEngine.current.position.takeProfit) tradingEngine.current.position.takeProfit.value = formulas.get(formulaId) + updateStopLossTakeProfitInitialValue(tradingEngine.current.position.takeProfit) updateStopLossTakeProfitBeginEnd(tradingEngine.current.position.takeProfit) } + function updateStopLossTakeProfitInitialValue(node) { + /* + We store the first Stop Loss or Take Profit value in a separate node. + To know if it is the first one we compare its current value to the initial value. + */ + if (node.initialValue.value === node.initialValue.config.initialValue) { + node.initialValue.value = node.value + } + } + + function updateStopLossTakeProfitFinalValue(node) { + /* + We set the final value just before calcualting the new value, since the new calculated + value is going to be discarded if the position is closed after it is calculated, but + during the same candle. + */ + node.finalValue.value = node.value + } + function updateStopLossTakeProfitBeginEnd(node) { /* Both the Stop Loss and the Take Profit have their own Begin and End.