diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index 1dbc6cc98..fc1cebcfe 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -60,11 +60,9 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng tradingEngine.current.position.positionQuotedAsset.beginBalance.value = tradingEngine.current.episode.episodeQuotedAsset.balance.value tradingEngine.current.position.situationName.value = situationName - /* Initializing Stop and Take Profit Stage / Phase */ - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Open Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Open Stage' - tradingEngine.current.position.stopLoss.stopLossPhase.value = 0 - tradingEngine.current.position.takeProfit.takeProfitPhase.value = 0 + /* Initializing Stop and Take Phase */ + tradingEngine.current.position.stopLoss.stopLossPhase.value = 1 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = 1 /* Updating Episode Counters */ tradingEngine.current.episode.episodeCounters.positions.value++ diff --git a/LowFrequencyTrading/TradingStages.js b/LowFrequencyTrading/TradingStages.js index 7961ac29b..b2a598110 100644 --- a/LowFrequencyTrading/TradingStages.js +++ b/LowFrequencyTrading/TradingStages.js @@ -259,116 +259,118 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM } async function runOpenStage() { - /* Abort Open Stage Check */ - if (tradingEngine.current.strategyOpenStage.status.value === 'Open' && tradingEngine.current.strategyCloseStage.status.value === 'Open') { - /* - if the Close stage is opened while the open stage is still open that means that - we need to stop placing orders, check what happened to the orders already placed, - and cancel all unfilled limit orders. We call this the Closing status of the Open Stage. - */ - changeStageStatus('Open Stage', 'Closing') + + checkIfWeNeedToAbortTheStage() + runWhenStatusIsOpening() + await runWhenStatusIsOpen() + await runWhenStatusIsClosing() + + function checkIfWeNeedToAbortTheStage() { + /* Abort Open Stage Check */ + if (tradingEngine.current.strategyOpenStage.status.value === 'Open' && tradingEngine.current.strategyCloseStage.status.value === 'Open') { + /* + if the Close stage is opened while the open stage is still open that means that + we need to stop placing orders, check what happened to the orders already placed, + and cancel all unfilled limit orders. We call this the Closing status of the Open Stage. + */ + changeStageStatus('Open Stage', 'Closing') + } } - /* Opening Status Procedure */ - if (tradingEngine.current.strategyOpenStage.status.value === 'Opening') { + function runWhenStatusIsOpening() { + /* Opening Status Procedure */ + if (tradingEngine.current.strategyOpenStage.status.value === 'Opening') { - /* This procedure is intended to run only once */ - let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage - let executionNode = tradingSystemStage.openExecution + /* This procedure is intended to run only once */ + let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let tradingEngineStage = tradingEngine.current.strategyOpenStage - /* Reset the Exchange Orders data structure to its initial value */ - tradingEngineModule.initializeNode(tradingEngine.exchangeOrders) + /* Reset the Exchange Orders data structure to its initial value */ + tradingEngineModule.initializeNode(tradingEngine.exchangeOrders) - tradingSystem.evalConditions(tradingSystemStage, 'Initial Definition') - tradingSystem.evalFormulas(tradingSystemStage, 'Initial Definition') + /* Entry Position size and rate */ + tradingSystem.evalFormulas(tradingSystemStage, 'Initial Targets') + tradingPositionModule.initialTargets(tradingSystemStage, tradingEngineStage) + initializeStageTargetSize() - /* Entry Position size and rate */ - tradingSystem.evalFormulas(tradingSystemStage, 'Initial Targets') - tradingPositionModule.initialTargets(tradingSystemStage, tradingEngine.current.strategyOpenStage) - initializeStageTargetSize() - - /* Check Execution at open stage node */ - tradingSystem.evalConditions(tradingSystemStage, 'Open Execution') - tradingSystem.evalFormulas(tradingSystemStage, 'Open Execution') - - await tradingExecutionModule.runExecution( - executionNode, - tradingEngine.current.strategyOpenStage - ) - - /* From here on, the state is officialy Open */ - changeStageStatus('Open Stage', 'Open') - return + /* From here on, the stage is officialy Open */ + changeStageStatus('Open Stage', 'Open') + } } - /* Open Status Procedure */ - if (tradingEngine.current.strategyOpenStage.status.value === 'Open') { - /* - While the Open Stage is Open, we do our regular stuff: place orders and check - what happened to the orders already placed. - */ - let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage - let executionNode = tradingSystemStage.openExecution + async function runWhenStatusIsOpen() { + /* Open Status Procedure */ + if (tradingEngine.current.strategyOpenStage.status.value === 'Open') { + /* + While the Open Stage is Open, we do our regular stuff: place orders and check + what happened to the orders already placed. + */ + let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let tradingEngineStage = tradingEngine.current.strategyOpenStage + let executionNode = tradingSystemStage.openExecution - /* Every Loop Cycle Activity */ - tradingSystem.evalConditions(tradingSystemStage, 'Open Execution') - tradingSystem.evalFormulas(tradingSystemStage, 'Open Execution') + /* Evaluate conditions and formulas so they are ready during the execution run */ + tradingSystem.evalConditions(executionNode, 'Open Execution') + tradingSystem.evalFormulas(executionNode, 'Open Execution') - await tradingExecutionModule.runExecution( - executionNode, - tradingEngine.current.strategyOpenStage - ) - checkIfStageNeedsToBeClosed(tradingEngine.current.strategyOpenStage, tradingSystemStage, 'Open Stage') + await tradingExecutionModule.runExecution( + executionNode, + tradingEngineStage + ) + checkIfStageNeedsToBeClosed(tradingEngineStage, tradingSystemStage, 'Open Stage') + } } - /* Closing Status Procedure */ - if (tradingEngine.current.strategyOpenStage.status.value === 'Closing') { - /* - During the closing stage status, we do not place new orders, just check - if the ones placed were filled, and cancel the ones not filled. - */ - let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage - let executionNode = tradingSystemStage.openExecution + async function runWhenStatusIsClosing() { + /* Closing Status Procedure */ + if (tradingEngine.current.strategyOpenStage.status.value === 'Closing') { + /* + During the closing stage status, we do not place new orders, just check + if the ones placed were filled, and cancel the ones not filled. + */ + let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = tradingSystemStage.openExecution - /* - Check if there are unfilled orders, we will check if they were executed, - and cancel the ones that were not. - */ - tradingSystem.evalConditions(tradingSystemStage, 'Open Execution') - tradingSystem.evalFormulas(tradingSystemStage, 'Open Execution') + /* + Check if there are unfilled orders, we will check if they were executed, + and cancel the ones that were not. + */ + tradingSystem.evalConditions(tradingSystemStage, 'Open Execution') + tradingSystem.evalFormulas(tradingSystemStage, 'Open Execution') - await tradingExecutionModule.runExecution( - executionNode, - tradingEngine.current.strategyOpenStage - ) + await tradingExecutionModule.runExecution( + executionNode, + tradingEngine.current.strategyOpenStage + ) - /* - The Closing is finished when the sizeFilled + feesPaid reaches the sizePlaced. - This can happens either because we update the sizeFilled value when we see - at the exchange that orders were filled, or we reduce the sizePlaced when - we cancel not yet filled orders. Here it does not matter the targetSize since - this status represent a force closure of this stage. - */ - switch (tradingEngine.current.strategyOpenStage.stageDefinedIn.value) { - case 'Base Asset': { - if ( - tradingEngine.current.strategyOpenStage.stageBaseAsset.sizeFilled.value + - tradingEngine.current.strategyOpenStage.stageBaseAsset.feesPaid.value >= - tradingEngine.current.strategyOpenStage.stageBaseAsset.sizePlaced.value - ) { - changeStageStatus('Open Stage', 'Closed') + /* + The Closing is finished when the sizeFilled + feesPaid reaches the sizePlaced. + This can happens either because we update the sizeFilled value when we see + at the exchange that orders were filled, or we reduce the sizePlaced when + we cancel not yet filled orders. Here it does not matter the targetSize since + this status represent a force closure of this stage. + */ + switch (tradingEngine.current.strategyOpenStage.stageDefinedIn.value) { + case 'Base Asset': { + if ( + tradingEngine.current.strategyOpenStage.stageBaseAsset.sizeFilled.value + + tradingEngine.current.strategyOpenStage.stageBaseAsset.feesPaid.value >= + tradingEngine.current.strategyOpenStage.stageBaseAsset.sizePlaced.value + ) { + changeStageStatus('Open Stage', 'Closed') + } + break } - break - } - case 'Quoted Asset': { - if ( - tradingEngine.current.strategyOpenStage.stageQuotedAsset.sizeFilled.value + - tradingEngine.current.strategyOpenStage.stageQuotedAsset.feesPaid.value >= - tradingEngine.current.strategyOpenStage.stageQuotedAsset.sizePlaced.value - ) { - changeStageStatus('Open Stage', 'Closed') + case 'Quoted Asset': { + if ( + tradingEngine.current.strategyOpenStage.stageQuotedAsset.sizeFilled.value + + tradingEngine.current.strategyOpenStage.stageQuotedAsset.feesPaid.value >= + tradingEngine.current.strategyOpenStage.stageQuotedAsset.sizePlaced.value + ) { + changeStageStatus('Open Stage', 'Closed') + } + break } - break } } } @@ -386,57 +388,50 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM } function runManageStage() { - /* Opening Status Procedure */ - if (tradingEngine.current.strategyManageStage.status.value === 'Opening') { - /* We jump the management at the loop cycle where the position is being taken. */ - changeStageStatus('Manage Stage', 'Open') - return + + runWhenStatusIsOpening() + runWhenStatusIsOpen() + + function runWhenStatusIsOpening() { + /* Opening Status Procedure */ + if (tradingEngine.current.strategyManageStage.status.value === 'Opening') { + + /* Now we switch to the Open status. */ + changeStageStatus('Manage Stage', 'Open') + } } - /* Open Status Procedure */ - if (tradingEngine.current.strategyManageStage.status.value === 'Open') { - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let manageStage = strategy.manageStage - tradingSystem.evalConditions(manageStage, 'Manage Stage') - tradingSystem.evalFormulas(manageStage, 'Manage Stage') + function runWhenStatusIsOpen() { + /* Open Status Procedure */ + if (tradingEngine.current.strategyManageStage.status.value === 'Open') { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let manageStage = strategy.manageStage - if ( - tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' || - tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage') { - let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage - tradingSystem.evalConditions(tradingSystemStage, 'Initial Definition') - tradingSystem.evalFormulas(tradingSystemStage, 'Initial Definition') + /* Evaluate all the stage conditions and formulas to have them ready */ + tradingSystem.evalConditions(manageStage, 'Manage Stage') + tradingSystem.evalFormulas(manageStage, 'Manage Stage') + + /* Stop Loss Management */ + checkStopPhasesEvents() + calculateStopLoss() + calculateStopLossPosition() + + /* Take Profit Management */ + checkTakeProfitPhaseEvents() + calculateTakeProfit() + calculateTakeProfitPosition() + + /* Checking if Stop or Take Profit were hit */ + checkStopLossOrTakeProfitWasHit() } - - /* Stop Loss Management */ - checkStopPhasesEvents() - calculateStopLoss() - calculateStopLossPosition() - - /* Take Profit Management */ - checkTakeProfitPhaseEvents() - calculateTakeProfit() - calculateTakeProfitPosition() - - /* Checking if Stop or Take Profit were hit */ - checkStopLossOrTakeProfitWasHit() } function calculateStopLoss() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage let manageStage = strategy.manageStage let phase - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.initialStopLoss !== undefined) { - phase = openStage.initialDefinition.initialStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] - } - } - } - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage !== undefined) { if (manageStage.managedStopLoss !== undefined) { phase = manageStage.managedStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] } @@ -456,19 +451,10 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM function calculateTakeProfit() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage let manageStage = strategy.manageStage let phase - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.initialTakeProfit !== undefined) { - phase = openStage.initialDefinition.initialTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] - } - } - } - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage !== undefined) { if (manageStage.managedTakeProfit !== undefined) { phase = manageStage.managedTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] } @@ -489,7 +475,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM function calculateStopLossPosition() { /* 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 + later if the price hit the Stop Loss or not at every Simulation candle. 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. */ @@ -505,7 +491,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM 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 + later if the price hit the Take Profit or not at every Simulation candle. 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. */ @@ -520,28 +506,13 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM function checkStopPhasesEvents() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode let phaseIndex let phase let stopLoss - let stage - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - stage = openStage - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.initialStopLoss !== undefined) { - parentNode = openStage.initialDefinition - phaseIndex = tradingEngine.current.position.stopLoss.stopLossPhase.value - stopLoss = openStage.initialDefinition.initialStopLoss - phase = stopLoss.phases[phaseIndex] - } - } - } - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - stage = manageStage + if (manageStage !== undefined) { if (manageStage.managedStopLoss !== undefined) { parentNode = manageStage phaseIndex = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 @@ -572,7 +543,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM tradingSystem.highlights.push(phase.id) tradingSystem.highlights.push(stopLoss.id) tradingSystem.highlights.push(parentNode.id) - tradingSystem.highlights.push(stage.id) + tradingSystem.highlights.push(manageStage.id) tradingPositionModule.updateStopLoss(tradingEngine.current.position.stopLoss.stopLossPhase.value + 1, 'Manage Stage') @@ -602,7 +573,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM tradingSystem.highlights.push(phase.id) tradingSystem.highlights.push(stopLoss.id) tradingSystem.highlights.push(parentNode.id) - tradingSystem.highlights.push(stage.id) + tradingSystem.highlights.push(manageStage.id) let moveToPhase = moveToPhaseEvent.referenceParent if (moveToPhase !== undefined) { @@ -633,22 +604,8 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM let phaseIndex let phase let takeProfit - let stage - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - stage = openStage - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.initialTakeProfit !== undefined) { - parentNode = openStage.initialDefinition - phaseIndex = tradingEngine.current.position.takeProfit.takeProfitPhase.value - takeProfit = openStage.initialDefinition.initialTakeProfit - phase = takeProfit.phases[phaseIndex] - } - } - } - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - stage = manageStage + if (manageStage !== undefined) { if (manageStage.managedTakeProfit !== undefined) { parentNode = manageStage phaseIndex = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 @@ -679,7 +636,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM tradingSystem.highlights.push(phase.id) tradingSystem.highlights.push(takeProfit.id) tradingSystem.highlights.push(parentNode.id) - tradingSystem.highlights.push(stage.id) + tradingSystem.highlights.push(manageStage.id) tradingPositionModule.updateTakeProfit(tradingEngine.current.position.takeProfit.takeProfitPhase.value + 1, 'Manage Stage') @@ -709,7 +666,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM tradingSystem.highlights.push(phase.id) tradingSystem.highlights.push(takeProfit.id) tradingSystem.highlights.push(parentNode.id) - tradingSystem.highlights.push(stage.id) + tradingSystem.highlights.push(manageStage.id) let moveToPhase = moveToPhaseEvent.referenceParent if (moveToPhase !== undefined) { @@ -785,68 +742,81 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM } async function runCloseStage() { - /* Opening Status Procedure */ - if (tradingEngine.current.strategyCloseStage.status.value === 'Opening') { + + runWhenStatusIsOpening() + await runWhenStatusIsOpen() + + function runWhenStatusIsOpening() { + /* Opening Status Procedure */ + if (tradingEngine.current.strategyCloseStage.status.value === 'Opening') { + /* + This will happen only once, as soon as the Take Profit or Stop was hit. + */ + let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + + /* Exit Position size and rate */ + tradingSystem.evalFormulas(tradingSystemStage, 'Initial Targets') + tradingPositionModule.initialTargets(tradingSystemStage, tradingEngine.current.strategyCloseStage) + + initializeStageTargetSize() + changeStageStatus('Close Stage', 'Open') + } + } + + async function runWhenStatusIsOpen() { /* - This will happen only once, as soon as the Take Profit or Stop was hit. - We wil not be placing orders at this time because we do not know - the total filled size of the Open Stage. By skiping execution now - we allow the open stage to get a final value for sizeFilled that we can use. + The following code will run only when the Open Stage is closed. + The reason for that is that we need the final filled size of the Open Stage + to be known before we start placing orders at the Close Stage. */ - let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + if (tradingEngine.current.strategyCloseStage.status.value === 'Open' && tradingEngine.current.strategyOpenStage.status.value === 'Closed') { + /* + This will happen as long as the Close Stage is Open. + */ + let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + let tradingEngineStage = tradingEngine.current.strategyCloseStage + let executionNode = tradingSystemStage.closeExecution - /* Exit Position size and rate */ - tradingSystem.evalFormulas(tradingSystemStage, 'Initial Targets') - tradingPositionModule.initialTargets(tradingSystemStage, tradingEngine.current.strategyCloseStage) + tradingSystem.evalConditions(tradingSystemStage, 'Close Execution') + tradingSystem.evalFormulas(tradingSystemStage, 'Close Execution') - initializeStageTargetSize() - changeStageStatus('Close Stage', 'Open') - return - } + await tradingExecutionModule.runExecution( + executionNode, + tradingEngine.current.strategyCloseStage + ) - /* Open Status Procedure */ - if (tradingEngine.current.strategyCloseStage.status.value === 'Open') { - /* - This will happen as long as the Close Stage is Open. + checkIfStageNeedsToBeClosed(tradingEngineStage, tradingSystemStage, 'Close Stage') + } + + /* + Exit Position Validation: we need al the previous stages to be either closed + or at their initial default value. The last is becasue after Closed objects are + initialized to their defualts. */ - let tradingSystemStage = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage - let executionNode = tradingSystemStage.closeExecution + if ( + ( + tradingEngine.current.strategyTriggerStage.status.value === 'Closed' || + tradingEngine.current.strategyTriggerStage.status.value === tradingEngine.current.strategyTriggerStage.status.config.initialValue + ) + && + ( + tradingEngine.current.strategyOpenStage.status.value === 'Closed' || + tradingEngine.current.strategyOpenStage.status.value === tradingEngine.current.strategyOpenStage.status.config.initialValue + ) + && + ( + tradingEngine.current.strategyManageStage.status.value === 'Closed' || + tradingEngine.current.strategyManageStage.status.value === tradingEngine.current.strategyManageStage.status.config.initialValue + ) + && + ( + tradingEngine.current.strategyCloseStage.status.value === 'Closed' + ) + ) { - tradingSystem.evalConditions(tradingSystemStage, 'Close Execution') - tradingSystem.evalFormulas(tradingSystemStage, 'Close Execution') - - await tradingExecutionModule.runExecution( - executionNode, - tradingEngine.current.strategyCloseStage - ) - - checkIfStageNeedsToBeClosed(tradingEngine.current.strategyCloseStage, tradingSystemStage, 'Close Stage') - } - - /* Exit Position Validation */ - if ( - ( - tradingEngine.current.strategyTriggerStage.status.value === 'Closed' || - tradingEngine.current.strategyTriggerStage.status.value === tradingEngine.current.strategyTriggerStage.status.config.initialValue - ) - && - ( - tradingEngine.current.strategyOpenStage.status.value === 'Closed' || - tradingEngine.current.strategyOpenStage.status.value === tradingEngine.current.strategyOpenStage.status.config.initialValue - ) - && - ( - tradingEngine.current.strategyManageStage.status.value === 'Closed' || - tradingEngine.current.strategyManageStage.status.value === tradingEngine.current.strategyManageStage.status.config.initialValue - ) - && - ( - tradingEngine.current.strategyCloseStage.status.value === 'Closed' - ) - ) { - - /* Exiting Everything now. */ - exitPositionAndStrategy() + /* Closing Everything now. */ + closePositionAndStrategy() + } } function initializeStageTargetSize() { @@ -860,8 +830,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM tradingEngine.current.strategyCloseStage.stageQuotedAsset.targetSize.value = global.PRECISE(tradingEngine.current.strategyCloseStage.stageQuotedAsset.targetSize.value, 10) } - function exitPositionAndStrategy() { - + function closePositionAndStrategy() { /* Taking Position Snapshot */ if (bot.SESSION.type === 'Backtesting Session') { if (sessionParameters.snapshots !== undefined) { @@ -920,7 +889,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM ) { positionFilled() } else { - checkCloseStageEvent() + checkCloseStageEvent(tradingSystemStage) } break } @@ -939,7 +908,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM ) { positionFilled() } else { - checkCloseStageEvent() + checkCloseStageEvent(tradingSystemStage) } break } @@ -947,7 +916,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM function positionFilled() { changeStageStatus(stageName, 'Closed', 'Position Size Filled') } - function checkCloseStageEvent() { + function checkCloseStageEvent(tradingSystemStage) { /* Check the Close Stage Event */ tradingSystem.evalConditions(tradingSystemStage, 'Close Stage Event') if (checkStopStageEvent(tradingSystemStage) === true) { @@ -957,7 +926,6 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM } function checkStopStageEvent(stage) { - /* Check the Close Stage Event. */ let stopStageEvent = stage.stopStageEvent if (stopStageEvent !== undefined) {