From f5b5f4b852bedc2935a9f3907d544b6ca16a87da Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 7 May 2019 09:58:45 +0200 Subject: [PATCH] Delete Phases Working --- FloatingSpace/StrategyPart.js | 12 +++-- FloatingSpace/StrategyParts.js | 8 +-- StrategySpace/Strategy.js | 90 +++++++++++++++++++++++----------- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/FloatingSpace/StrategyPart.js b/FloatingSpace/StrategyPart.js index 61053d1f2..b1f32d2de 100644 --- a/FloatingSpace/StrategyPart.js +++ b/FloatingSpace/StrategyPart.js @@ -1,6 +1,10 @@ function newStrategyPart () { const MODULE_NAME = 'Strategy Part' + const ERROR_LOG = true + + const logger = newWebDebugLog() + logger.fileName = MODULE_NAME let thisObject = { type: undefined, @@ -83,7 +87,7 @@ function newStrategyPart () { }] break } - case 'Strategy Entry': { + case 'Strategy Entry Event': { imagePath = 'Images/icons/style-01/startup.png' menuItemsInitialValues = [ { @@ -99,7 +103,7 @@ function newStrategyPart () { }] break } - case 'Strategy Exit': { + case 'Strategy Exit Event': { imagePath = 'Images/icons/style-01/support.png' menuItemsInitialValues = [ { @@ -115,7 +119,7 @@ function newStrategyPart () { }] break } - case 'Trade Entry': { + case 'Trade Entry Event': { imagePath = 'Images/icons/style-01/compass.png' menuItemsInitialValues = [ { @@ -324,7 +328,7 @@ function newStrategyPart () { if (pFloatingObject.payload.parentNode === undefined) { return } /* Here we do the trick of recalculation the position of the anchor by setting it to the position of its parent */ - let parentFloatingObject = floatingLayer.getFloatingObject(pFloatingObject.payload.parentNode.handle) + let parentFloatingObject = floatingLayer.getFloatingObject(pFloatingObject.payload.chainParent.handle) if (parentFloatingObject === undefined) { console.log(pFloatingObject) diff --git a/FloatingSpace/StrategyParts.js b/FloatingSpace/StrategyParts.js index 1f0e31869..248012cc9 100644 --- a/FloatingSpace/StrategyParts.js +++ b/FloatingSpace/StrategyParts.js @@ -39,19 +39,19 @@ function newStrategyParts () { level_1() break } - case 'Strategy Entry': { + case 'Strategy Entry Event': { level_2() break } - case 'Strategy Exit': { + case 'Strategy Exit Event': { level_2() break } - case 'Trade Entry': { + case 'Trade Entry Event': { level_2() break } - case 'Trade Exit': { + case 'Trade Exit Event': { level_2() break } diff --git a/StrategySpace/Strategy.js b/StrategySpace/Strategy.js index 3afbe11b3..9b9b58483 100644 --- a/StrategySpace/Strategy.js +++ b/StrategySpace/Strategy.js @@ -37,7 +37,7 @@ function newStrategy () { } - function createPart (partType, name, node, parentNode, title) { + function createPart (partType, name, node, parentNode, chainParent, title) { let payload = { position: { x: (viewPort.width - SIDE_PANEL_WIDTH) / 2 + SIDE_PANEL_WIDTH, @@ -55,6 +55,7 @@ function newStrategy () { payload.downLabel = name payload.node = node payload.parentNode = parentNode + payload.chainParent = chainParent payload.onMenuItemClick = onMenuItemClick node.handle = canvas.floatingSpace.strategyParts.createNewStrategyPart(partType, payload) @@ -68,84 +69,84 @@ function newStrategy () { function generateStrategyParts () { let lastPhase let strategy = thisObject.strategySource - createPart('Strategy', strategy.name, strategy, undefined) + createPart('Strategy', strategy.name, strategy, undefined, undefined) - createPart('Strategy Entry', '', strategy.entryPoint, strategy) + createPart('Strategy Entry Event', '', strategy.entryPoint, strategy, strategy) for (let k = 0; k < strategy.entryPoint.situations.length; k++) { let situation = strategy.entryPoint.situations[k] - createPart('Situation', situation.name, situation, strategy.entryPoint, 'Strategy Entry' + ' ' + 'Situation' + ' #' + (k + 1)) + createPart('Situation', situation.name, situation, strategy.entryPoint, strategy.entryPoint, 'Strategy Entry' + ' ' + 'Situation' + ' #' + (k + 1)) for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - createPart('Condition', condition.name, condition, situation, 'Condition' + ' #' + (m + 1)) + createPart('Condition', condition.name, condition, situation, situation, 'Condition' + ' #' + (m + 1)) } } - createPart('Strategy Exit', '', strategy.exitPoint, strategy) + createPart('Strategy Exit Event', '', strategy.exitPoint, strategy, strategy) for (let k = 0; k < strategy.exitPoint.situations.length; k++) { let situation = strategy.exitPoint.situations[k] - createPart('Situation', situation.name, situation, strategy.exitPoint, 'Strategy Exit' + ' ' + 'Situation' + ' #' + (k + 1)) + createPart('Situation', situation.name, situation, strategy.exitPoint, strategy.exitPoint, 'Strategy Exit' + ' ' + 'Situation' + ' #' + (k + 1)) for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - createPart('Condition', condition.name, condition, situation, 'Condition' + ' #' + (m + 1)) + createPart('Condition', condition.name, condition, situation, situation, 'Condition' + ' #' + (m + 1)) } } - createPart('Trade Entry', '', strategy.sellPoint, strategy) + createPart('Trade Entry Event', '', strategy.sellPoint, strategy, strategy) for (let k = 0; k < strategy.sellPoint.situations.length; k++) { let situation = strategy.sellPoint.situations[k] - createPart('Situation', situation.name, situation, strategy.sellPoint, 'Trade Entry' + ' ' + 'Situation' + ' #' + (k + 1)) + createPart('Situation', situation.name, situation, strategy.sellPoint, strategy.sellPoint, 'Trade Entry' + ' ' + 'Situation' + ' #' + (k + 1)) for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - createPart('Condition', condition.name, condition, situation, 'Condition' + ' #' + (m + 1)) + createPart('Condition', condition.name, condition, situation, situation, 'Condition' + ' #' + (m + 1)) } } - createPart('Stop', '', strategy.stopLoss, strategy) + createPart('Stop', '', strategy.stopLoss, strategy, strategy) for (let p = 0; p < strategy.stopLoss.phases.length; p++) { let phase = strategy.stopLoss.phases[p] - let parent + let chainParent if (p === 0) { - parent = strategy.stopLoss + chainParent = strategy.stopLoss } else { - parent = lastPhase + chainParent = lastPhase } lastPhase = phase - createPart('Phase', phase.name, phase, parent, 'Stop Phase' + ' #' + (p + 1)) + createPart('Phase', phase.name, phase, strategy.stopLoss, chainParent, 'Stop Phase' + ' #' + (p + 1)) for (let k = 0; k < phase.situations.length; k++) { let situation = phase.situations[k] - createPart('Situation', situation.name, situation, phase, 'Situation' + ' #' + (k + 1)) + createPart('Situation', situation.name, situation, phase, phase, 'Situation' + ' #' + (k + 1)) for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - createPart('Condition', condition.name, condition, situation, 'Condition' + ' #' + (m + 1)) + createPart('Condition', condition.name, condition, situation, situation, 'Condition' + ' #' + (m + 1)) } } } - createPart('Take Profit', '', strategy.buyOrder, strategy) + createPart('Take Profit', '', strategy.buyOrder, strategy, strategy) for (let p = 0; p < strategy.buyOrder.phases.length; p++) { let phase = strategy.buyOrder.phases[p] - let parent + let chainParent if (p === 0) { - parent = strategy.buyOrder + chainParent = strategy.buyOrder } else { - parent = lastPhase + chainParent = lastPhase } lastPhase = phase - createPart('Phase', phase.name, phase, parent, 'Take Profit Phase' + ' #' + (p + 1)) + createPart('Phase', phase.name, phase, strategy.buyOrder, chainParent, 'Take Profit Phase' + ' #' + (p + 1)) for (let k = 0; k < phase.situations.length; k++) { let situation = phase.situations[k] - createPart('Situation', situation.name, situation, phase, 'Situation' + ' #' + (k + 1)) + createPart('Situation', situation.name, situation, phase, phase, 'Situation' + ' #' + (k + 1)) for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - createPart('Condition', condition.name, condition, situation, 'Condition' + ' #' + (m + 1)) + createPart('Condition', condition.name, condition, situation, situation, 'Condition' + ' #' + (m + 1)) } } } @@ -238,14 +239,14 @@ function newStrategy () { case 'Edit Code': break - case 'Delete Phase': - + case 'Delete Phase': { + deletePhase(payload.node) break + } case 'Delete Situation': { deleteSituation(payload.node) break } - break case 'Delete Condition': { deleteCondition(payload.node) break @@ -255,6 +256,37 @@ function newStrategy () { } } + function deletePhase (node) { + let payload = node.payload + for (let k = 0; k < payload.parentNode.phases.length; k++) { + let phase = payload.parentNode.phases[k] + if (phase.name === node.name) { + for (let j = 0; j < phase.situations.length; j++) { + let situation = phase.situations[j] + for (let i = 0; i < situation.conditions.length; i++) { + let condition = situation.conditions[i] + destroyPart(condition) + cleanNode(condition) + } + situation.conditions = [] + destroyPart(situation) + phase.situations.splice(j, 1) + cleanNode(situation) + } + phase.situations = [] + /* Before deleting this phase we need to give its chainParent to the next phase down the chain */ + if (k < payload.parentNode.phases.length - 1) { + payload.parentNode.phases[k + 1].payload.chainParent = payload.chainParent + } + /* Continue destroying this phase */ + destroyPart(phase) + payload.parentNode.phases.splice(k, 1) + cleanNode(phase) + return + } + } + } + function deleteSituation (node) { let payload = node.payload for (let j = 0; j < payload.parentNode.situations.length; j++) { @@ -295,10 +327,10 @@ function newStrategy () { node.payload.downLabel = undefined node.payload.node = undefined node.payload.parentNode = undefined + node.payload.chainParent = undefined node.payload.onMenuItemClick = undefined node.handle = undefined node.payload = undefined node.cleaned = true } } -