From 19fcaa820f6f5b80d8e9e57302a2aecbeacdc9c8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 7 Jul 2020 19:16:01 +0200 Subject: [PATCH] Ports Changed --- LowFrequencyTrading/TradingExecution.js | 2 +- LowFrequencyTrading/TradingSimulation.js | 3 + LowFrequencyTrading/TradingSystem.js | 169 +++++++++++++++-------- 3 files changed, 114 insertions(+), 60 deletions(-) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index edf84a9d0..abdb8351b 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -26,7 +26,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { } function takePosition() { - /* Check that we are in LIVE MODE */ + /* Check that we are in LIVE or FORWARD MODE */ if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> Taking a Position in ' + bot.SESSION.type) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.size.value = ' + tradingEngine.current.position.size.value) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 208ff2c4c..87c5e53f2 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -165,6 +165,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT triggeringOff = tradingSystemModule.checkTriggerOff() takingPosition = tradingSystemModule.checkTakePosition() + /* Time to check the execution of orders */ + tradingSystemModule.checkExecution() + /* Stop Loss Management */ if (takingPosition !== true) { tradingSystemModule.checkStopPhasesEvents() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 0aaa58865..4436142c8 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -13,6 +13,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { checkTriggerOn: checkTriggerOn, checkTriggerOff: checkTriggerOff, checkTakePosition: checkTakePosition, + checkExecution: checkExecution, checkStopPhasesEvents: checkStopPhasesEvents, calculateStopLoss: calculateStopLoss, checkTakeProfitPhaseEvents: checkTakeProfitPhaseEvents, @@ -295,14 +296,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -350,14 +344,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -399,15 +386,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -439,6 +418,89 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { return false // This Means that we have not met the conditions to take position. } + function checkExecution() { + if (tradingEngine.current.strategy.stageType.value === 'Open Stage') { + let executionNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage.openExecution + checkExecutionAlgorithms(executionNode) + } + + if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { + let executionNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage.closeExecution + checkExecutionAlgorithms(executionNode) + } + + function checkExecutionAlgorithms(executionNode) { + for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { + let executionAlgorithm = executionNode.executionAlgorithms[i] + checkOrders(executionAlgorithm.marketBuyOrders) + checkOrders(executionAlgorithm.marketSellOrders) + checkOrders(executionAlgorithm.limitBuyOrders) + checkOrders(executionAlgorithm.limitSellOrders) + } + } + + function checkOrders(orders) { + for (let i = 0; i < orders.length; i++) { + let order = orders[i] + switch (order.status) { + case 'Open': { + let mustCancelOrder = checkOrderEvent(order.cancelOrderEvent) + if (mustCancelOrder === true) { + // Cancel Order + order.status = 'Closed' + order.exitType = 'Cancelled' + } + + let mustMoveOrder = checkOrderEvent(order.moveOrderEvent) + if (mustMoveOrder === true && order.status === 'Open') { + // Move Order + order.status = 'Open' + } + } + break + case 'Closed': { + // Nothing to do here + } + break + default: { + order.status = 'Not Open' + let mustCreateOrder = checkOrderEvent(order.createOrderEvent) + if (mustCreateOrder === true) { + // Create Order + order.status = 'Open' + } + } + } + } + } + + function checkOrderEvent(event) { + if (event !== undefined) { + for (let k = 0; k < event.situations.length; k++) { + let situation = event.situations[k] + let passed + if (situation.conditions.length > 0) { + passed = true + } + + passed = checkConditions(situation, passed) + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(event.id) + tradingSystem.highlights.push(event.payload.parentNode.id) // order + tradingSystem.highlights.push(event.payload.parentNode.payload.parentNode.id) // algorithm + tradingSystem.highlights.push(event.payload.parentNode.payload.parentNode.payload.parentNode.id) // execution + + announcementsModule.makeAnnoucements(event) + return true // only one event can pass at the time + } + } + } + } + } + function checkStopPhasesEvents() { if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] @@ -485,14 +547,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -526,15 +581,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -651,14 +698,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -692,15 +732,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { passed = true } - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) - } - if (value !== true) { passed = false } - } + passed = checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -959,5 +991,24 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { return false } } + + function checkConditions(situation, passed) { + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m] + let value = false + if (conditions.get(condition.id) !== undefined) { + value = conditions.get(condition.id) + } + if (value === true) { + tradingSystem.highlights.push(condition.id) + } + else { + passed = false + } + tradingSystem.values.push([condition.id, value]) + } + return passed + } }