From 7dc1aa648f09830dbf90823db6fc175ed08a321f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 20 Jun 2020 20:14:42 +0200 Subject: [PATCH] Strategies Objects being saved --- LowFrequencyTrading/TradingRecords.js | 46 +++++++++++++++--------- LowFrequencyTrading/TradingSimulation.js | 2 ++ LowFrequencyTrading/TradingSystem.js | 44 ++++++++++++++++------- 3 files changed, 63 insertions(+), 29 deletions(-) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 863ba8806..95af633d7 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -41,6 +41,10 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let product = dataset.parentNode let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) + if (product.config.codeName === 'Strategy-Objects') { + let a = 1 + } + if (bot.processingDailyFiles === true && dataset.config.type === 'Daily Files') { persistRecord() } @@ -53,28 +57,36 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let record = scanRecordDefinition(product) if (product.config.saveAsObjects === true) { - /* - We check that the existing file does not contain at the end a record that - does not signal that the object is closed. - If it does, we replace it with the new one until at some point it will signal - does the object is closed. - */ - let lastRecord = outputDatasetArray[outputDatasetArray.length - 1] - if (lastRecord !== undefined) { - for (let j = 0; j < product.record.properties.length; j++) { - let recordProperty = product.record.properties[j] - if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { - let propertyValue = record[j] - if (propertyValue !== product.config.propertyValueThatClosesObject) { + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { + let propertyValue = record[j] + /* + We check that the existing file does not contain at the end a record that + does not signal that the object is closed. + If it does, we replace it with the new one until at some point it will signal + does the object is closed. + */ + let lastRecord = outputDatasetArray[outputDatasetArray.length - 1] + if (lastRecord !== undefined) { + let lastRecordValue = lastRecord[j] + if (lastRecordValue !== product.config.propertyValueThatClosesObject) { outputDatasetArray.splice(outputDatasetArray.length - 1, 1) - } } + /* + We will add a record everytime that proeprty value does not match this + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + break } } + } else { + /* We add every record to the existing file. */ + outputDatasetArray.push(record) } - /* We add every record to the existing file. */ - outputDatasetArray.push(record) } } @@ -151,7 +163,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { value = targetNode } } - if (recordProperty.config.isString !== true) { + if (recordProperty.config.isString !== true && Array.isArray(value) !== true) { value = safeNumericValue(value) } record.push(value) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 4a0a5f154..b7516706a 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -158,6 +158,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT heartBeat() positionChartAtCurrentCandle() tradingSystemModule.reset(chart) + tradingSystemModule.mantainStrategies() + tradingSystemModule.mantainPositions() tradingEngineModule.updateEpisodeCountersAndStatistics() /* diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 06858a74f..6087d3a91 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -6,6 +6,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const MODULE_NAME = 'Trading System' let thisObject = { reset: reset, + mantainStrategies: mantainStrategies, + mantainPositions: mantainPositions, evalConditions: evalConditions, evalFormulas: evalFormulas, checkTriggerOn: checkTriggerOn, @@ -72,6 +74,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.running = [] } + function mantainStrategies() { + if (tradingEngine.current.strategy.status.value === 'Closed') { + resetStrategy() + } + } + + function mantainPositions() { + + } + function evalConditions() { evalNode(bot.simulationState.tradingSystem, 'Conditions') } @@ -264,12 +276,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.id) tradingEngine.current.strategy.stageType.value = 'Trigger Stage' - tradingEngine.current.strategy.status.value = 'Open' + openStrategy() tradingEngine.current.strategy.index.value = j - tradingEngine.current.strategy.begin.value = tradingEngine.current.candle.begin.value - tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value // TODO: overrride with the node Formula - tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value - tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value // TODO: overrride with the node Formula tradingEngine.current.strategy.situationName.value = situation.name tradingEngine.current.strategy.strategyName.value = strategy.name @@ -321,9 +329,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.triggerOff.id) tradingSystem.highlights.push(triggerStage.id) - tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value - tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value - tradingEngine.current.strategy.status.value = 'Closed' + closeTrateggy() tradingEngine.current.strategy.stageType.value = 'No Stage' tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue @@ -982,10 +988,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function exitStrategyAfterPosition() { if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value - tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value - tradingEngine.current.strategy.status.value = 'Closed' - + closeTrateggy() tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue tradingEngine.current.strategy.stageType.value = 'No Stage' @@ -1039,5 +1042,22 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } + function closeTrateggy() { + tradingEngine.current.strategy.status.value = 'Closed' + tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value + } + + function openStrategy() { + tradingEngine.current.strategy.status.value = 'Open' + tradingEngine.current.strategy.begin.value = tradingEngine.current.candle.begin.value + tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value // TODO: overrride with the node Formula + tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value // TODO: overrride with the node Formula + } + + function resetStrategy() { + tradingEngine.current.strategy.status.value = tradingEngine.current.strategy.status.config.initialValue + } }