diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js index 88d21cf76..d60920629 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js @@ -178,7 +178,7 @@ exports.newSuperalgosBotModulesTradingPosition = function (processIndex) { tradingEngine.tradingCurrent.position.takeProfit.takeProfitPhase.value = phase } - function initialTargets(tradingSystemStageNode, tradingEngineStageNode) { + function initialTargets(tradingSystemStageNode, tradingEngineStageNode, stageName) { if (tradingSystemStageNode.initialTargets === undefined) { const message = 'Initial Targets Node Missing' @@ -194,7 +194,7 @@ exports.newSuperalgosBotModulesTradingPosition = function (processIndex) { } setTargetRate() - setTargetSize() + return setTargetSize(stageName) function setTargetRate() { if (tradingSystemStageNode.initialTargets.targetRate === undefined) { @@ -262,7 +262,7 @@ exports.newSuperalgosBotModulesTradingPosition = function (processIndex) { } } - function setTargetSize() { + function setTargetSize(stageName) { /* Basic Validation */ if ( tradingSystemStageNode.initialTargets.targetSizeInBaseAsset !== undefined && @@ -298,6 +298,9 @@ exports.newSuperalgosBotModulesTradingPosition = function (processIndex) { badDefinitionUnhandledException(undefined, message, tradingSystemStageNode.initialTargets.targetSizeInBaseAsset, docs) } if (value === 0) { + if (stageName === 'Close Stage') { + return false; + } const message = 'Target Size Value Zero' let docs = { diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingStages.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingStages.js index 5501ba4f2..ffdf1911e 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingStages.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingStages.js @@ -266,7 +266,8 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { function checkIfWeNeedToAbortTheStage() { /* Abort Open Stage Check */ - if (tradingEngine.tradingCurrent.strategyOpenStage.status.value === 'Open' && tradingEngine.tradingCurrent.strategyCloseStage.status.value === 'Open') { + if (tradingEngine.tradingCurrent.strategyOpenStage.status.value === 'Open' && + (tradingEngine.tradingCurrent.strategyCloseStage.status.value === 'Opening' || tradingEngine.tradingCurrent.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, @@ -281,7 +282,7 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { if (tradingEngine.tradingCurrent.strategyOpenStage.status.value === 'Opening') { /* The system allows the user not to define an Open Stage, because the Open Stage is optional. - Here we are going to see if that is the case and if it is, we will inmidiatelly consider + Here we are going to see if that is the case and if it is, we will immediately consider the Open Stage as closed. */ if (tradingSystem.tradingStrategies[tradingEngine.tradingCurrent.strategy.index.value].openStage === undefined) { @@ -427,7 +428,9 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { */ if (manageStage === undefined) { changeStageStatus('Manage Stage', 'Closed', 'Manage Stage Undefined') - changeStageStatus('Close Stage', 'Opening') + if (tradingEngine.tradingCurrent.strategyOpenStage.status.value !== 'Open' && tradingEngine.tradingCurrent.strategyOpenStage.status.value !== 'Opening') { + changeStageStatus('Close Stage', 'Opening'); + } return } @@ -766,7 +769,7 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { or our Take Profit were hit. */ - /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ + /* Stop Loss condition: Here we verify if the Stop Loss was hit or not. */ if ( ( tradingEngine.tradingCurrent.position.stopLoss.stopLossPosition.value === 'Above' && @@ -838,7 +841,9 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { /* Exit Position size and rate */ tradingSystem.evalFormulas(tradingSystemStage, 'Initial Targets') - tradingPositionModuleObject.initialTargets(tradingSystemStage, tradingEngine.tradingCurrent.strategyCloseStage) + if (tradingPositionModuleObject.initialTargets(tradingSystemStage, tradingEngine.tradingCurrent.strategyCloseStage, 'Close Stage') === false) { + //console.log("No Close-Stage Target Asset to Trade: Strategy Closing."); + } initializeStageTargetSize() changeStageStatus('Close Stage', 'Open') @@ -955,9 +960,9 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { function exitPositionValidation() { /* - 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. + Exit Position Validation: we need all the previous stages to be either closed + or at their initial default value. The last is because after Closed objects are + initialized to their defaults. */ if (tradingEngine.tradingCurrent.position.status.value !== 'Open' && tradingEngine.tradingCurrent.position.status.value !== 'Closing') { return } if (tradingEngine.tradingCurrent.position.status.value !== 'Close' && tradingEngine.tradingCurrent.position.status.value !== 'Closing') { return } @@ -1145,8 +1150,8 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { checkUserDefinedCode(stageName, 'Open') } if (stage.status.value === 'Closed') { - closeStage(stage) checkUserDefinedCode(stageName, 'Closed') + closeStage(stage, stageName) } /* @@ -1167,10 +1172,23 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex) { stage.beginRate.value = tradingEngine.tradingCurrent.tradingEpisode.candle.close.value } - function closeStage(stage) { + function closeStage(stage, stageName) { /* Recording the closing at the Trading Engine Data Structure */ stage.end.value = tradingEngine.tradingCurrent.tradingEpisode.cycle.lastEnd.value stage.endRate.value = tradingEngine.tradingCurrent.tradingEpisode.candle.close.value + + if (stageName === 'Open Stage') { + if (tradingEngine.tradingCurrent.strategyCloseStage.status.value !== 'Opening' && tradingEngine.tradingCurrent.strategyCloseStage.status.value !== 'Open' && + tradingEngine.tradingCurrent.strategyManageStage.status.value !== 'Opening' && tradingEngine.tradingCurrent.strategyManageStage.status.value !== 'Open') { + changeStageStatus('Close Stage', 'Opening'); + } + } + if (stageName === 'Close Stage') { + if (tradingEngine.tradingCurrent.position.status.value === 'Open') { + tradingPositionModuleObject.closingPosition('Close Stage Closed') + /* NOTE:TODO: Announcement May need added Here */ + } + } } }