Reorganization of Strategy Space
This commit is contained in:
1 parent
46ef07c54c
commit
a36f8992b6
6 files changed
+749
-928
No files matched your search
@@ -105,7 +105,7 @@ function newRestartSimulation () {
|
||||
}
|
||||
try {
|
||||
thisObject.status = 'Saving'
|
||||
let result = await canvas.strategySpace.workplace.saveToStrategyzer()
|
||||
let result = await canvas.strategySpace.strategizerGateway.saveToStrategyzer(canvas.strategySpace.workspace.tradingSystem)
|
||||
if (result === true) {
|
||||
thisObject.status = 'Restarting'
|
||||
await graphQlRestartSimulation(simulationParams)
|
||||
|
||||
+16
-8
@@ -3,10 +3,12 @@ function newStrategySpace () {
|
||||
const MODULE_NAME = 'Strategy Space'
|
||||
let thisObject = {
|
||||
sidePanel: undefined,
|
||||
workplace: undefined,
|
||||
strategizerGateway: undefined,
|
||||
container: undefined,
|
||||
iconCollection: undefined,
|
||||
iconByPartType: undefined,
|
||||
workspace: undefined,
|
||||
isDeployed: false,
|
||||
draw: draw,
|
||||
getContainer: getContainer,
|
||||
makeVisible: makeVisible,
|
||||
@@ -26,10 +28,10 @@ function newStrategySpace () {
|
||||
return thisObject
|
||||
|
||||
async function initialize () {
|
||||
thisObject.workplace = newWorkspace()
|
||||
thisObject.strategizerGateway = newStrategizerGateway()
|
||||
|
||||
thisObject.workplace.initialize()
|
||||
await thisObject.workplace.loadFromStrategyzer()
|
||||
thisObject.strategizerGateway.initialize()
|
||||
await thisObject.strategizerGateway.loadFromStrategyzer()
|
||||
|
||||
loadIconCollection()
|
||||
buildIconByPartTypeMap()
|
||||
@@ -131,13 +133,19 @@ function newStrategySpace () {
|
||||
}
|
||||
|
||||
function makeVisible () {
|
||||
if (thisObject.workplace.isDeployed !== true) {
|
||||
thisObject.workplace.deploydTradingSystem()
|
||||
if (thisObject.isDeployed !== true) {
|
||||
deploydTradingSystem()
|
||||
}
|
||||
canvas.floatingSpace.makeVisible()
|
||||
visible = true
|
||||
}
|
||||
|
||||
function deploydTradingSystem () {
|
||||
thisObject.workspace = newWorkspace()
|
||||
thisObject.workspace.initialize(thisObject.strategizerGateway.strategizerData)
|
||||
thisObject.isDeployed = true
|
||||
}
|
||||
|
||||
function makeInvisible () {
|
||||
visible = false
|
||||
}
|
||||
@@ -145,8 +153,8 @@ function newStrategySpace () {
|
||||
function getContainer (point) {
|
||||
let container
|
||||
|
||||
if (thisObject.workplace !== undefined) {
|
||||
container = thisObject.workplace.getContainer(point)
|
||||
if (thisObject.strategizerGateway !== undefined) {
|
||||
container = thisObject.strategizerGateway.getContainer(point)
|
||||
if (container !== undefined) { return container }
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,349 @@
|
||||
|
||||
function newStrategizerGateway () {
|
||||
const MODULE_NAME = 'Strategizer Gateway'
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let thisObject = {
|
||||
strategizerData: undefined,
|
||||
container: undefined,
|
||||
loadFromStrategyzer: loadFromStrategyzer,
|
||||
saveToStrategyzer: saveToStrategyzer,
|
||||
draw: draw,
|
||||
getContainer: getContainer,
|
||||
initialize: initialize
|
||||
}
|
||||
|
||||
let colletionItems = []
|
||||
|
||||
thisObject.container = newContainer()
|
||||
thisObject.container.name = MODULE_NAME
|
||||
thisObject.container.initialize()
|
||||
|
||||
thisObject.container.frame.width = 0
|
||||
thisObject.container.frame.height = 0
|
||||
|
||||
thisObject.container.frame.position.x = 0
|
||||
thisObject.container.frame.position.y = 0
|
||||
|
||||
thisObject.container.isDraggeable = false
|
||||
thisObject.container.isClickeable = false
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize () {
|
||||
|
||||
}
|
||||
|
||||
async function saveToStrategyzer (tradingSystem) {
|
||||
try {
|
||||
const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY)
|
||||
|
||||
let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY)
|
||||
if (user === null) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> Can not save because user is not logged in. ') }
|
||||
return
|
||||
}
|
||||
|
||||
user = JSON.parse(user)
|
||||
|
||||
const authId = user.authId
|
||||
|
||||
const apolloClient = new Apollo.lib.ApolloClient({
|
||||
networkInterface: Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl,
|
||||
transportBatching: true
|
||||
}),
|
||||
connectToDevTools: true
|
||||
})
|
||||
|
||||
/* Updating all Strategies */
|
||||
|
||||
const networkInterface = Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl
|
||||
})
|
||||
|
||||
networkInterface.use([{
|
||||
applyMiddleware (req, next) {
|
||||
req.options.headers = {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
next()
|
||||
}
|
||||
}])
|
||||
|
||||
let dataToSave = buildGraphQLDataToSave(tradingSystem)
|
||||
|
||||
const updateStrategies = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
apolloClient.mutate({
|
||||
mutation: GRAPHQL_MUTATION_UPDATE_STRATEGIES,
|
||||
variables: {
|
||||
id: thisObject.strategizerData.id,
|
||||
strategy: dataToSave.strategy
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
resolve(true)
|
||||
})
|
||||
.catch(err => {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> ApolloClient error updating user strategies -> err = ' + err.stack) }
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
let result = await updateStrategies()
|
||||
|
||||
return result
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function buildGraphQLDataToSave (tradingSystem) {
|
||||
let graphQL_Mutation = ''
|
||||
|
||||
let dataToSave = {
|
||||
strategy: {
|
||||
subStrategies: []
|
||||
}
|
||||
}
|
||||
|
||||
for (m = 0; m < tradingSystem.strategies.length; m++) {
|
||||
let strategizerGatewayStrategy = tradingSystem.strategies[m]
|
||||
let strategy = {
|
||||
name: strategizerGatewayStrategy.name,
|
||||
active: strategizerGatewayStrategy.active,
|
||||
entryPoint: {
|
||||
situations: []
|
||||
},
|
||||
exitPoint: {
|
||||
situations: []
|
||||
},
|
||||
sellPoint: {
|
||||
situations: []
|
||||
},
|
||||
stopLoss: {
|
||||
phases: []
|
||||
},
|
||||
buyOrder: {
|
||||
phases: []
|
||||
}
|
||||
}
|
||||
|
||||
for (let k = 0; k < strategizerGatewayStrategy.entryPoint.situations.length; k++) {
|
||||
let strategizerGatewaySituation = strategizerGatewayStrategy.entryPoint.situations[k]
|
||||
let situation = {
|
||||
name: strategizerGatewaySituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < strategizerGatewaySituation.conditions.length; m++) {
|
||||
let strategizerGatewayCondition = strategizerGatewaySituation.conditions[m]
|
||||
let condition = {
|
||||
name: strategizerGatewayCondition.name,
|
||||
code: strategizerGatewayCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
strategy.entryPoint.situations.push(situation)
|
||||
}
|
||||
|
||||
for (let k = 0; k < strategizerGatewayStrategy.exitPoint.situations.length; k++) {
|
||||
let strategizerGatewaySituation = strategizerGatewayStrategy.exitPoint.situations[k]
|
||||
let situation = {
|
||||
name: strategizerGatewaySituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < strategizerGatewaySituation.conditions.length; m++) {
|
||||
let strategizerGatewayCondition = strategizerGatewaySituation.conditions[m]
|
||||
let condition = {
|
||||
name: strategizerGatewayCondition.name,
|
||||
code: strategizerGatewayCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
strategy.exitPoint.situations.push(situation)
|
||||
}
|
||||
|
||||
for (let k = 0; k < strategizerGatewayStrategy.sellPoint.situations.length; k++) {
|
||||
let strategizerGatewaySituation = strategizerGatewayStrategy.sellPoint.situations[k]
|
||||
let situation = {
|
||||
name: strategizerGatewaySituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < strategizerGatewaySituation.conditions.length; m++) {
|
||||
let strategizerGatewayCondition = strategizerGatewaySituation.conditions[m]
|
||||
let condition = {
|
||||
name: strategizerGatewayCondition.name,
|
||||
code: strategizerGatewayCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
strategy.sellPoint.situations.push(situation)
|
||||
}
|
||||
|
||||
for (let p = 0; p < strategizerGatewayStrategy.stopLoss.phases.length; p++) {
|
||||
let strategizerGatewayPhase = strategizerGatewayStrategy.stopLoss.phases[p]
|
||||
let phase = {
|
||||
name: strategizerGatewayPhase.name,
|
||||
code: strategizerGatewayPhase.code,
|
||||
situations: []
|
||||
}
|
||||
|
||||
for (let k = 0; k < strategizerGatewayPhase.situations.length; k++) {
|
||||
let strategizerGatewaySituation = strategizerGatewayPhase.situations[k]
|
||||
let situation = {
|
||||
name: strategizerGatewaySituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < strategizerGatewaySituation.conditions.length; m++) {
|
||||
let strategizerGatewayCondition = strategizerGatewaySituation.conditions[m]
|
||||
let condition = {
|
||||
name: strategizerGatewayCondition.name,
|
||||
code: strategizerGatewayCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
phase.situations.push(situation)
|
||||
}
|
||||
strategy.stopLoss.phases.push(phase)
|
||||
}
|
||||
|
||||
for (let p = 0; p < strategizerGatewayStrategy.buyOrder.phases.length; p++) {
|
||||
let strategizerGatewayPhase = strategizerGatewayStrategy.buyOrder.phases[p]
|
||||
let phase = {
|
||||
name: strategizerGatewayPhase.name,
|
||||
code: strategizerGatewayPhase.code,
|
||||
situations: []
|
||||
}
|
||||
|
||||
for (let k = 0; k < strategizerGatewayPhase.situations.length; k++) {
|
||||
let strategizerGatewaySituation = strategizerGatewayPhase.situations[k]
|
||||
let situation = {
|
||||
name: strategizerGatewaySituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < strategizerGatewaySituation.conditions.length; m++) {
|
||||
let strategizerGatewayCondition = strategizerGatewaySituation.conditions[m]
|
||||
let condition = {
|
||||
name: strategizerGatewayCondition.name,
|
||||
code: strategizerGatewayCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
phase.situations.push(situation)
|
||||
}
|
||||
strategy.buyOrder.phases.push(phase)
|
||||
}
|
||||
dataToSave.strategy.subStrategies.push(strategy)
|
||||
}
|
||||
|
||||
return dataToSave
|
||||
}
|
||||
|
||||
async function loadFromStrategyzer () {
|
||||
try {
|
||||
const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY)
|
||||
|
||||
let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY)
|
||||
if (user === null) {
|
||||
// if there is no user that means that we are logged off, which means this object can not be used.
|
||||
return
|
||||
}
|
||||
|
||||
user = JSON.parse(user)
|
||||
|
||||
const authId = user.authId
|
||||
|
||||
let sessionToken
|
||||
|
||||
const apolloClient = new Apollo.lib.ApolloClient({
|
||||
networkInterface: Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl,
|
||||
transportBatching: true
|
||||
}),
|
||||
connectToDevTools: true
|
||||
})
|
||||
|
||||
/* Getting the FB that own the Strategies */
|
||||
|
||||
let tradingSystemSimulationClone = ''
|
||||
let clones = window.localStorage.getItem('userClones')
|
||||
if (clones === null || clones === '') {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> no user clones found at local storage. Can not get Strategies without them. ') }
|
||||
return
|
||||
}
|
||||
clones = JSON.parse(clones)
|
||||
for (let i = 0; i < clones.length; i++) {
|
||||
let clone = clones[i]
|
||||
if (clone.botType === 'Indicator') {
|
||||
tradingSystemSimulationClone = clone.botSlug
|
||||
}
|
||||
}
|
||||
/* Getting all Strategies */
|
||||
|
||||
const networkInterface = Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl
|
||||
})
|
||||
|
||||
networkInterface.use([{
|
||||
applyMiddleware (req, next) {
|
||||
req.options.headers = {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
next()
|
||||
}
|
||||
}])
|
||||
|
||||
const getStrategies = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
apolloClient.query({
|
||||
query: GRAPHQL_QUERY_GET_STRATEGIES,
|
||||
variables: { fbSlug: tradingSystemSimulationClone}
|
||||
})
|
||||
.then(response => {
|
||||
window.localStorage.setItem('userStrategies', JSON.stringify(response.data.strategizer_StrategyByFb))
|
||||
thisObject.strategizerData = JSON.parse(JSON.stringify(response.data.strategizer_StrategyByFb))
|
||||
|
||||
resolve({ strategies: response.data.strategizer_StrategyByFb.subStrategies})
|
||||
})
|
||||
.catch(err => {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> ApolloClient error getting user strategies -> err = ' + err.stack) }
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// To avoid race conditions, add asynchronous fetches to array
|
||||
let fetchDataPromises = []
|
||||
|
||||
fetchDataPromises.push(getStrategies())
|
||||
|
||||
// When all asynchronous fetches resolve, authenticate user or throw error.
|
||||
await Promise.all(fetchDataPromises).then(result => {
|
||||
|
||||
}, err => {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> GraphQL Fetch Error -> err = ' + err.stack) }
|
||||
})
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function getContainer (point) {
|
||||
let container
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
function draw () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
function newStrategyCollectionItem () {
|
||||
const MODULE_NAME = 'Strategy Collection Item'
|
||||
let thisObject = {
|
||||
icon: undefined,
|
||||
strategySource: undefined,
|
||||
strategy: undefined,
|
||||
container: undefined,
|
||||
draw: draw,
|
||||
getContainer: getContainer,
|
||||
initialize: initialize
|
||||
}
|
||||
|
||||
let isInitialized = false
|
||||
|
||||
thisObject.container = newContainer()
|
||||
thisObject.container.name = MODULE_NAME
|
||||
thisObject.container.initialize()
|
||||
thisObject.container.isClickeable = true
|
||||
thisObject.container.isDraggeable = false
|
||||
|
||||
let status = 'off'
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize (strategy) {
|
||||
thisObject.strategySource = strategy
|
||||
|
||||
const ITEM_WIDTH = SIDE_PANEL_WIDTH - 20
|
||||
const ITEM_HEIGHT = 80
|
||||
|
||||
thisObject.container.frame.width = ITEM_WIDTH
|
||||
thisObject.container.frame.height = ITEM_HEIGHT
|
||||
|
||||
const LEFT_MARGIN = (thisObject.container.parentContainer.frame.width - thisObject.container.frame.width) / 2
|
||||
const TOP_MARGIN = 100
|
||||
|
||||
thisObject.container.frame.position.x = LEFT_MARGIN
|
||||
thisObject.container.frame.position.y = TOP_MARGIN
|
||||
|
||||
thisObject.container.eventHandler.listenToEvent('onMouseClick', onMouseClick)
|
||||
isInitialized = true
|
||||
}
|
||||
|
||||
function onMouseClick (event) {
|
||||
if (status === 'off') {
|
||||
canvas.floatingSpace.makeVisible()
|
||||
thisObject.strategy = newTradingSystem()
|
||||
thisObject.strategy.initialize(thisObject.strategySource)
|
||||
status = 'on'
|
||||
} else {
|
||||
canvas.floatingSpace.makeInvisible()
|
||||
thisObject.strategy.finalize()
|
||||
status = 'off'
|
||||
}
|
||||
}
|
||||
|
||||
function getContainer (point) {
|
||||
let container
|
||||
if (thisObject.container.frame.isThisPointHere(point, true) === true) {
|
||||
return thisObject.container
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
function draw () {
|
||||
if (isInitialized === false) { return }
|
||||
|
||||
borders()
|
||||
text()
|
||||
arrow()
|
||||
icon()
|
||||
}
|
||||
|
||||
function text () {
|
||||
if (thisObject.strategySource === undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
const LEFT_MARGIN = 80
|
||||
const TOP_MARGIN = 45
|
||||
|
||||
let strategy = thisObject.strategySource
|
||||
|
||||
let labelpoint = {
|
||||
x: LEFT_MARGIN,
|
||||
y: TOP_MARGIN
|
||||
}
|
||||
|
||||
labelpoint = thisObject.container.frame.frameThisPoint(labelpoint)
|
||||
|
||||
let labelToPrint = strategy.name
|
||||
labelToPrint = ''
|
||||
labelToPrint = labelToPrint.substring(0, 30)
|
||||
let opacity = 1
|
||||
let fontSize = 15
|
||||
|
||||
printLabel(labelToPrint, labelpoint.x, labelpoint.y, opacity, fontSize)
|
||||
}
|
||||
|
||||
function borders () {
|
||||
let params = {
|
||||
cornerRadius: 8,
|
||||
lineWidth: 0.1,
|
||||
opacity: 1,
|
||||
container: thisObject.container,
|
||||
borderColor: UI_COLOR.DARK,
|
||||
backgroundColor: UI_COLOR.WHITE,
|
||||
castShadow: true
|
||||
}
|
||||
|
||||
roundedCornersBackground(params)
|
||||
}
|
||||
|
||||
function arrow () {
|
||||
const X_OFFSET = 400
|
||||
const Y_OFFSET = 40
|
||||
const HEIGHT = 12
|
||||
const WIDTH = 6
|
||||
const LINE_WIDTH = 2
|
||||
const OPACITY = 0.6
|
||||
|
||||
point1 = {
|
||||
x: X_OFFSET,
|
||||
y: Y_OFFSET - HEIGHT / 2
|
||||
}
|
||||
|
||||
point2 = {
|
||||
x: X_OFFSET + WIDTH,
|
||||
y: Y_OFFSET
|
||||
}
|
||||
|
||||
point3 = {
|
||||
x: X_OFFSET,
|
||||
y: Y_OFFSET + HEIGHT / 2
|
||||
}
|
||||
|
||||
/* Now the transformations. */
|
||||
|
||||
point1 = thisObject.container.frame.frameThisPoint(point1)
|
||||
point2 = thisObject.container.frame.frameThisPoint(point2)
|
||||
point3 = thisObject.container.frame.frameThisPoint(point3)
|
||||
|
||||
browserCanvasContext.setLineDash([0, 0])
|
||||
|
||||
/* The Arrow */
|
||||
|
||||
browserCanvasContext.beginPath()
|
||||
|
||||
browserCanvasContext.moveTo(point1.x, point1.y)
|
||||
browserCanvasContext.lineTo(point2.x, point2.y)
|
||||
browserCanvasContext.lineTo(point3.x, point3.y)
|
||||
|
||||
browserCanvasContext.lineWidth = LINE_WIDTH
|
||||
browserCanvasContext.strokeStyle = 'rgba(' + UI_COLOR.DARK + ', ' + OPACITY + ')'
|
||||
browserCanvasContext.stroke()
|
||||
}
|
||||
|
||||
function icon () {
|
||||
if (thisObject.icon === undefined) { return }
|
||||
|
||||
const X_OFFSET = 45
|
||||
const Y_OFFSET = 40
|
||||
const IMAGE_SIZE = 40
|
||||
|
||||
point1 = {
|
||||
x: X_OFFSET,
|
||||
y: Y_OFFSET
|
||||
}
|
||||
|
||||
/* Now the transformations. */
|
||||
|
||||
point1 = thisObject.container.frame.frameThisPoint(point1)
|
||||
|
||||
browserCanvasContext.drawImage(thisObject.icon, point1.x - IMAGE_SIZE / 2, point1.y - IMAGE_SIZE / 2, IMAGE_SIZE, IMAGE_SIZE)
|
||||
}
|
||||
}
|
||||
@@ -1,410 +0,0 @@
|
||||
|
||||
function newTradingSystem () {
|
||||
const MODULE_NAME = 'Trading System'
|
||||
|
||||
let thisObject = {
|
||||
protocolData: undefined,
|
||||
container: undefined,
|
||||
getContainer: getContainer,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
thisObject.container = newContainer()
|
||||
thisObject.container.initialize(MODULE_NAME)
|
||||
thisObject.container.isClickeable = false
|
||||
thisObject.container.isDraggeable = false
|
||||
thisObject.container.isWheelable = false
|
||||
thisObject.container.detectMouseOver = false
|
||||
thisObject.container.frame.radius = 0
|
||||
thisObject.container.frame.position.x = 0
|
||||
thisObject.container.frame.position.y = 0
|
||||
thisObject.container.frame.width = 0
|
||||
thisObject.container.frame.height = 0
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize () {
|
||||
destroyStrategyParts()
|
||||
thisObject.protocolData = undefined
|
||||
thisObject.container.finalize()
|
||||
thisObject.container = undefined
|
||||
}
|
||||
|
||||
function initialize (protocolData) {
|
||||
thisObject.protocolData = {
|
||||
id: protocolData.id,
|
||||
strategies: protocolData.subStrategies
|
||||
}
|
||||
generateStrategyParts()
|
||||
}
|
||||
|
||||
function getContainer (point) {
|
||||
|
||||
}
|
||||
|
||||
function createPart (partType, name, node, parentNode, chainParent, title) {
|
||||
let payload = {}
|
||||
if (chainParent === undefined) {
|
||||
payload.targetPosition = {
|
||||
x: canvas.floatingSpace.container.frame.width / 2,
|
||||
y: canvas.floatingSpace.container.frame.height / 2
|
||||
}
|
||||
} else {
|
||||
payload.targetPosition = {
|
||||
x: chainParent.payload.position.x,
|
||||
y: chainParent.payload.position.y
|
||||
}
|
||||
}
|
||||
|
||||
if (title !== undefined) {
|
||||
payload.subTitle = title
|
||||
} else {
|
||||
payload.subTitle = partType
|
||||
}
|
||||
|
||||
payload.visible = true
|
||||
payload.title = name
|
||||
payload.node = node
|
||||
payload.parentNode = parentNode
|
||||
payload.chainParent = chainParent
|
||||
payload.onMenuItemClick = onMenuItemClick
|
||||
|
||||
if (node.id === undefined) {
|
||||
node.id = newUniqueId()
|
||||
}
|
||||
node.payload = payload
|
||||
node.type = partType
|
||||
canvas.floatingSpace.strategyPartConstructor.createStrategyPart(payload)
|
||||
}
|
||||
|
||||
function destroyPart (node) {
|
||||
canvas.floatingSpace.strategyPartConstructor.destroyStrategyPart(node.payload)
|
||||
}
|
||||
|
||||
function generateStrategyParts () {
|
||||
let lastPhase
|
||||
let tradingSystem = thisObject.protocolData
|
||||
|
||||
createPart('Trading System', '', tradingSystem, undefined, undefined)
|
||||
|
||||
for (m = 0; m < tradingSystem.strategies.length; m++) {
|
||||
let strategy = tradingSystem.strategies[m]
|
||||
createPart('Strategy', strategy.name, strategy, tradingSystem, tradingSystem)
|
||||
|
||||
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.entryPoint, 'Strategy Entry' + ' ' + 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
|
||||
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.exitPoint, 'Strategy Exit' + ' ' + 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
|
||||
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, strategy.sellPoint, 'Trade Entry' + ' ' + 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
|
||||
createPart('Stop', '', strategy.stopLoss, strategy, strategy)
|
||||
for (let p = 0; p < strategy.stopLoss.phases.length; p++) {
|
||||
let phase = strategy.stopLoss.phases[p]
|
||||
|
||||
let chainParent
|
||||
if (p === 0) {
|
||||
chainParent = strategy.stopLoss
|
||||
} else {
|
||||
chainParent = lastPhase
|
||||
}
|
||||
lastPhase = phase
|
||||
createPart('Phase', phase.name, phase, strategy.stopLoss, chainParent, 'Stop Phase')
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
createPart('Situation', situation.name, situation, phase, phase, 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
createPart('Take Profit', '', strategy.buyOrder, strategy, strategy)
|
||||
for (let p = 0; p < strategy.buyOrder.phases.length; p++) {
|
||||
let phase = strategy.buyOrder.phases[p]
|
||||
let chainParent
|
||||
if (p === 0) {
|
||||
chainParent = strategy.buyOrder
|
||||
} else {
|
||||
chainParent = lastPhase
|
||||
}
|
||||
lastPhase = phase
|
||||
createPart('Phase', phase.name, phase, strategy.buyOrder, chainParent, 'Take Profit Phase')
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
createPart('Situation', situation.name, situation, phase, phase, 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function destroyStrategyParts () {
|
||||
let strategy = thisObject.protocolData
|
||||
destroyPart(strategy)
|
||||
|
||||
destroyPart(strategy.entryPoint)
|
||||
for (let k = 0; k < strategy.entryPoint.situations.length; k++) {
|
||||
let situation = strategy.entryPoint.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.exitPoint)
|
||||
for (let k = 0; k < strategy.exitPoint.situations.length; k++) {
|
||||
let situation = strategy.exitPoint.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.sellPoint)
|
||||
for (let k = 0; k < strategy.sellPoint.situations.length; k++) {
|
||||
let situation = strategy.sellPoint.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.stopLoss)
|
||||
for (let p = 0; p < strategy.stopLoss.phases.length; p++) {
|
||||
let phase = strategy.stopLoss.phases[p]
|
||||
destroyPart(strategy.stopLoss)
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.buyOrder)
|
||||
for (let p = 0; p < strategy.buyOrder.phases.length; p++) {
|
||||
let phase = strategy.buyOrder.phases[p]
|
||||
destroyPart(strategy.buyOrder)
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function onMenuItemClick (payload, action) {
|
||||
switch (action) {
|
||||
case 'Save Trading System':
|
||||
{
|
||||
let result = await canvas.strategySpace.workplace.saveToStrategyzer()
|
||||
return result
|
||||
break
|
||||
}
|
||||
|
||||
case 'Open Settings':
|
||||
break
|
||||
case 'Delete Strategy':
|
||||
|
||||
break
|
||||
|
||||
case 'Reload Strategy':
|
||||
|
||||
break
|
||||
|
||||
case 'Save Strategy':
|
||||
|
||||
break
|
||||
|
||||
case 'Edit Code':
|
||||
|
||||
break
|
||||
case 'Add Phase':
|
||||
{
|
||||
let phaseParent = payload.node
|
||||
let m = phaseParent.phases.length
|
||||
let phase = {
|
||||
name: 'New Phase',
|
||||
code: '',
|
||||
situations: []
|
||||
}
|
||||
phaseParent.phases.push(phase)
|
||||
let phaseChainParent
|
||||
if (m > 0) {
|
||||
phaseChainParent = phaseParent.phases[m - 1]
|
||||
} else {
|
||||
phaseChainParent = phaseParent
|
||||
}
|
||||
createPart('Phase', phase.name, phase, phaseParent, phaseChainParent, 'Phase')
|
||||
}
|
||||
break
|
||||
case 'Add Situation':
|
||||
{
|
||||
let phase = payload.node
|
||||
let m = phase.situations.length
|
||||
let situation = {
|
||||
name: 'New Situation',
|
||||
conditions: []
|
||||
}
|
||||
phase.situations.push(situation)
|
||||
createPart('Situation', situation.name, situation, phase, phase, 'Situation')
|
||||
}
|
||||
break
|
||||
case 'Add Condition':
|
||||
{
|
||||
let situation = payload.node
|
||||
let m = situation.conditions.length
|
||||
let condition = {
|
||||
name: 'New Condition',
|
||||
code: ''
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
break
|
||||
case 'Delete Phase': {
|
||||
deletePhase(payload.node)
|
||||
break
|
||||
}
|
||||
case 'Delete Situation': {
|
||||
deleteSituation(payload.node)
|
||||
break
|
||||
}
|
||||
case 'Delete Condition': {
|
||||
deleteCondition(payload.node)
|
||||
break
|
||||
}
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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.id === node.id) {
|
||||
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++) {
|
||||
let situation = payload.parentNode.situations[j]
|
||||
if (situation.id === node.id) {
|
||||
for (let i = 0; i < situation.conditions.length; i++) {
|
||||
let condition = situation.conditions[i]
|
||||
destroyPart(condition)
|
||||
cleanNode(condition)
|
||||
}
|
||||
situation.conditions = []
|
||||
destroyPart(situation)
|
||||
payload.parentNode.situations.splice(j, 1)
|
||||
cleanNode(situation)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function deleteCondition (node) {
|
||||
let payload = node.payload
|
||||
for (let i = 0; i < payload.parentNode.conditions.length; i++) {
|
||||
let condition = payload.parentNode.conditions[i]
|
||||
if (condition.id === node.id) {
|
||||
destroyPart(node)
|
||||
payload.parentNode.conditions.splice(i, 1)
|
||||
cleanNode(condition)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cleanNode (node) {
|
||||
node.payload.targetPosition.x = undefined
|
||||
node.payload.targetPosition.y = undefined
|
||||
node.payload.visible = undefined
|
||||
node.payload.subTitle = undefined
|
||||
node.payload.title = 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
|
||||
}
|
||||
}
|
||||
+383
-332
@@ -1,359 +1,410 @@
|
||||
|
||||
function newWorkspace () {
|
||||
const MODULE_NAME = 'Workspace'
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let thisObject = {
|
||||
isDeployed: false,
|
||||
strategizerData: undefined,
|
||||
container: undefined,
|
||||
tradingSystem: undefined,
|
||||
loadFromStrategyzer: loadFromStrategyzer,
|
||||
saveToStrategyzer: saveToStrategyzer,
|
||||
deploydTradingSystem: deploydTradingSystem,
|
||||
draw: draw,
|
||||
container: undefined,
|
||||
getContainer: getContainer,
|
||||
initialize: initialize
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
let colletionItems = []
|
||||
|
||||
thisObject.container = newContainer()
|
||||
thisObject.container.name = MODULE_NAME
|
||||
thisObject.container.initialize()
|
||||
|
||||
thisObject.container.initialize(MODULE_NAME)
|
||||
thisObject.container.isClickeable = false
|
||||
thisObject.container.isDraggeable = false
|
||||
thisObject.container.isWheelable = false
|
||||
thisObject.container.detectMouseOver = false
|
||||
thisObject.container.frame.radius = 0
|
||||
thisObject.container.frame.position.x = 0
|
||||
thisObject.container.frame.position.y = 0
|
||||
thisObject.container.frame.width = 0
|
||||
thisObject.container.frame.height = 0
|
||||
|
||||
thisObject.container.frame.position.x = 0
|
||||
thisObject.container.frame.position.y = 0
|
||||
|
||||
thisObject.container.isDraggeable = false
|
||||
thisObject.container.isClickeable = false
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize () {
|
||||
|
||||
function finalize () {
|
||||
destroyStrategyParts()
|
||||
thisObject.tradingSystem = undefined
|
||||
thisObject.container.finalize()
|
||||
thisObject.container = undefined
|
||||
}
|
||||
|
||||
async function saveToStrategyzer () {
|
||||
try {
|
||||
const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY)
|
||||
|
||||
let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY)
|
||||
if (user === null) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> Can not save because user is not logged in. ') }
|
||||
return
|
||||
}
|
||||
|
||||
user = JSON.parse(user)
|
||||
|
||||
const authId = user.authId
|
||||
|
||||
const apolloClient = new Apollo.lib.ApolloClient({
|
||||
networkInterface: Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl,
|
||||
transportBatching: true
|
||||
}),
|
||||
connectToDevTools: true
|
||||
})
|
||||
|
||||
/* Updating all Strategies */
|
||||
|
||||
const networkInterface = Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl
|
||||
})
|
||||
|
||||
networkInterface.use([{
|
||||
applyMiddleware (req, next) {
|
||||
req.options.headers = {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
next()
|
||||
}
|
||||
}])
|
||||
|
||||
let dataToSave = buildGraphQLDataToSave()
|
||||
|
||||
const updateStrategies = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
apolloClient.mutate({
|
||||
mutation: GRAPHQL_MUTATION_UPDATE_STRATEGIES,
|
||||
variables: {
|
||||
id: thisObject.strategizerData.id,
|
||||
strategy: dataToSave.strategy
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
resolve(true)
|
||||
})
|
||||
.catch(err => {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> ApolloClient error updating user strategies -> err = ' + err.stack) }
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
let result = await updateStrategies()
|
||||
|
||||
return result
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> err = ' + err.stack) }
|
||||
function initialize (tradingSystem) {
|
||||
thisObject.tradingSystem = {
|
||||
id: tradingSystem.id,
|
||||
strategies: tradingSystem.subStrategies
|
||||
}
|
||||
}
|
||||
|
||||
function buildGraphQLDataToSave () {
|
||||
let graphQL_Mutation = ''
|
||||
|
||||
let dataToSave = {
|
||||
strategy: {
|
||||
subStrategies: []
|
||||
}
|
||||
}
|
||||
|
||||
for (m = 0; m < thisObject.tradingSystem.protocolData.strategies.length; m++) {
|
||||
let workspaceStrategy = thisObject.tradingSystem.protocolData.strategies[m]
|
||||
let strategy = {
|
||||
name: workspaceStrategy.name,
|
||||
active: workspaceStrategy.active,
|
||||
entryPoint: {
|
||||
situations: []
|
||||
},
|
||||
exitPoint: {
|
||||
situations: []
|
||||
},
|
||||
sellPoint: {
|
||||
situations: []
|
||||
},
|
||||
stopLoss: {
|
||||
phases: []
|
||||
},
|
||||
buyOrder: {
|
||||
phases: []
|
||||
}
|
||||
}
|
||||
|
||||
for (let k = 0; k < workspaceStrategy.entryPoint.situations.length; k++) {
|
||||
let workspaceSituation = workspaceStrategy.entryPoint.situations[k]
|
||||
let situation = {
|
||||
name: workspaceSituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < workspaceSituation.conditions.length; m++) {
|
||||
let workspaceCondition = workspaceSituation.conditions[m]
|
||||
let condition = {
|
||||
name: workspaceCondition.name,
|
||||
code: workspaceCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
strategy.entryPoint.situations.push(situation)
|
||||
}
|
||||
|
||||
for (let k = 0; k < workspaceStrategy.exitPoint.situations.length; k++) {
|
||||
let workspaceSituation = workspaceStrategy.exitPoint.situations[k]
|
||||
let situation = {
|
||||
name: workspaceSituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < workspaceSituation.conditions.length; m++) {
|
||||
let workspaceCondition = workspaceSituation.conditions[m]
|
||||
let condition = {
|
||||
name: workspaceCondition.name,
|
||||
code: workspaceCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
strategy.exitPoint.situations.push(situation)
|
||||
}
|
||||
|
||||
for (let k = 0; k < workspaceStrategy.sellPoint.situations.length; k++) {
|
||||
let workspaceSituation = workspaceStrategy.sellPoint.situations[k]
|
||||
let situation = {
|
||||
name: workspaceSituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < workspaceSituation.conditions.length; m++) {
|
||||
let workspaceCondition = workspaceSituation.conditions[m]
|
||||
let condition = {
|
||||
name: workspaceCondition.name,
|
||||
code: workspaceCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
strategy.sellPoint.situations.push(situation)
|
||||
}
|
||||
|
||||
for (let p = 0; p < workspaceStrategy.stopLoss.phases.length; p++) {
|
||||
let workspacePhase = workspaceStrategy.stopLoss.phases[p]
|
||||
let phase = {
|
||||
name: workspacePhase.name,
|
||||
code: workspacePhase.code,
|
||||
situations: []
|
||||
}
|
||||
|
||||
for (let k = 0; k < workspacePhase.situations.length; k++) {
|
||||
let workspaceSituation = workspacePhase.situations[k]
|
||||
let situation = {
|
||||
name: workspaceSituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < workspaceSituation.conditions.length; m++) {
|
||||
let workspaceCondition = workspaceSituation.conditions[m]
|
||||
let condition = {
|
||||
name: workspaceCondition.name,
|
||||
code: workspaceCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
phase.situations.push(situation)
|
||||
}
|
||||
strategy.stopLoss.phases.push(phase)
|
||||
}
|
||||
|
||||
for (let p = 0; p < workspaceStrategy.buyOrder.phases.length; p++) {
|
||||
let workspacePhase = workspaceStrategy.buyOrder.phases[p]
|
||||
let phase = {
|
||||
name: workspacePhase.name,
|
||||
code: workspacePhase.code,
|
||||
situations: []
|
||||
}
|
||||
|
||||
for (let k = 0; k < workspacePhase.situations.length; k++) {
|
||||
let workspaceSituation = workspacePhase.situations[k]
|
||||
let situation = {
|
||||
name: workspaceSituation.name,
|
||||
conditions: []
|
||||
}
|
||||
|
||||
for (let m = 0; m < workspaceSituation.conditions.length; m++) {
|
||||
let workspaceCondition = workspaceSituation.conditions[m]
|
||||
let condition = {
|
||||
name: workspaceCondition.name,
|
||||
code: workspaceCondition.code
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
}
|
||||
phase.situations.push(situation)
|
||||
}
|
||||
strategy.buyOrder.phases.push(phase)
|
||||
}
|
||||
dataToSave.strategy.subStrategies.push(strategy)
|
||||
}
|
||||
|
||||
return dataToSave
|
||||
}
|
||||
|
||||
async function loadFromStrategyzer () {
|
||||
try {
|
||||
const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY)
|
||||
|
||||
let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY)
|
||||
if (user === null) {
|
||||
// if there is no user that means that we are logged off, which means this object can not be used.
|
||||
return
|
||||
}
|
||||
|
||||
user = JSON.parse(user)
|
||||
|
||||
const authId = user.authId
|
||||
|
||||
let sessionToken
|
||||
|
||||
const apolloClient = new Apollo.lib.ApolloClient({
|
||||
networkInterface: Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl,
|
||||
transportBatching: true
|
||||
}),
|
||||
connectToDevTools: true
|
||||
})
|
||||
|
||||
/* Getting the FB that own the Strategies */
|
||||
|
||||
let tradingSystemSimulationClone = ''
|
||||
let clones = window.localStorage.getItem('userClones')
|
||||
if (clones === null || clones === '') {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> no user clones found at local storage. Can not get Strategies without them. ') }
|
||||
return
|
||||
}
|
||||
clones = JSON.parse(clones)
|
||||
for (let i = 0; i < clones.length; i++) {
|
||||
let clone = clones[i]
|
||||
if (clone.botType === 'Indicator') {
|
||||
tradingSystemSimulationClone = clone.botSlug
|
||||
}
|
||||
}
|
||||
|
||||
/* Getting all Strategies */
|
||||
|
||||
const networkInterface = Apollo.lib.createNetworkInterface({
|
||||
uri: window.canvasApp.graphQL.masterAppApiUrl
|
||||
})
|
||||
|
||||
networkInterface.use([{
|
||||
applyMiddleware (req, next) {
|
||||
req.options.headers = {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
next()
|
||||
}
|
||||
}])
|
||||
|
||||
const getStrategies = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
apolloClient.query({
|
||||
query: GRAPHQL_QUERY_GET_STRATEGIES,
|
||||
variables: { fbSlug: tradingSystemSimulationClone}
|
||||
})
|
||||
.then(response => {
|
||||
window.localStorage.setItem('userStrategies', JSON.stringify(response.data.strategizer_StrategyByFb))
|
||||
thisObject.strategizerData = JSON.parse(JSON.stringify(response.data.strategizer_StrategyByFb))
|
||||
|
||||
resolve({ strategies: response.data.strategizer_StrategyByFb.subStrategies})
|
||||
})
|
||||
.catch(err => {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> ApolloClient error getting user strategies -> err = ' + err.stack) }
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// To avoid race conditions, add asynchronous fetches to array
|
||||
let fetchDataPromises = []
|
||||
|
||||
fetchDataPromises.push(getStrategies())
|
||||
|
||||
// When all asynchronous fetches resolve, authenticate user or throw error.
|
||||
await Promise.all(fetchDataPromises).then(result => {
|
||||
|
||||
}, err => {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> GraphQL Fetch Error -> err = ' + err.stack) }
|
||||
})
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function deploydTradingSystem () {
|
||||
thisObject.tradingSystem = newTradingSystem()
|
||||
thisObject.tradingSystem.initialize(thisObject.strategizerData)
|
||||
thisObject.isDeployed = true
|
||||
generateStrategyParts()
|
||||
}
|
||||
|
||||
function getContainer (point) {
|
||||
let container
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
function draw () {
|
||||
function createPart (partType, name, node, parentNode, chainParent, title) {
|
||||
let payload = {}
|
||||
if (chainParent === undefined) {
|
||||
payload.targetPosition = {
|
||||
x: canvas.floatingSpace.container.frame.width / 2,
|
||||
y: canvas.floatingSpace.container.frame.height / 2
|
||||
}
|
||||
} else {
|
||||
payload.targetPosition = {
|
||||
x: chainParent.payload.position.x,
|
||||
y: chainParent.payload.position.y
|
||||
}
|
||||
}
|
||||
|
||||
if (title !== undefined) {
|
||||
payload.subTitle = title
|
||||
} else {
|
||||
payload.subTitle = partType
|
||||
}
|
||||
|
||||
payload.visible = true
|
||||
payload.title = name
|
||||
payload.node = node
|
||||
payload.parentNode = parentNode
|
||||
payload.chainParent = chainParent
|
||||
payload.onMenuItemClick = onMenuItemClick
|
||||
|
||||
if (node.id === undefined) {
|
||||
node.id = newUniqueId()
|
||||
}
|
||||
node.payload = payload
|
||||
node.type = partType
|
||||
canvas.floatingSpace.strategyPartConstructor.createStrategyPart(payload)
|
||||
}
|
||||
|
||||
function destroyPart (node) {
|
||||
canvas.floatingSpace.strategyPartConstructor.destroyStrategyPart(node.payload)
|
||||
}
|
||||
|
||||
function generateStrategyParts () {
|
||||
let lastPhase
|
||||
let tradingSystem = thisObject.tradingSystem
|
||||
|
||||
createPart('Trading System', '', tradingSystem, undefined, undefined)
|
||||
|
||||
for (m = 0; m < tradingSystem.strategies.length; m++) {
|
||||
let strategy = tradingSystem.strategies[m]
|
||||
createPart('Strategy', strategy.name, strategy, tradingSystem, tradingSystem)
|
||||
|
||||
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.entryPoint, 'Strategy Entry' + ' ' + 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
|
||||
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.exitPoint, 'Strategy Exit' + ' ' + 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
|
||||
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, strategy.sellPoint, 'Trade Entry' + ' ' + 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
|
||||
createPart('Stop', '', strategy.stopLoss, strategy, strategy)
|
||||
for (let p = 0; p < strategy.stopLoss.phases.length; p++) {
|
||||
let phase = strategy.stopLoss.phases[p]
|
||||
|
||||
let chainParent
|
||||
if (p === 0) {
|
||||
chainParent = strategy.stopLoss
|
||||
} else {
|
||||
chainParent = lastPhase
|
||||
}
|
||||
lastPhase = phase
|
||||
createPart('Phase', phase.name, phase, strategy.stopLoss, chainParent, 'Stop Phase')
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
createPart('Situation', situation.name, situation, phase, phase, 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
createPart('Take Profit', '', strategy.buyOrder, strategy, strategy)
|
||||
for (let p = 0; p < strategy.buyOrder.phases.length; p++) {
|
||||
let phase = strategy.buyOrder.phases[p]
|
||||
let chainParent
|
||||
if (p === 0) {
|
||||
chainParent = strategy.buyOrder
|
||||
} else {
|
||||
chainParent = lastPhase
|
||||
}
|
||||
lastPhase = phase
|
||||
createPart('Phase', phase.name, phase, strategy.buyOrder, chainParent, 'Take Profit Phase')
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
createPart('Situation', situation.name, situation, phase, phase, 'Situation')
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function destroyStrategyParts () {
|
||||
let strategy = thisObject.tradingSystem
|
||||
destroyPart(strategy)
|
||||
|
||||
destroyPart(strategy.entryPoint)
|
||||
for (let k = 0; k < strategy.entryPoint.situations.length; k++) {
|
||||
let situation = strategy.entryPoint.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.exitPoint)
|
||||
for (let k = 0; k < strategy.exitPoint.situations.length; k++) {
|
||||
let situation = strategy.exitPoint.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.sellPoint)
|
||||
for (let k = 0; k < strategy.sellPoint.situations.length; k++) {
|
||||
let situation = strategy.sellPoint.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.stopLoss)
|
||||
for (let p = 0; p < strategy.stopLoss.phases.length; p++) {
|
||||
let phase = strategy.stopLoss.phases[p]
|
||||
destroyPart(strategy.stopLoss)
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
destroyPart(strategy.buyOrder)
|
||||
for (let p = 0; p < strategy.buyOrder.phases.length; p++) {
|
||||
let phase = strategy.buyOrder.phases[p]
|
||||
destroyPart(strategy.buyOrder)
|
||||
|
||||
for (let k = 0; k < phase.situations.length; k++) {
|
||||
let situation = phase.situations[k]
|
||||
destroyPart(situation)
|
||||
|
||||
for (let m = 0; m < situation.conditions.length; m++) {
|
||||
let condition = situation.conditions[m]
|
||||
destroyPart(condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function onMenuItemClick (payload, action) {
|
||||
switch (action) {
|
||||
case 'Save Trading System':
|
||||
{
|
||||
let result = await canvas.strategySpace.strategizerGateway.saveToStrategyzer(canvas.strategySpace.workspace.tradingSystem)
|
||||
return result
|
||||
break
|
||||
}
|
||||
|
||||
case 'Open Settings':
|
||||
break
|
||||
case 'Delete Strategy':
|
||||
|
||||
break
|
||||
|
||||
case 'Reload Strategy':
|
||||
|
||||
break
|
||||
|
||||
case 'Save Strategy':
|
||||
|
||||
break
|
||||
|
||||
case 'Edit Code':
|
||||
|
||||
break
|
||||
case 'Add Phase':
|
||||
{
|
||||
let phaseParent = payload.node
|
||||
let m = phaseParent.phases.length
|
||||
let phase = {
|
||||
name: 'New Phase',
|
||||
code: '',
|
||||
situations: []
|
||||
}
|
||||
phaseParent.phases.push(phase)
|
||||
let phaseChainParent
|
||||
if (m > 0) {
|
||||
phaseChainParent = phaseParent.phases[m - 1]
|
||||
} else {
|
||||
phaseChainParent = phaseParent
|
||||
}
|
||||
createPart('Phase', phase.name, phase, phaseParent, phaseChainParent, 'Phase')
|
||||
}
|
||||
break
|
||||
case 'Add Situation':
|
||||
{
|
||||
let phase = payload.node
|
||||
let m = phase.situations.length
|
||||
let situation = {
|
||||
name: 'New Situation',
|
||||
conditions: []
|
||||
}
|
||||
phase.situations.push(situation)
|
||||
createPart('Situation', situation.name, situation, phase, phase, 'Situation')
|
||||
}
|
||||
break
|
||||
case 'Add Condition':
|
||||
{
|
||||
let situation = payload.node
|
||||
let m = situation.conditions.length
|
||||
let condition = {
|
||||
name: 'New Condition',
|
||||
code: ''
|
||||
}
|
||||
situation.conditions.push(condition)
|
||||
createPart('Condition', condition.name, condition, situation, situation, 'Condition')
|
||||
}
|
||||
break
|
||||
case 'Delete Phase': {
|
||||
deletePhase(payload.node)
|
||||
break
|
||||
}
|
||||
case 'Delete Situation': {
|
||||
deleteSituation(payload.node)
|
||||
break
|
||||
}
|
||||
case 'Delete Condition': {
|
||||
deleteCondition(payload.node)
|
||||
break
|
||||
}
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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.id === node.id) {
|
||||
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++) {
|
||||
let situation = payload.parentNode.situations[j]
|
||||
if (situation.id === node.id) {
|
||||
for (let i = 0; i < situation.conditions.length; i++) {
|
||||
let condition = situation.conditions[i]
|
||||
destroyPart(condition)
|
||||
cleanNode(condition)
|
||||
}
|
||||
situation.conditions = []
|
||||
destroyPart(situation)
|
||||
payload.parentNode.situations.splice(j, 1)
|
||||
cleanNode(situation)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function deleteCondition (node) {
|
||||
let payload = node.payload
|
||||
for (let i = 0; i < payload.parentNode.conditions.length; i++) {
|
||||
let condition = payload.parentNode.conditions[i]
|
||||
if (condition.id === node.id) {
|
||||
destroyPart(node)
|
||||
payload.parentNode.conditions.splice(i, 1)
|
||||
cleanNode(condition)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cleanNode (node) {
|
||||
node.payload.targetPosition.x = undefined
|
||||
node.payload.targetPosition.y = undefined
|
||||
node.payload.visible = undefined
|
||||
node.payload.subTitle = undefined
|
||||
node.payload.title = 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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user