From 42d9fc7e2c1b7f7b0a1258786ed82ab01c9ae5b8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 9 May 2019 08:04:57 +0200 Subject: [PATCH] Part Title as different object --- FloatingSpace/FloatingObject.js | 4 +- FloatingSpace/StrategyPart.js | 53 +++--- FloatingSpace/StrategyPartTitle.js | 171 ++++++++++++++++++ FloatingSpace/StrategyParts.js | 10 +- StrategySpace/ConstNewStrategy.js | 267 ----------------------------- StrategySpace/Strategy.js | 6 +- 6 files changed, 202 insertions(+), 309 deletions(-) create mode 100644 FloatingSpace/StrategyPartTitle.js diff --git a/FloatingSpace/FloatingObject.js b/FloatingSpace/FloatingObject.js index 560f1d3d5..d4c27396d 100644 --- a/FloatingSpace/FloatingObject.js +++ b/FloatingSpace/FloatingObject.js @@ -66,7 +66,7 @@ function newFloatingObject () { thisObject.underlayingObject = undefined } - function initialize (pType, pSubType, floatingLayer) { + function initialize (pType, floatingLayer) { switch (pType) { case 'Profile Ball': { @@ -82,7 +82,7 @@ function newFloatingObject () { case 'Strategy Part': { thisObject.underlayingObject = newStrategyPart() - thisObject.underlayingObject.initialize(floatingLayer, pSubType, thisObject.payload) + thisObject.underlayingObject.initialize(floatingLayer, thisObject.payload) thisObject.underlayingObject.container.connectToParent(thisObject.container, false, false, true, true, false, false, true, true) break } diff --git a/FloatingSpace/StrategyPart.js b/FloatingSpace/StrategyPart.js index 0eeb1ed7e..b70212ae2 100644 --- a/FloatingSpace/StrategyPart.js +++ b/FloatingSpace/StrategyPart.js @@ -13,6 +13,7 @@ function newStrategyPart () { container: undefined, payload: undefined, codeEditor: undefined, + partTitle: undefined, physics: physics, drawBackground: drawBackground, drawMiddleground: drawMiddleground, @@ -51,6 +52,8 @@ function newStrategyPart () { thisObject.payload = undefined thisObject.menu.finalize() thisObject.menu = undefined + thisObject.partTitle.finalize() + thisObject.partTitle = undefined if (thisObject.codeEditor !== undefined) { thisObject.codeEditor.finalize() @@ -62,12 +65,12 @@ function newStrategyPart () { imagePath = undefined } - function initialize (pFloatingLayer, pType, pPayload) { - thisObject.payload = pPayload + function initialize (pFloatingLayer, payload) { + thisObject.payload = payload floatingLayer = pFloatingLayer let menuItemsInitialValues = [] - switch (pType) { + switch (thisObject.payload.node.type) { case 'Strategy': { imagePath = 'Images/icons/style-01/quality.png' menuItemsInitialValues = [ @@ -314,14 +317,22 @@ function newStrategyPart () { break } default: { - if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> Part Type not Recognized -> type = ' + pType) } + if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> Part Type not Recognized -> type = ' + thisObject.payload.node.type) } } } +/* Initialize the Menu */ + thisObject.menu = newCircularMenu() thisObject.menu.initialize(menuItemsInitialValues, thisObject.payload) thisObject.menu.container.connectToParent(thisObject.container, false, false, true, true, false, false, true, true) +/* Initialize Part Title */ + + thisObject.partTitle = newStrategyPartTitle() + thisObject.partTitle.container.connectToParent(thisObject.container, false, false, true, true, false, false, true, true) + thisObject.partTitle.initialize(thisObject.payload) + /* Load Part Image */ if (imagePath !== undefined) { @@ -342,6 +353,9 @@ function newStrategyPart () { function getContainer (point) { let container + container = thisObject.partTitle.getContainer(point) + if (container !== undefined) { return container } + container = thisObject.menu.getContainer(point) if (container !== undefined) { return container } @@ -354,6 +368,7 @@ function newStrategyPart () { function physics () { thisObject.menu.physics() + thisObject.partTitle.physics() if (thisObject.codeEditor !== undefined) { thisObject.codeEditor.physics() @@ -371,10 +386,6 @@ function newStrategyPart () { function onNotFocus () { thisObject.isOnFocus = false - - if (thisObject.codeEditor !== undefined) { - thisObject.codeEditor.deactivate() - } } function onMouseClick (event) { @@ -391,6 +402,7 @@ function newStrategyPart () { function drawMiddleground (params) { if (thisObject.isOnFocus === false) { drawText(params) + thisObject.partTitle.draw(params) } } @@ -412,6 +424,7 @@ function newStrategyPart () { } drawText(params) + thisObject.partTitle.draw(params) if (thisObject.codeEditor !== undefined) { if (thisObject.codeEditor.visible === false) { @@ -481,30 +494,6 @@ function newStrategyPart () { let fontSize = params.currentFontSize let label - if (radius > 6 && thisObject.isOnFocus === true) { - const MAX_LABEL_LENGTH = 25 - - browserCanvasContext.strokeStyle = params.labelStrokeStyle - - browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY - - label = params.payload.downLabel - - if (label !== undefined) { - if (label.length > MAX_LABEL_LENGTH) { - label = label.substring(0, MAX_LABEL_LENGTH) + '...' - } - - labelPoint = { - x: position.x - label.length / 2 * fontSize * FONT_ASPECT_RATIO * 1.2, - y: position.y - radius * 1 / 2 - fontSize * FONT_ASPECT_RATIO - 10 - } - - browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY - browserCanvasContext.fillStyle = params.labelStrokeStyle - browserCanvasContext.fillText(label, labelPoint.x, labelPoint.y) - } - } if (radius > 6) { const MAX_LABEL_LENGTH = 30 diff --git a/FloatingSpace/StrategyPartTitle.js b/FloatingSpace/StrategyPartTitle.js new file mode 100644 index 000000000..fddf36c15 --- /dev/null +++ b/FloatingSpace/StrategyPartTitle.js @@ -0,0 +1,171 @@ + +function newStrategyPartTitle () { + const MODULE_NAME = 'Strategy Part Title' + const ERROR_LOG = true + + const logger = newWebDebugLog() + logger.fileName = MODULE_NAME + + let thisObject = { + container: undefined, + payload: undefined, + physics: physics, + draw: draw, + getContainer: getContainer, + finalize: finalize, + initialize: initialize + } + + thisObject.container = newContainer() + thisObject.container.initialize(MODULE_NAME) + thisObject.container.isClickeable = true + thisObject.container.isDraggeable = false + thisObject.container.frame.radius = 0 + + let selfFocusEventSubscriptionId + let selfNotFocuskEventSubscriptionId + let selfMouseClickEventSubscriptionId + + return thisObject + + function finalize () { + thisObject.container.eventHandler.stopListening(selfFocusEventSubscriptionId) + thisObject.container.eventHandler.stopListening(selfNotFocuskEventSubscriptionId) + thisObject.container.eventHandler.stopListening(selfMouseClickEventSubscriptionId) + + thisObject.container.finalize() + thisObject.container = undefined + thisObject.payload = undefined + } + + function initialize (payload) { + thisObject.payload = payload + + switch (payload.node.type) { + case 'Strategy': { + break + } + case 'Strategy Entry Event': { + break + } + case 'Strategy Exit Event': { + break + } + case 'Trade Entry Event': { + break + } + case 'Stop': { + break + } + case 'Take Profit': { + break + } + case 'Phase': { + break + } + case 'Situation': { + break + } + case 'Condition': { + break + } + default: { + if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> Part Type not Recognized -> type = ' + payload.node.type) } + } + } + + selfFocusEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onFocus', onFocus) + selfNotFocuskEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onNotFocus', onNotFocus) + selfMouseClickEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseClick', onMouseClick) + } + + function getContainer (point) { + let container + + if (thisObject.container.frame.isThisPointHere(point, true) === true) { + return thisObject.container + } else { + return undefined + } + } + + function physics () { + thisObject.container.frame.position.x = 0 - label.length / 2 * fontSize * FONT_ASPECT_RATIO * 1.2 + thisObject.container.frame.position.y = 0 - radius * 1 / 2 - fontSize * FONT_ASPECT_RATIO - 10 + + thisObject.container.frame.width = thisObject.container.frame.radius + thisObject.container.frame.height = 20 + } + + function onFocus () { + thisObject.isOnFocus = true + } + + function onNotFocus () { + thisObject.isOnFocus = false + } + + function onMouseClick (event) { + + } + + function draw (params) { + if (thisObject.isOnFocus === true) { + drawText(params) + } + } + + function drawText (params) { +/* Text Follows */ + let position = { + x: 0, + y: 0 + } + + position = thisObject.container.frame.frameThisPoint(position) + + let backgroundParams = { + cornerRadius: 3, + lineWidth: 0.1, + container: thisObject.container, + borderColor: UI_COLOR.DARK, + backgroundColor: UI_COLOR.RED, + castShadow: false, + opacity: 1 + } + + roundedCornersBackground(backgroundParams) + + let radius = thisObject.container.frame.radius + /* Label Text */ + let labelPoint + let fontSize = params.currentFontSize + let label + + if (radius > 6 && thisObject.isOnFocus === true) { + const MAX_LABEL_LENGTH = 25 + + browserCanvasContext.strokeStyle = params.labelStrokeStyle + + browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY + + label = params.payload.downLabel + + if (label !== undefined) { + if (label.length > MAX_LABEL_LENGTH) { + label = label.substring(0, MAX_LABEL_LENGTH) + '...' + } + + labelPoint = { + x: position.x - label.length / 2 * fontSize * FONT_ASPECT_RATIO * 1.2, + y: position.y - radius * 1 / 2 - fontSize * FONT_ASPECT_RATIO - 10 + } + + browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY + browserCanvasContext.fillStyle = params.labelStrokeStyle + browserCanvasContext.fillText(label, labelPoint.x, labelPoint.y) + } + } + } +} + diff --git a/FloatingSpace/StrategyParts.js b/FloatingSpace/StrategyParts.js index f53cf01eb..25391cacc 100644 --- a/FloatingSpace/StrategyParts.js +++ b/FloatingSpace/StrategyParts.js @@ -26,15 +26,14 @@ function newStrategyParts () { floatingLayer = pFloatingLayer } - function createNewStrategyPart (type, payload) { + function createNewStrategyPart (payload) { let floatingObject = newFloatingObject() floatingObject.payload = payload - floatingObject.initialize('Strategy Part', type, floatingLayer) - floatingObject.underlayingObject.type = type // after initialize + floatingObject.initialize('Strategy Part', payload.node.type, floatingLayer) const FRICTION = 0.97 - switch (type) { + switch (payload.node.type) { case 'Strategy': { level_1() break @@ -76,7 +75,7 @@ function newStrategyParts () { break } default: { - if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> Part Type not Recognized -> type = ' + type) } + if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> Part Type not Recognized -> type = ' + payload.node.type) } return } } @@ -147,3 +146,4 @@ function newStrategyParts () { floatingLayer.killFloatingObject(pFloatingObjectHandle) } } + diff --git a/StrategySpace/ConstNewStrategy.js b/StrategySpace/ConstNewStrategy.js index 197c07277..e69de29bb 100644 --- a/StrategySpace/ConstNewStrategy.js +++ b/StrategySpace/ConstNewStrategy.js @@ -1,267 +0,0 @@ - const GRAPHQL_MUTATION_UPDATE_STRATEGY = Apollo.gql` -mutation { - strategizer_EditStrategy( - id: "sdfs" - strategy: { - subStrategies: [ - { - active: true - name: "Trend Following" - entryPoint: { - situations: [ - { - name: "Price Collapsing" - conditions: [ - { - name: "%B Moving Average going down" - code: "percentageBandwidth.previous.direction === 'down' && percentageBandwidth.direction === 'down'" - } - { - name: "%B Bandwidth going up" - code: "percentageBandwidth.previous.previous.bandwidth < percentageBandwidth.previous.bandwidth && percentageBandwidth.previous.bandwidth < percentageBandwidth.bandwidth" - } - { - name: "Candles Min going down" - code: "candle.previous.previous.min > candle.previous.min && candle.previous.min > candle.min" - } - ] - } - ] - } - exitPoint: { - situations: [ - { - name: "Market Reversing" - conditions: [ - { - name: "Close above Band Moving Average" - code: "candle.close > bollingerBand.movingAverage" - } - ] - } - ] - } - sellPoint: { - situations: [ - { - name: "Min below lower bollingerBand." - conditions: [ - { - name: "3 Candles MIN below Lower Band" - code: "candle.previous.previous.min < bollingerBand.previous.previous.movingAverage - bollingerBand.previous.previous.deviation && candle.previous.min < bollingerBand.previous.movingAverage - bollingerBand.previous.deviation && candle.min < bollingerBand.movingAverage - bollingerBand.deviation" - } - ] - } - ] - } - stopLoss: { - phases: [ - { - name: "Following Sell Rate" - code: "newStopLoss = sellRate + sellRate * (stopLossPercentage - stopLossDecay) / 100" - situations: [ - { - name: "Candle below Moving Average" - conditions: [ - { - name: "Candle fully below Band Moving Average" - code: "candle.max < bollingerBand.movingAverage" - } - { - name: "Band Moving Average going down" - code: "bollingerBand.direction === 'down'" - } - ] - } - ] - } - { - name: "Above Bands Moving Average" - code: "newStopLoss = bollingerBand.movingAverage + bollingerBand.movingAverage * (stopLossPercentage - stopLossDecay) / 100" - situations: [ - { - name: "Candle below Moving Average" - conditions: [ - { - name: "Candle MAX below lower band" - code: "candle.max < bollingerBand.movingAverage - bollingerBand.deviation" - } - ] - } - ] - } - { - name: "At Bands Moving Average" - code: "newStopLoss = bollingerBand.movingAverage" - situations: [] - } - ] - } - buyOrder: { - phases: [ - { - name: "12 times standard deviation" - code: "buyOrder = bollingerBand.movingAverage - bollingerBand.standardDeviation * 12" - situations: [ - { - name: "Candle cut by lower band" - conditions: [ - { -name: "Max above lower band" - code: "candle.max > bollingerBand.movingAverage - bollingerBand.deviation" - } - { - name: "MIN below lower band" - code: "candle.min < bollingerBand.movingAverage - bollingerBand.deviation" - } - { - name: "Band Moving Average going down" - code: "bollingerBand.previous.movingAverage > bollingerBand.movingAverage" - } - ] - } - ] - } - { - name: "10 times standard deviation" - code: "buyOrder = bollingerBand.movingAverage - bollingerBand.standardDeviation * 10" - situations: [ - { - name: "%B starting to revert" - conditions: [ - { - name: "%B Moving Average going up" - code: "percentageBandwidth.direction === 'up'" - } - { - name: "%B Moving Average above 0" - code: "percentageBandwidth.movingAverage > 0" - } - ] - } - ] - } - { - name: "4 times standard deviation" - code: "buyOrder = bollingerBand.movingAverage - bollingerBand.standardDeviation * 4" - situations: [ - { - name: "%B going down again" - conditions: [ - { - name: "%B Moving Average going down" - code: "percentageBandwidth.direction === 'down'" - } - ] - } - ] - } - { - name: "3 times standard deviation" - code: "buyOrder = bollingerBand.movingAverage - bollingerBand.standardDeviation * 3" - situations: [ - { - name: "%B going up and above 30" - conditions: [ - { - name: "%B Moving Average going up" - code: "percentageBandwidth.direction === 'up'" - } - { - name: "%B Moving Average above 30" - code: "percentageBandwidth.movingAverage > 30" - } - ] - } - ] - } - { - name: "At lower band" - code: "buyOrder = bollingerBand.movingAverage - bollingerBand.standardDeviation * 2" - situations: [] - } - ] - } - } - { - active: true - name: "Range Trading" - entryPoint: { - situations: [ - { - name: "Gentle Up Trend" - conditions: [ - { - name: "Sub-Channel going up" - code: "subChannel.direction === 'up'" - } - { - name: "Sub-Channel Side|Gentle|Medium" - code: "subChannel.slope === 'Side' || subChannel.slope === 'Gentle' || subChannel.slope === 'Medium'" - } - { - name: "Candle Close above Lower Band" - code: "candle.close > bollingerBand.movingAverage + bollingerBand.deviation" - } - ] - } - ] - } - exitPoint: { - situations: [ - { - name: "Outside Sub-Channel" - conditions: [ - { - name: "Going down or too Steep" - code: "subChannel.direction === 'Down' || subChannel.slope === 'Steep' || subChannel.slope === 'Extreme'" - - -} - ] - } - ] - } - sellPoint: { - situations: [ - { - name: "Min below lower bollingerBand." - conditions: [ - { - name: "%B Moving Average going down" - code: "percentageBandwidth.previous.movingAverage > percentageBandwidth.movingAverage" - } - { - name: "%B Moving Average above 90" - code: "percentageBandwidth.previous.movingAverage > 90" - } - ] - } - ] - } - stopLoss: { - phases: [ - { - name: "Following Sell Rate" - code: "newStopLoss = sellRate + sellRate * (stopLossPercentage - stopLossDecay) / 100" - situations: [] - } - ] - } - buyOrder: { - phases: [ - { - name: "Between Band Moving Average and Lower Band" - code: "buyOrder = bollingerBand.movingAverage - bollingerBand.standardDeviation" - situations: [] - } - ] - } - } - ] - } - ) { - id - } -} -` diff --git a/StrategySpace/Strategy.js b/StrategySpace/Strategy.js index af427bb89..2567cbc9d 100644 --- a/StrategySpace/Strategy.js +++ b/StrategySpace/Strategy.js @@ -64,12 +64,12 @@ function newStrategy () { payload.chainParent = chainParent payload.onMenuItemClick = onMenuItemClick - node.handle = canvas.floatingSpace.strategyParts.createNewStrategyPart(partType, payload) - node.payload = payload - if (node.id === undefined) { node.id = newUniqueId() } + node.payload = payload + node.type = partType + node.handle = canvas.floatingSpace.strategyParts.createNewStrategyPart(payload) } function destroyPart (node) {