From ea7b912e009e6b8553cf5980f57545222692d342 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 2 Jun 2020 12:38:43 +0200 Subject: [PATCH 001/113] Trading System Module Creaeted --- Trading/TradingSimulation.js | 175 ++++++----------------------------- Trading/TradingSystem.js | 165 +++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+), 145 deletions(-) create mode 100644 Trading/TradingSystem.js diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 0f78d4c25..db10a804c 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -84,127 +84,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() - if (variable.isInitialized !== true || bot.RESUME === false) { - variable.isInitialized = true - - variable.episode = {} // An Episode represents each execution of the Simulation - variable.episode.count = {} // Everything in here means the total count since the begining of the episode. - variable.episode.stat = {} // Everything in here means the statistics calculated since the begining of the episode. - - variable.current = {} // Everything in here means that is happening right now. - variable.current.balance = {} // Balances at the current candle of the Simulation. - variable.current.distance = {} // Everything in here means the distance, measured in periods to whatever happened in the past, usually events. - variable.current.distance.toEvent = {} // Distance to Events that already happened. - variable.current.strategy = {} // Everything in here are variables related to the current strategy being executed. - variable.current.position = {} // Everything in here are variables related to the current position being executed. - - variable.current.balance.baseAsset = bot.VALUES_TO_USE.initialBalanceA - variable.current.balance.quotedAsset = bot.VALUES_TO_USE.initialBalanceB - variable.current.distance.toEvent.triggerOn = 0 - variable.current.distance.toEvent.triggerOff = 0 - variable.current.distance.toEvent.takePosition = 0 - variable.current.distance.toEvent.closePosition = 0 - - variable.last = {} // Everything in here means that it already happened. - variable.last.position = {} // Las values of variables at the last position. - - variable.last.position.profitLoss = 0 - variable.last.position.ROI = 0 - - variable.previous = {} // Everything in here means the the value that was current before a new current value was set. - variable.previous.balance = {} // Balances values previous to the current ones. - - variable.previous.balance.baseAsset = 0 - variable.previous.balance.quotedAsset = 0 - - variable.episode.parameters = {} // Parameters give you access to information received by the Simulation as parameters. - variable.episode.parameters.initial = {} // Everything in here means the status at the begining of the episode received as a parameter of the Sumulation. - variable.episode.parameters.minimum = {} // Everything in here means the minimum in the whole episode received as a parameter of the Sumulation. - variable.episode.parameters.maximum = {} // Everything in here means the maximum in the whole episode received as a parameter of the Sumulation. - variable.episode.parameters.initial.balance = {} - variable.episode.parameters.minimum.balance = {} - variable.episode.parameters.maximum.balance = {} - - variable.episode.parameters.baseAsset = bot.VALUES_TO_USE.baseAsset - variable.episode.parameters.quotedAsset = bot.VALUES_TO_USE.quotedAsset - variable.episode.parameters.marketBaseAsset = bot.market.baseAsset - variable.episode.parameters.marketQuotedAsset = bot.market.quotedAsset - variable.episode.parameters.timeFrame = timeFrame // The numeric value of the time frame, in milliseconds - variable.episode.parameters.timeFrameLabel = timeFrameLabel // The alpha-numeric value of the time frame - - variable.episode.parameters.initial.balance.baseAsset = bot.VALUES_TO_USE.initialBalanceA - variable.episode.parameters.minimum.balance.baseAsset = bot.VALUES_TO_USE.minimumBalanceA - variable.episode.parameters.maximum.balance.baseAsset = bot.VALUES_TO_USE.maximumBalanceA - - variable.episode.parameters.initial.balance.quotedAsset = bot.VALUES_TO_USE.initialBalanceB - variable.episode.parameters.minimum.balance.quotedAsset = bot.VALUES_TO_USE.minimumBalanceB - variable.episode.parameters.maximum.balance.quotedAsset = bot.VALUES_TO_USE.maximumBalanceB - - variable.episode.count.positions = 0 - variable.episode.count.fails = 0 - variable.episode.count.hits = 0 - variable.episode.count.periods = 0 - - variable.episode.stat.profitLoss = 0 - variable.episode.stat.hitRatio = 0 - variable.episode.stat.days = 0 - variable.episode.stat.ROI = 0 - variable.episode.stat.anualizedRateOfReturn = 0 - - inializeCurrentStrategy() - initializeCurrentPosition() - - variable.announcements = [] - } - - function inializeCurrentStrategy() { - - variable.current.strategy.name = '' - variable.current.strategy.index = -1 - variable.current.strategy.stage = 'No Stage' - variable.current.strategy.begin = 0 - variable.current.strategy.end = 0 - variable.current.strategy.status = 0 - variable.current.strategy.number = 0 - variable.current.strategy.beginRate = 0 - variable.current.strategy.endRate = 0 - variable.current.strategy.situationName = '' - - } - - function initializeCurrentPosition() { - - variable.current.position.begin = 0 - variable.current.position.end = 0 - variable.current.position.rate = 0 - variable.current.position.size = 0 - variable.current.position.status = 0 - variable.current.position.exitType = 0 - variable.current.position.beginRate = 0 - variable.current.position.endRate = 0 - variable.current.position.situationName = '' - - variable.current.position.stat = {} - variable.current.position.stat.ROI = 0 - variable.current.position.stat.days = 0 - - variable.current.position.count = {} - variable.current.position.count.periods = 0 - - variable.current.position.stopLoss = {} - variable.current.position.stopLoss.value = 0 - variable.current.position.stopLoss.phase = -1 - variable.current.position.stopLoss.stage = 'No Stage' - - variable.current.position.takeProfit = {} - variable.current.position.takeProfit.value = 0 - variable.current.position.takeProfit.phase = -1 - variable.current.position.takeProfit.stage = 'No Stage' - - } - /* Main Array and Maps */ - let propertyName = 'at' + variable.episode.parameters.timeFrameLabel.replace('-', '') + let propertyName = 'at' + sessionParameters.timeFrame.config.label.replace('-', '') let candles = chart[propertyName].candles let currentChart = chart[propertyName] @@ -223,12 +104,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function initializeLoop() { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initializeLoop -> Entering function.') } - if (bot.RESUME === true && variable.last.candle !== undefined) { - /* Estimate the Initial Candle based on the last candle saved at variable */ + if (bot.RESUME === true && tradingEngine.last.candle !== undefined) { + /* Estimate the Initial Candle based on the last candle saved at tradingEngine */ let firstBegin = candles[0].begin - let lastBegin = variable.last.candle.begin + let lastBegin = tradingEngine.last.candle.begin let diff = lastBegin - firstBegin - let amount = diff / variable.episode.parameters.timeFrame + let amount = diff / sessionParameters.timeFrame.config.value currentCandleIndex = Math.trunc(amount) + 1 // Because we need to start from the next candle if (currentCandleIndex < 0 || currentCandleIndex > candles.length - 1) { @@ -247,7 +128,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let firstEnd = candles[0].end let targetEnd = sessionParameters.timeRange.config.initialDatetime.valueOf() let diff = targetEnd - firstEnd - let amount = diff / variable.episode.parameters.timeFrame + let amount = diff / sessionParameters.timeFrame.config.value currentCandleIndex = Math.trunc(amount) if (currentCandleIndex < 0) { currentCandleIndex = 0 } @@ -387,12 +268,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } previousLoopingDay = loopingDay.valueOf() - variable.episode.count.periods++ - variable.episode.stat.days = variable.episode.count.periods * variable.episode.parameters.timeFrame / ONE_DAY_IN_MILISECONDS + tradingEngine.episode.episodeCounters.periods++ + tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS if (processingDailyFiles) { /* We skip the candle at the head of the market because currentCandleIndex has not closed yet. */ - let candlesPerDay = ONE_DAY_IN_MILISECONDS / variable.episode.parameters.timeFrame + let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value if (currentCandleIndex === candles.length - 1) { if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { /* We are at the head of the market, thus we skip the last candle because it has not close yet. */ @@ -400,7 +281,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT controlLoop() return /* Note here that in the last candle of the first day or the second day it will use an incomplete candle and partially calculated indicators. - if we skip these two variable.episode.count.periods, then there will be a hole in the file since the last period will be missing. */ + if we skip these two tradingEngine.episode.episodeCounters.periods, then there will be a hole in the file since the last period will be missing. */ } } } else { // We are processing Market Files @@ -411,9 +292,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } - let conditions = new Map() // Here we store the conditions values that will be use in the simulator for decision making. + const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') + let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) + tradingEngineModule.initialize(chart) + tradingEngineModule.evalConditions() + tradingEngineModule.finalize() + let formulas = new Map() - let conditionsValues = [] // Here we store the conditions values that will be written on file for the plotter. let formulasErrors = [] // Here we store the errors produced by all phase formulas. let formulasValues = [] // Here we store the values produced by all phase formulas. @@ -1489,7 +1374,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } variable.current.position.count.periods++ - variable.current.position.stat.days = variable.current.position.count.periods * variable.episode.parameters.timeFrame / ONE_DAY_IN_MILISECONDS + variable.current.position.stat.days = variable.current.position.count.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS } else { variable.current.position.count.periods = 0 variable.current.position.stat.days = 0 @@ -1740,7 +1625,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ if (variable.executionContext !== undefined) { if (variable.executionContext.periods !== undefined) { - if (variable.episode.count.periods <= variable.executionContext.periods) { + if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') } takePositionAtSimulation() return @@ -1784,7 +1669,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.executionContext = { status: 'Taking Position', - periods: variable.episode.count.periods + periods: tradingEngine.episode.episodeCounters.periods } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') } @@ -1799,7 +1684,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } variable.executionContext = { status: 'In a Position', - periods: variable.episode.count.periods, + periods: tradingEngine.episode.episodeCounters.periods, amountA: amountA, amountB: amountB, orderId: order.id @@ -1927,7 +1812,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ if (variable.executionContext !== undefined) { if (variable.executionContext.periods !== undefined) { - if (variable.episode.count.periods <= variable.executionContext.periods) { + if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') } closePositionAtSimulation() return @@ -1958,7 +1843,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.executionContext = { status: 'Closing Position', - periods: variable.episode.count.periods + periods: tradingEngine.episode.episodeCounters.periods } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') } @@ -1973,7 +1858,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } variable.executionContext = { status: 'Position Closed', - periods: variable.episode.count.periods, + periods: tradingEngine.episode.episodeCounters.periods, amountA: amountA, amountB: amountB, orderId: order.id @@ -2054,11 +1939,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.baseAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.baseAsset - 1 variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions - variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / variable.episode.stat.days * 365 + variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / tradingEngine.episode.episodeStatistics.days * 365 } else { variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.quotedAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.quotedAsset - 1 variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions - variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / variable.episode.stat.days * 365 + variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / tradingEngine.episode.episodeStatistics.days * 365 } addRecords() @@ -2202,8 +2087,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.episode.count.fails, variable.episode.stat.hitRatio, variable.episode.stat.ROI, - variable.episode.count.periods, - variable.episode.stat.days, + tradingEngine.episode.episodeCounters.periods, + tradingEngine.episode.episodeStatistics.days, variable.episode.stat.anualizedRateOfReturn, variable.current.position.rate, variable.last.position.ROI, @@ -2340,7 +2225,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } - if (variable.episode.count.periods > lastPeriodAnnounced) { + if (tradingEngine.episode.episodeCounters.periods > lastPeriodAnnounced) { if (isNaN(value) === false) { /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ let valueVariation @@ -2367,12 +2252,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ if (newAnnouncementRecord.periods !== undefined) { - newAnnouncementRecord.periods = variable.episode.count.periods + newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods newAnnouncementRecord.value = value } else { newAnnouncementRecord = { key: key, - periods: variable.episode.count.periods, + periods: tradingEngine.episode.episodeCounters.periods, value: value } variable.announcements.push(newAnnouncementRecord) diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js new file mode 100644 index 000000000..3378f7cf0 --- /dev/null +++ b/Trading/TradingSystem.js @@ -0,0 +1,165 @@ +exports.newTradingSystem = function newTradingSystem(bot, logger) { + + const MODULE_NAME = 'Trading System' + + let thisObject = { + evalConditions: evalConditions, + evalFormulas: evalFormulas, + initialize: initialize, + finalize: finalize + } + + let chart + + let conditions = new Map() + let conditionsValues = [] + let conditionsErrors = [] + + let formulas = new Map() + let formulasValues = [] + let formulasErrors = [] + + return thisObject + + function initialize(pChart) { + chart = pChart + } + + function finalize() { + chart = undefined + + conditions = undefined + conditionsValues = undefined + conditionsErrors = undefined + + formulas = undefined + formulasValues = undefined + formulasErrors = undefined + } + + function evalConditions() { + evalNode(bot.TRADING_SYSTEM, 'Conditions') + } + + function evalFormulas() { + evalNode(bot.TRADING_SYSTEM, 'Formulas') + } + + function evalNode(node, evaluating) { + if (node === undefined) { return } + + /* Here we check if there is a codition to be evaluated */ + if (node.type === 'Condition' && evaluating === 'Conditions') { + if (node.code !== undefined) { + /* We will eval this condition */ + evalCondition(node) + } + } + + /* Here we check if there is a formula to be evaluated */ + if (node.type === 'Formula' && evaluating === 'Formulas') { + if (node.code !== undefined) { + /* We will eval this condition */ + evalFormula(node) + } + } + + /* Now we go down through all this node children */ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (node[property.name] !== undefined) { + evalNode(node[property.name]) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (node[property.name] !== undefined) { + let nodePropertyArray = node[property.name] + object[property.name] = [] + for (let m = 0; m < nodePropertyArray.length; m++) { + evalNode(nodePropertyArray[m]) + } + } + break + } + } + } + } + } + + function evalCondition(node) { + let value + + try { + value = eval(node.code) + } catch (err) { + /* + One possible error is that the conditions references a .previous that is undefined. For this + reason and others, we will simply set the value to false. + */ + value = false + + if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || + node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 + ) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + node.error = err.message + ' @ ' + (new Date(candle.begin)).toLocaleString() + } + } + + conditions.set(node.id, value) + + if (value === true) { + conditionsValues.push(1) + } else { + conditionsValues.push(0) + } + } + + function evalFormula(node) { + let value + + try { + value = eval(node.code) + } catch (err) { + /* + One possible error is that the conditions references a .previous that is undefined. For this + reason and others, we will simply set the value to false. + */ + value = false + + if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || + node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 + ) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + node.error = err.message + ' @ ' + (new Date(candle.begin)).toLocaleString() + } + } + + formulas.set(node.id, value) + + if (value === true) { + formulaValues.push(1) + } else { + formulaValues.push(0) + } + } +} + From 043c5049f18191304cac1b27537025cd6ce7b38c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 3 Jun 2020 10:57:07 +0200 Subject: [PATCH 002/113] Until Take Position --- Trading/TradingSimulation.js | 798 ++++++----------------------------- Trading/TradingSystem.js | 30 +- 2 files changed, 154 insertions(+), 674 deletions(-) diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index db10a804c..2d1d9a634 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -294,559 +294,32 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) - tradingEngineModule.initialize(chart) - tradingEngineModule.evalConditions() - tradingEngineModule.finalize() - - let formulas = new Map() - let formulasErrors = [] // Here we store the errors produced by all phase formulas. - let formulasValues = [] // Here we store the values produced by all phase formulas. - - /* We define and evaluate all conditions to be used later during the simulation loop. */ - for (let j = 0; j < tradingSystem.strategies.length; j++) { - let strategy = tradingSystem.strategies[j] - - let positionSize = 0 - let positionRate = 0 - - /* Continue with the rest of the formulas and conditions */ - - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.triggerOn !== undefined) { - for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { - let situation = triggerStage.triggerOn.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - if (triggerStage.triggerOff !== undefined) { - for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { - let situation = triggerStage.triggerOff.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - if (triggerStage.takePosition !== undefined) { - for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { - let situation = triggerStage.takePosition.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - - let openStage = strategy.openStage - - if (openStage !== undefined) { - /* Default Values */ - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - positionSize = variable.current.balance.baseAsset - positionRate = candle.close - } else { - positionSize = variable.current.balance.quotedAsset - positionRate = candle.close - } - - let initialDefinition = openStage.initialDefinition - - if (initialDefinition !== undefined) { - if (variable.current.position.size !== 0) { - positionSize = variable.current.position.size - } else { - if (initialDefinition.positionSize !== undefined) { - if (initialDefinition.positionSize.formula !== undefined) { - try { - let code = initialDefinition.positionSize.formula.code - positionSize = eval(code) - addCodeToSnapshot(code) - } catch (err) { - initialDefinition.positionSize.formula.error = err.message - } - if (isNaN(positionSize)) { - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - positionSize = variable.current.balance.baseAsset - } else { - positionSize = variable.current.balance.quotedAsset - } - } else { - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - if (positionSize > variable.current.balance.baseAsset) { positionSize = variable.current.balance.baseAsset } - } else { - if (positionSize > variable.current.balance.quotedAsset) { positionSize = variable.current.balance.quotedAsset } - } - } - } - } - } - - if (variable.current.position.rate !== 0) { - positionRate = variable.current.position.rate - } else { - if (initialDefinition.positionRate !== undefined) { - if (initialDefinition.positionRate.formula !== undefined) { - try { - let code = initialDefinition.positionRate.formula.code - positionRate = eval(initialDefinition.positionRate.formula.code) - addCodeToSnapshot(code) - } catch (err) { - initialDefinition.positionRate.formula.error = err.message - } - if (isNaN(positionRate)) { - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - positionRate = candle.close - } else { - positionRate = candle.close - } - } - } - } - } - - if (initialDefinition.stopLoss !== undefined) { - for (let p = 0; p < initialDefinition.stopLoss.phases.length; p++) { - let phase = initialDefinition.stopLoss.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaError = 'Formula evaluates to Infinity.' - formulaValue = MAX_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Open Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.stopLoss -> MAX_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_STOP_LOSS_VALUE) { - formulaValue = MIN_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Open Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_STOP_LOSS_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.stopLoss -> MIN_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - - if (initialDefinition.takeProfit !== undefined) { - for (let p = 0; p < initialDefinition.takeProfit.phases.length; p++) { - let phase = initialDefinition.takeProfit.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaValue = MAX_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Open Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.takeProfit -> MAX_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_TAKE_PROFIT_VALUE) { - formulaValue = MIN_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Open Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_TAKE_PROFIT_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> initialDefinition.takeProfit -> MIN_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - } - - strategy.positionSize = positionSize - strategy.positionRate = positionRate - } - - let manageStage = strategy.manageStage - - if (manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - for (let p = 0; p < manageStage.stopLoss.phases.length; p++) { - let phase = manageStage.stopLoss.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaError = '' - formulaValue = MAX_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.stopLoss -> MAX_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_STOP_LOSS_VALUE) { - formulaValue = MIN_STOP_LOSS_VALUE - if (variable.current.position.stopLoss.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_STOP_LOSS_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.stopLoss -> MIN_STOP_LOSS_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - - if (manageStage.takeProfit !== undefined) { - for (let p = 0; p < manageStage.takeProfit.phases.length; p++) { - let phase = manageStage.takeProfit.phases[p] - - /* Evaluate Formula */ - let formulaValue - let formulaError = '' - - if (phase.formula !== undefined) { - try { - let code = phase.formula.code - formulaValue = eval(code) - addCodeToSnapshot(code) - if (formulaValue === Infinity) { - formulaError = 'Formula evaluates to Infinity.' - formulaValue = MAX_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula evaluates to Infinity.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.takeProfit -> MAX_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - } catch (err) { - if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - formulaError = err.message - } - } - if (isNaN(formulaValue)) { formulaValue = 0 } - if (formulaValue < MIN_TAKE_PROFIT_VALUE) { - formulaValue = MIN_TAKE_PROFIT_VALUE - if (variable.current.position.takeProfit.stage === 'Manage Stage') { - formulaError = 'WARNING: Formula is evaluating below the MIN_TAKE_PROFIT_VALUE.' - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[WARN] runSimulation -> loop -> manageStage.takeProfit -> MIN_TAKE_PROFIT_VALUE -> formulaError = ' + formulaError) } - } - } - - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p - formulas.set(key, formulaValue) - } - - /* next phase event */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - - let situation = nextPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - - /* move to phase events */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - - let situation = moveToPhaseEvent.situations[k] - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p + '-' + n + '-' + k + '-' + m - - if (condition.javascriptCode !== undefined) { - newCondition(key, condition.javascriptCode, chart) - } - } - } - } - } - } - } - } - - function newCondition(key, node, chart) { - let condition - let value - - try { - let code = node.code - value = eval(code) - addCodeToSnapshot(code) - } catch (err) { - /* - One possible error is that the conditions references a .previous that is undefined. For this - reason and others, we will simply set the value to false. - */ - value = false - - if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || - node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 - ) { - /* - We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. - */ - } else { - node.error = err.message + ' @ ' + (new Date(candle.begin)).toLocaleString() - } - } - - condition = { - key: key, - value: value - } - - conditions.set(condition.key, condition) - - if (condition.value) { - conditionsValues.push(1) - } else { - conditionsValues.push(0) - } - } - } + tradingSystemModule.initialize(chart) + tradingSystemModule.evalConditions() + tradingSystemModule.evalFormulas() + tradingSystemModule.finalize() /* Trigger On Conditions */ if ( - variable.current.strategy.stage === 'No Stage' && - variable.current.strategy.index === -1 + tradingEngine.current.strategy.stageType.value === 'No Stage' && + tradingEngine.current.strategy.index.value === -1 ) { + /* + First thing to do is to check if we are below the minimum balance, or above the maximun balance. + If we are, we are not gooing to trigger on again. + */ let minimumBalance let maximumBalance let balance - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - balance = variable.current.balance.baseAsset - minimumBalance = variable.episode.parameters.minimum.balance.baseAsset - maximumBalance = variable.episode.parameters.maximum.balance.baseAsset + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + balance = tradingEngine.current.balance.baseAsset.value + minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance + maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance } else { balance = variable.current.balance.quotedAsset - minimumBalance = variable.episode.parameters.minimum.balance.quotedAsset - maximumBalance = variable.episode.parameters.maximum.balance.quotedAsset + minimumBalance = sessionParameters.sessionQuotedAsset.config.minimumBalance + maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance } let stopRunningDate = new Date(candle.begin) @@ -872,17 +345,15 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT into one strategy, we will ignore market conditions for others. However there is also a strategy trigger off which can be hit before taking a position. If hit, we would be outside a strategy again and looking for the condition to enter all over again. - */ for (let j = 0; j < tradingSystem.strategies.length; j++) { - if ( - variable.current.strategy.stage !== 'No Stage' || - variable.current.strategy.index !== -1 + if ( // If a strategy was already picked during the loop, we exit it + tradingEngine.current.strategy.stageType.value !== 'No Stage' || + tradingEngine.current.strategy.index.value !== -1 ) { continue } let strategy = tradingSystem.strategies[j] - let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -897,11 +368,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value + if (conditions.get(condition.id) !== undefined) { + value = conditions.get(condition.id).value } if (value === false) { passed = false } @@ -909,22 +378,22 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (passed) { - variable.current.strategy.stage = 'Trigger Stage' + tradingEngine.current.strategy.stageType.value = 'Trigger Stage' checkAnnouncements(triggerStage) - variable.current.strategy.index = j - variable.current.strategy.begin = candle.begin - variable.current.strategy.beginRate = candle.min - variable.current.strategy.endRate = candle.min // In case the strategy does not get exited - variable.current.strategy.situationName = situation.name - variable.current.strategy.name = strategy.name + tradingEngine.current.strategy.index.value = j + tradingEngine.current.strategy.begin.value = candle.begin + tradingEngine.current.strategy.beginRate.value = candle.min + tradingEngine.current.strategy.endRate.value = candle.min // In case the strategy does not get exited + tradingEngine.current.strategy.situationName.value = situation.name + tradingEngine.current.strategy.strategyName.value = strategy.name - variable.current.distance.toEvent.triggerOn = 1 + tradingEngine.current.distanceToEvent.triggerOn.value = 1 checkAnnouncements(triggerStage.triggerOn) saveAsLastTriggerOnSnapshot = true - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Switching to Trigger Stage because conditions at Trigger On Event were met.') } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering into Strategy: ' + strategy.name) } break } } @@ -934,9 +403,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } /* Trigger Off Condition */ - if (variable.current.strategy.stage === 'Trigger Stage') { - let strategy = tradingSystem.strategies[variable.current.strategy.index] - + if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -950,29 +418,26 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - let key = variable.current.strategy.index + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value + if (conditions.get(condition.id) !== undefined) { + value = conditions.get(condition.id).value } if (value === false) { passed = false } } if (passed) { - variable.current.strategy.number = variable.current.strategy.index - variable.current.strategy.end = candle.end - variable.current.strategy.endRate = candle.min - variable.current.strategy.status = 1 // This means the strategy is closed, i.e. that has a begin and end. - variable.current.strategy.stage = 'No Stage' - variable.current.strategy.index = -1 + tradingEngine.current.strategy.end.value = candle.end + tradingEngine.current.strategy.endRate.value = candle.min + tradingEngine.current.strategy.status.value = 'Closed' + tradingEngine.current.strategy.stageType.value = 'No Stage' + tradingEngine.current.strategy.index.value = -1 - variable.current.distance.toEvent.triggerOff = 1 + tradingEngine.current.distanceToEvent.triggerOff.value = 1 checkAnnouncements(triggerStage.triggerOff) - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Switching to No Stage because conditions at the Trigger Off Event were met.') } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Exiting Strategy: ' + strategy.name) } break } } @@ -981,8 +446,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } /* Take Position Condition */ - if (variable.current.strategy.stage === 'Trigger Stage') { - let strategy = tradingSystem.strategies[variable.current.strategy.index] + if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage @@ -997,18 +462,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] - let key = variable.current.strategy.index + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value + if (conditions.get(condition.id) !== undefined) { + value = conditions.get(condition.id).value } if (value === false) { passed = false } } if (passed) { - variable.current.strategy.stage = 'Open Stage' + tradingEngine.current.strategy.stageType.value = 'Open Stage' checkAnnouncements(strategy.openStage) variable.current.position.stopLoss.stage = 'Open Stage' @@ -1032,7 +495,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Stop Loss Management */ if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') && + (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && takePositionNow !== true ) { checkStopPhases() @@ -1040,12 +503,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function checkStopPhases() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode - let j = variable.current.strategy.index + let j = tradingEngine.current.strategy.index.value let stageKey let initialDefinitionKey = '' let p @@ -1098,7 +561,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.stopLoss.phase++ variable.current.position.stopLoss.stage = 'Manage Stage' if (variable.current.position.takeProfit.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' + tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Take Profit') } @@ -1148,7 +611,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.stopLoss.stage = 'Manage Stage' if (variable.current.position.takeProfit.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' + tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Take Profit') } @@ -1162,7 +625,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function calculateStopLoss() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let phase @@ -1172,7 +635,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (openStage.initialDefinition !== undefined) { if (openStage.initialDefinition.stopLoss !== undefined) { phase = openStage.initialDefinition.stopLoss.phases[variable.current.position.stopLoss.phase] - key = variable.current.strategy.index + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase) + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase) } } } @@ -1180,7 +643,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (variable.current.position.stopLoss.stage === 'Manage Stage' && manageStage !== undefined) { if (manageStage.stopLoss !== undefined) { phase = manageStage.stopLoss.phases[variable.current.position.stopLoss.phase - 1] - key = variable.current.strategy.index + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase - 1) + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase - 1) } } @@ -1199,7 +662,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Take Profit Management */ if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') && + (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && takePositionNow !== true ) { checkTakeProfitPhases() @@ -1207,12 +670,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function checkTakeProfitPhases() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode - let j = variable.current.strategy.index + let j = tradingEngine.current.strategy.index.value let stageKey let initialDefinitionKey = '' let p @@ -1266,7 +729,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.takeProfit.phase++ variable.current.position.takeProfit.stage = 'Manage Stage' if (variable.current.position.stopLoss.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' + tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Stop') } @@ -1316,7 +779,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.takeProfit.stage = 'Manage Stage' if (variable.current.position.stopLoss.phase > 0) { - variable.current.strategy.stage = 'Manage Stage' + tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Stop') } @@ -1330,7 +793,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function calculateTakeProfit() { - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let phase @@ -1340,7 +803,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (openStage.initialDefinition !== undefined) { if (openStage.initialDefinition.takeProfit !== undefined) { phase = openStage.initialDefinition.takeProfit.phases[variable.current.position.takeProfit.phase] - key = variable.current.strategy.index + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase) + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase) } } } @@ -1348,7 +811,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (variable.current.position.takeProfit.stage === 'Manage Stage' && manageStage !== undefined) { if (manageStage.takeProfit !== undefined) { phase = manageStage.takeProfit.phases[variable.current.position.takeProfit.phase - 1] - key = variable.current.strategy.index + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase - 1) + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase - 1) } } @@ -1367,7 +830,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Keeping Position Counters Up-to-date */ if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') + (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') ) { if (takePositionNow === true) { variable.current.position.count.periods = 0 @@ -1382,15 +845,15 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Keeping Distance Counters Up-to-date */ if ( - variable.current.distance.toEvent.triggerOn > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.triggerOn.value > 0 // with this we avoind counting before the first event happens. ) { - variable.current.distance.toEvent.triggerOn++ + tradingEngine.current.distanceToEvent.triggerOn.value++ } if ( - variable.current.distance.toEvent.triggerOff > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.triggerOff.value > 0 // with this we avoind counting before the first event happens. ) { - variable.current.distance.toEvent.triggerOff++ + tradingEngine.current.distanceToEvent.triggerOff.value++ } if ( @@ -1407,17 +870,17 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Checking if Stop or Take Profit were hit */ if ( - (variable.current.strategy.stage === 'Open Stage' || variable.current.strategy.stage === 'Manage Stage') && + (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && takePositionNow !== true ) { - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] /* Checking what happened since the last execution. We need to know if the Stop Loss or our Take Profit were hit. */ /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - if ((variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset && candle.max >= variable.current.position.stopLoss.value) || (variable.episode.parameters.baseAsset !== variable.episode.parameters.marketBaseAsset && candle.min <= variable.current.position.stopLoss.value)) { + if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.max >= variable.current.position.stopLoss.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.min <= variable.current.position.stopLoss.value)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } /* Hit Point Validation @@ -1426,7 +889,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { if (variable.current.position.stopLoss.value < candle.min) { variable.current.position.stopLoss.value = candle.min } @@ -1441,7 +904,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Apply the Slippage */ let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { slippedStopLoss = slippedStopLoss + slippageAmount } else { slippedStopLoss = slippedStopLoss - slippageAmount @@ -1449,7 +912,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT closeRate = slippedStopLoss - variable.current.strategy.stage = 'Close Stage' + tradingEngine.current.strategy.stageType.value = 'Close Stage' checkAnnouncements(strategy.closeStage, 'Stop') variable.current.position.stopLoss.stage = 'No Stage' @@ -1464,7 +927,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ - if ((variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset && candle.min <= variable.current.position.takeProfit.value) || (variable.episode.parameters.baseAsset !== variable.episode.parameters.marketBaseAsset && candle.max >= variable.current.position.takeProfit.value)) { + if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.min <= variable.current.position.takeProfit.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.max >= variable.current.position.takeProfit.value)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } /* Hit Point Validation: @@ -1473,7 +936,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { if (variable.current.position.takeProfit.value > candle.max) { variable.current.position.takeProfit.value = candle.max } @@ -1487,7 +950,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Apply the Slippage */ let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { slippedTakeProfit = slippedTakeProfit + slippageAmount } else { slippedTakeProfit = slippedTakeProfit - slippageAmount @@ -1495,7 +958,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT closeRate = slippedTakeProfit - variable.current.strategy.stage = 'Close Stage' + tradingEngine.current.strategy.stageType.value = 'Close Stage' checkAnnouncements(strategy.closeStage, 'Take Profit') variable.current.position.stopLoss.stage = 'No Stage' @@ -1522,7 +985,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.distance.toEvent.takePosition = 1 /* Position size and rate */ - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] variable.current.position.size = strategy.positionSize variable.current.position.rate = strategy.positionRate @@ -1530,7 +993,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* We take what was calculated at the formula and apply the slippage. */ let slippageAmount = variable.current.position.rate * bot.VALUES_TO_USE.slippage.positionRate / 100 - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { variable.current.position.rate = variable.current.position.rate - slippageAmount } else { variable.current.position.rate = variable.current.position.rate + slippageAmount @@ -1651,7 +1114,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let amountB let orderSide - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { orderSide = 'sell' orderPrice = variable.current.position.rate @@ -1725,7 +1188,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT calculateTakeProfit() calculateStopLoss() - variable.previous.balance.baseAsset = variable.current.balance.baseAsset + variable.previous.balance.baseAsset = tradingEngine.current.balance.baseAsset.value variable.previous.balance.quotedAsset = variable.current.balance.quotedAsset variable.last.position.profitLoss = 0 @@ -1733,15 +1196,15 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let feePaid = 0 - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { feePaid = variable.current.position.size * variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + variable.current.position.size * variable.current.position.rate - feePaid - variable.current.balance.baseAsset = variable.current.balance.baseAsset - variable.current.position.size + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - variable.current.position.size } else { feePaid = variable.current.position.size / variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 - variable.current.balance.baseAsset = variable.current.balance.baseAsset + variable.current.position.size / variable.current.position.rate - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + variable.current.position.size / variable.current.position.rate - feePaid variable.current.balance.quotedAsset = variable.current.balance.quotedAsset - variable.current.position.size } @@ -1761,7 +1224,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.distance.toEvent.closePosition = 1 /* Position size and rate */ - let strategy = tradingSystem.strategies[variable.current.strategy.index] + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ /* Check that we are in LIVE MODE */ @@ -1825,7 +1288,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let amountB let orderSide - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { orderSide = 'buy' orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. @@ -1837,8 +1300,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT orderPrice = ticker.last - 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - amountA = variable.current.balance.baseAsset * orderPrice - amountB = variable.current.balance.baseAsset + amountA = tradingEngine.current.balance.baseAsset.value * orderPrice + amountB = tradingEngine.current.balance.baseAsset.value } variable.executionContext = { @@ -1898,29 +1361,29 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let feePaid = 0 - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { strategy.positionSize = variable.current.balance.quotedAsset / closeRate strategy.positionRate = closeRate feePaid = variable.current.balance.quotedAsset / closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 - variable.current.balance.baseAsset = variable.current.balance.baseAsset + variable.current.balance.quotedAsset / closeRate - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + variable.current.balance.quotedAsset / closeRate - feePaid variable.current.balance.quotedAsset = 0 } else { - strategy.positionSize = variable.current.balance.baseAsset * closeRate + strategy.positionSize = tradingEngine.current.balance.baseAsset.value * closeRate strategy.positionRate = closeRate - feePaid = variable.current.balance.baseAsset * closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.balance.baseAsset.value * closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + variable.current.balance.baseAsset * closeRate - feePaid - variable.current.balance.baseAsset = 0 + variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + tradingEngine.current.balance.baseAsset.value * closeRate - feePaid + tradingEngine.current.balance.baseAsset.value = 0 } - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { - variable.last.position.profitLoss = variable.current.balance.baseAsset - variable.previous.balance.baseAsset + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + variable.last.position.profitLoss = tradingEngine.current.balance.baseAsset.value - variable.previous.balance.baseAsset variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } - variable.episode.stat.profitLoss = variable.current.balance.baseAsset - variable.episode.parameters.initial.balance.baseAsset + variable.episode.stat.profitLoss = tradingEngine.current.balance.baseAsset.value - variable.episode.parameters.initial.balance.baseAsset } else { variable.last.position.profitLoss = variable.current.balance.quotedAsset - variable.previous.balance.quotedAsset variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size @@ -1936,7 +1399,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.episode.count.fails++ } - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.baseAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.baseAsset - 1 variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / tradingEngine.episode.episodeStatistics.days * 365 @@ -1967,18 +1430,17 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } /* Closing the Closing Stage */ - if (variable.current.strategy.stage === 'Close Stage') { + if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { - variable.current.strategy.number = variable.current.strategy.index - variable.current.strategy.end = candle.end - variable.current.strategy.endRate = candle.min - variable.current.strategy.status = 1 // This means the strategy is closed, i.e. that has a begin and end. + tradingEngine.current.strategy.end.value = candle.end + tradingEngine.current.strategy.endRate.value = candle.min + tradingEngine.current.strategy.status.value = 'Closed' - variable.current.strategy.index = -1 - variable.current.strategy.stage = 'No Stage' + tradingEngine.current.strategy.index.value = -1 + tradingEngine.current.strategy.stageType.value = 'No Stage' timerToCloseStage = 0 - variable.current.distance.toEvent.triggerOff = 1 + tradingEngine.current.distanceToEvent.triggerOff.value = 1 if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing the Closing Stage -> Exiting Close Stage.') } } else { @@ -2027,8 +1489,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let closeValues = [ variable.episode.count.positions, // Position Number (new Date(candle.begin)).toISOString(), // Datetime - tradingSystem.strategies[variable.current.strategy.index].name, // Strategy Name - variable.current.strategy.situationName, // Trigger On Situation + tradingSystem.strategies[tradingEngine.current.strategy.index.value].name, // Strategy Name + tradingEngine.current.strategy.situationName.value, // Trigger On Situation variable.current.position.situationName, // Take Position Situation hitOrFial(), // Result variable.last.position.ROI, // ROI @@ -2066,8 +1528,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Simulation Record */ let simulationRecord - if (variable.current.balance.baseAsset === Infinity) { - variable.current.balance.baseAsset = Number.MAX_SAFE_INTEGER + if (tradingEngine.current.balance.baseAsset.value === Infinity) { + tradingEngine.current.balance.baseAsset.value = Number.MAX_SAFE_INTEGER } if (variable.current.balance.quotedAsset === Infinity) { @@ -2077,7 +1539,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT simulationRecord = [ candle.begin, candle.end, - variable.current.balance.baseAsset, + tradingEngine.current.balance.baseAsset.value, variable.current.balance.quotedAsset, variable.episode.stat.profitLoss, variable.last.position.profitLoss, @@ -2097,14 +1559,14 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.takeProfit.phase, variable.current.position.size, variable.episode.parameters.initial.balance.baseAsset, - variable.episode.parameters.minimum.balance.baseAsset, - variable.episode.parameters.maximum.balance.baseAsset, + sessionParameters.sessionBaseAsset.config.minimumBalance, + sessionParameters.sessionBaseAsset.config.maximumBalance, variable.episode.parameters.initial.balance.quotedAsset, - variable.episode.parameters.minimum.balance.quotedAsset, - variable.episode.parameters.maximum.balance.quotedAsset, - '"' + variable.episode.parameters.baseAsset + '"', + sessionParameters.sessionQuotedAsset.config.minimumBalance, + sessionParameters.sessionQuotedAsset.config.maximumBalance, + '"' + sessionParameters.sessionBaseAsset.name + '"', '"' + variable.episode.parameters.quotedAsset + '"', - '"' + variable.episode.parameters.marketBaseAsset + '"', + '"' + bot.market.marketBaseAsset + '"', '"' + variable.episode.parameters.marketQuotedAsset + '"', variable.current.position.count.periods, variable.current.position.stat.days @@ -2118,7 +1580,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let conditionsRecord = [ candle.begin, candle.end, - variable.current.strategy.index, + tradingEngine.current.strategy.index.value, variable.current.position.stopLoss.phase, variable.current.position.takeProfit.phase, conditionsValues, @@ -2134,22 +1596,22 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT Lets see if there will be an open strategy ... Except if we are at the head of the market (remember we skipped the last candle for not being closed.) */ - if (variable.current.strategy.begin !== 0 && variable.current.strategy.end === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { - variable.current.strategy.status = 2 // This means the strategy is open, i.e. that has a begin but no end. - variable.current.strategy.end = candle.end + if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { + tradingEngine.current.strategy.status.value = 'Open' + tradingEngine.current.strategy.end.value = candle.end } /* Prepare the information for the Strategies File */ - if (variable.current.strategy.begin !== 0 && variable.current.strategy.end !== 0) { + if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value !== 0) { let strategyRecord = [ - variable.current.strategy.begin, - variable.current.strategy.end, - variable.current.strategy.status, - variable.current.strategy.number, - variable.current.strategy.beginRate, - variable.current.strategy.endRate, - '"' + variable.current.strategy.situationName + '"', - '"' + variable.current.strategy.name + '"' + tradingEngine.current.strategy.begin.value, + tradingEngine.current.strategy.end.value, + tradingEngine.current.strategy.status.value, + variable.current.strategy.index, + tradingEngine.current.strategy.beginRate.value, + tradingEngine.current.strategy.endRate.value, + '"' + tradingEngine.current.strategy.situationName.value + '"', + '"' + tradingEngine.current.strategy.strategyName.value + '"' ] strategiesArray.push(strategyRecord) @@ -2174,7 +1636,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.endRate = candle.close /* Here we will calculate the ongoing ROI */ - if (variable.episode.parameters.baseAsset === variable.episode.parameters.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { variable.current.position.stat.ROI = (variable.current.position.rate - candle.close) / variable.current.position.rate * 100 } else { variable.current.position.stat.ROI = (candle.close - variable.current.position.rate) / variable.current.position.rate * 100 diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index 3378f7cf0..c32f3a716 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -1,6 +1,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const MODULE_NAME = 'Trading System' + const FULL_LOG = true let thisObject = { evalConditions: evalConditions, @@ -100,13 +101,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function evalCondition(node) { let value + let error try { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> code = ' + node.code) } + value = eval(node.code) } catch (err) { /* - One possible error is that the conditions references a .previous that is undefined. For this - reason and others, we will simply set the value to false. + One possible error is that the conditions references a .previous that is undefined. This + will not be considered an error. */ value = false @@ -117,7 +121,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. */ } else { - node.error = err.message + ' @ ' + (new Date(candle.begin)).toLocaleString() + error = err.message + node.error = error } } @@ -128,17 +133,24 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } else { conditionsValues.push(0) } + + formulasErrors.push(error) + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> value = ' + value) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> error = ' + error) } } function evalFormula(node) { let value + let error try { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> code = ' + node.code) } value = eval(node.code) } catch (err) { /* - One possible error is that the conditions references a .previous that is undefined. For this - reason and others, we will simply set the value to false. + One possible error is that the formula references a .previous that is undefined. This + will not be considered an error. */ value = false @@ -149,7 +161,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. */ } else { - node.error = err.message + ' @ ' + (new Date(candle.begin)).toLocaleString() + error = err.message + node.error = error } } @@ -160,6 +173,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } else { formulaValues.push(0) } + + formulasErrors.push(error) + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } } } From f00757bf7a33ed67ee67414fb406f2f76722580f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 3 Jun 2020 12:14:44 +0200 Subject: [PATCH 003/113] Move functions to trading system --- Trading/TradingSimulation.js | 361 ++++++++++------------------------- Trading/TradingSystem.js | 181 ++++++++++++++++++ 2 files changed, 287 insertions(+), 255 deletions(-) diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 2d1d9a634..474613adb 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -297,9 +297,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.initialize(chart) tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() - tradingSystemModule.finalize() - /* Trigger On Conditions */ + + + /* Checks for Minimun and Maximun Balance. We do the check while not inside any strategy only. */ if ( tradingEngine.current.strategy.stageType.value === 'No Stage' && tradingEngine.current.strategy.index.value === -1 @@ -317,7 +318,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance } else { - balance = variable.current.balance.quotedAsset + balance = tradingEngine.current.balance.quotedAsset.value minimumBalance = sessionParameters.sessionQuotedAsset.config.minimumBalance maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance } @@ -336,162 +337,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT afterLoop() return } - - /* - Here we need to pick a strategy, or if there is not suitable strategy for the current - market conditions, we pass until the next period. - - To pick a new strategy we will evaluate what we call the trigger on. Once we enter - into one strategy, we will ignore market conditions for others. However there is also - a strategy trigger off which can be hit before taking a position. If hit, we would - be outside a strategy again and looking for the condition to enter all over again. - */ - - for (let j = 0; j < tradingSystem.strategies.length; j++) { - if ( // If a strategy was already picked during the loop, we exit it - tradingEngine.current.strategy.stageType.value !== 'No Stage' || - tradingEngine.current.strategy.index.value !== -1 - ) { continue } - - let strategy = tradingSystem.strategies[j] - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.triggerOn !== undefined) { - for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { - let situation = triggerStage.triggerOn.situations[k] - let passed - if (situation.conditions.length > 0) { - 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).value - } - - if (value === false) { passed = false } - } - - if (passed) { - - tradingEngine.current.strategy.stageType.value = 'Trigger Stage' - checkAnnouncements(triggerStage) - - tradingEngine.current.strategy.index.value = j - tradingEngine.current.strategy.begin.value = candle.begin - tradingEngine.current.strategy.beginRate.value = candle.min - tradingEngine.current.strategy.endRate.value = candle.min // In case the strategy does not get exited - tradingEngine.current.strategy.situationName.value = situation.name - tradingEngine.current.strategy.strategyName.value = strategy.name - - tradingEngine.current.distanceToEvent.triggerOn.value = 1 - - checkAnnouncements(triggerStage.triggerOn) - saveAsLastTriggerOnSnapshot = true - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering into Strategy: ' + strategy.name) } - break - } - } - } - } - } } - /* Trigger Off Condition */ - if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.triggerOff !== undefined) { - for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { - let situation = triggerStage.triggerOff.situations[k] - let passed - if (situation.conditions.length > 0) { - 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).value - } - - if (value === false) { passed = false } - } - - if (passed) { - tradingEngine.current.strategy.end.value = candle.end - tradingEngine.current.strategy.endRate.value = candle.min - tradingEngine.current.strategy.status.value = 'Closed' - tradingEngine.current.strategy.stageType.value = 'No Stage' - tradingEngine.current.strategy.index.value = -1 - - tradingEngine.current.distanceToEvent.triggerOff.value = 1 - - checkAnnouncements(triggerStage.triggerOff) - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Exiting Strategy: ' + strategy.name) } - break - } - } - } - } - } - - /* Take Position Condition */ - if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.takePosition !== undefined) { - for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { - let situation = triggerStage.takePosition.situations[k] - let passed - if (situation.conditions.length > 0) { - 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).value - } - - if (value === false) { passed = false } - } - - if (passed) { - tradingEngine.current.strategy.stageType.value = 'Open Stage' - checkAnnouncements(strategy.openStage) - - variable.current.position.stopLoss.stage = 'Open Stage' - variable.current.position.takeProfit.stage = 'Open Stage' - variable.current.position.stopLoss.phase = 0 - variable.current.position.takeProfit.phase = 0 - - takePositionNow = true - variable.current.position.situationName = situation.name - - checkAnnouncements(triggerStage.takePosition) - saveAsLastTakePositionSnapshot = true - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Conditions at the Take Position Event were met.') } - break - } - } - } - } - } + tradingSystemModule.checkTriggerOn() + tradingSystemModule.checkTriggerOff() + tradingSystemModule.checkTakePosition() + tradingSystemModule.finalize() /* Stop Loss Management */ if ( @@ -513,22 +364,22 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let initialDefinitionKey = '' let p - if (variable.current.position.stopLoss.stage === 'Open Stage' && openStage !== undefined) { + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { if (openStage.initialDefinition !== undefined) { if (openStage.initialDefinition.stopLoss !== undefined) { parentNode = openStage.initialDefinition initialDefinitionKey = '-' + 'initialDefinition' stageKey = 'openStage' - p = variable.current.position.stopLoss.phase + p = tradingEngine.current.position.stopLoss.stopLossPhase.value } } } - if (variable.current.position.stopLoss.stage === 'Manage Stage' && manageStage !== undefined) { + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { if (manageStage.stopLoss !== undefined) { parentNode = manageStage stageKey = 'manageStage' - p = variable.current.position.stopLoss.phase - 1 + p = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 } } @@ -558,9 +409,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } if (passed) { - variable.current.position.stopLoss.phase++ - variable.current.position.stopLoss.stage = 'Manage Stage' - if (variable.current.position.takeProfit.phase > 0) { + tradingEngine.current.position.stopLoss.stopLossPhase.value++ + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' + if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Take Profit') } @@ -601,7 +452,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (moveToPhase !== undefined) { for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { - variable.current.position.stopLoss.phase = q + 1 + tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 } } } else { @@ -609,8 +460,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT continue } - variable.current.position.stopLoss.stage = 'Manage Stage' - if (variable.current.position.takeProfit.phase > 0) { + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' + if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Take Profit') } @@ -631,30 +482,30 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let phase let key - if (variable.current.position.stopLoss.stage === 'Open Stage' && openStage !== undefined) { + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { if (openStage.initialDefinition !== undefined) { if (openStage.initialDefinition.stopLoss !== undefined) { - phase = openStage.initialDefinition.stopLoss.phases[variable.current.position.stopLoss.phase] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase) + phase = openStage.initialDefinition.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value) } } } - if (variable.current.position.stopLoss.stage === 'Manage Stage' && manageStage !== undefined) { + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { if (manageStage.stopLoss !== undefined) { - phase = manageStage.stopLoss.phases[variable.current.position.stopLoss.phase - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (variable.current.position.stopLoss.phase - 1) + phase = manageStage.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value - 1) } } if (phase !== undefined) { if (phase.formula !== undefined) { - let previousValue = variable.current.position.stopLoss.value + let previousValue = tradingEngine.current.position.stopLoss.value - variable.current.position.stopLoss.value = formulas.get(key) + tradingEngine.current.position.stopLoss.value = formulas.get(key) - if (variable.current.position.stopLoss.value !== previousValue) { - checkAnnouncements(phase, variable.current.position.stopLoss.value) + if (tradingEngine.current.position.stopLoss.value !== previousValue) { + checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) } } } @@ -680,22 +531,22 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let initialDefinitionKey = '' let p - if (variable.current.position.takeProfit.stage === 'Open Stage' && openStage !== undefined) { + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { if (openStage.initialDefinition !== undefined) { if (openStage.initialDefinition.takeProfit !== undefined) { parentNode = openStage.initialDefinition initialDefinitionKey = '-' + 'initialDefinition' stageKey = 'openStage' - p = variable.current.position.takeProfit.phase + p = tradingEngine.current.position.takeProfit.takeProfitPhase.value } } } - if (variable.current.position.takeProfit.stage === 'Manage Stage' && manageStage !== undefined) { + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { if (manageStage.takeProfit !== undefined) { parentNode = manageStage stageKey = 'manageStage' - p = variable.current.position.takeProfit.phase - 1 + p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 } } @@ -726,9 +577,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } if (passed) { - variable.current.position.takeProfit.phase++ - variable.current.position.takeProfit.stage = 'Manage Stage' - if (variable.current.position.stopLoss.phase > 0) { + tradingEngine.current.position.takeProfit.takeProfitPhase.value++ + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' + if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Stop') } @@ -769,7 +620,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (moveToPhase !== undefined) { for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { - variable.current.position.takeProfit.phase = q + 1 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 } } } else { @@ -777,8 +628,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT continue } - variable.current.position.takeProfit.stage = 'Manage Stage' - if (variable.current.position.stopLoss.phase > 0) { + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' + if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Stop') } @@ -799,30 +650,30 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let phase let key - if (variable.current.position.takeProfit.stage === 'Open Stage' && openStage !== undefined) { + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { if (openStage.initialDefinition !== undefined) { if (openStage.initialDefinition.takeProfit !== undefined) { - phase = openStage.initialDefinition.takeProfit.phases[variable.current.position.takeProfit.phase] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase) + phase = openStage.initialDefinition.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value) } } } - if (variable.current.position.takeProfit.stage === 'Manage Stage' && manageStage !== undefined) { + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { if (manageStage.takeProfit !== undefined) { - phase = manageStage.takeProfit.phases[variable.current.position.takeProfit.phase - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (variable.current.position.takeProfit.phase - 1) + phase = manageStage.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1) } } if (phase !== undefined) { if (phase.formula !== undefined) { - let previousValue = variable.current.position.stopLoss.value + let previousValue = tradingEngine.current.position.stopLoss.value - variable.current.position.takeProfit.value = formulas.get(key) + tradingEngine.current.position.takeProfit.value = formulas.get(key) - if (variable.current.position.takeProfit.value !== previousValue) { - checkAnnouncements(phase, variable.current.position.takeProfit.value) + if (tradingEngine.current.position.takeProfit.value !== previousValue) { + checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) } } } @@ -833,14 +684,14 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') ) { if (takePositionNow === true) { - variable.current.position.count.periods = 0 + tradingEngine.current.position.positionCounters.periods.value = 0 } - variable.current.position.count.periods++ - variable.current.position.stat.days = variable.current.position.count.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS + tradingEngine.current.position.positionCounters.periods.value++ + tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS } else { - variable.current.position.count.periods = 0 - variable.current.position.stat.days = 0 + tradingEngine.current.position.positionCounters.periods.value = 0 + tradingEngine.current.position.positionStatistics.days.value = 0 } /* Keeping Distance Counters Up-to-date */ @@ -857,15 +708,15 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } if ( - variable.current.distance.toEvent.takePosition > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.takePosition.value > 0 // with this we avoind counting before the first event happens. ) { - variable.current.distance.toEvent.takePosition++ + tradingEngine.current.distanceToEvent.takePosition.value++ } if ( - variable.current.distance.toEvent.closePosition > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.closePosition.value > 0 // with this we avoind counting before the first event happens. ) { - variable.current.distance.toEvent.closePosition++ + tradingEngine.current.distanceToEvent.closePosition.value++ } /* Checking if Stop or Take Profit were hit */ @@ -880,7 +731,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.max >= variable.current.position.stopLoss.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.min <= variable.current.position.stopLoss.value)) { + if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.stopLoss.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.stopLoss.value)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } /* Hit Point Validation @@ -890,16 +741,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT */ if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (variable.current.position.stopLoss.value < candle.min) { - variable.current.position.stopLoss.value = candle.min + if (tradingEngine.current.position.stopLoss.value < candle.min) { + tradingEngine.current.position.stopLoss.value = candle.min } } else { - if (variable.current.position.stopLoss.value > candle.max) { - variable.current.position.stopLoss.value = candle.max + if (tradingEngine.current.position.stopLoss.value > candle.max) { + tradingEngine.current.position.stopLoss.value = candle.max } } - let slippedStopLoss = variable.current.position.stopLoss.value + let slippedStopLoss = tradingEngine.current.position.stopLoss.value /* Apply the Slippage */ let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 @@ -915,8 +766,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.strategy.stageType.value = 'Close Stage' checkAnnouncements(strategy.closeStage, 'Stop') - variable.current.position.stopLoss.stage = 'No Stage' - variable.current.position.takeProfit.stage = 'No Stage' + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' variable.current.position.end = candle.end variable.current.position.status = 1 variable.current.position.exitType = 1 @@ -927,7 +778,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ - if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.min <= variable.current.position.takeProfit.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.max >= variable.current.position.takeProfit.value)) { + if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.takeProfit.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.takeProfit.value)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } /* Hit Point Validation: @@ -937,16 +788,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT */ if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (variable.current.position.takeProfit.value > candle.max) { - variable.current.position.takeProfit.value = candle.max + if (tradingEngine.current.position.takeProfit.value > candle.max) { + tradingEngine.current.position.takeProfit.value = candle.max } } else { - if (variable.current.position.takeProfit.value < candle.min) { - variable.current.position.takeProfit.value = candle.min + if (tradingEngine.current.position.takeProfit.value < candle.min) { + tradingEngine.current.position.takeProfit.value = candle.min } } - let slippedTakeProfit = variable.current.position.takeProfit.value + let slippedTakeProfit = tradingEngine.current.position.takeProfit.value /* Apply the Slippage */ let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 @@ -961,8 +812,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.strategy.stageType.value = 'Close Stage' checkAnnouncements(strategy.closeStage, 'Take Profit') - variable.current.position.stopLoss.stage = 'No Stage' - variable.current.position.takeProfit.stage = 'No Stage' + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' variable.current.position.end = candle.end variable.current.position.status = 1 @@ -982,7 +833,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Entering code block.') } /* Inicializing this counter */ - variable.current.distance.toEvent.takePosition = 1 + tradingEngine.current.distanceToEvent.takePosition.value = 1 /* Position size and rate */ let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] @@ -1189,7 +1040,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT calculateStopLoss() variable.previous.balance.baseAsset = tradingEngine.current.balance.baseAsset.value - variable.previous.balance.quotedAsset = variable.current.balance.quotedAsset + variable.previous.balance.quotedAsset = tradingEngine.current.balance.quotedAsset.value variable.last.position.profitLoss = 0 variable.last.position.ROI = 0 @@ -1199,13 +1050,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { feePaid = variable.current.position.size * variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + variable.current.position.size * variable.current.position.rate - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + variable.current.position.size * variable.current.position.rate - feePaid tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - variable.current.position.size } else { feePaid = variable.current.position.size / variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + variable.current.position.size / variable.current.position.rate - feePaid - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset - variable.current.position.size + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - variable.current.position.size } addRecords() @@ -1221,7 +1072,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT closePositionNow = false /* Inicializing this counter */ - variable.current.distance.toEvent.closePosition = 1 + tradingEngine.current.distanceToEvent.closePosition.value = 1 /* Position size and rate */ let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] @@ -1293,8 +1144,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - amountA = variable.current.balance.quotedAsset - amountB = variable.current.balance.quotedAsset / orderPrice + amountA = tradingEngine.current.balance.quotedAsset.value + amountB = tradingEngine.current.balance.quotedAsset.value / orderPrice } else { orderSide = 'sell' @@ -1362,20 +1213,20 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let feePaid = 0 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - strategy.positionSize = variable.current.balance.quotedAsset / closeRate + strategy.positionSize = tradingEngine.current.balance.quotedAsset.value / closeRate strategy.positionRate = closeRate - feePaid = variable.current.balance.quotedAsset / closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.balance.quotedAsset.value / closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + variable.current.balance.quotedAsset / closeRate - feePaid - variable.current.balance.quotedAsset = 0 + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / closeRate - feePaid + tradingEngine.current.balance.quotedAsset.value = 0 } else { strategy.positionSize = tradingEngine.current.balance.baseAsset.value * closeRate strategy.positionRate = closeRate feePaid = tradingEngine.current.balance.baseAsset.value * closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 - variable.current.balance.quotedAsset = variable.current.balance.quotedAsset + tradingEngine.current.balance.baseAsset.value * closeRate - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * closeRate - feePaid tradingEngine.current.balance.baseAsset.value = 0 } @@ -1385,10 +1236,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } variable.episode.stat.profitLoss = tradingEngine.current.balance.baseAsset.value - variable.episode.parameters.initial.balance.baseAsset } else { - variable.last.position.profitLoss = variable.current.balance.quotedAsset - variable.previous.balance.quotedAsset + variable.last.position.profitLoss = tradingEngine.current.balance.quotedAsset.value - variable.previous.balance.quotedAsset variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } - variable.episode.stat.profitLoss = variable.current.balance.quotedAsset - variable.episode.parameters.initial.balance.quotedAsset + variable.episode.stat.profitLoss = tradingEngine.current.balance.quotedAsset.value - variable.episode.parameters.initial.balance.quotedAsset } variable.current.position.stat.ROI = variable.last.position.ROI @@ -1411,17 +1262,17 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT addRecords() - variable.current.position.stopLoss.value = 0 - variable.current.position.takeProfit.value = 0 + tradingEngine.current.position.stopLoss.value = 0 + tradingEngine.current.position.takeProfit.value = 0 variable.current.position.rate = 0 variable.current.position.size = 0 timerToCloseStage = candle.begin - variable.current.position.stopLoss.stage = 'No Stage' - variable.current.position.takeProfit.stage = 'No Stage' - variable.current.position.stopLoss.phase = -1 - variable.current.position.takeProfit.phase = -1 + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = -1 if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> closePositionAtSimulation -> Exiting Loop Body after closing position at simulation.') } controlLoop() @@ -1491,7 +1342,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT (new Date(candle.begin)).toISOString(), // Datetime tradingSystem.strategies[tradingEngine.current.strategy.index.value].name, // Strategy Name tradingEngine.current.strategy.situationName.value, // Trigger On Situation - variable.current.position.situationName, // Take Position Situation + tradingEngine.current.position.situationName.value, // Take Position Situation hitOrFial(), // Result variable.last.position.ROI, // ROI exitType() // Exit Type @@ -1532,18 +1383,18 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.balance.baseAsset.value = Number.MAX_SAFE_INTEGER } - if (variable.current.balance.quotedAsset === Infinity) { - variable.current.balance.quotedAsset = Number.MAX_SAFE_INTEGER + if (tradingEngine.current.balance.quotedAsset.value === Infinity) { + tradingEngine.current.balance.quotedAsset.value = Number.MAX_SAFE_INTEGER } simulationRecord = [ candle.begin, candle.end, tradingEngine.current.balance.baseAsset.value, - variable.current.balance.quotedAsset, + tradingEngine.current.balance.quotedAsset.value, variable.episode.stat.profitLoss, variable.last.position.profitLoss, - variable.current.position.stopLoss.value, + tradingEngine.current.position.stopLoss.value, variable.episode.count.positions, variable.episode.count.hits, variable.episode.count.fails, @@ -1554,9 +1405,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.episode.stat.anualizedRateOfReturn, variable.current.position.rate, variable.last.position.ROI, - variable.current.position.takeProfit.value, - variable.current.position.stopLoss.phase, - variable.current.position.takeProfit.phase, + tradingEngine.current.position.takeProfit.value, + tradingEngine.current.position.stopLoss.stopLossPhase.value, + tradingEngine.current.position.takeProfit.takeProfitPhase.value, variable.current.position.size, variable.episode.parameters.initial.balance.baseAsset, sessionParameters.sessionBaseAsset.config.minimumBalance, @@ -1568,8 +1419,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT '"' + variable.episode.parameters.quotedAsset + '"', '"' + bot.market.marketBaseAsset + '"', '"' + variable.episode.parameters.marketQuotedAsset + '"', - variable.current.position.count.periods, - variable.current.position.stat.days + tradingEngine.current.position.positionCounters.periods.value, + tradingEngine.current.position.positionStatistics.days.value ] recordsArray.push(simulationRecord) @@ -1581,8 +1432,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT candle.begin, candle.end, tradingEngine.current.strategy.index.value, - variable.current.position.stopLoss.phase, - variable.current.position.takeProfit.phase, + tradingEngine.current.position.stopLoss.stopLossPhase.value, + tradingEngine.current.position.takeProfit.takeProfitPhase.value, conditionsValues, formulasErrors, formulasValues @@ -1653,7 +1504,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT variable.current.position.beginRate, variable.current.position.endRate, variable.current.position.exitType, - '"' + variable.current.position.situationName + '"' + '"' + tradingEngine.current.position.situationName.value + '"' ] positionsArray.push(positionRecord) diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index c32f3a716..393b1ae23 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -6,6 +6,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let thisObject = { evalConditions: evalConditions, evalFormulas: evalFormulas, + checkTriggerOn: checkTriggerOn, + checkTriggerOff: checkTriggerOff, + checkTakePosition: checkTakePosition, initialize: initialize, finalize: finalize } @@ -179,5 +182,183 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } } + + function checkTriggerOn() { + let tradingSystem = bot.TRADING_SYSTEM + let tradingEngine = bot.TRADING_ENGINE + + /* Trigger On Conditions */ + if ( + tradingEngine.current.strategy.stageType.value === 'No Stage' && + tradingEngine.current.strategy.index.value === -1 + ) { + + /* + Here we need to pick a strategy, or if there is not suitable strategy for the current + market conditions, we pass until the next period. + + To pick a new strategy we will evaluate what we call the trigger on. Once we enter + into one strategy, we will ignore market conditions for others. However there is also + a strategy trigger off which can be hit before taking a position. If hit, we would + be outside a strategy again and looking for the condition to enter all over again. + */ + + for (let j = 0; j < tradingSystem.strategies.length; j++) { + if ( // If a strategy was already picked during the loop, we exit it + tradingEngine.current.strategy.stageType.value !== 'No Stage' || + tradingEngine.current.strategy.index.value !== -1 + ) { continue } + + let strategy = tradingSystem.strategies[j] + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + if (triggerStage.triggerOn !== undefined) { + for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { + let situation = triggerStage.triggerOn.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + + tradingEngine.current.strategy.stageType.value = 'Trigger Stage' + checkAnnouncements(triggerStage) + + tradingEngine.current.strategy.index.value = j + tradingEngine.current.strategy.begin.value = candle.begin + tradingEngine.current.strategy.beginRate.value = candle.min + tradingEngine.current.strategy.endRate.value = candle.min // In case the strategy does not get exited + tradingEngine.current.strategy.situationName.value = situation.name + tradingEngine.current.strategy.strategyName.value = strategy.name + + tradingEngine.current.distanceToEvent.triggerOn.value = 1 + + checkAnnouncements(triggerStage.triggerOn) + saveAsLastTriggerOnSnapshot = true + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering into Strategy: ' + strategy.name) } + break + } + } + } + } + } + } + } + + function checkTriggerOff() { + let tradingSystem = bot.TRADING_SYSTEM + let tradingEngine = bot.TRADING_ENGINE + + /* Trigger Off Condition */ + if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + if (triggerStage.triggerOff !== undefined) { + for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { + let situation = triggerStage.triggerOff.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + tradingEngine.current.strategy.end.value = candle.end + tradingEngine.current.strategy.endRate.value = candle.min + tradingEngine.current.strategy.status.value = 'Closed' + tradingEngine.current.strategy.stageType.value = 'No Stage' + tradingEngine.current.strategy.index.value = -1 + + tradingEngine.current.distanceToEvent.triggerOff.value = 1 + + checkAnnouncements(triggerStage.triggerOff) + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Exiting Strategy: ' + strategy.name) } + break + } + } + } + } + } + + } + + function checkTakePosition() { + let tradingSystem = bot.TRADING_SYSTEM + let tradingEngine = bot.TRADING_ENGINE + + /* Take Position Condition */ + if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + if (triggerStage.takePosition !== undefined) { + for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { + let situation = triggerStage.takePosition.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + tradingEngine.current.strategy.stageType.value = 'Open Stage' + checkAnnouncements(strategy.openStage) + + tradingEngine.current.position.stopLossStage.value = 'Open Stage' + tradingEngine.current.position.takeProfitStage.value = 'Open Stage' + tradingEngine.current.position.stopLossPhase.value = 0 + tradingEngine.current.position.takeProfitPhase.value = 0 + + takePositionNow = true + tradingEngine.current.position.situationName.value = situation.name + + checkAnnouncements(triggerStage.takePosition) + saveAsLastTakePositionSnapshot = true + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Conditions at the Take Position Event were met.') } + break + } + } + } + } + } + } } From 2c3d53b3a40b62c10bbd3c05591c0946ac930ba7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 3 Jun 2020 13:23:21 +0200 Subject: [PATCH 004/113] All variable converted except execution and announcements --- Trading/TradingSimulation.js | 182 +++++++++++++++++------------------ 1 file changed, 90 insertions(+), 92 deletions(-) diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 474613adb..98f33d7c6 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -768,10 +768,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - variable.current.position.end = candle.end - variable.current.position.status = 1 - variable.current.position.exitType = 1 - variable.current.position.endRate = closeRate + tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.status.value = 1 + tradingEngine.current.position.exitType.value = 1 + tradingEngine.current.position.endRate.value = closeRate closePositionNow = true } @@ -815,10 +815,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - variable.current.position.end = candle.end - variable.current.position.status = 1 - variable.current.position.exitType = 2 - variable.current.position.endRate = closeRate + tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.status.value = 1 + tradingEngine.current.position.exitType.value = 2 + tradingEngine.current.position.endRate.value = closeRate closePositionNow = true addToSnapshots = true @@ -838,16 +838,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Position size and rate */ let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - variable.current.position.size = strategy.positionSize - variable.current.position.rate = strategy.positionRate + tradingEngine.current.position.size.value = strategy.positionSize + tradingEngine.current.position.rate.value = strategy.positionRate /* We take what was calculated at the formula and apply the slippage. */ - let slippageAmount = variable.current.position.rate * bot.VALUES_TO_USE.slippage.positionRate / 100 + let slippageAmount = tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.slippage.positionRate / 100 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - variable.current.position.rate = variable.current.position.rate - slippageAmount + tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount } else { - variable.current.position.rate = variable.current.position.rate + slippageAmount + tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount } if (bot.startMode === 'Live') { @@ -855,12 +855,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> strategy.positionSize = ' + strategy.positionSize) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> strategy.positionRate = ' + strategy.positionRate) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> variable.current.position.rate = ' + variable.current.position.rate) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) } /* Update the trade record information. */ - variable.current.position.begin = candle.begin - variable.current.position.beginRate = variable.current.position.rate + tradingEngine.current.position.begin.value = candle.begin + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value /* Check if we need to execute. */ if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ @@ -968,17 +968,17 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { orderSide = 'sell' - orderPrice = variable.current.position.rate + orderPrice = tradingEngine.current.position.rate.value - amountA = variable.current.position.size * orderPrice - amountB = variable.current.position.size + amountA = tradingEngine.current.position.size.value * orderPrice + amountB = tradingEngine.current.position.size.value } else { orderSide = 'buy' - orderPrice = variable.current.position.rate + orderPrice = tradingEngine.current.position.rate.value - amountA = variable.current.position.size - amountB = variable.current.position.size / orderPrice + amountA = tradingEngine.current.position.size.value + amountB = tradingEngine.current.position.size.value / orderPrice } variable.executionContext = { @@ -1039,24 +1039,24 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT calculateTakeProfit() calculateStopLoss() - variable.previous.balance.baseAsset = tradingEngine.current.balance.baseAsset.value - variable.previous.balance.quotedAsset = tradingEngine.current.balance.quotedAsset.value + tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - variable.last.position.profitLoss = 0 - variable.last.position.ROI = 0 + tradingEngine.last.position.profitLoss.value = 0 + tradingEngine.last.position.ROI.value = 0 let feePaid = 0 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - feePaid = variable.current.position.size * variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + variable.current.position.size * variable.current.position.rate - feePaid - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - variable.current.position.size + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value } else { - feePaid = variable.current.position.size / variable.current.position.rate * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + variable.current.position.size / variable.current.position.rate - feePaid - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - variable.current.position.size + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value } addRecords() @@ -1208,7 +1208,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function closePositionAtSimulation() { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> closePositionAtSimulation -> Entering function.') } - variable.episode.count.positions++ + tradingEngine.episode.positionCounters.positions.value++ let feePaid = 0 @@ -1231,33 +1231,33 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - variable.last.position.profitLoss = tradingEngine.current.balance.baseAsset.value - variable.previous.balance.baseAsset - variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size - if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } - variable.episode.stat.profitLoss = tradingEngine.current.balance.baseAsset.value - variable.episode.parameters.initial.balance.baseAsset + tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value + tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance } else { - variable.last.position.profitLoss = tradingEngine.current.balance.quotedAsset.value - variable.previous.balance.quotedAsset - variable.last.position.ROI = variable.last.position.profitLoss * 100 / variable.current.position.size - if (isNaN(variable.last.position.ROI)) { variable.last.position.ROI = 0 } - variable.episode.stat.profitLoss = tradingEngine.current.balance.quotedAsset.value - variable.episode.parameters.initial.balance.quotedAsset + tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value + tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance } - variable.current.position.stat.ROI = variable.last.position.ROI + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.ROI.value - if (variable.last.position.profitLoss > 0) { - variable.episode.count.hits++ + if (tradingEngine.last.position.profitLoss.value > 0) { + tradingEngine.episode.episodeCounters.hits.value++ } else { - variable.episode.count.fails++ + tradingEngine.episode.episodeCounters.fails.value++ } if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.baseAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.baseAsset - 1 - variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions - variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / tradingEngine.episode.episodeStatistics.days * 365 + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 } else { - variable.episode.stat.ROI = (variable.episode.parameters.initial.balance.quotedAsset + variable.episode.stat.profitLoss) / variable.episode.parameters.initial.balance.quotedAsset - 1 - variable.episode.stat.hitRatio = variable.episode.count.hits / variable.episode.count.positions - variable.episode.stat.anualizedRateOfReturn = variable.episode.stat.ROI / tradingEngine.episode.episodeStatistics.days * 365 + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 } addRecords() @@ -1265,8 +1265,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.position.stopLoss.value = 0 tradingEngine.current.position.takeProfit.value = 0 - variable.current.position.rate = 0 - variable.current.position.size = 0 + tradingEngine.current.position.rate.value = 0 + tradingEngine.current.position.size.value = 0 timerToCloseStage = candle.begin tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' @@ -1316,10 +1316,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT addPositionRecord() /* We will remmember the last candle processed, so that if it is the last one of this run we can use it to continue from there next time. */ - variable.last.candle = { - begin: candle.begin, - end: candle.end - } + tradingEngine.last.candle.begin = candle.begin + tradingEngine.last.candle.end = candle.end /* After everything at the simulation level was done, we will do the annoucements that are pending. */ makeAnnoucements() @@ -1338,22 +1336,22 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (addToSnapshots === true) { let closeValues = [ - variable.episode.count.positions, // Position Number + tradingEngine.episode.positionCounters.positions.value, // Position Number (new Date(candle.begin)).toISOString(), // Datetime tradingSystem.strategies[tradingEngine.current.strategy.index.value].name, // Strategy Name tradingEngine.current.strategy.situationName.value, // Trigger On Situation tradingEngine.current.position.situationName.value, // Take Position Situation hitOrFial(), // Result - variable.last.position.ROI, // ROI + tradingEngine.last.position.ROI.value, // ROI exitType() // Exit Type ] function hitOrFial() { - if (variable.last.position.ROI > 0) { return 'HIT' } else { return 'FAIL' } + if (tradingEngine.last.position.ROI.value > 0) { return 'HIT' } else { return 'FAIL' } } function exitType() { - switch (variable.current.position.exitType) { + switch (tradingEngine.current.position.exitType.value) { case 1: return 'Stop' case 2: return 'Take Profit' } @@ -1392,33 +1390,33 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT candle.end, tradingEngine.current.balance.baseAsset.value, tradingEngine.current.balance.quotedAsset.value, - variable.episode.stat.profitLoss, - variable.last.position.profitLoss, + tradingEngine.episode.episodeStatistics.profitLoss.value, + tradingEngine.last.position.profitLoss.value, tradingEngine.current.position.stopLoss.value, - variable.episode.count.positions, - variable.episode.count.hits, - variable.episode.count.fails, - variable.episode.stat.hitRatio, - variable.episode.stat.ROI, + tradingEngine.episode.positionCounters.positions.value, + tradingEngine.episode.episodeCounters.hits.value, + tradingEngine.episode.episodeCounters.fails.value, + tradingEngine.episode.episodeStatistics.hitRatio.value, + tradingEngine.episode.episodeStatistics.ROI.value, tradingEngine.episode.episodeCounters.periods, tradingEngine.episode.episodeStatistics.days, - variable.episode.stat.anualizedRateOfReturn, - variable.current.position.rate, - variable.last.position.ROI, + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value, + tradingEngine.current.position.rate.value, + tradingEngine.last.position.ROI.value, tradingEngine.current.position.takeProfit.value, tradingEngine.current.position.stopLoss.stopLossPhase.value, tradingEngine.current.position.takeProfit.takeProfitPhase.value, - variable.current.position.size, - variable.episode.parameters.initial.balance.baseAsset, + tradingEngine.current.position.size.value, + sessionParameters.sessionBaseAsset.config.initialBalance, sessionParameters.sessionBaseAsset.config.minimumBalance, sessionParameters.sessionBaseAsset.config.maximumBalance, - variable.episode.parameters.initial.balance.quotedAsset, + sessionParameters.sessionQuotedAsset.config.initialBalance, sessionParameters.sessionQuotedAsset.config.minimumBalance, sessionParameters.sessionQuotedAsset.config.maximumBalance, '"' + sessionParameters.sessionBaseAsset.name + '"', - '"' + variable.episode.parameters.quotedAsset + '"', + '"' + sessionParameters.sessionQuotedAsset.name + '"', '"' + bot.market.marketBaseAsset + '"', - '"' + variable.episode.parameters.marketQuotedAsset + '"', + '"' + bot.market.marketQuotedAsset + '"', tradingEngine.current.position.positionCounters.periods.value, tradingEngine.current.position.positionStatistics.days.value ] @@ -1458,7 +1456,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.strategy.begin.value, tradingEngine.current.strategy.end.value, tradingEngine.current.strategy.status.value, - variable.current.strategy.index, + tradingEngine.current.strategy.index.value, tradingEngine.current.strategy.beginRate.value, tradingEngine.current.strategy.endRate.value, '"' + tradingEngine.current.strategy.situationName.value + '"', @@ -1476,34 +1474,34 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT Lets see if there will be an open trade ... Except if we are at the head of the market (remember we skipped the last candle for not being closed.) */ - if (variable.current.position.begin !== 0 && - variable.current.position.end === 0 && + if (tradingEngine.current.position.begin.value !== 0 && + tradingEngine.current.position.end.value === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { /* This means the trade is open */ - variable.current.position.status = 2 - variable.current.position.end = candle.end - variable.current.position.endRate = candle.close + tradingEngine.current.position.status.value = 2 + tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.endRate.value = candle.close /* Here we will calculate the ongoing ROI */ if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - variable.current.position.stat.ROI = (variable.current.position.rate - candle.close) / variable.current.position.rate * 100 + tradingEngine.current.position.positionStatistics.ROI.value = (tradingEngine.current.position.rate.value - candle.close) / tradingEngine.current.position.rate.value * 100 } else { - variable.current.position.stat.ROI = (candle.close - variable.current.position.rate) / variable.current.position.rate * 100 + tradingEngine.current.position.positionStatistics.ROI.value = (candle.close - tradingEngine.current.position.rate.value) / tradingEngine.current.position.rate.value * 100 } } /* Prepare the information for the Positions File */ - if (variable.current.position.begin !== 0 && variable.current.position.end !== 0) { + if (tradingEngine.current.position.begin.value !== 0 && tradingEngine.current.position.end.value !== 0) { let positionRecord = [ - variable.current.position.begin, - variable.current.position.end, - variable.current.position.status, - variable.current.position.stat.ROI, - variable.current.position.beginRate, - variable.current.position.endRate, - variable.current.position.exitType, + tradingEngine.current.position.begin.value, + tradingEngine.current.position.end.value, + tradingEngine.current.position.status.value, + tradingEngine.current.position.positionStatistics.ROI.value, + tradingEngine.current.position.beginRate.value, + tradingEngine.current.position.endRate.value, + tradingEngine.current.position.exitType.value, '"' + tradingEngine.current.position.situationName.value + '"' ] From 15cf159a9dcb68337cfb780e4743b1013ccfc480 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Jun 2020 09:28:15 +0200 Subject: [PATCH 005/113] check stop and take profit --- Trading/TradingSimulation.js | 326 +---------------------------------- Trading/TradingSystem.js | 319 +++++++++++++++++++++++++++++++++- 2 files changed, 315 insertions(+), 330 deletions(-) diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 98f33d7c6..f9d871183 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -342,173 +342,14 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.checkTriggerOn() tradingSystemModule.checkTriggerOff() tradingSystemModule.checkTakePosition() - tradingSystemModule.finalize() /* Stop Loss Management */ if ( (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && takePositionNow !== true ) { - checkStopPhases() - calculateStopLoss() - } - - function checkStopPhases() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let parentNode - let j = tradingEngine.current.strategy.index.value - let stageKey - let initialDefinitionKey = '' - let p - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - parentNode = openStage.initialDefinition - initialDefinitionKey = '-' + 'initialDefinition' - stageKey = 'openStage' - p = tradingEngine.current.position.stopLoss.stopLossPhase.value - } - } - } - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - parentNode = manageStage - stageKey = 'manageStage' - p = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 - } - } - - if (parentNode !== undefined) { - let phase = parentNode.stopLoss.phases[p] - - /* Check the next Phase Event. */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let situation = nextPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - tradingEngine.current.position.stopLoss.stopLossPhase.value++ - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' - if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' - checkAnnouncements(manageStage, 'Take Profit') - } - - checkAnnouncements(nextPhaseEvent) - return - } - } - } - - /* Check the Move to Phase Events. */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - let situation = moveToPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + n + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - - let moveToPhase = moveToPhaseEvent.referenceParent - if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { - if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { - tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 - } - } - } else { - moveToPhaseEvent.error = 'This Node needs to reference a Phase.' - continue - } - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' - if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' - checkAnnouncements(manageStage, 'Take Profit') - } - - checkAnnouncements(moveToPhaseEvent) - return - } - } - } - } - } - } - - function calculateStopLoss() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - let key - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - phase = openStage.initialDefinition.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value) - } - } - } - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - phase = manageStage.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value - 1) - } - } - - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.stopLoss.value - - tradingEngine.current.position.stopLoss.value = formulas.get(key) - - if (tradingEngine.current.position.stopLoss.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) - } - } - } + tradingSystemModule.checkStopPhases() + tradingSystemModule.calculateStopLoss() } /* Take Profit Management */ @@ -516,168 +357,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && takePositionNow !== true ) { - checkTakeProfitPhases() - calculateTakeProfit() + tradingSystemModule.checkTakeProfitPhases() + tradingSystemModule.calculateTakeProfit() } - function checkTakeProfitPhases() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let parentNode - let j = tradingEngine.current.strategy.index.value - let stageKey - let initialDefinitionKey = '' - let p - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - parentNode = openStage.initialDefinition - initialDefinitionKey = '-' + 'initialDefinition' - stageKey = 'openStage' - p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - } - } - } - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - parentNode = manageStage - stageKey = 'manageStage' - p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 - } - } - - if (parentNode !== undefined) { - let phase = parentNode.takeProfit.phases[p] - if (phase === undefined) { return } // trying to jump to a phase that does not exists. - - /* Check the next Phase Event. */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let situation = nextPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - tradingEngine.current.position.takeProfit.takeProfitPhase.value++ - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' - if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' - checkAnnouncements(manageStage, 'Stop') - } - - checkAnnouncements(nextPhaseEvent) - return - } - } - } - - /* Check the Move to Phase Events. */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - let situation = moveToPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - passed = true - } - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + n + '-' + k + '-' + m - - let value = false - if (conditions.get(key) !== undefined) { - value = conditions.get(key).value - } - - if (value === false) { passed = false } - } - - if (passed) { - - let moveToPhase = moveToPhaseEvent.referenceParent - if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { - if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { - tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 - } - } - } else { - moveToPhaseEvent.error = 'This Node needs to reference a Phase.' - continue - } - - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' - if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' - checkAnnouncements(manageStage, 'Stop') - } - - checkAnnouncements(moveToPhaseEvent) - return - } - } - } - } - } - } - - function calculateTakeProfit() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - let key - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - phase = openStage.initialDefinition.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value) - } - } - } - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - phase = manageStage.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1) - } - } - - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.stopLoss.value - - tradingEngine.current.position.takeProfit.value = formulas.get(key) - - if (tradingEngine.current.position.takeProfit.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) - } - } - } - } + tradingSystemModule.finalize() /* Keeping Position Counters Up-to-date */ if ( diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index 393b1ae23..1cc854c51 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -9,11 +9,17 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { checkTriggerOn: checkTriggerOn, checkTriggerOff: checkTriggerOff, checkTakePosition: checkTakePosition, + checkStopPhases: checkStopPhases, + calculateStopLoss: calculateStopLoss, + checkTakeProfitPhases: checkTakeProfitPhases, + calculateTakeProfit: calculateTakeProfit, initialize: initialize, finalize: finalize } let chart + let tradingSystem + let tradingEngine let conditions = new Map() let conditionsValues = [] @@ -27,10 +33,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function initialize(pChart) { chart = pChart + tradingSystem = bot.TRADING_SYSTEM + tradingEngine = bot.TRADING_ENGINE } function finalize() { chart = undefined + tradingSystem = undefined + tradingEngine = undefined conditions = undefined conditionsValues = undefined @@ -184,9 +194,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function checkTriggerOn() { - let tradingSystem = bot.TRADING_SYSTEM - let tradingEngine = bot.TRADING_ENGINE - /* Trigger On Conditions */ if ( tradingEngine.current.strategy.stageType.value === 'No Stage' && @@ -260,9 +267,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function checkTriggerOff() { - let tradingSystem = bot.TRADING_SYSTEM - let tradingEngine = bot.TRADING_ENGINE - /* Trigger Off Condition */ if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] @@ -309,9 +313,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function checkTakePosition() { - let tradingSystem = bot.TRADING_SYSTEM - let tradingEngine = bot.TRADING_ENGINE - /* Take Position Condition */ if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] @@ -360,5 +361,305 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } } + + function checkStopPhases() { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let j = tradingEngine.current.strategy.index.value + let p + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.stopLoss !== undefined) { + parentNode = openStage.initialDefinition + p = tradingEngine.current.position.stopLoss.stopLossPhase.value + } + } + } + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + parentNode = manageStage + p = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 + } + } + + if (parentNode !== undefined) { + let phase = parentNode.stopLoss.phases[p] + + /* Check the next Phase Event. */ + let nextPhaseEvent = phase.nextPhaseEvent + if (nextPhaseEvent !== undefined) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + let situation = nextPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + tradingEngine.current.position.stopLoss.stopLossPhase.value++ + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' + if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { + tradingEngine.current.strategy.stageType.value = 'Manage Stage' + checkAnnouncements(manageStage, 'Take Profit') + } + + checkAnnouncements(nextPhaseEvent) + return + } + } + } + + /* Check the Move to Phase Events. */ + for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { + let moveToPhaseEvent = phase.moveToPhaseEvents[n] + if (moveToPhaseEvent !== undefined) { + for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { + let situation = moveToPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + + let moveToPhase = moveToPhaseEvent.referenceParent + if (moveToPhase !== undefined) { + for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { + if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { + tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 + } + } + } else { + moveToPhaseEvent.error = 'This Node needs to reference a Phase.' + continue + } + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' + if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { + tradingEngine.current.strategy.stageType.value = 'Manage Stage' + checkAnnouncements(manageStage, 'Take Profit') + } + + checkAnnouncements(moveToPhaseEvent) + return + } + } + } + } + } + } + + function calculateStopLoss() { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.stopLoss !== undefined) { + phase = openStage.initialDefinition.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value) + } + } + } + + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + phase = manageStage.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value - 1) + } + } + + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.stopLoss.value + + tradingEngine.current.position.stopLoss.value = formulas.get(key) + + if (tradingEngine.current.position.stopLoss.value !== previousValue) { + checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) + } + } + } + } + + + function checkTakeProfitPhases() { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let j = tradingEngine.current.strategy.index.value + let p + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.takeProfit !== undefined) { + parentNode = openStage.initialDefinition + p = tradingEngine.current.position.takeProfit.takeProfitPhase.value + } + } + } + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + parentNode = manageStage + p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 + } + } + + if (parentNode !== undefined) { + let phase = parentNode.takeProfit.phases[p] + if (phase === undefined) { return } // trying to jump to a phase that does not exists. + + /* Check the next Phase Event. */ + let nextPhaseEvent = phase.nextPhaseEvent + if (nextPhaseEvent !== undefined) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + let situation = nextPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + tradingEngine.current.position.takeProfit.takeProfitPhase.value++ + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' + if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { + tradingEngine.current.strategy.stageType.value = 'Manage Stage' + checkAnnouncements(manageStage, 'Stop') + } + + checkAnnouncements(nextPhaseEvent) + return + } + } + } + + /* Check the Move to Phase Events. */ + for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { + let moveToPhaseEvent = phase.moveToPhaseEvents[n] + if (moveToPhaseEvent !== undefined) { + for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { + let situation = moveToPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + + if (value === false) { passed = false } + } + + if (passed) { + + let moveToPhase = moveToPhaseEvent.referenceParent + if (moveToPhase !== undefined) { + for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { + if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { + tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 + } + } + } else { + moveToPhaseEvent.error = 'This Node needs to reference a Phase.' + continue + } + + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' + if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { + tradingEngine.current.strategy.stageType.value = 'Manage Stage' + checkAnnouncements(manageStage, 'Stop') + } + + checkAnnouncements(moveToPhaseEvent) + return + } + } + } + } + } + } + + function calculateTakeProfit() { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.takeProfit !== undefined) { + phase = openStage.initialDefinition.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value) + } + } + } + + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + phase = manageStage.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1) + } + } + + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.stopLoss.value + + tradingEngine.current.position.takeProfit.value = formulas.get(key) + + if (tradingEngine.current.position.takeProfit.value !== previousValue) { + checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) + } + } + } + } } From 707239e68442e02d62c96b5af0312415913e0ca5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Jun 2020 10:49:56 +0200 Subject: [PATCH 006/113] Until take position now --- Trading/TradingEngine.js | 58 +++++++++++- Trading/TradingSimulation.js | 175 ++++------------------------------- Trading/TradingSystem.js | 156 ++++++++++++++++++++++++++++++- 3 files changed, 222 insertions(+), 167 deletions(-) diff --git a/Trading/TradingEngine.js b/Trading/TradingEngine.js index 30207da99..d856b0c38 100644 --- a/Trading/TradingEngine.js +++ b/Trading/TradingEngine.js @@ -3,23 +3,29 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { const MODULE_NAME = 'Trading Engine' let thisObject = { + updatePositionCounters: updatePositionCounters, + updateDistanceToEventsCounters: updateDistanceToEventsCounters, initialize: initialize, finalize: finalize } + let tradingEngine + return thisObject function initialize() { /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed */ - if (bot.TRADING_ENGINE.isInitialized !== true || bot.RESUME === false) { - bot.TRADING_ENGINE.isInitialized = true - initializeNode(bot.TRADING_ENGINE) + tradingEngine = bot.TRADING_ENGINE + + if (tradingEngine.isInitialized !== true || bot.RESUME === false) { + tradingEngine.isInitialized = true + initializeNode(tradingEngine) } } function finalize() { - + tradingEngine = undefined } function initializeNode(node) { @@ -65,5 +71,49 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } } } + + function updatePositionCounters() { + /* Keeping Position Counters Up-to-date */ + if ( + (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') + ) { + if (takePositionNow === true) { + tradingEngine.current.position.positionCounters.periods.value = 0 + } + + tradingEngine.current.position.positionCounters.periods.value++ + tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS + } else { + tradingEngine.current.position.positionCounters.periods.value = 0 + tradingEngine.current.position.positionStatistics.days.value = 0 + } + } + + function updateDistanceToEventsCounters() { + /* Keeping Distance Counters Up-to-date */ + if ( + tradingEngine.current.distanceToEvent.triggerOn.value > 0 // with this we avoind counting before the first event happens. + ) { + tradingEngine.current.distanceToEvent.triggerOn.value++ + } + + if ( + tradingEngine.current.distanceToEvent.triggerOff.value > 0 // with this we avoind counting before the first event happens. + ) { + tradingEngine.current.distanceToEvent.triggerOff.value++ + } + + if ( + tradingEngine.current.distanceToEvent.takePosition.value > 0 // with this we avoind counting before the first event happens. + ) { + tradingEngine.current.distanceToEvent.takePosition.value++ + } + + if ( + tradingEngine.current.distanceToEvent.closePosition.value > 0 // with this we avoind counting before the first event happens. + ) { + tradingEngine.current.distanceToEvent.closePosition.value++ + } + } } diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index f9d871183..609191d13 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -294,7 +294,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) - tradingSystemModule.initialize(chart) + tradingSystemModule.initialize(chart, candle) tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() @@ -341,7 +341,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.checkTriggerOn() tradingSystemModule.checkTriggerOff() - tradingSystemModule.checkTakePosition() + takePositionNow = tradingSystemModule.checkTakePosition() /* Stop Loss Management */ if ( @@ -361,154 +361,20 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.calculateTakeProfit() } - tradingSystemModule.finalize() - /* Keeping Position Counters Up-to-date */ - if ( - (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') - ) { - if (takePositionNow === true) { - tradingEngine.current.position.positionCounters.periods.value = 0 - } - - tradingEngine.current.position.positionCounters.periods.value++ - tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS - } else { - tradingEngine.current.position.positionCounters.periods.value = 0 - tradingEngine.current.position.positionStatistics.days.value = 0 - } - - /* Keeping Distance Counters Up-to-date */ - if ( - tradingEngine.current.distanceToEvent.triggerOn.value > 0 // with this we avoind counting before the first event happens. - ) { - tradingEngine.current.distanceToEvent.triggerOn.value++ - } - - if ( - tradingEngine.current.distanceToEvent.triggerOff.value > 0 // with this we avoind counting before the first event happens. - ) { - tradingEngine.current.distanceToEvent.triggerOff.value++ - } - - if ( - tradingEngine.current.distanceToEvent.takePosition.value > 0 // with this we avoind counting before the first event happens. - ) { - tradingEngine.current.distanceToEvent.takePosition.value++ - } - - if ( - tradingEngine.current.distanceToEvent.closePosition.value > 0 // with this we avoind counting before the first event happens. - ) { - tradingEngine.current.distanceToEvent.closePosition.value++ - } + tradingEngineModule.updatePositionCounters() + tradingEngineModule.updateDistanceToEventsCounters() /* Checking if Stop or Take Profit were hit */ if ( (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && takePositionNow !== true ) { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - /* Checking what happened since the last execution. We need to know if the Stop Loss - or our Take Profit were hit. */ - - /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - - if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.stopLoss.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.stopLoss.value)) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } - /* - Hit Point Validation - - This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (tradingEngine.current.position.stopLoss.value < candle.min) { - tradingEngine.current.position.stopLoss.value = candle.min - } - } else { - if (tradingEngine.current.position.stopLoss.value > candle.max) { - tradingEngine.current.position.stopLoss.value = candle.max - } - } - - let slippedStopLoss = tradingEngine.current.position.stopLoss.value - - /* Apply the Slippage */ - let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - slippedStopLoss = slippedStopLoss + slippageAmount - } else { - slippedStopLoss = slippedStopLoss - slippageAmount - } - - closeRate = slippedStopLoss - - tradingEngine.current.strategy.stageType.value = 'Close Stage' - checkAnnouncements(strategy.closeStage, 'Stop') - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.end.value = candle.end - tradingEngine.current.position.status.value = 1 - tradingEngine.current.position.exitType.value = 1 - tradingEngine.current.position.endRate.value = closeRate - - closePositionNow = true - } - - /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ - - if ((sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.takeProfit.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.takeProfit.value)) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } - /* - Hit Point Validation: - - This prevents misscalculations when a formula places the take profit in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (tradingEngine.current.position.takeProfit.value > candle.max) { - tradingEngine.current.position.takeProfit.value = candle.max - } - } else { - if (tradingEngine.current.position.takeProfit.value < candle.min) { - tradingEngine.current.position.takeProfit.value = candle.min - } - } - - let slippedTakeProfit = tradingEngine.current.position.takeProfit.value - /* Apply the Slippage */ - let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - slippedTakeProfit = slippedTakeProfit + slippageAmount - } else { - slippedTakeProfit = slippedTakeProfit - slippageAmount - } - - closeRate = slippedTakeProfit - - tradingEngine.current.strategy.stageType.value = 'Close Stage' - checkAnnouncements(strategy.closeStage, 'Take Profit') - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - - tradingEngine.current.position.end.value = candle.end - tradingEngine.current.position.status.value = 1 - tradingEngine.current.position.exitType.value = 2 - tradingEngine.current.position.endRate.value = closeRate - - closePositionNow = true - addToSnapshots = true - } + closePositionNow = tradingSystemModule.checkStopLossOrTakeProfitWasHit() } + tradingSystemModule.finalize() + /* Taking a Position */ if ( takePositionNow === true @@ -520,10 +386,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.current.distanceToEvent.takePosition.value = 1 /* Position size and rate */ - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - tradingEngine.current.position.size.value = strategy.positionSize - tradingEngine.current.position.rate.value = strategy.positionRate + tradingEngine.current.position.size.value = getPositionSize() + tradingEngine.current.position.rate.value = getPositionRate() /* We take what was calculated at the formula and apply the slippage. */ let slippageAmount = tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.slippage.positionRate / 100 @@ -536,8 +400,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (bot.startMode === 'Live') { logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> Taking a Position in Live Mode.') - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> strategy.positionSize = ' + strategy.positionSize) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> strategy.positionRate = ' + strategy.positionRate) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.size.value = ' + tradingEngine.current.position.size.value) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) } @@ -897,20 +761,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let feePaid = 0 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - strategy.positionSize = tradingEngine.current.balance.quotedAsset.value / closeRate - strategy.positionRate = closeRate - feePaid = tradingEngine.current.balance.quotedAsset.value / closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / closeRate - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid tradingEngine.current.balance.quotedAsset.value = 0 } else { - strategy.positionSize = tradingEngine.current.balance.baseAsset.value * closeRate - strategy.positionRate = closeRate - feePaid = tradingEngine.current.balance.baseAsset.value * closeRate * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * closeRate - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid tradingEngine.current.balance.baseAsset.value = 0 } @@ -1370,10 +1230,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function afterLoop() { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> afterLoop -> Entering function.') } - /* - Before returning we need to see if we have to record some of our counters at the variable. - To do that, the condition to be met is that this execution must include all candles of the current day. - */ + tradingEngineModule.finalize() if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> callback -> recordsArray.length = ' + recordsArray.length) } diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index 1cc854c51..fa01e77e6 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -13,11 +13,15 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { calculateStopLoss: calculateStopLoss, checkTakeProfitPhases: checkTakeProfitPhases, calculateTakeProfit: calculateTakeProfit, + checkStopLossOrTakeProfitWasHit: checkStopLossOrTakeProfitWasHit, + getPositionSize: getPositionSize, + getPositionRate: getPositionRate, initialize: initialize, finalize: finalize } let chart + let candle let tradingSystem let tradingEngine @@ -31,14 +35,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { return thisObject - function initialize(pChart) { + function initialize(pChart, pCandle) { chart = pChart + candle = pCandle tradingSystem = bot.TRADING_SYSTEM tradingEngine = bot.TRADING_ENGINE } function finalize() { chart = undefined + candle = undefined tradingSystem = undefined tradingEngine = undefined @@ -347,19 +353,19 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.stopLossPhase.value = 0 tradingEngine.current.position.takeProfitPhase.value = 0 - takePositionNow = true tradingEngine.current.position.situationName.value = situation.name checkAnnouncements(triggerStage.takePosition) saveAsLastTakePositionSnapshot = true if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Conditions at the Take Position Event were met.') } - break + return true } } } } } + return false } function checkStopPhases() { @@ -511,7 +517,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } - function checkTakeProfitPhases() { let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] @@ -661,5 +666,148 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } } + + function checkStopLossOrTakeProfitWasHit() { + { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + + /* Checking what happened since the last execution. We need to know if the Stop Loss + or our Take Profit were hit. */ + + /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ + + if ( + (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.stopLoss.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.stopLoss.value) + ) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } + /* + Hit Point Validation + + This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (tradingEngine.current.position.stopLoss.value < candle.min) { + tradingEngine.current.position.stopLoss.value = candle.min + } + } else { + if (tradingEngine.current.position.stopLoss.value > candle.max) { + tradingEngine.current.position.stopLoss.value = candle.max + } + } + + let slippedStopLoss = tradingEngine.current.position.stopLoss.value + + /* Apply the Slippage */ + let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + slippedStopLoss = slippedStopLoss + slippageAmount + } else { + slippedStopLoss = slippedStopLoss - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedStopLoss + + tradingEngine.current.strategy.stageType.value = 'Close Stage' + checkAnnouncements(strategy.closeStage, 'Stop') + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.status.value = 1 + tradingEngine.current.position.exitType.value = 1 + + return true + } + + /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ + + if ( + (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.takeProfit.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.takeProfit.value) + ) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } + /* + Hit Point Validation: + + This prevents misscalculations when a formula places the take profit in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (tradingEngine.current.position.takeProfit.value > candle.max) { + tradingEngine.current.position.takeProfit.value = candle.max + } + } else { + if (tradingEngine.current.position.takeProfit.value < candle.min) { + tradingEngine.current.position.takeProfit.value = candle.min + } + } + + let slippedTakeProfit = tradingEngine.current.position.takeProfit.value + /* Apply the Slippage */ + let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + slippedTakeProfit = slippedTakeProfit + slippageAmount + } else { + slippedTakeProfit = slippedTakeProfit - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedTakeProfit + + tradingEngine.current.strategy.stageType.value = 'Close Stage' + checkAnnouncements(strategy.closeStage, 'Take Profit') + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + + tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.status.value = 1 + tradingEngine.current.position.exitType.value = 2 + + addToSnapshots = true + return true + } + } + return false + } + + function getPositionSize() { + let balance + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + balance = tradingEngine.current.balance.baseAsset.value + } else { + balance = tradingEngine.current.balance.quotedAsset.value + } + const DEFAULT_VALUE = balance + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + + if (strategy.openStage === undefined) return { DEFAULT_VALUE } + if (strategy.openStage.initialDefinition === undefined) return { DEFAULT_VALUE } + if (strategy.openStage.initialDefinition.positionSize === undefined) return { DEFAULT_VALUE } + if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return { DEFAULT_VALUE } + + let value = formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) + if (value === undefined) return { DEFAULT_VALUE } + return value + } + + function getPositionRate() { + const DEFAULT_VALUE = candle.close + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + + if (strategy.openStage === undefined) return { DEFAULT_VALUE } + if (strategy.openStage.initialDefinition === undefined) return { DEFAULT_VALUE } + if (strategy.openStage.initialDefinition.positionRate === undefined) return { DEFAULT_VALUE } + if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return { DEFAULT_VALUE } + + let value = formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) + if (value === undefined) return { DEFAULT_VALUE } + return value + } } From b308132ad8aff686a7f2cce26ee7f8cbcec370bb Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Jun 2020 13:55:09 +0200 Subject: [PATCH 007/113] More Modules --- Trading/Announcements.js | 22 ++ Trading/Snapshots.js | 73 ++++ Trading/TradingEngine.js | 124 ++++++ Trading/TradingExecution.js | 326 ++++++++++++++++ Trading/TradingRecords.js | 163 ++++++++ Trading/TradingSimulation.js | 704 +++-------------------------------- Trading/TradingSystem.js | 15 +- 7 files changed, 772 insertions(+), 655 deletions(-) create mode 100644 Trading/Announcements.js create mode 100644 Trading/Snapshots.js create mode 100644 Trading/TradingExecution.js create mode 100644 Trading/TradingRecords.js diff --git a/Trading/Announcements.js b/Trading/Announcements.js new file mode 100644 index 000000000..e8db3bb7a --- /dev/null +++ b/Trading/Announcements.js @@ -0,0 +1,22 @@ +exports.newAnnouncements = function newAnnouncements(bot, logger) { + + const MODULE_NAME = 'Announcements' + + let thisObject = { + initialize: initialize, + finalize: finalize + } + + let tradingEngine + + return thisObject + + function initialize() { + tradingEngine = bot.TRADING_ENGINE + } + + function finalize() { + tradingEngine = undefined + } + +} \ No newline at end of file diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js new file mode 100644 index 000000000..8a220817e --- /dev/null +++ b/Trading/Snapshots.js @@ -0,0 +1,73 @@ +exports.newSnapshots = function newSnapshots(bot, logger) { + + const MODULE_NAME = 'Snapshots' + + let thisObject = { + manageSnapshots: manageSnapshots, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + + return thisObject + + function initialize() { + tradingEngine = bot.TRADING_ENGINE + } + + function finalize() { + tradingEngine = undefined + } + + function manageSnapshots() { + /* Snapshots Management (before we generate the trade record and delete that info) */ + if (saveAsLastTriggerOnSnapshot === true) { + snapshots.lastTriggerOn = snapshotDataRecord + saveAsLastTriggerOnSnapshot = false + } + + if (saveAsLastTakePositionSnapshot === true) { + snapshots.lastTakePosition = snapshotDataRecord + saveAsLastTakePositionSnapshot = false + } + + if (addToSnapshots === true) { + let closeValues = [ + tradingEngine.episode.positionCounters.positions.value, // Position Number + (new Date(candle.begin)).toISOString(), // Datetime + tradingSystem.strategies[tradingEngine.current.strategy.index.value].name, // Strategy Name + tradingEngine.current.strategy.situationName.value, // Trigger On Situation + tradingEngine.current.position.situationName.value, // Take Position Situation + hitOrFial(), // Result + tradingEngine.last.position.ROI.value, // ROI + exitType() // Exit Type + ] + + function hitOrFial() { + if (tradingEngine.last.position.ROI.value > 0) { return 'HIT' } else { return 'FAIL' } + } + + function exitType() { + switch (tradingEngine.current.position.exitType.value) { + case 1: return 'Stop' + case 2: return 'Take Profit' + } + } + + + if (positionedAtYesterday === false) { + snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) + snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) + } + snapshots.lastTriggerOn = undefined + snapshots.lastTakePosition = undefined + addToSnapshots = false + } + + let closeHeaders = ['Trade Number', 'Close Datetime', 'Strategy', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] + if (snapshots.headers === undefined) { + snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshotLoopHeaders))) + } + } +} \ No newline at end of file diff --git a/Trading/TradingEngine.js b/Trading/TradingEngine.js index d856b0c38..3d05fe4ee 100644 --- a/Trading/TradingEngine.js +++ b/Trading/TradingEngine.js @@ -5,6 +5,10 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let thisObject = { updatePositionCounters: updatePositionCounters, updateDistanceToEventsCounters: updateDistanceToEventsCounters, + getReadyToTakePosition: getReadyToTakePosition, + takePosition: takePosition, + getReadyToClosePosition: getReadyToClosePosition, + closePosition: closePosition, initialize: initialize, finalize: finalize } @@ -115,5 +119,125 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.current.distanceToEvent.closePosition.value++ } } + + function getReadyToTakePosition(candle) { + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.takePosition.value = 1 + + /* Position size and rate */ + tradingEngine.current.position.size.value = getPositionSize() + tradingEngine.current.position.rate.value = getPositionRate() + + /* We take what was calculated at the formula and apply the slippage. */ + let slippageAmount = tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.slippage.positionRate / 100 + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount + } else { + tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount + } + + /* Update the trade record information. */ + tradingEngine.current.position.begin.value = candle.begin + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value + } + + function takePosition() { + calculateTakeProfit() // TODO: Check if this is really necesary + calculateStopLoss() // TODO: Check if this is really necesary + + tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + + tradingEngine.last.position.profitLoss.value = 0 + tradingEngine.last.position.ROI.value = 0 + + let feePaid = 0 + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value + } else { + feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value + } + } + + function getReadyToClosePosition() { + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.closePosition.value = 1 + + /* Position size and rate */ + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + } + + function closePosition() { + tradingEngine.episode.positionCounters.positions.value++ + + let feePaid = 0 + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + + feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = 0 + } else { + + feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid + tradingEngine.current.balance.baseAsset.value = 0 + } + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value + tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance + } else { + tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value + tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance + } + + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.ROI.value + + if (tradingEngine.last.position.profitLoss.value > 0) { + tradingEngine.episode.episodeCounters.hits.value++ + } else { + tradingEngine.episode.episodeCounters.fails.value++ + } + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 + } else { + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 + } + + addRecords() + + tradingEngine.current.position.stopLoss.value = 0 + tradingEngine.current.position.takeProfit.value = 0 + + tradingEngine.current.position.rate.value = 0 + tradingEngine.current.position.size.value = 0 + + timerToCloseStage = candle.begin + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = -1 + + } } diff --git a/Trading/TradingExecution.js b/Trading/TradingExecution.js new file mode 100644 index 000000000..2df00a402 --- /dev/null +++ b/Trading/TradingExecution.js @@ -0,0 +1,326 @@ +exports.newTradingExecution = function newTradingExecution(bot, logger) { + + const MODULE_NAME = 'Trading Execution' + + let thisObject = { + takePosition: takePosition, + closePosition: closePosition, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + + return thisObject + + function initialize() { + + tradingEngine = bot.TRADING_ENGINE + + } + + function finalize() { + tradingEngine = undefined + } + + function takePosition() { + /* Check if we need to execute. */ + if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ + /* Check that we are in LIVE MODE */ + if (bot.startMode === 'Live') { + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> Taking a Position in Live Mode.') + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.size.value = ' + tradingEngine.current.position.size.value) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) + + /* We see if we need to put the actual order at the exchange. */ + if (variable.executionContext !== undefined) { + switch (variable.executionContext.status) { + case 'Without a Position': { // We need to put the order because It was not put yet. + if (strategy.openStage !== undefined) { + if (strategy.openStage.openExecution !== undefined) { + putOpeningOrder() + return + } + } + break + } + case 'Position Closed': { // Waiting for a confirmation that the position was closed. + if (strategy.openStage !== undefined) { + if (strategy.openStage.openExecution !== undefined) { + putOpeningOrder() + return + } + } + break + } + case 'Taking Position': { // Waiting for a confirmation that the position was taken. + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is Taking Position.') } + break + } + case 'In a Position': { // This should mean that we already put the order at the exchange. + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is In a Position.') } + break + } + } + } else { // The context does not exist so it means we are not in a position. + if (strategy.openStage !== undefined) { + if (strategy.openStage.openExecution !== undefined) { + putOpeningOrder() + return + } + } + } + } else { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not trading live.') } + } + } else { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not the last closed candle.') } + } + + } + + function putOpeningOrder() { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Entering function.') } + + /* We wont take a position unless we are withing the sessionParameters.timeRange.config.initialDatetime and the sessionParameters.timeRange.config.finalDatetime range */ + if (sessionParameters.timeRange.config.initialDatetime !== undefined) { + if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } + takePositionAtSimulation() + return + } + } + + /* We wont take a position if we are past the final datetime */ + if (sessionParameters.timeRange.config.finalDatetime !== undefined) { + if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } + takePositionAtSimulation() + return + } + } + + /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ + if (variable.executionContext !== undefined) { + if (variable.executionContext.periods !== undefined) { + if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') } + takePositionAtSimulation() + return + } + } + } + + /* We are not going to place orders based on outdated information. The next filter prevents firing orders when backtesting. */ + if (currentDay) { + let today = new Date(Math.trunc((new Date().valueOf()) / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) + let processDay = new Date(Math.trunc(currentDay.valueOf() / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) + if (today.valueOf() !== processDay.valueOf()) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> processDay = ' + processDay) } + takePositionAtSimulation() + return + } + } + + let orderPrice + let amountA + let amountB + let orderSide + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + orderSide = 'sell' + + orderPrice = tradingEngine.current.position.rate.value + + amountA = tradingEngine.current.position.size.value * orderPrice + amountB = tradingEngine.current.position.size.value + } else { + orderSide = 'buy' + + orderPrice = tradingEngine.current.position.rate.value + + amountA = tradingEngine.current.position.size.value + amountB = tradingEngine.current.position.size.value / orderPrice + } + + variable.executionContext = { + status: 'Taking Position', + periods: tradingEngine.episode.episodeCounters.periods + } + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') } + exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) + + function onOrderCreated(err, order) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Entering function.') } + + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } + variable.executionContext = { + status: 'In a Position', + periods: tradingEngine.episode.episodeCounters.periods, + amountA: amountA, + amountB: amountB, + orderId: order.id + } + takePositionAtSimulation() + return + } + case global.DEFAULT_FAIL_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } + strategy.openStage.openExecution.error = err.message + afterLoop() + return + } + case global.DEFAULT_RETRY_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } + strategy.openStage.openExecution.error = err.message + afterLoop() + return + } + } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return + } catch (err) { + logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> err = ' + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return + } + } + } + + function closePosition() { + if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ + /* Check that we are in LIVE MODE */ + if (bot.startMode === 'Live') { + /* We see if we need to put the actual order at the exchange. */ + if (variable.executionContext !== undefined) { + switch (variable.executionContext.status) { + case 'Without a Position': { // No way to close anything at the exchange. + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Without a Position.') } + break + } + case 'In a Position': { // This should mean that we already put the order at the exchange. + if (strategy.closeStage !== undefined) { + if (strategy.closeStage.closeExecution !== undefined) { + putClosingOrder() + return + } + } + break + } + case 'Closing Position': { // Waiting for a confirmation that the position was taken. + if (strategy.closeStage !== undefined) { + if (strategy.closeStage.closeExecution !== undefined) { + putClosingOrder() + return + } + } + break + } + + case 'Position Closed': { // + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Position Closed.') } + break + } + } + } + } + } + } + + function putClosingOrder() { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putClosingOrder -> Entering function.') } + + /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ + if (variable.executionContext !== undefined) { + if (variable.executionContext.periods !== undefined) { + if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') } + closePositionAtSimulation() + return + } + } + } + + let orderPrice + let amountA + let amountB + let orderSide + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + orderSide = 'buy' + + orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. + + amountA = tradingEngine.current.balance.quotedAsset.value + amountB = tradingEngine.current.balance.quotedAsset.value / orderPrice + } else { + orderSide = 'sell' + + orderPrice = ticker.last - 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. + + amountA = tradingEngine.current.balance.baseAsset.value * orderPrice + amountB = tradingEngine.current.balance.baseAsset.value + } + + variable.executionContext = { + status: 'Closing Position', + periods: tradingEngine.episode.episodeCounters.periods + } + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') } + exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) + + function onOrderCreated(err, order) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Entering function.') } + + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } + variable.executionContext = { + status: 'Position Closed', + periods: tradingEngine.episode.episodeCounters.periods, + amountA: amountA, + amountB: amountB, + orderId: order.id + } + closePositionAtSimulation() + return + } + case global.DEFAULT_FAIL_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } + /* We will assume that the problem is temporary, and expect that it will work at the next execution. */ + strategy.closeStage.closeExecution.error = err.message + afterLoop() + return + } + case global.DEFAULT_RETRY_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } + strategy.closeStage.closeExecution.error = err.message + afterLoop() + return + } + } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return + } catch (err) { + logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> err = ' + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return + } + } + } +} + diff --git a/Trading/TradingRecords.js b/Trading/TradingRecords.js new file mode 100644 index 000000000..222c0786d --- /dev/null +++ b/Trading/TradingRecords.js @@ -0,0 +1,163 @@ +exports.newTradingRecords = function newTradingRecords(bot, logger) { + + const MODULE_NAME = 'Trading Records' + + let thisObject = { + addSimulationRecord: addSimulationRecord, + addConditionsRecord: addConditionsRecord, + addStrategyRecord: addStrategyRecord, + addPositionRecord: addPositionRecord, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + + return thisObject + + function initialize() { + tradingEngine = bot.TRADING_ENGINE + } + + function finalize() { + tradingEngine = undefined + } + + function addSimulationRecord() { + /* Simulation Record */ + let simulationRecord + + if (tradingEngine.current.balance.baseAsset.value === Infinity) { + tradingEngine.current.balance.baseAsset.value = Number.MAX_SAFE_INTEGER + } + + if (tradingEngine.current.balance.quotedAsset.value === Infinity) { + tradingEngine.current.balance.quotedAsset.value = Number.MAX_SAFE_INTEGER + } + + simulationRecord = [ + candle.begin, + candle.end, + tradingEngine.current.balance.baseAsset.value, + tradingEngine.current.balance.quotedAsset.value, + tradingEngine.episode.episodeStatistics.profitLoss.value, + tradingEngine.last.position.profitLoss.value, + tradingEngine.current.position.stopLoss.value, + tradingEngine.episode.positionCounters.positions.value, + tradingEngine.episode.episodeCounters.hits.value, + tradingEngine.episode.episodeCounters.fails.value, + tradingEngine.episode.episodeStatistics.hitRatio.value, + tradingEngine.episode.episodeStatistics.ROI.value, + tradingEngine.episode.episodeCounters.periods, + tradingEngine.episode.episodeStatistics.days, + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value, + tradingEngine.current.position.rate.value, + tradingEngine.last.position.ROI.value, + tradingEngine.current.position.takeProfit.value, + tradingEngine.current.position.stopLoss.stopLossPhase.value, + tradingEngine.current.position.takeProfit.takeProfitPhase.value, + tradingEngine.current.position.size.value, + sessionParameters.sessionBaseAsset.config.initialBalance, + sessionParameters.sessionBaseAsset.config.minimumBalance, + sessionParameters.sessionBaseAsset.config.maximumBalance, + sessionParameters.sessionQuotedAsset.config.initialBalance, + sessionParameters.sessionQuotedAsset.config.minimumBalance, + sessionParameters.sessionQuotedAsset.config.maximumBalance, + '"' + sessionParameters.sessionBaseAsset.name + '"', + '"' + sessionParameters.sessionQuotedAsset.name + '"', + '"' + bot.market.marketBaseAsset + '"', + '"' + bot.market.marketQuotedAsset + '"', + tradingEngine.current.position.positionCounters.periods.value, + tradingEngine.current.position.positionStatistics.days.value + ] + + recordsArray.push(simulationRecord) + } + + function addConditionsRecord() { + /* Prepare the information for the Conditions File */ + let conditionsRecord = [ + candle.begin, + candle.end, + tradingEngine.current.strategy.index.value, + tradingEngine.current.position.stopLoss.stopLossPhase.value, + tradingEngine.current.position.takeProfit.takeProfitPhase.value, + conditionsValues, + formulasErrors, + formulasValues + ] + + conditionsArray.push(conditionsRecord) + } + + function addStrategyRecord() { + /* + Lets see if there will be an open strategy ... + Except if we are at the head of the market (remember we skipped the last candle for not being closed.) + */ + if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { + tradingEngine.current.strategy.status.value = 'Open' + tradingEngine.current.strategy.end.value = candle.end + } + + /* Prepare the information for the Strategies File */ + if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value !== 0) { + let strategyRecord = [ + tradingEngine.current.strategy.begin.value, + tradingEngine.current.strategy.end.value, + tradingEngine.current.strategy.status.value, + tradingEngine.current.strategy.index.value, + tradingEngine.current.strategy.beginRate.value, + tradingEngine.current.strategy.endRate.value, + '"' + tradingEngine.current.strategy.situationName.value + '"', + '"' + tradingEngine.current.strategy.strategyName.value + '"' + ] + + strategiesArray.push(strategyRecord) + + inializeCurrentStrategy() + } + } + + function addPositionRecord() { + /* + Lets see if there will be an open trade ... + Except if we are at the head of the market (remember we skipped the last candle for not being closed.) + */ + if (tradingEngine.current.position.begin.value !== 0 && + tradingEngine.current.position.end.value === 0 && + currentCandleIndex === candles.length - 2 && + lastCandle.end !== lastInstantOfTheDay) { + + /* This means the trade is open */ + tradingEngine.current.position.status.value = 2 + tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.endRate.value = candle.close + + /* Here we will calculate the ongoing ROI */ + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + tradingEngine.current.position.positionStatistics.ROI.value = (tradingEngine.current.position.rate.value - candle.close) / tradingEngine.current.position.rate.value * 100 + } else { + tradingEngine.current.position.positionStatistics.ROI.value = (candle.close - tradingEngine.current.position.rate.value) / tradingEngine.current.position.rate.value * 100 + } + } + + /* Prepare the information for the Positions File */ + if (tradingEngine.current.position.begin.value !== 0 && tradingEngine.current.position.end.value !== 0) { + let positionRecord = [ + tradingEngine.current.position.begin.value, + tradingEngine.current.position.end.value, + tradingEngine.current.position.status.value, + tradingEngine.current.position.positionStatistics.ROI.value, + tradingEngine.current.position.beginRate.value, + tradingEngine.current.position.endRate.value, + tradingEngine.current.position.exitType.value, + '"' + tradingEngine.current.position.situationName.value + '"' + ] + + positionsArray.push(positionRecord) + + initializeCurrentPosition() + } + } +} \ No newline at end of file diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 609191d13..51b721e32 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -84,6 +84,26 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() + const TRADING_ENGINE_MODULE = require('./TradingExecution.js') + let tradingExecutionModule = TRADING_ENGINE_MODULE.newTradingExecution(bot, logger) + tradingExecutionModule.initialize() + + const TRADING_RECORDS_MODULE = require('./TradingRecords.js') + let tradingRecordsModule = TRADING_RECORDS_MODULE.newTradingRecords(bot, logger) + tradingRecordsModule.initialize() + + const SNAPSHOTS_MODULE = require('./Snapshots.js') + let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) + snapshotsModule.initialize() + + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') + let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + announcementsModule.initialize() + + const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') + let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) + tradingSystemModule.initialize() + /* Main Array and Maps */ let propertyName = 'at' + sessionParameters.timeFrame.config.label.replace('-', '') let candles = chart[propertyName].candles @@ -292,9 +312,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } - const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') - let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) - tradingSystemModule.initialize(chart, candle) + + tradingSystemModule.setCandle(candle) + tradingSystemModule.setChart(chart) tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() @@ -373,455 +393,26 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT closePositionNow = tradingSystemModule.checkStopLossOrTakeProfitWasHit() } - tradingSystemModule.finalize() + /* Taking a Position */ if ( takePositionNow === true ) { takePositionNow = false - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Entering code block.') } - - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.takePosition.value = 1 - - /* Position size and rate */ - tradingEngine.current.position.size.value = getPositionSize() - tradingEngine.current.position.rate.value = getPositionRate() - - /* We take what was calculated at the formula and apply the slippage. */ - let slippageAmount = tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.slippage.positionRate / 100 - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount - } else { - tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount - } - - if (bot.startMode === 'Live') { - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> Taking a Position in Live Mode.') - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.size.value = ' + tradingEngine.current.position.size.value) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) - } - - /* Update the trade record information. */ - tradingEngine.current.position.begin.value = candle.begin - tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value - - /* Check if we need to execute. */ - if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ - /* Check that we are in LIVE MODE */ - if (bot.startMode === 'Live') { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // We need to put the order because It was not put yet. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - break - } - case 'Position Closed': { // Waiting for a confirmation that the position was closed. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - break - } - case 'Taking Position': { // Waiting for a confirmation that the position was taken. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is Taking Position.') } - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is In a Position.') } - break - } - } - } else { // The context does not exist so it means we are not in a position. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not trading live.') } - } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not the last closed candle.') } - } - - takePositionAtSimulation() - - return - - function putOpeningOrder() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Entering function.') } - - /* We wont take a position unless we are withing the sessionParameters.timeRange.config.initialDatetime and the sessionParameters.timeRange.config.finalDatetime range */ - if (sessionParameters.timeRange.config.initialDatetime !== undefined) { - if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } - takePositionAtSimulation() - return - } - } - - /* We wont take a position if we are past the final datetime */ - if (sessionParameters.timeRange.config.finalDatetime !== undefined) { - if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } - takePositionAtSimulation() - return - } - } - - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (variable.executionContext !== undefined) { - if (variable.executionContext.periods !== undefined) { - if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') } - takePositionAtSimulation() - return - } - } - } - - /* We are not going to place orders based on outdated information. The next filter prevents firing orders when backtesting. */ - if (currentDay) { - let today = new Date(Math.trunc((new Date().valueOf()) / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - let processDay = new Date(Math.trunc(currentDay.valueOf() / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - if (today.valueOf() !== processDay.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> processDay = ' + processDay) } - takePositionAtSimulation() - return - } - } - - let orderPrice - let amountA - let amountB - let orderSide - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - orderSide = 'sell' - - orderPrice = tradingEngine.current.position.rate.value - - amountA = tradingEngine.current.position.size.value * orderPrice - amountB = tradingEngine.current.position.size.value - } else { - orderSide = 'buy' - - orderPrice = tradingEngine.current.position.rate.value - - amountA = tradingEngine.current.position.size.value - amountB = tradingEngine.current.position.size.value / orderPrice - } - - variable.executionContext = { - status: 'Taking Position', - periods: tradingEngine.episode.episodeCounters.periods - } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') } - exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) - - function onOrderCreated(err, order) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Entering function.') } - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } - variable.executionContext = { - status: 'In a Position', - periods: tradingEngine.episode.episodeCounters.periods, - amountA: amountA, - amountB: amountB, - orderId: order.id - } - takePositionAtSimulation() - return - } - case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } - strategy.openStage.openExecution.error = err.message - afterLoop() - return - } - case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } - strategy.openStage.openExecution.error = err.message - afterLoop() - return - } - } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } - } - } - - function takePositionAtSimulation() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionAtSimulation -> Entering function.') } - - /* Continue with the simulation */ - calculateTakeProfit() - calculateStopLoss() - - tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - - tradingEngine.last.position.profitLoss.value = 0 - tradingEngine.last.position.ROI.value = 0 - - let feePaid = 0 - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value - } else { - feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value - } - - addRecords() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionAtSimulation -> Exiting Loop Body after taking position at simulation.') } - controlLoop() - return - } + tradingEngineModule.getReadyToTakePosition(candle) + tradingExecutionModule.takePosition() + tradingEngineModule.takePosition() } - if (closePositionNow === true) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Entering code block.') } - + /* Closing a Position */ + if ( + closePositionNow === true + ) { closePositionNow = false - - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.closePosition.value = 1 - - /* Position size and rate */ - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ - /* Check that we are in LIVE MODE */ - if (bot.startMode === 'Live') { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // No way to close anything at the exchange. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Without a Position.') } - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } - } - break - } - case 'Closing Position': { // Waiting for a confirmation that the position was taken. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } - } - break - } - - case 'Position Closed': { // - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Position Closed.') } - break - } - } - } - } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Not within the last 10 candles.') } - } - - closePositionAtSimulation() - return - - function putClosingOrder() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putClosingOrder -> Entering function.') } - - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (variable.executionContext !== undefined) { - if (variable.executionContext.periods !== undefined) { - if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') } - closePositionAtSimulation() - return - } - } - } - - let orderPrice - let amountA - let amountB - let orderSide - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - orderSide = 'buy' - - orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - - amountA = tradingEngine.current.balance.quotedAsset.value - amountB = tradingEngine.current.balance.quotedAsset.value / orderPrice - } else { - orderSide = 'sell' - - orderPrice = ticker.last - 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - - amountA = tradingEngine.current.balance.baseAsset.value * orderPrice - amountB = tradingEngine.current.balance.baseAsset.value - } - - variable.executionContext = { - status: 'Closing Position', - periods: tradingEngine.episode.episodeCounters.periods - } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') } - exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) - - function onOrderCreated(err, order) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Entering function.') } - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } - variable.executionContext = { - status: 'Position Closed', - periods: tradingEngine.episode.episodeCounters.periods, - amountA: amountA, - amountB: amountB, - orderId: order.id - } - closePositionAtSimulation() - return - } - case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } - /* We will assume that the problem is temporary, and expect that it will work at the next execution. */ - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return - } - case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return - } - } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } - } - } - - function closePositionAtSimulation() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> closePositionAtSimulation -> Entering function.') } - - tradingEngine.episode.positionCounters.positions.value++ - - let feePaid = 0 - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - - feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = 0 - } else { - - feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 - - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.baseAsset.value = 0 - } - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value - tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance - } else { - tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value - tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance - } - - tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.ROI.value - - if (tradingEngine.last.position.profitLoss.value > 0) { - tradingEngine.episode.episodeCounters.hits.value++ - } else { - tradingEngine.episode.episodeCounters.fails.value++ - } - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 - } else { - tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 - } - - addRecords() - - tradingEngine.current.position.stopLoss.value = 0 - tradingEngine.current.position.takeProfit.value = 0 - - tradingEngine.current.position.rate.value = 0 - tradingEngine.current.position.size.value = 0 - - timerToCloseStage = candle.begin - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 - tradingEngine.current.position.takeProfit.takeProfitPhase.value = -1 - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> closePositionAtSimulation -> Exiting Loop Body after closing position at simulation.') } - controlLoop() - return - } + tradingEngineModule.getReadyToClosePosition(candle) + tradingExecutionModule.closePosition() + tradingEngineModule.closePosition() } /* Closing the Closing Stage */ @@ -843,218 +434,23 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } - /* Not a buy or sell condition */ + snapshotsModule.manageSnapshots() + tradingRecordsModule.addSimulationRecord() + tradingRecordsModule.addConditionsRecord() + tradingRecordsModule.addStrategyRecord() + tradingRecordsModule.addPositionRecord() + + /* We will remmember the last candle processed, so that if it is the last one of this run we can use it to continue from there next time. */ + tradingEngine.last.candle.begin = candle.begin + tradingEngine.last.candle.end = candle.end + + /* After everything at the simulation level was done, we will do the annoucements that are pending. */ + makeAnnoucements() + - addRecords() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Exiting Loop Body after adding a record.') } controlLoop() return - function addRecords() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> addRecords -> Entering function.') } - - manageSnapshots() - addSimulationRecord() - addConditionsRecord() - addStrategyRecord() - addPositionRecord() - - /* We will remmember the last candle processed, so that if it is the last one of this run we can use it to continue from there next time. */ - tradingEngine.last.candle.begin = candle.begin - tradingEngine.last.candle.end = candle.end - - /* After everything at the simulation level was done, we will do the annoucements that are pending. */ - makeAnnoucements() - - function manageSnapshots() { - /* Snapshots Management (before we generate the trade record and delete that info) */ - if (saveAsLastTriggerOnSnapshot === true) { - snapshots.lastTriggerOn = snapshotDataRecord - saveAsLastTriggerOnSnapshot = false - } - - if (saveAsLastTakePositionSnapshot === true) { - snapshots.lastTakePosition = snapshotDataRecord - saveAsLastTakePositionSnapshot = false - } - - if (addToSnapshots === true) { - let closeValues = [ - tradingEngine.episode.positionCounters.positions.value, // Position Number - (new Date(candle.begin)).toISOString(), // Datetime - tradingSystem.strategies[tradingEngine.current.strategy.index.value].name, // Strategy Name - tradingEngine.current.strategy.situationName.value, // Trigger On Situation - tradingEngine.current.position.situationName.value, // Take Position Situation - hitOrFial(), // Result - tradingEngine.last.position.ROI.value, // ROI - exitType() // Exit Type - ] - - function hitOrFial() { - if (tradingEngine.last.position.ROI.value > 0) { return 'HIT' } else { return 'FAIL' } - } - - function exitType() { - switch (tradingEngine.current.position.exitType.value) { - case 1: return 'Stop' - case 2: return 'Take Profit' - } - } - - - if (positionedAtYesterday === false) { - snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) - snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) - } - snapshots.lastTriggerOn = undefined - snapshots.lastTakePosition = undefined - addToSnapshots = false - } - - let closeHeaders = ['Trade Number', 'Close Datetime', 'Strategy', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] - if (snapshots.headers === undefined) { - snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshotLoopHeaders))) - } - } - - function addSimulationRecord() { - /* Simulation Record */ - let simulationRecord - - if (tradingEngine.current.balance.baseAsset.value === Infinity) { - tradingEngine.current.balance.baseAsset.value = Number.MAX_SAFE_INTEGER - } - - if (tradingEngine.current.balance.quotedAsset.value === Infinity) { - tradingEngine.current.balance.quotedAsset.value = Number.MAX_SAFE_INTEGER - } - - simulationRecord = [ - candle.begin, - candle.end, - tradingEngine.current.balance.baseAsset.value, - tradingEngine.current.balance.quotedAsset.value, - tradingEngine.episode.episodeStatistics.profitLoss.value, - tradingEngine.last.position.profitLoss.value, - tradingEngine.current.position.stopLoss.value, - tradingEngine.episode.positionCounters.positions.value, - tradingEngine.episode.episodeCounters.hits.value, - tradingEngine.episode.episodeCounters.fails.value, - tradingEngine.episode.episodeStatistics.hitRatio.value, - tradingEngine.episode.episodeStatistics.ROI.value, - tradingEngine.episode.episodeCounters.periods, - tradingEngine.episode.episodeStatistics.days, - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value, - tradingEngine.current.position.rate.value, - tradingEngine.last.position.ROI.value, - tradingEngine.current.position.takeProfit.value, - tradingEngine.current.position.stopLoss.stopLossPhase.value, - tradingEngine.current.position.takeProfit.takeProfitPhase.value, - tradingEngine.current.position.size.value, - sessionParameters.sessionBaseAsset.config.initialBalance, - sessionParameters.sessionBaseAsset.config.minimumBalance, - sessionParameters.sessionBaseAsset.config.maximumBalance, - sessionParameters.sessionQuotedAsset.config.initialBalance, - sessionParameters.sessionQuotedAsset.config.minimumBalance, - sessionParameters.sessionQuotedAsset.config.maximumBalance, - '"' + sessionParameters.sessionBaseAsset.name + '"', - '"' + sessionParameters.sessionQuotedAsset.name + '"', - '"' + bot.market.marketBaseAsset + '"', - '"' + bot.market.marketQuotedAsset + '"', - tradingEngine.current.position.positionCounters.periods.value, - tradingEngine.current.position.positionStatistics.days.value - ] - - recordsArray.push(simulationRecord) - } - - function addConditionsRecord() { - /* Prepare the information for the Conditions File */ - let conditionsRecord = [ - candle.begin, - candle.end, - tradingEngine.current.strategy.index.value, - tradingEngine.current.position.stopLoss.stopLossPhase.value, - tradingEngine.current.position.takeProfit.takeProfitPhase.value, - conditionsValues, - formulasErrors, - formulasValues - ] - - conditionsArray.push(conditionsRecord) - } - - function addStrategyRecord() { - /* - Lets see if there will be an open strategy ... - Except if we are at the head of the market (remember we skipped the last candle for not being closed.) - */ - if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { - tradingEngine.current.strategy.status.value = 'Open' - tradingEngine.current.strategy.end.value = candle.end - } - - /* Prepare the information for the Strategies File */ - if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value !== 0) { - let strategyRecord = [ - tradingEngine.current.strategy.begin.value, - tradingEngine.current.strategy.end.value, - tradingEngine.current.strategy.status.value, - tradingEngine.current.strategy.index.value, - tradingEngine.current.strategy.beginRate.value, - tradingEngine.current.strategy.endRate.value, - '"' + tradingEngine.current.strategy.situationName.value + '"', - '"' + tradingEngine.current.strategy.strategyName.value + '"' - ] - - strategiesArray.push(strategyRecord) - - inializeCurrentStrategy() - } - } - - function addPositionRecord() { - /* - Lets see if there will be an open trade ... - Except if we are at the head of the market (remember we skipped the last candle for not being closed.) - */ - if (tradingEngine.current.position.begin.value !== 0 && - tradingEngine.current.position.end.value === 0 && - currentCandleIndex === candles.length - 2 && - lastCandle.end !== lastInstantOfTheDay) { - - /* This means the trade is open */ - tradingEngine.current.position.status.value = 2 - tradingEngine.current.position.end.value = candle.end - tradingEngine.current.position.endRate.value = candle.close - - /* Here we will calculate the ongoing ROI */ - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - tradingEngine.current.position.positionStatistics.ROI.value = (tradingEngine.current.position.rate.value - candle.close) / tradingEngine.current.position.rate.value * 100 - } else { - tradingEngine.current.position.positionStatistics.ROI.value = (candle.close - tradingEngine.current.position.rate.value) / tradingEngine.current.position.rate.value * 100 - } - } - - /* Prepare the information for the Positions File */ - if (tradingEngine.current.position.begin.value !== 0 && tradingEngine.current.position.end.value !== 0) { - let positionRecord = [ - tradingEngine.current.position.begin.value, - tradingEngine.current.position.end.value, - tradingEngine.current.position.status.value, - tradingEngine.current.position.positionStatistics.ROI.value, - tradingEngine.current.position.beginRate.value, - tradingEngine.current.position.endRate.value, - tradingEngine.current.position.exitType.value, - '"' + tradingEngine.current.position.situationName.value + '"' - ] - - positionsArray.push(positionRecord) - - initializeCurrentPosition() - } - } - } function checkAnnouncements(node, value) { /* @@ -1231,8 +627,12 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> afterLoop -> Entering function.') } tradingEngineModule.finalize() + tradingExecutionModule.finalize() + tradingSystemModule.finalize() + tradingRecordsModule.finalize() + snapshotsModule.finalize() + announcementsModule.finalize() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> callback -> recordsArray.length = ' + recordsArray.length) } callback(tradingSystem, snapshots.headers, snapshots.triggerOn, snapshots.takePosition) } diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index fa01e77e6..b9b766155 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -4,6 +4,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const FULL_LOG = true let thisObject = { + setCandle: setCandle, + setChart: setChart, evalConditions: evalConditions, evalFormulas: evalFormulas, checkTriggerOn: checkTriggerOn, @@ -22,6 +24,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let chart let candle + let tradingSystem let tradingEngine @@ -35,9 +38,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { return thisObject - function initialize(pChart, pCandle) { - chart = pChart - candle = pCandle + function initialize() { tradingSystem = bot.TRADING_SYSTEM tradingEngine = bot.TRADING_ENGINE } @@ -57,6 +58,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { formulasErrors = undefined } + function setCandle(pCandle) { + candle = pCandle // We need candle to be a local object accessible from conditions and formulas. + } + + function setChart(pChart) { + chart = pChart // We need chat to be a local object accessible from conditions and formulas. + } + function evalConditions() { evalNode(bot.TRADING_SYSTEM, 'Conditions') } From 8cf7d3a62b0886e165c12911decc31e0f03c9a84 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Jun 2020 16:17:14 +0200 Subject: [PATCH 008/113] setCurrentCandle --- Trading/Announcements.js | 89 ++++++++++++++++++++ Trading/Snapshots.js | 57 +++++++++++++ Trading/TradingEngine.js | 54 ++++++++++++- Trading/TradingSimulation.js | 153 +---------------------------------- Trading/TradingSystem.js | 48 +++++------ 5 files changed, 223 insertions(+), 178 deletions(-) diff --git a/Trading/Announcements.js b/Trading/Announcements.js index e8db3bb7a..4f9d92f5a 100644 --- a/Trading/Announcements.js +++ b/Trading/Announcements.js @@ -3,6 +3,8 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { const MODULE_NAME = 'Announcements' let thisObject = { + checkAnnouncements: checkAnnouncements, + makeAnnoucements: makeAnnoucements, initialize: initialize, finalize: finalize } @@ -19,4 +21,91 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { tradingEngine = undefined } + function checkAnnouncements(node, value) { + /* + Value is an optional parameter that represents the value that the announcement is monitoring for change (for numeric values only). + If we do receive this value, we will only make the annoucement if the variance is grater than the user pre-defined value + for this variance. + */ + + if (node.announcements !== undefined) { + for (let i = 0; i < node.announcements.length; i++) { + let announcement = node.announcements[i] + let key = node.type + '-' + announcement.name + '-' + announcement.id + + let lastPeriodAnnounced = -1 + let newAnnouncementRecord = {} + + for (let j = 0; j < variable.announcements.length; j++) { + let announcementRecord = variable.announcements[j] + if (announcementRecord.key === key) { + lastPeriodAnnounced = announcementRecord.periods + newAnnouncementRecord = announcementRecord + break + } + } + + if (tradingEngine.episode.episodeCounters.periods > lastPeriodAnnounced) { + if (isNaN(value) === false) { + /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ + let valueVariation + + let config = announcement.config + valueVariation = config.valueVariation + + if (newAnnouncementRecord.value !== undefined && valueVariation !== undefined) { + let upperLimit = newAnnouncementRecord.value + newAnnouncementRecord.value * valueVariation / 100 + let lowerLimit = newAnnouncementRecord.value - newAnnouncementRecord.value * valueVariation / 100 + if (value > lowerLimit && value < upperLimit) { + /* There is not enough variation to announce this again. */ + return + } + } + } + + /* + We store the announcement temporarily at an Array to differ its execution, becasue we need to evaulate its formula + and at this point in time the potential variables used at the formula are still not set. + */ + announcement.value = value + announcementsToBeMade.push(announcement) + + /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ + if (newAnnouncementRecord.periods !== undefined) { + newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods + newAnnouncementRecord.value = value + } else { + newAnnouncementRecord = { + key: key, + periods: tradingEngine.episode.episodeCounters.periods, + value: value + } + variable.announcements.push(newAnnouncementRecord) + } + } + } + } + } + + function makeAnnoucements() { + /* Here we go through all the annoucements that need to be done during this loop, and we just do them. */ + for (let i = 0; i < announcementsToBeMade.length; i++) { + announcement = announcementsToBeMade[i] + /* Here we check if there is a formula attached to the annoucement, we evaluate it to get the annoucement text. */ + let formulaValue + if (announcement.formula !== undefined) { + try { + let value = announcement.value + formulaValue = eval(announcement.formula.code) + } catch (err) { + announcement.formula.error = err.message + } + } + announcement.formulaValue = formulaValue + + if (bot.SESSION.socialBots !== undefined) { + bot.SESSION.socialBots.announce(announcement) + } + } + } } \ No newline at end of file diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js index 8a220817e..0b2b56e9e 100644 --- a/Trading/Snapshots.js +++ b/Trading/Snapshots.js @@ -4,6 +4,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let thisObject = { manageSnapshots: manageSnapshots, + addCodeToSnapshot: addCodeToSnapshot, initialize: initialize, finalize: finalize } @@ -70,4 +71,60 @@ exports.newSnapshots = function newSnapshots(bot, logger) { snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshotLoopHeaders))) } } + + function addCodeToSnapshot(code) { + if (code === undefined) { return } + + try { + let instructionsArray = code.split(' ') + for (let i = 0; i < instructionsArray.length; i++) { + let instruction = instructionsArray[i] + instruction = instruction.replace('(', '') + instruction = instruction.replace(')', '') + instruction = instruction.replace(//g, '') + instruction = instruction.replace(/<=/g, '') + instruction = instruction.replace(/>=/g, '') + instruction = instruction.replace(/!=/g, '') + instruction = instruction.replace(/!==/g, '') + instruction = instruction.replace(/==/g, '') + instruction = instruction.replace(/===/g, '') + instruction = instruction.replace(/{/g, '') + instruction = instruction.replace(/}/g, '') + if (instruction.indexOf('chart') >= 0) { + let parts = instruction.split('.') + let timeFrame = parts[1] + let product = parts[2] + let property + checkPrevious(3) + function checkPrevious(index) { + property = parts[index] + if (property === 'previous') { + product = product + '.previous' + checkPrevious(index + 1) + } + } + + // Example: chart.at01hs.popularSMA.sma200 - chart.at01hs.popularSMA.sma100 < 10 + if (timeFrame !== 'atAnyvariable.timeFrame') { + timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) + } + let key = timeFrame + '-' + product + '-' + property + let existingKey = snapshotKeys.get(key) + + if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. + snapshotKeys.set(key, key) + snapshotLoopHeaders.push(key) + + let value = eval(instruction) + snapshotDataRecord.push(value) + } + } + } + } catch (err) { + logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> code = ' + code) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } } \ No newline at end of file diff --git a/Trading/TradingEngine.js b/Trading/TradingEngine.js index 3d05fe4ee..1a7c3344e 100644 --- a/Trading/TradingEngine.js +++ b/Trading/TradingEngine.js @@ -3,6 +3,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { const MODULE_NAME = 'Trading Engine' let thisObject = { + setCurrentCandle: setCurrentCandle, updatePositionCounters: updatePositionCounters, updateDistanceToEventsCounters: updateDistanceToEventsCounters, getReadyToTakePosition: getReadyToTakePosition, @@ -32,6 +33,58 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine = undefined } + function setCurrentCandle(candle) { + cloneValues(tradingEngine.current.candle, tradingEngine.previous.candle) + + tradingEngine.current.candle.begin.value = candle.begin + tradingEngine.current.candle.end.value = candle.end + tradingEngine.current.candle.open.value = candle.open + tradingEngine.current.candle.close.value = candle.close + tradingEngine.current.candle.min.value = candle.min + tradingEngine.current.candle.max.value = candle.max + } + + function cloneValues(originNode, destinationNode) { + if (originNode === undefined) { return } + if (destinationNode === undefined) { return } + + /* Here copy the node value */ + destinationNode.value = originNode.value + + /* Now we go down through all the children of the origin node, assuming the destination node has the same children structure*/ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(originNode.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (originNode[property.name] !== undefined && destinationNode[property.name] !== undefined) { + cloneValues(originNode[property.name], destinationNode[property.name]) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (originNode[property.name] !== undefined && destinationNode[property.name] !== undefined) { + let originNodePropertyArray = originNode[property.name] + let destinationNodePropertyArray = destinationNode[property.name] + for (let m = 0; m < originNodePropertyArray.length; m++) { + cloneValues(originNodePropertyArray[m], destinationNodePropertyArray[m]) + } + } + break + } + } + } + } + } + function initializeNode(node) { if (node === undefined) { return } @@ -64,7 +117,6 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { case 'array': { if (node[property.name] !== undefined) { let nodePropertyArray = node[property.name] - object[property.name] = [] for (let m = 0; m < nodePropertyArray.length; m++) { initializeNode(nodePropertyArray[m]) } diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 51b721e32..a72167407 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -180,6 +180,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let announcementsToBeMade = [] let candle = candles[currentCandleIndex] + tradingEngineModule.setCurrentCandle(candle) + /* Not processing while out of user-defined time range */ if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } @@ -312,8 +314,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } - - tradingSystemModule.setCandle(candle) tradingSystemModule.setChart(chart) tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() @@ -445,156 +445,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.last.candle.end = candle.end /* After everything at the simulation level was done, we will do the annoucements that are pending. */ - makeAnnoucements() - + annoucementsModule.makeAnnoucements() controlLoop() - return - - - function checkAnnouncements(node, value) { - /* - Value is an optional parameter that represents the value that the announcement is monitoring for change (for numeric values only). - If we do receive this value, we will only make the annoucement if the variance is grater than the user pre-defined value - for this variance. - */ - - if (node.announcements !== undefined) { - for (let i = 0; i < node.announcements.length; i++) { - let announcement = node.announcements[i] - let key = node.type + '-' + announcement.name + '-' + announcement.id - - let lastPeriodAnnounced = -1 - let newAnnouncementRecord = {} - - for (let j = 0; j < variable.announcements.length; j++) { - let announcementRecord = variable.announcements[j] - if (announcementRecord.key === key) { - lastPeriodAnnounced = announcementRecord.periods - newAnnouncementRecord = announcementRecord - break - } - } - - if (tradingEngine.episode.episodeCounters.periods > lastPeriodAnnounced) { - if (isNaN(value) === false) { - /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ - let valueVariation - - let config = announcement.config - valueVariation = config.valueVariation - - if (newAnnouncementRecord.value !== undefined && valueVariation !== undefined) { - let upperLimit = newAnnouncementRecord.value + newAnnouncementRecord.value * valueVariation / 100 - let lowerLimit = newAnnouncementRecord.value - newAnnouncementRecord.value * valueVariation / 100 - if (value > lowerLimit && value < upperLimit) { - /* There is not enough variation to announce this again. */ - return - } - } - } - - /* - We store the announcement temporarily at an Array to differ its execution, becasue we need to evaulate its formula - and at this point in time the potential variables used at the formula are still not set. - */ - announcement.value = value - announcementsToBeMade.push(announcement) - - /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ - if (newAnnouncementRecord.periods !== undefined) { - newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods - newAnnouncementRecord.value = value - } else { - newAnnouncementRecord = { - key: key, - periods: tradingEngine.episode.episodeCounters.periods, - value: value - } - variable.announcements.push(newAnnouncementRecord) - } - } - } - } - } - - function makeAnnoucements() { - /* Here we go through all the annoucements that need to be done during this loop, and we just do them. */ - for (let i = 0; i < announcementsToBeMade.length; i++) { - announcement = announcementsToBeMade[i] - /* Here we check if there is a formula attached to the annoucement, we evaluate it to get the annoucement text. */ - let formulaValue - if (announcement.formula !== undefined) { - try { - let value = announcement.value - formulaValue = eval(announcement.formula.code) - } catch (err) { - announcement.formula.error = err.message - } - } - announcement.formulaValue = formulaValue - - if (bot.SESSION.socialBots !== undefined) { - bot.SESSION.socialBots.announce(announcement) - } - } - } - - function addCodeToSnapshot(code) { - if (code === undefined) { return } - - try { - let instructionsArray = code.split(' ') - for (let i = 0; i < instructionsArray.length; i++) { - let instruction = instructionsArray[i] - instruction = instruction.replace('(', '') - instruction = instruction.replace(')', '') - instruction = instruction.replace(//g, '') - instruction = instruction.replace(/<=/g, '') - instruction = instruction.replace(/>=/g, '') - instruction = instruction.replace(/!=/g, '') - instruction = instruction.replace(/!==/g, '') - instruction = instruction.replace(/==/g, '') - instruction = instruction.replace(/===/g, '') - instruction = instruction.replace(/{/g, '') - instruction = instruction.replace(/}/g, '') - if (instruction.indexOf('chart') >= 0) { - let parts = instruction.split('.') - let timeFrame = parts[1] - let product = parts[2] - let property - checkPrevious(3) - function checkPrevious(index) { - property = parts[index] - if (property === 'previous') { - product = product + '.previous' - checkPrevious(index + 1) - } - } - - // Example: chart.at01hs.popularSMA.sma200 - chart.at01hs.popularSMA.sma100 < 10 - if (timeFrame !== 'atAnyvariable.timeFrame') { - timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) - } - let key = timeFrame + '-' + product + '-' + property - let existingKey = snapshotKeys.get(key) - - if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. - snapshotKeys.set(key, key) - snapshotLoopHeaders.push(key) - - let value = eval(instruction) - snapshotDataRecord.push(value) - } - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> code = ' + code) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } } function controlLoop() { diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index b9b766155..5d372763a 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -4,7 +4,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const FULL_LOG = true let thisObject = { - setCandle: setCandle, setChart: setChart, evalConditions: evalConditions, evalFormulas: evalFormulas, @@ -23,7 +22,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } let chart - let candle let tradingSystem let tradingEngine @@ -45,7 +43,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function finalize() { chart = undefined - candle = undefined + tradingSystem = undefined tradingEngine = undefined @@ -58,10 +56,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { formulasErrors = undefined } - function setCandle(pCandle) { - candle = pCandle // We need candle to be a local object accessible from conditions and formulas. - } - function setChart(pChart) { chart = pChart // We need chat to be a local object accessible from conditions and formulas. } @@ -260,9 +254,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { checkAnnouncements(triggerStage) tradingEngine.current.strategy.index.value = j - tradingEngine.current.strategy.begin.value = candle.begin - tradingEngine.current.strategy.beginRate.value = candle.min - tradingEngine.current.strategy.endRate.value = candle.min // In case the strategy does not get exited + tradingEngine.current.strategy.begin.value = tradingEngine.current.candle.begin.value + tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value // In case the strategy does not get exited tradingEngine.current.strategy.situationName.value = situation.name tradingEngine.current.strategy.strategyName.value = strategy.name @@ -307,8 +301,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } if (passed) { - tradingEngine.current.strategy.end.value = candle.end - tradingEngine.current.strategy.endRate.value = candle.min + 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' tradingEngine.current.strategy.stageType.value = 'No Stage' tradingEngine.current.strategy.index.value = -1 @@ -686,8 +680,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ if ( - (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.stopLoss.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.stopLoss.value) + (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) ) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } /* @@ -698,12 +692,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { */ if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (tradingEngine.current.position.stopLoss.value < candle.min) { - tradingEngine.current.position.stopLoss.value = candle.min + if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value } } else { - if (tradingEngine.current.position.stopLoss.value > candle.max) { - tradingEngine.current.position.stopLoss.value = candle.max + if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.max.value } } @@ -725,7 +719,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value tradingEngine.current.position.status.value = 1 tradingEngine.current.position.exitType.value = 1 @@ -735,8 +729,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ if ( - (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && candle.min <= tradingEngine.current.position.takeProfit.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && candle.max >= tradingEngine.current.position.takeProfit.value) + (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) ) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } /* @@ -747,12 +741,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { */ if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (tradingEngine.current.position.takeProfit.value > candle.max) { - tradingEngine.current.position.takeProfit.value = candle.max + if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value } } else { - if (tradingEngine.current.position.takeProfit.value < candle.min) { - tradingEngine.current.position.takeProfit.value = candle.min + if (tradingEngine.current.position.takeProfit.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.min.value } } @@ -774,7 +768,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.end.value = candle.end + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value tradingEngine.current.position.status.value = 1 tradingEngine.current.position.exitType.value = 2 @@ -806,7 +800,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function getPositionRate() { - const DEFAULT_VALUE = candle.close + const DEFAULT_VALUE = tradingEngine.current.candle.close.value let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] if (strategy.openStage === undefined) return { DEFAULT_VALUE } From ea924198f2e8c12af26b3665beb1f6529c8933fa Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Jun 2020 22:21:51 +0200 Subject: [PATCH 009/113] sanpshots --- Trading/Snapshots.js | 11 +++++++++++ Trading/TradingSimulation.js | 9 --------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js index 0b2b56e9e..31a7746b3 100644 --- a/Trading/Snapshots.js +++ b/Trading/Snapshots.js @@ -11,6 +11,17 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let tradingEngine + /* We are going to take snapshots of the values of indicators at key moments of the sumulation. */ + + let snapshotKeys = new Map() + snapshotLoopHeaders = [] + snapshotDataRecord = [] + saveAsLastTriggerOnSnapshot = false + saveAsLastTakePositionSnapshot = false + addToSnapshots = false + + let announcementsToBeMade = [] + return thisObject function initialize() { diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index a72167407..a80a2fcea 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -168,16 +168,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering function.') } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + currentCandleIndex) } - /* We are going to take snapshots of the values of indicators at key moments of the sumulation. */ - let snapshotKeys = new Map() - snapshotLoopHeaders = [] - snapshotDataRecord = [] - saveAsLastTriggerOnSnapshot = false - saveAsLastTakePositionSnapshot = false - addToSnapshots = false - - let announcementsToBeMade = [] let candle = candles[currentCandleIndex] tradingEngineModule.setCurrentCandle(candle) From f0b58645ffe3cc6135fcadb1e3abc0fff14937af Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Jun 2020 11:24:02 +0200 Subject: [PATCH 010/113] current candle index --- Trading/TradingRecords.js | 7 ++++++- Trading/TradingSimulation.js | 33 +++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Trading/TradingRecords.js b/Trading/TradingRecords.js index 222c0786d..fcdf8e415 100644 --- a/Trading/TradingRecords.js +++ b/Trading/TradingRecords.js @@ -95,7 +95,12 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { Lets see if there will be an open strategy ... Except if we are at the head of the market (remember we skipped the last candle for not being closed.) */ - if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value === 0 && currentCandleIndex === candles.length - 2 && lastCandle.end !== lastInstantOfTheDay) { + if ( + tradingEngine.current.strategy.begin.value !== 0 && + tradingEngine.current.strategy.end.value === 0 && + currentCandleIndex === candles.length - 2 && + lastCandle.end !== lastInstantOfTheDay + ) { tradingEngine.current.strategy.status.value = 'Open' tradingEngine.current.strategy.end.value = candle.end } diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index a80a2fcea..853945d00 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -112,9 +112,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Last Candle */ let lastCandle = candles[candles.length - 1] - /* Main Simulation Loop: We go thourgh all the candles at this time period. */ - let currentCandleIndex - /* For Loop Level heartbeat */ let loopingDay let previousLoopingDay @@ -131,10 +128,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let diff = lastBegin - firstBegin let amount = diff / sessionParameters.timeFrame.config.value - currentCandleIndex = Math.trunc(amount) + 1 // Because we need to start from the next candle - if (currentCandleIndex < 0 || currentCandleIndex > candles.length - 1) { + tradingEngine.current.candle.index.value = Math.trunc(amount) + 1 // Because we need to start from the next candle + if (tradingEngine.current.candle.index.value < 0 || tradingEngine.current.candle.index.value > candles.length - 1) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> Cannot resume simulation.') - logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> currentCandleIndex = ' + currentCandleIndex) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> tradingEngine.current.candle.index.value = ' + tradingEngine.current.candle.index.value) logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> firstBegin = ' + firstBegin) logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> lastBegin = ' + lastBegin) logger.write(MODULE_NAME, '[ERROR] runSimulation -> initializeLoop -> diff = ' + diff) @@ -150,14 +147,14 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let diff = targetEnd - firstEnd let amount = diff / sessionParameters.timeFrame.config.value - currentCandleIndex = Math.trunc(amount) - if (currentCandleIndex < 0) { currentCandleIndex = 0 } - if (currentCandleIndex > candles.length - 1) { + tradingEngine.current.candle.index.value = Math.trunc(amount) + if (tradingEngine.current.candle.index.value < 0) { tradingEngine.current.candle.index.value = 0 } + if (tradingEngine.current.candle.index.value > candles.length - 1) { /* This will happen when the sessionParameters.timeRange.config.initialDatetime is beyond the last candle available, meaning that the dataSet needs to be updated with more up-to-date data. */ - currentCandleIndex = candles.length - 1 + tradingEngine.current.candle.index.value = candles.length - 1 } } @@ -166,10 +163,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function loop() { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering function.') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + currentCandleIndex) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + tradingEngine.current.candle.index.value) } - let candle = candles[currentCandleIndex] + let candle = candles[tradingEngine.current.candle.index.value] tradingEngineModule.setCurrentCandle(candle) @@ -264,7 +261,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (loopingDay.valueOf() !== previousLoopingDay) { let processingDate = loopingDay.getUTCFullYear() + '-' + utilities.pad(loopingDay.getUTCMonth() + 1, 2) + '-' + utilities.pad(loopingDay.getUTCDate(), 2) - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + currentCandleIndex + ' @ ' + processingDate) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + tradingEngine.current.candle.index.value + ' @ ' + processingDate) } /* Telling the world we are alive and doing well */ let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime.valueOf()) @@ -285,9 +282,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS if (processingDailyFiles) { - /* We skip the candle at the head of the market because currentCandleIndex has not closed yet. */ + /* We skip the candle at the head of the market because tradingEngine.current.candle.index.value has not closed yet. */ let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value - if (currentCandleIndex === candles.length - 1) { + if (tradingEngine.current.candle.index.value === candles.length - 1) { if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { /* We are at the head of the market, thus we skip the last candle because it has not close yet. */ if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle because it is the last one and has not been closed yet.') } @@ -298,7 +295,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } } else { // We are processing Market Files - if (currentCandleIndex === candles.length - 1) { + if (tradingEngine.current.candle.index.value === candles.length - 1) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle because it is the last one and has not been closed yet.') } controlLoop() return @@ -459,8 +456,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT return } - currentCandleIndex++ - if (currentCandleIndex < candles.length) { + tradingEngine.current.candle.index.value++ + if (tradingEngine.current.candle.index.value < candles.length) { setImmediate(loop) // This will execute the next loop in the next iteration of the NodeJs event loop allowing for other callbacks to be executed. } else { afterLoop() From f8eb06b4866a8f72ad9d46b9e5a08fb3dd328fd1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Jun 2020 12:31:21 +0200 Subject: [PATCH 011/113] Overall Simulation Structure done --- Trading/Snapshots.js | 18 +- Trading/TradingSimulation.js | 391 +++++++++++++++++------------------ 2 files changed, 203 insertions(+), 206 deletions(-) diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js index 31a7746b3..7edfd34f9 100644 --- a/Trading/Snapshots.js +++ b/Trading/Snapshots.js @@ -11,8 +11,17 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let tradingEngine - /* We are going to take snapshots of the values of indicators at key moments of the sumulation. */ + /* Snapshots of Trigger On and Take Positions */ + let snapshots = { + headers: undefined, + triggerOn: [], + takePosition: [], + lastTriggerOn: undefined, + lastTakePosition: undefined + } + + /* We are going to take snapshots of the values of indicators at key moments of the sumulation. */ let snapshotKeys = new Map() snapshotLoopHeaders = [] snapshotDataRecord = [] @@ -68,10 +77,9 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } - if (positionedAtYesterday === false) { - snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) - snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) - } + snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) + snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) + snapshots.lastTriggerOn = undefined snapshots.lastTakePosition = undefined addToSnapshots = false diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 853945d00..4e24d8d2e 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -42,15 +42,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT processingDailyFiles = true } - /* Snapshots of Trigger On and Take Positions */ - let snapshots = { - headers: undefined, - triggerOn: [], - takePosition: [], - lastTriggerOn: undefined, - lastTakePosition: undefined - } - let tradingSystem = bot.TRADING_SYSTEM let tradingEngine = bot.TRADING_ENGINE let sessionParameters = bot.SESSION.parameters @@ -61,12 +52,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let timerToCloseStage = 0 /* Stop Loss Management */ - const MIN_STOP_LOSS_VALUE = 0.0000000001 // We can not let the stop be zero to avoid division by 0 error or infinity numbers as a result. const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER /* Take Profit Management */ - const MIN_TAKE_PROFIT_VALUE = 0.0000000001 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER @@ -80,6 +69,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT lastInstantOfTheDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1 } + /* These are the Modules we will need to run the Simulation */ const TRADING_ENGINE_MODULE = require('./TradingEngine.js') let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() @@ -104,17 +94,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) tradingSystemModule.initialize() - /* Main Array and Maps */ + /* Setting up the candles array: The whole simulation is based on the array of candles at the time-frame defined at the session parameters. */ let propertyName = 'at' + sessionParameters.timeFrame.config.label.replace('-', '') let candles = chart[propertyName].candles - let currentChart = chart[propertyName] /* Last Candle */ - let lastCandle = candles[candles.length - 1] + let lastCandle = candles[candles.length - 1] // TODO Maybe remove this - /* For Loop Level heartbeat */ - let loopingDay - let previousLoopingDay + /* Variable needed for heartbeat functionality */ + let heartBeatDate + let previousheartBeatDate initializeLoop() @@ -162,191 +151,38 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function loop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering function.') } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + tradingEngine.current.candle.index.value) } - - let candle = candles[tradingEngine.current.candle.index.value] - - tradingEngineModule.setCurrentCandle(candle) - - /* Not processing while out of user-defined time range */ - if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } - controlLoop() - return - } - if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle after the sessionParameters.timeRange.config.finalDatetime.') } - afterLoop() - return - } - - /* Here we update at the chart data structure the objects for each product representing where we are currently standing at the simulation loop */ - if (processingDailyFiles) { - for (let j = 0; j < global.dailyFilePeriods.length; j++) { - let mapKey = dailyFilePeriods[j][1] - let propertyName = 'at' + mapKey.replace('-', '') - let thisChart = chart[propertyName] - - for (let k = 0; k < dataDependencies.length; k++) { - let dataDependencyNode = dataDependencies[k] - if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Daily') { continue } - let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName - let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let currentElement = getElement(thisChart[pluralVariableName], candle, 'Daily' + '-' + mapKey + '-' + pluralVariableName) - thisChart[singularVariableName] = currentElement - } - } - } - - /* Finding the Current Element on Market Files */ - for (let j = 0; j < global.marketFilesPeriods.length; j++) { - let mapKey = marketFilesPeriods[j][1] - let propertyName = 'at' + mapKey.replace('-', '') - let thisChart = chart[propertyName] - - for (let k = 0; k < dataDependencies.length; k++) { - let dataDependencyNode = dataDependencies[k] - if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Market') { continue } - let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName - let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let currentElement = getElement(thisChart[pluralVariableName], candle, 'Market' + '-' + mapKey + '-' + pluralVariableName) - thisChart[singularVariableName] = currentElement - } - } - - /* Finding the Current Element on Single Files */ - function isItInside(elementWithTimestamp, elementWithBeginEnd) { - if (elementWithTimestamp.timestamp >= elementWithBeginEnd.begin && elementWithTimestamp.timestamp <= elementWithBeginEnd.end) { - return true - } else { - return false - } - } - - let propertyName = 'atAnyTimeFrame' - let thisChart = chart[propertyName] - - for (let k = 0; k < dataDependencies.length; k++) { - let dataDependencyNode = dataDependencies[k] - if (dataDependencyNode.referenceParent.config.codeName !== 'Single-File') { continue } - let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName - let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let elementArray = thisChart[pluralVariableName] - let currentElement - if (elementArray !== undefined) { - currentElement = elementArray[elementArray.length - 1] - } - thisChart[singularVariableName] = currentElement - } - - /* While we are processing the previous day. */ - let positionedAtYesterday = false - if (currentDay) { - positionedAtYesterday = (candle.end < currentDay.valueOf()) - } + let candle = candles[tradingEngine.current.candle.index.value] // This is the current candle the Simulation is working at. if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle Begin @ ' + (new Date(candle.begin)).toLocaleString()) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle End @ ' + (new Date(candle.end)).toLocaleString()) } - let ticker = { - bid: candle.close, - ask: candle.close, - last: candle.close + if (checkIfWeAreAtTheHeadOfTheMarket() === false) { + afterLoop() + return } - /* We will produce a simulation level heartbeat in order to inform the user this is running. */ - loopingDay = new Date(Math.trunc(candle.begin / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - if (loopingDay.valueOf() !== previousLoopingDay) { - let processingDate = loopingDay.getUTCFullYear() + '-' + utilities.pad(loopingDay.getUTCMonth() + 1, 2) + '-' + utilities.pad(loopingDay.getUTCDate(), 2) - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + tradingEngine.current.candle.index.value + ' @ ' + processingDate) } - - /* Telling the world we are alive and doing well */ - let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime.valueOf()) - let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime.valueOf()) - - let currentDateString = loopingDay.getUTCFullYear() + '-' + utilities.pad(loopingDay.getUTCMonth() + 1, 2) + '-' + utilities.pad(loopingDay.getUTCDate(), 2) - let currentDate = new Date(loopingDay) - let percentage = global.getPercentage(fromDate, currentDate, lastDate) - bot.processHeartBeat(currentDateString, percentage) - - if (global.areEqualDates(currentDate, new Date()) === false) { - logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) - } - } - previousLoopingDay = loopingDay.valueOf() - - tradingEngine.episode.episodeCounters.periods++ - tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS - - if (processingDailyFiles) { - /* We skip the candle at the head of the market because tradingEngine.current.candle.index.value has not closed yet. */ - let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value - if (tradingEngine.current.candle.index.value === candles.length - 1) { - if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { - /* We are at the head of the market, thus we skip the last candle because it has not close yet. */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle because it is the last one and has not been closed yet.') } - controlLoop() - return - /* Note here that in the last candle of the first day or the second day it will use an incomplete candle and partially calculated indicators. - if we skip these two tradingEngine.episode.episodeCounters.periods, then there will be a hole in the file since the last period will be missing. */ - } - } - } else { // We are processing Market Files - if (tradingEngine.current.candle.index.value === candles.length - 1) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Skipping Candle because it is the last one and has not been closed yet.') } - controlLoop() - return - } + if (checkInitialAndFinalDatetime() === false) { + afterLoop() + return } + if (checkMinimunAndMaximunBalance() === false) { + afterLoop() + return + } + + + heartBeat() + + tradingEngineModule.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas. + positionChartAtCurrentCandle() + + tradingEngineModule.updateEpisodeCountersAndStatistics() tradingSystemModule.setChart(chart) tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() - - - - /* Checks for Minimun and Maximun Balance. We do the check while not inside any strategy only. */ - if ( - tradingEngine.current.strategy.stageType.value === 'No Stage' && - tradingEngine.current.strategy.index.value === -1 - ) { - /* - First thing to do is to check if we are below the minimum balance, or above the maximun balance. - If we are, we are not gooing to trigger on again. - */ - let minimumBalance - let maximumBalance - let balance - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - balance = tradingEngine.current.balance.baseAsset.value - minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance - maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance - } else { - balance = tradingEngine.current.balance.quotedAsset.value - minimumBalance = sessionParameters.sessionQuotedAsset.config.minimumBalance - maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance - } - - let stopRunningDate = new Date(candle.begin) - if (balance <= minimumBalance) { - tradingSystem.error = 'Min Balance @ ' + stopRunningDate.toLocaleString() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> ' + tradingSystem.error) } - afterLoop() - return - } - - if (balance >= maximumBalance) { - tradingSystem.error = 'Max Balance @ ' + stopRunningDate.toLocaleString() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> ' + tradingSystem.error) } - afterLoop() - return - } - } - tradingSystemModule.checkTriggerOn() tradingSystemModule.checkTriggerOff() takePositionNow = tradingSystemModule.checkTakePosition() @@ -369,8 +205,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.calculateTakeProfit() } - - tradingEngineModule.updatePositionCounters() + tradingEngineModule.updatePositionCountersAndStatistics() tradingEngineModule.updateDistanceToEventsCounters() /* Checking if Stop or Take Profit were hit */ @@ -381,8 +216,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT closePositionNow = tradingSystemModule.checkStopLossOrTakeProfitWasHit() } - - /* Taking a Position */ if ( takePositionNow === true @@ -403,7 +236,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngineModule.closePosition() } - /* Closing the Closing Stage */ + /* Closing the Closing Stage TODO This should die once we have execution working. */ if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { tradingEngine.current.strategy.end.value = candle.end @@ -427,13 +260,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingRecordsModule.addConditionsRecord() tradingRecordsModule.addStrategyRecord() tradingRecordsModule.addPositionRecord() - - /* We will remmember the last candle processed, so that if it is the last one of this run we can use it to continue from there next time. */ - tradingEngine.last.candle.begin = candle.begin - tradingEngine.last.candle.end = candle.end - - /* After everything at the simulation level was done, we will do the annoucements that are pending. */ - annoucementsModule.makeAnnoucements() + annoucementsModule.makeAnnoucements() // After everything at the simulation level was done, we will do the annoucements that are pending. controlLoop() } @@ -515,6 +342,168 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } + + function heartBeat() { + /* We will produce a simulation level heartbeat in order to inform the user this is running. */ + heartBeatDate = new Date(Math.trunc(candle.begin / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) + if (heartBeatDate.valueOf() !== previousheartBeatDate) { + let processingDate = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) + + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + tradingEngine.current.candle.index.value + ' @ ' + processingDate) } + + /* Telling the world we are alive and doing well */ + let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime.valueOf()) + let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime.valueOf()) + + let currentDateString = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) + let currentDate = new Date(heartBeatDate) + let percentage = global.getPercentage(fromDate, currentDate, lastDate) + bot.processHeartBeat(currentDateString, percentage) + + if (global.areEqualDates(currentDate, new Date()) === false) { + logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) + } + } + previousheartBeatDate = heartBeatDate.valueOf() + } + + function positionChartAtCurrentCandle() { + /* + In conditions and Formulas, we want users to have an easy sintax to refer to indicators. In order to achieve that, we need the user to have + easy access to the current candle for instance, or the current bollinger band, meaning the one the Simulation is currently standing at. + For that reason we do the following processing, to have at the chart data structure the current objects of each indicator / time frame. + */ + + /* Finding the Current Element on Market Files */ + if (processingDailyFiles) { + for (let j = 0; j < global.dailyFilePeriods.length; j++) { + let mapKey = dailyFilePeriods[j][1] + let propertyName = 'at' + mapKey.replace('-', '') + let thisChart = chart[propertyName] + + for (let k = 0; k < dataDependencies.length; k++) { + let dataDependencyNode = dataDependencies[k] + if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Daily') { continue } + let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName + let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName + let currentElement = getElement(thisChart[pluralVariableName], candle, 'Daily' + '-' + mapKey + '-' + pluralVariableName) + thisChart[singularVariableName] = currentElement + } + } + } + + /* Finding the Current Element on Market Files */ + for (let j = 0; j < global.marketFilesPeriods.length; j++) { + let mapKey = marketFilesPeriods[j][1] + let propertyName = 'at' + mapKey.replace('-', '') + let thisChart = chart[propertyName] + + for (let k = 0; k < dataDependencies.length; k++) { + let dataDependencyNode = dataDependencies[k] + if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Market') { continue } + let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName + let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName + let currentElement = getElement(thisChart[pluralVariableName], candle, 'Market' + '-' + mapKey + '-' + pluralVariableName) + thisChart[singularVariableName] = currentElement + } + } + + /* Finding the Current Element At Single Files*/ + let propertyName = 'atAnyTimeFrame' + let thisChart = chart[propertyName] + + for (let k = 0; k < dataDependencies.length; k++) { + let dataDependencyNode = dataDependencies[k] + if (dataDependencyNode.referenceParent.config.codeName !== 'Single-File') { continue } + let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName + let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName + let elementArray = thisChart[pluralVariableName] + let currentElement + if (elementArray !== undefined) { + currentElement = elementArray[elementArray.length - 1] + } + thisChart[singularVariableName] = currentElement + } + } + + function checkIfWeAreAtTheHeadOfTheMarket() { + /* +We skip the candle at the head of the market because it has not closed yet. The procedure to determine if we are at the head of the market is different +when we are processing Market Files than when we are processing Daily Files. TODO Check that the procedure is good for Beta 6 +*/ + if (processingDailyFiles) { // We are processing Daily Files + let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value + if (tradingEngine.current.candle.index.value === candles.length - 1) { + if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkIfWeAreAtTheHeadOfTheMarket -> Skipping Candle because it is the last one and has not been closed yet.') } + return false + /* + Note here that in the last candle of the first day or the second day it will use an incomplete candle and partially calculated indicators. + if we skip these two periods, then there will be a hole in the file since the last period will be missing. + */ + } + } + } else { // We are processing Market Files + if (tradingEngine.current.candle.index.value === candles.length - 1) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkIfWeAreAtTheHeadOfTheMarket -> Skipping Candle because it is the last one and has not been closed yet.') } + return false + } + } + return true + } + + function checkInitialAndFinalDatetime() { + /* Here we check that the current candle is not outside of user-defined time range at the session parameters.*/ + if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } + return false + } + if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle after the sessionParameters.timeRange.config.finalDatetime.') } + return false + } + return true + } + + function checkMinimunAndMaximunBalance() { + /* Checks for Minimun and Maximun Balance. We do the check while not inside any strategy only. */ + if ( + tradingEngine.current.strategy.stageType.value === 'No Stage' && + tradingEngine.current.strategy.index.value === -1 + ) { + /* + First thing to do is to check if we are below the minimum balance, or above the maximun balance. + If we are, we are not gooing to trigger on again. + */ + let minimumBalance + let maximumBalance + let balance + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + balance = tradingEngine.current.balance.baseAsset.value + minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance + maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance + } else { + balance = tradingEngine.current.balance.quotedAsset.value + minimumBalance = sessionParameters.sessionQuotedAsset.config.minimumBalance + maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance + } + + let stopRunningDate = new Date(candle.begin) + if (balance <= minimumBalance) { + tradingSystem.error = 'Min Balance @ ' + stopRunningDate.toLocaleString() + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } + return false + } + + if (balance >= maximumBalance) { + tradingSystem.error = 'Max Balance @ ' + stopRunningDate.toLocaleString() + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } + return false + } + } + return true + } } catch (err) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> err = ' + err.stack) callBackFunction(global.DEFAULT_FAIL_RESPONSE) From 2dec70b3e43dccaee90c801dc5b7432c228b20c2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Jun 2020 12:35:20 +0200 Subject: [PATCH 012/113] Update TradingSimulation.js --- Trading/TradingSimulation.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 4e24d8d2e..946984692 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -46,20 +46,20 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingEngine = bot.TRADING_ENGINE let sessionParameters = bot.SESSION.parameters - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } - let timerToCloseStage = 0 + let timerToCloseStage = 0 // TODO - /* Stop Loss Management */ + /* Stop Loss Management TODO */ const MIN_STOP_LOSS_VALUE = 0.0000000001 // We can not let the stop be zero to avoid division by 0 error or infinity numbers as a result. const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER - /* Take Profit Management */ + /* Take Profit Management TODO */ const MIN_TAKE_PROFIT_VALUE = 0.0000000001 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER - /* Variables for this executioin only */ + /* Variables to know when we need to open or close a position. */ let takePositionNow = false let closePositionNow = false @@ -101,7 +101,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Last Candle */ let lastCandle = candles[candles.length - 1] // TODO Maybe remove this - /* Variable needed for heartbeat functionality */ + /* Variables needed for heartbeat functionality */ let heartBeatDate let previousheartBeatDate From 962d2d117a9637d5f26fe57255d7cc8a572ca272 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Jun 2020 15:06:22 +0200 Subject: [PATCH 013/113] checkParameters function --- Session.js | 186 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 141 insertions(+), 45 deletions(-) diff --git a/Session.js b/Session.js index 3e84808ed..23613c37c 100644 --- a/Session.js +++ b/Session.js @@ -79,6 +79,9 @@ } } + if (checkParemeters() === false) { return } + + /* Extract values from different sources and consolidate them under one structure that is going to be used later on. */ setValuesToUse(message) @@ -116,6 +119,121 @@ } } + function checkParemeters() { + /* + Here we check all the Session Parameters received. If something critical is missing we abort returning false. If something + non critical is missing, we complete it with a default value. + */ + + if (bot.SESSION.parameters === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Node with no Parameters."); } + return false + } + + /* Time Range */ + if (bot.SESSION.parameters.timeRange === undefined) { // if the Time Range is missing we create a default one. + bot.SESSION.parameters.timeRange = { + name: 'Missing Time Range', + type: 'Time Range', + config: { + initialDatetime: (new Date()).valueOf(), + finalDatetime: (new Date()).valueOf() + } + } + } + if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) { // if the initialDatetime is missing we create a default one. + bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date()).valueOf() + } + if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { // if the finalDatetime is missing we create a default one. + bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date()).valueOf() + } + + /* Time Frame */ + if (bot.SESSION.parameters.timeFrame === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Time Frame."); } + return false + } + if (bot.SESSION.parameters.timeFrame.label === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Time Frame Label configuration."); } + return false + } + + /* Session Base Asset */ + if (bot.SESSION.parameters.sessionBaseAsset === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Session Base Asset."); } + return false + } + if (bot.SESSION.parameters.sessionBaseAsset.config.initialBalance === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Session Base Asset with no initialBalance configuration."); } + return false + } + if (bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance === undefined) { + bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = 0 + } + if (bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance === undefined) { + bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER + } + + /* Session Quoted Asset */ + if (bot.SESSION.parameters.sessionQuotedAsset === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Session Quoted Asset."); } + return false + } + if (bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance === undefined) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Session Quoted Asset with no initialBalance configuration."); } + return false + } + if (bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance === undefined) { + bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = 0 + } + if (bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance === undefined) { + bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER + } + + /* Slippage */ + if (bot.SESSION.parameters.slippage === undefined) { // if the Slippage is missing we create a default one. + bot.SESSION.parameters.timeRange = { + name: 'Missing Slippage', + type: 'Slippage', + config: { + positionRate: 0, + stopLoss: 0, + takeProfit: 0 + } + } + } + if (bot.SESSION.parameters.slippage.config.positionRate === undefined) { // if the positionRate is missing we create a default one. + bot.SESSION.parameters.slippage.config.positionRate = 0 + } + if (bot.SESSION.parameters.slippage.config.stopLoss === undefined) { // if the stopLoss is missing we create a default one. + bot.SESSION.parameters.slippage.config.stopLoss = 0 + } + if (bot.SESSION.parameters.slippage.config.takeProfit === undefined) { // if the takeProfit is missing we create a default one. + bot.SESSION.parameters.slippage.config.takeProfit = 0 + } + + /* Fee Structure */ + if (bot.SESSION.parameters.feeStructure === undefined) { // if the Fee Structure is missing we create a default one. + bot.SESSION.parameters.feeStructure = { + name: 'Missing Fee Structure', + type: 'Fee Structure', + config: { + maker: 0, + taker: 0 + } + } + } + if (bot.SESSION.parameters.feeStructure.config.maker === undefined) { // if the maker is missing we create a default one. + bot.SESSION.parameters.feeStructure.config.maker = 0 + } + if (bot.SESSION.parameters.feeStructure.config.taker === undefined) { // if the taker is missing we create a default one. + bot.SESSION.parameters.feeStructure.config.taker = 0 + } + + return true + } + + function stopSession(message) { try { bot.STOP_SESSION = true @@ -127,39 +245,38 @@ } function checkDatetimes() { - if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding initialDatetime with present datetime because " + bot.VALUES_TO_USE.timeRange.initialDatetime + " is in the past."); } - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() + if (bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf() < (new Date()).valueOf()) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding initialDatetime with present datetime because " + bot.SESSION.parameters.timeRange.config.initialDatetime + " is in the past."); } + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() } - if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() < (new Date()).valueOf()) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding finalDatetime with present datetime plus one year because " + bot.VALUES_TO_USE.timeRange.finalDatetime + " is in the past."); } - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() + ONE_YEAR_IN_MILISECONDS + if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() < (new Date()).valueOf()) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding finalDatetime with present datetime plus one year because " + bot.SESSION.parameters.timeRange.config.finalDatetime + " is in the past."); } + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() + ONE_YEAR_IN_MILISECONDS } } function startBackTesting(message) { bot.startMode = "Backtest" - if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() > (new Date()).valueOf()) { - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() + if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() > (new Date()).valueOf()) { + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() } bot.resumeExecution = false; bot.hasTheBotJustStarted = true - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) return true } function startLiveTrading(message) { if (process.env.KEY === undefined || process.env.SECRET === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startLiveTrading -> Key name or Secret not provided, not possible to run the process in Live mode."); } - console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.") + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> Key 'codeName' or 'secret' not provided. Plese check that and try again."); } return } bot.startMode = "Live" checkDatetimes() bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) return true @@ -176,7 +293,7 @@ bot.startMode = "Live" checkDatetimes() bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true /* Reduce the balance */ @@ -207,7 +324,7 @@ checkDatetimes() bot.resumeExecution = false; bot.hasTheBotJustStarted = true - bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) + bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) return true } @@ -234,49 +351,28 @@ } function setValuesToUse(message) { - /* - Base on the information received we will determined which values ultimatelly are going to be used, - and once we do, they will become constants across the multiple loops executions. - */ - - /* Set all default values */ - bot.VALUES_TO_USE = { - slippage: { - positionRate: 0, - stopLoss: 0, - takeProfit: 0 - }, - feeStructure: { - maker: 0, - taker: 0 - }, - timeRange: { - initialDatetime: new Date(), - finalDatetime: new Date() - } - } /* Session Type Dependant Default Values */ switch (bot.SESSION.type) { case 'Backtesting Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date((new Date()).valueOf() - ONE_YEAR_IN_MILISECONDS) - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date((new Date()).valueOf() - ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() break } case 'Live Trading Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) break } case 'Fordward Testing Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) break } case 'Paper Trading Session': { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) break } } @@ -415,14 +511,14 @@ if (isNaN(Date.parse(config.initialDatetime)) === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use initialDatatime provided at Session Parameters because it is not a valid Date."); } else { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date(config.initialDatetime) + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date(config.initialDatetime) } } if (config.finalDatetime !== undefined) { if (isNaN(Date.parse(config.finalDatetime)) === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use finalDatetime provided at Session Parameters because it is not a valid Date."); } else { - bot.VALUES_TO_USE.timeRange.finalDatetime = new Date(config.finalDatetime) + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date(config.finalDatetime) } } } From e646a66b5778eac500319432c62be6f39e301a52 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 11:38:39 +0200 Subject: [PATCH 014/113] New Social Bots and Telegram Bots modules --- Session.js | 417 +++++++---------------------------- SocialBots.js | 80 +++++++ TelegramBot.js | 51 +++++ Trading/Snapshots.js | 1 - Trading/TradingExecution.js | 6 +- TradingBotProcessMainLoop.js | 46 ++-- 6 files changed, 237 insertions(+), 364 deletions(-) create mode 100644 SocialBots.js create mode 100644 TelegramBot.js diff --git a/Session.js b/Session.js index 23613c37c..2db5d9190 100644 --- a/Session.js +++ b/Session.js @@ -8,14 +8,15 @@ initialize: initialize } + const SOCIAL_BOTS_MODULE = require('./SocialBots.js') + let socialBotsModule = SOCIAL_BOTS_MODULE.newSocialBots(bot, logger) + return thisObject; function initialize(pProcessConfig, callBackFunction) { try { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - let telegramAPI - /* Initialize this info so that everything is logged propeerly */ bot.SESSION = { @@ -59,34 +60,14 @@ bot.SESSION = JSON.parse(message.event.session) bot.DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter) bot.RESUME = message.event.resume + bot.SESSION.stop = sessionStop // stop function - /* Setup the APP_SCHEMA_MAP based on the APP_SCHEMA_ARRAY */ - bot.APP_SCHEMA_MAP = new Map() - for (let i = 0; i < bot.APP_SCHEMA_ARRAY.length; i++) { - let nodeDefinition = bot.APP_SCHEMA_ARRAY[i] - let key = nodeDefinition.type - bot.APP_SCHEMA_MAP.set(key, nodeDefinition) - } - - /* Set the folderName for logging, reports, context and data output */ - let config - if (bot.SESSION.config !== undefined) { - config = bot.SESSION.config - if (config.folderName === undefined) { - bot.SESSION.folderName = bot.SESSION.id - } else { - bot.SESSION.folderName = config.folderName + "-" + bot.SESSION.id - } - } + setUpAppSchema() + setUpSessionFolderName() if (checkParemeters() === false) { return } - - /* Extract values from different sources and consolidate them under one structure that is going to be used later on. */ - setValuesToUse(message) - - /* Set up Social Bots */ - initializeSocialBots() + socialBotsModule.initialize() let allGood switch (bot.SESSION.type) { @@ -113,9 +94,44 @@ } else { bot.STOP_SESSION = true } + + socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' was started.") } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> runSession -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function stopSession(commandOrigin) { + try { + if (reason === undefined) { commandOrigin = ' from the User Interface.' } + socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' was stopped " + commandOrigin) + bot.STOP_SESSION = true + socialBotsModule.finalize() + } catch (err) { + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> stopSession -> err = " + err.stack); + } + } + + function setUpAppSchema() { + /* Setup the APP_SCHEMA_MAP based on the APP_SCHEMA_ARRAY */ + bot.APP_SCHEMA_MAP = new Map() + for (let i = 0; i < bot.APP_SCHEMA_ARRAY.length; i++) { + let nodeDefinition = bot.APP_SCHEMA_ARRAY[i] + let key = nodeDefinition.type + bot.APP_SCHEMA_MAP.set(key, nodeDefinition) + } + } + + function setUpSessionFolderName() { + /* Set the folderName for logging, reports, context and data output */ + let config + if (bot.SESSION.config !== undefined) { + config = bot.SESSION.config + if (config.folderName === undefined) { + bot.SESSION.folderName = bot.SESSION.id + } else { + bot.SESSION.folderName = config.folderName + "-" + bot.SESSION.id + } } } @@ -147,6 +163,30 @@ if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { // if the finalDatetime is missing we create a default one. bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date()).valueOf() } + /* Session Type Forced Values */ + switch (bot.SESSION.type) { + case 'Live Trading Session': { + if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() > (new Date()).valueOf()) { + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() + } + break + } + case 'Live Trading Session': { + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + break + } + case 'Fordward Testing Session': { + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + break + } + case 'Paper Trading Session': { + bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + break + } + } /* Time Frame */ if (bot.SESSION.parameters.timeFrame === undefined) { @@ -233,48 +273,20 @@ return true } - - function stopSession(message) { - try { - bot.STOP_SESSION = true - finalizeSocialBots() - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> stopSession -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function checkDatetimes() { - if (bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf() < (new Date()).valueOf()) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding initialDatetime with present datetime because " + bot.SESSION.parameters.timeRange.config.initialDatetime + " is in the past."); } - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - } - if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() < (new Date()).valueOf()) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding finalDatetime with present datetime plus one year because " + bot.SESSION.parameters.timeRange.config.finalDatetime + " is in the past."); } - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() + ONE_YEAR_IN_MILISECONDS - } - } - function startBackTesting(message) { - bot.startMode = "Backtest" - if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() > (new Date()).valueOf()) { - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() - } - bot.resumeExecution = false; - bot.hasTheBotJustStarted = true - bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.resumeExecution = false; // TODO change this for the new way of handling resuming + bot.hasTheBotJustStarted = true // TODO see if this is necesary + bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // TODO see if this is necesary return true } function startLiveTrading(message) { - if (process.env.KEY === undefined || process.env.SECRET === undefined) { + if (process.env.KEY === undefined || process.env.SECRET === undefined) { // TODO Remove this from Process .env if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> Key 'codeName' or 'secret' not provided. Plese check that and try again."); } - return + return false } - bot.startMode = "Live" - checkDatetimes() bot.resumeExecution = false; bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true @@ -287,11 +299,9 @@ if (process.env.KEY === undefined || process.env.SECRET === undefined) { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startFordwardTesting -> Key name or Secret not provided, not possible to run the process in Forward Testing mode."); } console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.") - return + return false } - bot.startMode = "Live" - checkDatetimes() bot.resumeExecution = false; bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true @@ -304,14 +314,14 @@ balancePercentage = bot.SESSION.config.balancePercentage } - bot.VALUES_TO_USE.initialBalanceA = bot.VALUES_TO_USE.initialBalanceA * balancePercentage / 100 - bot.VALUES_TO_USE.initialBalanceB = bot.VALUES_TO_USE.initialBalanceB * balancePercentage / 100 + bot.SESSION.parameters.sessionBaseAsset.config.initialBalance = bot.SESSION.parameters.sessionBaseAsset.config.initialBalance * balancePercentage / 100 + bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance = bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance * balancePercentage / 100 - bot.VALUES_TO_USE.minimumBalanceA = bot.VALUES_TO_USE.minimumBalanceA * balancePercentage / 100 - bot.VALUES_TO_USE.minimumBalanceB = bot.VALUES_TO_USE.minimumBalanceB * balancePercentage / 100 + bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance * balancePercentage / 100 + bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance * balancePercentage / 100 - bot.VALUES_TO_USE.maximumBalanceA = bot.VALUES_TO_USE.maximumBalanceA * balancePercentage / 100 - bot.VALUES_TO_USE.maximumBalanceB = bot.VALUES_TO_USE.maximumBalanceB * balancePercentage / 100 + bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100 + bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100 pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) @@ -319,13 +329,11 @@ } function startPaperTrading(message) { - bot.startMode = "Backtest" - checkDatetimes() bot.resumeExecution = false; bot.hasTheBotJustStarted = true bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) - pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) + pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.label) return true } @@ -350,271 +358,6 @@ } } - function setValuesToUse(message) { - - /* Session Type Dependant Default Values */ - - switch (bot.SESSION.type) { - case 'Backtesting Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date((new Date()).valueOf() - ONE_YEAR_IN_MILISECONDS) - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() - break - } - case 'Live Trading Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) - break - } - case 'Fordward Testing Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) - break - } - case 'Paper Trading Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) - break - } - } - - let tradingSystem = bot.TRADING_SYSTEM - - if (tradingSystem !== undefined) { - - /* Applying Session Level Parameters */ - if (bot.SESSION !== undefined) { - if (bot.SESSION.parameters !== undefined) { - - /* Base Asset and Initial Balances. */ - { - if (bot.SESSION.parameters.baseAsset !== undefined) { - if (bot.SESSION.parameters.baseAsset.referenceParent !== undefined) { - if (bot.SESSION.parameters.baseAsset.referenceParent.referenceParent !== undefined) { - - let config = bot.SESSION.parameters.baseAsset.referenceParent.referenceParent.config - - if (config.codeName !== undefined) { - bot.VALUES_TO_USE.baseAsset = config.codeName; - } - - config = bot.SESSION.parameters.baseAsset.config - - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { - if (config.initialBalance !== undefined) { - bot.VALUES_TO_USE.initialBalanceA = config.initialBalance; - bot.VALUES_TO_USE.initialBalanceB = 0 - } - if (config.minimumBalance !== undefined) { - bot.VALUES_TO_USE.minimumBalanceA = config.minimumBalance; - bot.VALUES_TO_USE.minimumBalanceB = 0 - } - if (config.maximumBalance !== undefined) { - bot.VALUES_TO_USE.maximumBalanceA = config.maximumBalance; - bot.VALUES_TO_USE.maximumBalanceB = 0 - } - } else { - if (config.initialBalance !== undefined) { - bot.VALUES_TO_USE.initialBalanceB = config.initialBalance; - bot.VALUES_TO_USE.initialBalanceA = 0 - } - if (config.minimumBalance !== undefined) { - bot.VALUES_TO_USE.minimumBalanceB = config.minimumBalance; - bot.VALUES_TO_USE.minimumBalanceA = 0 - } - if (config.maximumBalance !== undefined) { - bot.VALUES_TO_USE.maximumBalanceB = config.maximumBalance; - bot.VALUES_TO_USE.maximumBalanceA = 0 - } - } - - /* Solve when values are missing. */ - if (bot.VALUES_TO_USE.minimumBalanceA === undefined) { - bot.VALUES_TO_USE.minimumBalanceA = 0 - } - - if (bot.VALUES_TO_USE.minimumBalanceB === undefined) { - bot.VALUES_TO_USE.minimumBalanceB = 0 - } - - if (bot.VALUES_TO_USE.maximumBalanceA === undefined) { - bot.VALUES_TO_USE.maximumBalanceA = 10000000000000000 - } - - if (bot.VALUES_TO_USE.maximumBalanceB === undefined) { - bot.VALUES_TO_USE.maximumBalanceB = 10000000000000000 - } - } - } - } - } - - /* Quoted Asset. */ - { - if (bot.SESSION.parameters.quotedAsset !== undefined) { - if (bot.SESSION.parameters.quotedAsset.referenceParent !== undefined) { - if (bot.SESSION.parameters.quotedAsset.referenceParent.referenceParent !== undefined) { - - let config = bot.SESSION.parameters.quotedAsset.referenceParent.referenceParent.config - - if (config.codeName !== undefined) { - bot.VALUES_TO_USE.quotedAsset = config.codeName; - } - } - } - } - } - - /* Time Frame */ - if (bot.SESSION.parameters.timeFrame !== undefined) { - bot.VALUES_TO_USE.timeFrame = bot.SESSION.parameters.timeFrame.config.value - } - - /* Slippage */ - if (bot.SESSION.parameters.slippage !== undefined) { - if (bot.SESSION.parameters.slippage.config !== undefined) { - - let config = bot.SESSION.parameters.slippage.config - - if (config.positionRate !== undefined) { - bot.VALUES_TO_USE.slippage.positionRate = config.positionRate - } - if (config.stopLoss !== undefined) { - bot.VALUES_TO_USE.slippage.stopLoss = config.stopLoss - } - if (config.takeProfit !== undefined) { - bot.VALUES_TO_USE.slippage.takeProfit = config.takeProfit - } - } - } - - /* Fee Structure */ - if (bot.SESSION.parameters.feeStructure !== undefined) { - if (bot.SESSION.parameters.feeStructure.config !== undefined) { - - let config = bot.SESSION.parameters.feeStructure.config - - if (config.maker !== undefined) { - bot.VALUES_TO_USE.feeStructure.maker = config.maker - } - if (config.taker !== undefined) { - bot.VALUES_TO_USE.feeStructure.taker = config.taker - } - } - } - - /* Time Range */ - if (bot.SESSION.parameters.timeRange !== undefined) { - if (bot.SESSION.parameters.timeRange.config !== undefined) { - - let config = bot.SESSION.parameters.timeRange.config - if (config.initialDatetime !== undefined) { - if (isNaN(Date.parse(config.initialDatetime)) === true) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use initialDatatime provided at Session Parameters because it is not a valid Date."); - } else { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date(config.initialDatetime) - } - } - if (config.finalDatetime !== undefined) { - if (isNaN(Date.parse(config.finalDatetime)) === true) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use finalDatetime provided at Session Parameters because it is not a valid Date."); - } else { - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date(config.finalDatetime) - } - } - } - } - } - } - } - } - - function initializeSocialBots() { - - if (bot.SESSION.socialBots !== undefined) { - if (bot.SESSION.socialBots.bots !== undefined) { - for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { - let socialBot = bot.SESSION.socialBots.bots[i] - if (socialBot.type === "Telegram Bot") { - let config = socialBot.config - socialBot.botInstance = initializeTelegramBot(config.botToken, config.chatId) - } - } - } - - function announce(announcement) { - if (bot.SESSION.socialBots.bots !== undefined) { - for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { - let socialBot = bot.SESSION.socialBots.bots[i] - try { - let config = announcement.config - - if (socialBot.type === "Telegram Bot") { - if (announcement.formulaValue !== undefined) { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } else { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, config.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } - } - - } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> err = " + err.stack); - } - } - } - } - - bot.SESSION.socialBots.announce = announce - } - } - - function finalizeSocialBots() { - if (bot.SESSION.socialBots === undefined) { return } - if (bot.SESSION.socialBots.bots !== undefined) { - for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { - let socialBot = bot.SESSION.socialBots.bots[i] - if (socialBot.type === "Telegram Bot") { - let config = socialBot.config - socialBot.botInstance = finalizeTelegramBot(config.chatId) - } - } - } - } - - function initializeTelegramBot(botToken, chatId) { - /* Telegram Bot Initialization */ - try { - const Telegraf = require('telegraf') - let telegramBot - telegramBot = new Telegraf(botToken) - telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.')) - telegramBot.help((ctx) => ctx.reply('I can stop the session if you say STOP.')) - telegramBot.hears('STOP', (ctx) => stopSession()) - telegramBot.launch() - - const Telegram = require('telegraf/telegram') - - telegramAPI = new Telegram(botToken) - - const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started with an initial balance of " + " " + bot.VALUES_TO_USE.initialBalanceA + " " + bot.VALUES_TO_USE.baseAsset + "." - telegramAPI.sendMessage(chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> Telegram API error -> err = " + err)) - - let botInstance = { - telegramBot: telegramBot, - telegramAPI: telegramAPI, - chatId: chatId - } - return botInstance - } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> err = " + err.stack); - } - - } - - function finalizeTelegramBot(chatId) { - const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was stopped." - telegramAPI.sendMessage(chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> Telegram API error -> err = " + err)) - } - } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); diff --git a/SocialBots.js b/SocialBots.js new file mode 100644 index 000000000..742a8976b --- /dev/null +++ b/SocialBots.js @@ -0,0 +1,80 @@ +exports.newSocialBots = function newSocialBots(bot, logger) { + + const MODULE_NAME = 'Social Bots' + + let thisObject = { + sendMessage: sendMessage, + initialize: initialize, + finalize: finalize + } + + const TELEGRAM_BOT_MODULE = require('./TelegramBot.js') + + return thisObject + + function initialize() { + if (bot.SESSION.socialBots !== undefined) { + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + if (socialBot.type === "Telegram Bot") { + let config = socialBot.config + socialBot.botInstance = SOCIAL_BOTS_MODULE.newTelegramBot(bot, logger) + socialBot.botInstance.initialize(config.botToken, config.chatId) + } + } + } + + function announce(announcement) { + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + try { + let config = announcement.config + + if (socialBot.type === "Telegram Bot") { + if (announcement.formulaValue !== undefined) { + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) + } else { + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, config.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) + } + } + + } catch (err) { + parentLogger.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); + } + } + } + } + + bot.SESSION.socialBots.announce = announce + } + } + + function finalize() { + if (bot.SESSION.socialBots === undefined) { return } + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + if (socialBot.type === "Telegram Bot") { + socialBot.botInstance.finalize() + } + } + } + } + + + + function sendMessage(message) { + if (bot.SESSION.socialBots !== undefined) { + if (bot.SESSION.socialBots.bots !== undefined) { + for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { + let socialBot = bot.SESSION.socialBots.bots[i] + if (socialBot.type === "Telegram Bot") { + socialBot.botInstance.sendMessage(message) + } + } + } + } + } +} \ No newline at end of file diff --git a/TelegramBot.js b/TelegramBot.js new file mode 100644 index 000000000..d0677c46d --- /dev/null +++ b/TelegramBot.js @@ -0,0 +1,51 @@ +exports.newTelegramBot = function newTelegramBot(bot, logger) { + + const MODULE_NAME = 'Telegram Bot' + + let thisObject = { + telegramBot: undefined, + telegramAPI: undefined, + chatId: undefined, + sendMessage: sendMessage, + initialize: initialize, + finalize: finalize + } + + return thisObject + + function initialize(botToken, chatId) { + /* Telegram Bot Initialization */ + try { + thisObject.chatId = chatId + + const Telegraf = require('telegraf') + + thisObject.telegramBot = new Telegraf(botToken) + thisObject.telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.')) + thisObject.telegramBot.help((ctx) => ctx.reply('I can stop the session if you type STOP.')) + thisObject.telegramBot.hears('STOP', (ctx) => bot.SESSION.stop(' by STOP command at Telegram.')) + thisObject.telegramBot.launch() + + const Telegram = require('telegraf/telegram') + + thisObject.telegramAPI = new Telegram(botToken) + + const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started with an initial balance of " + " " + bot.VALUES_TO_USE.initialBalanceA + " " + bot.VALUES_TO_USE.baseAsset + "." + thisObject.telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeTelegramBot -> Telegram API error -> err = " + err)) + + } catch (err) { + parentLogger.write(MODULE_NAME, "[WARN] initialize -> err = " + err.stack); + } + + } + + function finalize() { + const messge = "I have been ordered to shut down. Goodbye!" + telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] finalize -> Telegram API error -> err = " + err)) + } + + function sendMessage(message) { + thisObject.telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] announce -> Telegram API error -> err = " + err)) + } + +} \ No newline at end of file diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js index 7edfd34f9..49faa2995 100644 --- a/Trading/Snapshots.js +++ b/Trading/Snapshots.js @@ -76,7 +76,6 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } } - snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) diff --git a/Trading/TradingExecution.js b/Trading/TradingExecution.js index 2df00a402..4aa191a79 100644 --- a/Trading/TradingExecution.js +++ b/Trading/TradingExecution.js @@ -27,8 +27,8 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* Check if we need to execute. */ if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ /* Check that we are in LIVE MODE */ - if (bot.startMode === 'Live') { - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> Taking a Position in Live 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) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) @@ -201,7 +201,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { function closePosition() { if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ /* Check that we are in LIVE MODE */ - if (bot.startMode === 'Live') { + if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { /* We see if we need to put the actual order at the exchange. */ if (variable.executionContext !== undefined) { switch (variable.executionContext.status) { diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index 579de4f33..73264fec4 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -13,7 +13,7 @@ const MULTI_PERIOD = require(ROOT_DIR + 'MultiPeriod'); const FILE_STORAGE = require('./FileStorage.js'); const SESSION = require(ROOT_DIR + 'Session'); - + let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); let session = SESSION.newSession(bot, parentLogger) @@ -87,7 +87,7 @@ USER_BOT_COMMONS = {} USER_BOT_COMMONS.newCommons = eval(text); // Use this for production - session.initialize(onSessionInitialized) + session.initialize(onSessionInitialized) function onSessionInitialized(err) { callBackFunction(err); @@ -95,7 +95,7 @@ } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -117,7 +117,7 @@ try { - processHeartBeat(undefined, undefined, "Running...") + processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { str = str.toString(); return str.length < max ? pad(" " + str, max) : str; @@ -142,7 +142,7 @@ /* Heartbeats sent to the UI */ bot.sessionHeartBeat = sessionHeartBeat bot.processHeartBeat = processHeartBeat - processHeartBeat() + processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ @@ -153,7 +153,7 @@ const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); const DATA_SET = require(ROOT_DIR + 'DataSet'); const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ @@ -178,7 +178,7 @@ /* Checking if we should process this loop or not.*/ if (bot.STOP_SESSION === true) { - + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Waiting for " + bot.processNode.session.type + " " + bot.processNode.session.name + " to be ran."); } console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) @@ -408,7 +408,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -419,7 +419,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -484,7 +484,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -495,7 +495,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -578,7 +578,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeContext -> onInizialized -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeContext -> onInizialized -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -589,7 +589,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -667,7 +667,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -678,7 +678,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -775,7 +775,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -786,7 +786,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -910,7 +910,7 @@ switch (nextWaitTime) { case 'Normal': { let waitTime - if (bot.startMode === "Live") { + if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { waitTime = processConfig.liveWaitTime } else { waitTime = processConfig.normalWaitTime @@ -939,21 +939,21 @@ checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); } break; case 'Sleep': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") logger.persist(); } break; case 'Coma': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") logger.persist(); } break; @@ -1017,7 +1017,7 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); @@ -1060,7 +1060,7 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); From dde5ee155e311d57f4e30bb14efb4483cca03e06 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 11:39:43 +0200 Subject: [PATCH 015/113] Update SocialBots.js --- SocialBots.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/SocialBots.js b/SocialBots.js index 742a8976b..736f50fd9 100644 --- a/SocialBots.js +++ b/SocialBots.js @@ -63,8 +63,6 @@ exports.newSocialBots = function newSocialBots(bot, logger) { } } - - function sendMessage(message) { if (bot.SESSION.socialBots !== undefined) { if (bot.SESSION.socialBots.bots !== undefined) { From ef66aec05007e9db148147b4da2ce362b1e9bda8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 11:47:54 +0200 Subject: [PATCH 016/113] Update MultiPeriod.js --- MultiPeriod.js | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index c414630a3..7f203c983 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -35,8 +35,6 @@ logger.fileName = MODULE_NAME; logger.initialize(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - statusDependencies = pStatusDependencies; dataDependenciesModule = pDataDependencies; processConfig = pProcessConfig; @@ -68,7 +66,6 @@ try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } let currentTimeFrame let currentTimeFrameLabel @@ -94,7 +91,6 @@ try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Entering function."); } let thisReport; let reportKey; @@ -209,8 +205,6 @@ function processSingleFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> Entering function."); } - try { let dependencyIndex = 0; dataFiles = new Map(); @@ -220,7 +214,6 @@ function dependencyLoopBody() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> Entering function."); } let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; @@ -229,7 +222,6 @@ function getFile() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> Entering function."); } if (datasetModule.node.config.codeName !== "Single-File") { dependencyControlLoop(); @@ -248,7 +240,6 @@ function onFileReceived(err, text) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); } if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { @@ -290,7 +281,6 @@ function dependencyControlLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyControlLoop -> Entering function."); } dependencyIndex++; @@ -316,7 +306,6 @@ function processMarketFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> Entering function."); } try { let n; @@ -324,7 +313,6 @@ function timeFramesLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoop -> Entering function."); } /* We will iterate through all posible timeFrames. */ @@ -339,7 +327,6 @@ function timeFramesLoopBody() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> Entering function."); } const timeFrame = global.marketFilesPeriods[n][0]; const timeFrameLabel = global.marketFilesPeriods[n][1]; @@ -354,7 +341,6 @@ function dependencyLoopBody() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> Entering function."); } let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; @@ -363,7 +349,6 @@ function getFile() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> Entering function."); } if (dependency.referenceParent.config.codeName !== "Multi-Period-Market") { dependencyControlLoop(); @@ -383,7 +368,6 @@ function onFileReceived(err, text) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); } if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { @@ -423,7 +407,6 @@ function dependencyControlLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> dependencyControlLoop -> Entering function."); } dependencyIndex++; if (dependencyIndex < dataDependenciesModule.nodeArray.length) { @@ -448,7 +431,6 @@ function timeFramesControlLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesControlLoop -> Entering function."); } n++; if (n < global.marketFilesPeriods.length) { timeFramesLoopBody(); @@ -469,7 +451,6 @@ } function processDailyFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> Entering function."); } try { let n = 0; if (currentTimeFrame) { @@ -483,7 +464,6 @@ try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> Entering function."); } bot.multiPeriodProcessDatetime = new Date(bot.multiPeriodProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); previousDay = new Date(bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); @@ -523,7 +503,6 @@ function checkStopTaskGracefully() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> checkStopTaskGracefully -> Entering function."); } /* Validation that we dont need to stop. */ if (global.STOP_TASK_GRACEFULLY === true) { callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -538,7 +517,6 @@ function checkStopProcessing() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> checkStopProcessing -> Entering function."); } /* Validation that we dont need to stop. */ if (bot.STOP_SESSION === true) { @@ -556,7 +534,6 @@ function timeFramesLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoop -> Entering function."); } /* Telling the world we are alive and doing well */ let processingDate = bot.multiPeriodProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); @@ -576,7 +553,6 @@ function timeFramesLoopBody() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> Entering function."); } const timeFrame = global.dailyFilePeriods[n][0]; const timeFrameLabel = global.dailyFilePeriods[n][1]; @@ -605,7 +581,6 @@ function dependencyLoopBody() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> Entering function."); } let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; @@ -622,7 +597,6 @@ function getPreviousFile() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> Entering function."); } if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { dependencyControlLoop(); @@ -650,7 +624,6 @@ function onFileReceived(err, text) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Entering function."); } if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> text = " + text); } if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { @@ -700,7 +673,6 @@ function getCurrentFile() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> Entering function."); } if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { dependencyControlLoop(); @@ -727,7 +699,6 @@ function onFileReceived(err, text) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Entering function."); } if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> text = " + text); } if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { @@ -770,7 +741,6 @@ function dependencyControlLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> dependencyControlLoop -> Entering function."); } dependencyIndex++; if (dependencyIndex < dataDependenciesModule.nodeArray.length) { @@ -796,7 +766,6 @@ function timeFramesControlLoop() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesControlLoop -> Entering function."); } n++; if (n < global.dailyFilePeriods.length) { timeFramesLoopBody(); @@ -818,7 +787,6 @@ function callTheBot() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> Entering function."); } botInstance.start( multiPeriodDataFiles, currentTimeFrame, @@ -829,7 +797,6 @@ function onBotFinished(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> Entering function."); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { callBackFunction(err); return; @@ -847,7 +814,6 @@ function onWritten(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> Entering function."); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); @@ -865,7 +831,6 @@ function onDailyStatusReport() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onDailyStatusReport -> Entering function."); } /* The next run we need the process to continue at the date it finished. */ processConfig.framework.startDate.resumeExecution = true; @@ -879,7 +844,6 @@ function onMarketStatusReport() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> Entering function."); } /* This is where we check if we need reached the user defined end datetime. */ const now = new Date() @@ -921,7 +885,6 @@ function writeDataRanges(currentTimeFrameLabel, callBack) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRanges -> Entering function."); } let outputDatasetIndex = -1; controlLoop(); @@ -949,7 +912,6 @@ function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel, callBack) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> Entering function."); } let dataRange = { begin: pBegin.valueOf(), end: pEnd.valueOf() + ONE_DAY_IN_MILISECONDS @@ -962,7 +924,6 @@ fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Entering function."); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); @@ -991,8 +952,6 @@ } function writeDailyStatusReport(lastFileDate, callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDailyStatusReport -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDailyStatusReport -> lastFileDate = " + lastFileDate); } try { let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; @@ -1012,7 +971,6 @@ } function writeMarketStatusReport(callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeMarketStatusReport -> Entering function."); } try { let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; let thisReport = statusDependencies.statusReports.get(reportKey); From 0d30bbe07f46d5240ecfbe1ed634812fe182466a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 11:51:05 +0200 Subject: [PATCH 017/113] Update MultiPeriod.js --- MultiPeriod.js | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index 7f203c983..05b6a64af 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -63,13 +63,9 @@ } function start(callBackFunction) { - try { - - let currentTimeFrame let currentTimeFrameLabel - let market = bot.market; let botNeverRan = true; @@ -88,10 +84,7 @@ getContextVariables(); function getContextVariables() { - try { - - let thisReport; let reportKey; let statusReport; @@ -222,7 +215,6 @@ function getFile() { try { - if (datasetModule.node.config.codeName !== "Single-File") { dependencyControlLoop(); return @@ -238,7 +230,6 @@ datasetModule.getTextFile(filePath, fileName, onFileReceived); function onFileReceived(err, text) { - try { if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } @@ -270,7 +261,6 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); @@ -279,9 +269,7 @@ } function dependencyControlLoop() { - try { - dependencyIndex++; if (dependencyIndex < dataDependenciesModule.nodeArray.length) { @@ -305,8 +293,6 @@ } function processMarketFiles() { - - try { let n; timeFramesLoop(); @@ -341,15 +327,12 @@ function dependencyLoopBody() { try { - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - getFile(); function getFile() { try { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Market") { dependencyControlLoop(); return @@ -385,7 +368,6 @@ dataFiles.set(dependency.id, dataFile); dependencyControlLoop(); - } catch (err) { logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); @@ -461,10 +443,7 @@ advanceTime(); function advanceTime() { - try { - - bot.multiPeriodProcessDatetime = new Date(bot.multiPeriodProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); previousDay = new Date(bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); @@ -501,7 +480,6 @@ } function checkStopTaskGracefully() { - try { /* Validation that we dont need to stop. */ if (global.STOP_TASK_GRACEFULLY === true) { @@ -517,7 +495,6 @@ function checkStopProcessing() { try { - /* Validation that we dont need to stop. */ if (bot.STOP_SESSION === true) { callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -581,7 +558,6 @@ function dependencyLoopBody() { try { - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; @@ -597,7 +573,6 @@ function getPreviousFile() { try { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { dependencyControlLoop(); return @@ -673,7 +648,6 @@ function getCurrentFile() { try { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { dependencyControlLoop(); return @@ -828,10 +802,8 @@ } } - function onDailyStatusReport() { try { - /* The next run we need the process to continue at the date it finished. */ processConfig.framework.startDate.resumeExecution = true; @@ -844,7 +816,6 @@ function onMarketStatusReport() { try { - /* This is where we check if we need reached the user defined end datetime. */ const now = new Date() if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { @@ -885,7 +856,6 @@ function writeDataRanges(currentTimeFrameLabel, callBack) { try { - let outputDatasetIndex = -1; controlLoop(); @@ -952,7 +922,6 @@ } function writeDailyStatusReport(lastFileDate, callBack) { - try { let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; let thisReport = statusDependencies.statusReports.get(reportKey); From 4a492d64769377cf6c74750c5974fce7d95c7ea5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 11:56:44 +0200 Subject: [PATCH 018/113] Update MultiPeriod.js --- MultiPeriod.js | 126 +++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 78 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index 05b6a64af..c49542156 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -197,98 +197,68 @@ } function processSingleFiles() { + let dependencyIndex = 0; + dataFiles = new Map(); + dependencyLoopBody(); - try { - let dependencyIndex = 0; - dataFiles = new Map(); + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + getFile(); - dependencyLoopBody(); + function getFile() { + if (datasetModule.node.config.codeName !== "Single-File") { + dependencyControlLoop(); + return + } - function dependencyLoopBody() { + if (dataDependenciesModule.isItADepenency('atAnyTimeFrame', datasetModule.node.parentNode.config.singularVariableName) !== true) { + dependencyControlLoop(); + return + } - try { + let fileName = "Data.json"; + let filePath = datasetModule.node.parentNode.config.codeName + '/' + datasetModule.node.config.codeName; + datasetModule.getTextFile(filePath, fileName, onFileReceived); - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + function onFileReceived(err, text) { + try { + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - getFile(); - - function getFile() { - try { - if (datasetModule.node.config.codeName !== "Single-File") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency('atAnyTimeFrame', datasetModule.node.parentNode.config.singularVariableName) !== true) { - dependencyControlLoop(); - return - } - - let fileName = "Data.json"; - let filePath = datasetModule.node.parentNode.config.codeName + '/' + datasetModule.node.config.codeName; - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - if (err.message === 'File does not exist.') { - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); - callBackFunction(err); - } - return; - } - - let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); - - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + if (err.message === 'File does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + } else { + logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); + callBackFunction(err); } + return; } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - function dependencyControlLoop() { - try { - dependencyIndex++; + let dataFile = JSON.parse(text); + dataFiles.set(dependency.id, dataFile); + + dependencyControlLoop(); - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - dependencyLoopBody(); - } else { - let mapKey = "Single Files" - multiPeriodDataFiles.set(mapKey, dataFiles) - processMarketFiles(); } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } } } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processSingleFiles -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + function dependencyControlLoop() { + dependencyIndex++ + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody() + } else { + let mapKey = "Single Files" + multiPeriodDataFiles.set(mapKey, dataFiles) + processMarketFiles() + } } } From 0fbe3f1ce51306911d036573f6442f772ee6a4cf Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 12:18:34 +0200 Subject: [PATCH 019/113] Update MultiPeriod.js --- MultiPeriod.js | 1001 ++++++++++++++++++++---------------------------- 1 file changed, 414 insertions(+), 587 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index c49542156..f3077fdd8 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -263,667 +263,494 @@ } function processMarketFiles() { - try { - let n; - timeFramesLoop(); + let n; + timeFramesLoop(); - function timeFramesLoop() { - try { - /* - We will iterate through all posible timeFrames. - */ - n = 0 // loop Variable representing each possible period as defined at the timeFrames array. - timeFramesLoopBody(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + function timeFramesLoop() { + /* + We will iterate through all posible timeFrames. + */ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody(); + } + + function timeFramesLoopBody() { + const timeFrame = global.marketFilesPeriods[n][0]; + const timeFrameLabel = global.marketFilesPeriods[n][1]; + + let dependencyIndex = 0; + dataFiles = new Map(); + + if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { + currentTimeFrame = global.marketFilesPeriods[n][0]; + currentTimeFrameLabel = global.marketFilesPeriods[n][1]; + } + dependencyLoopBody(); + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + getFile(); + + function getFile() { + if (dependency.referenceParent.config.codeName !== "Multi-Period-Market") { + dependencyControlLoop(); + return + } + + if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { + if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + dependencyControlLoop(); + return + } + } + + let fileName = "Data.json"; + let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; + datasetModule.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + try { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + if (err.message === 'File does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + } else { + logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); + callBackFunction(err); + } + return; + } + + let dataFile = JSON.parse(text); + dataFiles.set(dependency.id, dataFile); + + dependencyControlLoop(); + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } } } - function timeFramesLoopBody() { - try { - const timeFrame = global.marketFilesPeriods[n][0]; - const timeFrameLabel = global.marketFilesPeriods[n][1]; - - let dependencyIndex = 0; - dataFiles = new Map(); - - if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { - currentTimeFrame = global.marketFilesPeriods[n][0]; - currentTimeFrameLabel = global.marketFilesPeriods[n][1]; - } + function dependencyControlLoop() { + dependencyIndex++; + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { dependencyLoopBody(); - - function dependencyLoopBody() { - try { - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - getFile(); - - function getFile() { - try { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Market") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let fileName = "Data.json"; - let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - if (err.message === 'File does not exist.') { - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.message); - callBackFunction(err); - } - return; - } - - let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); - - dependencyControlLoop(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - try { - - dependencyIndex++; - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - dependencyLoopBody(); - } else { - let mapKey = global.marketFilesPeriods[n][1]; - multiPeriodDataFiles.set(mapKey, dataFiles) - timeFramesControlLoop(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesControlLoop() { - try { - n++; - if (n < global.marketFilesPeriods.length) { - timeFramesLoopBody(); - } else { - processDailyFiles(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> timeFramesControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } else { + let mapKey = global.marketFilesPeriods[n][1]; + multiPeriodDataFiles.set(mapKey, dataFiles) + timeFramesControlLoop(); } } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processMarketFiles -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + function timeFramesControlLoop() { + n++; + if (n < global.marketFilesPeriods.length) { + timeFramesLoopBody(); + } else { + processDailyFiles(); + } } } function processDailyFiles() { - try { - let n = 0; - if (currentTimeFrame) { - callTheBot() - return + let n = 0; + if (currentTimeFrame) { + callTheBot() + return + } + bot.multiPeriodProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. + advanceTime(); + + function advanceTime() { + bot.multiPeriodProcessDatetime = new Date(bot.multiPeriodProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); + previousDay = new Date(bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.multiPeriodProcessDatetime = " + bot.multiPeriodProcessDatetime); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } + + /* Validation that we are not going past the user defined end date. */ + if (bot.multiPeriodProcessDatetime.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + + const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } + + bot.STOP_SESSION = true + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; } - bot.multiPeriodProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. - advanceTime(); - function advanceTime() { - try { - bot.multiPeriodProcessDatetime = new Date(bot.multiPeriodProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); - previousDay = new Date(bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); + /* Validation that we are not going past the head of the market. */ + if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf() + ONE_DAY_IN_MILISECONDS - 1) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.multiPeriodProcessDatetime = " + bot.multiPeriodProcessDatetime); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } + const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } - /* Validation that we are not going past the user defined end date. */ - if (bot.multiPeriodProcessDatetime.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } - const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } + checkStopTaskGracefully(); + } - bot.STOP_SESSION = true - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } + function checkStopTaskGracefully() { + /* Validation that we dont need to stop. */ + if (global.STOP_TASK_GRACEFULLY === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + checkStopProcessing(); + } - /* Validation that we are not going past the head of the market. */ - if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf() + ONE_DAY_IN_MILISECONDS - 1) { + function checkStopProcessing() { + /* Validation that we dont need to stop. */ + if (bot.STOP_SESSION === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + timeFramesLoop(); + } - const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } + function timeFramesLoop() { + /* Telling the world we are alive and doing well */ + let processingDate = bot.multiPeriodProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); + bot.processHeartBeat(processingDate) + /* + We will iterate through all posible timeFrames. + */ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody(); + } - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } + function timeFramesLoopBody() { + const timeFrame = global.dailyFilePeriods[n][0]; + const timeFrameLabel = global.dailyFilePeriods[n][1]; - checkStopTaskGracefully(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> advanceTime -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + if (processConfig.framework.validtimeFrames !== undefined) { + let validPeriod = false; + for (let i = 0; i < processConfig.framework.validtimeFrames.length; i++) { + let period = processConfig.framework.validtimeFrames[i]; + if (period === timeFrameLabel) { validPeriod = true } + } + if (validPeriod === false) { + timeFramesControlLoop(); + return; } } - function checkStopTaskGracefully() { - try { - /* Validation that we dont need to stop. */ - if (global.STOP_TASK_GRACEFULLY === true) { - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - checkStopProcessing(); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> checkStopTaskGracefully -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { + currentTimeFrame = global.dailyFilePeriods[n][0]; + currentTimeFrameLabel = global.dailyFilePeriods[n][1]; } - function checkStopProcessing() { - try { - /* Validation that we dont need to stop. */ - if (bot.STOP_SESSION === true) { - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; + let dependencyIndex = 0; + dataFiles = new Map(); + dependencyLoopBody(); + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + + let previousFile; + let currentFile; + + if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { + getPreviousFile(); + } else { + previousFile = []; + getCurrentFile() + } + + function getPreviousFile() { + if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { + dependencyControlLoop(); + return } - timeFramesLoop(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> checkStopProcessing -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesLoop() { - try { - - /* Telling the world we are alive and doing well */ - let processingDate = bot.multiPeriodProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); - bot.processHeartBeat(processingDate) - /* - We will iterate through all posible timeFrames. - */ - n = 0 // loop Variable representing each possible period as defined at the timeFrames array. - timeFramesLoopBody(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesLoopBody() { - try { - - const timeFrame = global.dailyFilePeriods[n][0]; - const timeFrameLabel = global.dailyFilePeriods[n][1]; - - if (processConfig.framework.validtimeFrames !== undefined) { - let validPeriod = false; - for (let i = 0; i < processConfig.framework.validtimeFrames.length; i++) { - let period = processConfig.framework.validtimeFrames[i]; - if (period === timeFrameLabel) { validPeriod = true } - } - if (validPeriod === false) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> Discarding period for not being listed as a valid period. -> timeFrameLabel = " + timeFrameLabel); } - timeFramesControlLoop(); - return; + if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { + if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + dependencyControlLoop(); + return } } - if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { - currentTimeFrame = global.dailyFilePeriods[n][0]; - currentTimeFrameLabel = global.dailyFilePeriods[n][1]; - } + let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); + let filePath - let dependencyIndex = 0; - dataFiles = new Map(); - dependencyLoopBody(); - - function dependencyLoopBody() { - try { - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - let previousFile; - let currentFile; - - if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { - getPreviousFile(); - } else { - previousFile = []; - getCurrentFile() - } - - function getPreviousFile() { - try { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); - let filePath - - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> text = " + text); } - - if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { - - /* - Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. - When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find - all the dependencies and the first run of the bot is successful. - - After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. - */ - logger.write(MODULE_NAME, "[WARN] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found."); - - advanceTime(); - return; - } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - previousFile = JSON.parse(text); - getCurrentFile(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function getCurrentFile() { - try { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let dateForPath = bot.multiPeriodProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> text = " + text); } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Not Ok -> err = " + err.code); - callBackFunction(err); - return; - } - - currentFile = JSON.parse(text); - let dataFile = previousFile.concat(currentFile); - dataFiles.set(dependency.id, dataFile); - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - try { - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - dependencyLoopBody(); - } else { - let mapKey = global.dailyFilePeriods[n][1]; - multiPeriodDataFiles.set(mapKey, dataFiles) - timeFramesControlLoop(); - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function timeFramesControlLoop() { - try { - n++; - if (n < global.dailyFilePeriods.length) { - timeFramesLoopBody(); + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; } else { - n = 0; - if (currentTimeFrame !== undefined) { - callTheBot(); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> Time Frame not Recognized. Can not continue."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } + let fileName = "Data.json"; - function callTheBot() { - try { - botInstance.start( - multiPeriodDataFiles, - currentTimeFrame, - currentTimeFrameLabel, - bot.multiPeriodProcessDatetime, - variable, - onBotFinished); + datasetModule.getTextFile(filePath, fileName, onFileReceived); - function onBotFinished(err) { + function onFileReceived(err, text) { try { + if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { + /* + Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. + When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find + all the dependencies and the first run of the bot is successful. + + After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. + */ + logger.write(MODULE_NAME, "[WARN] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found."); + advanceTime(); + return; + } + + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + return; + } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); callBackFunction(err); return; } - botNeverRan = false - bot.RESUME = true // From here on, all other loops executions must resume from where we left at this current run. - - if (currentTimeFrame > global.dailyFilePeriods[0][0]) { - writeMarketStatusReport(onMarketStatusReport) - - } else { - writeDataRanges(currentTimeFrameLabel, onWritten); - } - - function onWritten(err) { - try { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); - callBackFunction(err); - return; - } - - writeDailyStatusReport(bot.multiPeriodProcessDatetime, onDailyStatusReport); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function onDailyStatusReport() { - try { - /* The next run we need the process to continue at the date it finished. */ - processConfig.framework.startDate.resumeExecution = true; - - advanceTime(); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onDailyStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function onMarketStatusReport() { - try { - /* This is where we check if we need reached the user defined end datetime. */ - const now = new Date() - if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { - - const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> " + logText); } - - bot.STOP_SESSION = true - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } + previousFile = JSON.parse(text); + getCurrentFile(); } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> err = " + err.stack); + + function getCurrentFile() { + if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { + dependencyControlLoop(); + return + } + + if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { + if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + dependencyControlLoop(); + return + } + } + + let dateForPath = bot.multiPeriodProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + let fileName = "Data.json"; + + datasetModule.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + try { + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); + callBackFunction(global.DEFAULT_RETRY_RESPONSE); + return; + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Not Ok -> err = " + err.code); + callBackFunction(err); + return; + } + + currentFile = JSON.parse(text); + let dataFile = previousFile.concat(currentFile); + dataFiles.set(dependency.id, dataFile); + dependencyControlLoop(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody(); + } else { + let mapKey = global.dailyFilePeriods[n][1]; + multiPeriodDataFiles.set(mapKey, dataFiles) + timeFramesControlLoop(); + } + } + } + + function timeFramesControlLoop() { + n++; + if (n < global.dailyFilePeriods.length) { + timeFramesLoopBody(); + } else { + n = 0; + if (currentTimeFrame !== undefined) { + callTheBot(); + } else { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> Time Frame not Recognized. Can not continue."); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + function callTheBot() { + botInstance.start( + multiPeriodDataFiles, + currentTimeFrame, + currentTimeFrameLabel, + bot.multiPeriodProcessDatetime, + variable, + onBotFinished); + + function onBotFinished(err) { + try { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err); + return; + } + + botNeverRan = false + bot.RESUME = true // From here on, all other loops executions must resume from where we left at this current run. + + if (currentTimeFrame > global.dailyFilePeriods[0][0]) { + writeMarketStatusReport(onMarketStatusReport) + + } else { + writeDataRanges(currentTimeFrameLabel, onWritten); + } + + function onWritten(err) { + try { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); + callBackFunction(err); + return; + } + + writeDailyStatusReport(bot.multiPeriodProcessDatetime, onDailyStatusReport); + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function onDailyStatusReport() { + /* The next run we need the process to continue at the date it finished. */ + processConfig.framework.startDate.resumeExecution = true; + advanceTime(); + } + + function onMarketStatusReport() { + /* This is where we check if we need reached the user defined end datetime. */ + const now = new Date() + if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> " + logText); } + + bot.STOP_SESSION = true + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } } } function writeDataRanges(currentTimeFrameLabel, callBack) { - try { - let outputDatasetIndex = -1; - controlLoop(); + let outputDatasetIndex = -1; + controlLoop(); - function productLoopBody() { - let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; - writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodProcessDatetime, productCodeName, currentTimeFrameLabel, controlLoop); - } - - function controlLoop() { - outputDatasetIndex++; - if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { - productLoopBody(); - } else { - callBack(global.DEFAULT_OK_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRanges -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); + function productLoopBody() { + let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; + writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodProcessDatetime, productCodeName, currentTimeFrameLabel, controlLoop); } - } - - function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel, callBack) { - try { - let dataRange = { - begin: pBegin.valueOf(), - end: pEnd.valueOf() + ONE_DAY_IN_MILISECONDS - }; - - let fileContent = JSON.stringify(dataRange); - let fileName = '/Data.Range.json'; - let filePath = bot.filePathRoot + "/Output/" + bot.SESSION.folderName + "/" + productCodeName + "/" + 'Multi-Period-Daily' + fileName; - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); - - function onFileCreated(err) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); - callBack(err); - return; - } - - if (LOG_FILE_CONTENT === true) { - logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent); - } - - let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset - let event = { - dateRange: dataRange - } - - global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) - + function controlLoop() { + outputDatasetIndex++; + if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { + productLoopBody(); + } else { callBack(global.DEFAULT_OK_RESPONSE); } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); + } + + function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel, callBack) { + let dataRange = { + begin: pBegin.valueOf(), + end: pEnd.valueOf() + ONE_DAY_IN_MILISECONDS + }; + + let fileContent = JSON.stringify(dataRange); + let fileName = '/Data.Range.json'; + let filePath = bot.filePathRoot + "/Output/" + bot.SESSION.folderName + "/" + productCodeName + "/" + 'Multi-Period-Daily' + fileName; + + fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); + callBack(err); + return; + } + + if (LOG_FILE_CONTENT === true) { + logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent); + } + + let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset + let event = { + dateRange: dataRange + } + + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) + + callBack(global.DEFAULT_OK_RESPONSE); } } function writeDailyStatusReport(lastFileDate, callBack) { - try { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; - let thisReport = statusDependencies.statusReports.get(reportKey); + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; + let thisReport = statusDependencies.statusReports.get(reportKey); - thisReport.file.lastExecution = bot.currentDaytime; - thisReport.file.lastFile = lastFileDate; - thisReport.file.variable = variable; - thisReport.save(callBack); + thisReport.file.lastExecution = bot.currentDaytime; + thisReport.file.lastFile = lastFileDate; + thisReport.file.variable = variable; + thisReport.save(callBack); - bot.hasTheBotJustStarted = false; - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDailyStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + bot.hasTheBotJustStarted = false; } function writeMarketStatusReport(callBack) { - try { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; - let thisReport = statusDependencies.statusReports.get(reportKey); + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; + let thisReport = statusDependencies.statusReports.get(reportKey); - thisReport.file.lastExecution = bot.processDatetime; - thisReport.file.variable = variable; - thisReport.save(callBack); + thisReport.file.lastExecution = bot.processDatetime; + thisReport.file.variable = variable; + thisReport.save(callBack); - logger.newInternalLoop(bot.codeName, bot.process, bot.multiPeriodProcessDatetime); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeMarketStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + logger.newInternalLoop(bot.codeName, bot.process, bot.multiPeriodProcessDatetime); } } catch (err) { From 3b7310dc5b8cbf9d22080baa741d377e8cac64dc Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 13:14:18 +0200 Subject: [PATCH 020/113] simulationState --- MultiPeriod.js | 97 +++++------------------------------- Trading/TradingBot.js | 6 +-- Trading/TradingEngine.js | 10 +++- Trading/TradingSimulation.js | 3 +- Trading/TradingSystem.js | 13 +++++ 5 files changed, 38 insertions(+), 91 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index f3077fdd8..2ffe071fd 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -79,7 +79,7 @@ let previousDay; // Holds the date of the previous day relative to the processing date. - let variable + let simulationState getContextVariables(); @@ -152,8 +152,8 @@ } thisReport = statusReport.file; - variable = thisReport.variable; - if (variable === undefined) { variable = {} } // This should happen only when there is no status report + simulationState = thisReport.simulationState; + if (simulationState === undefined) { simulationState = {} } // This should happen only when there is no status report if (thisReport.lastFile !== undefined) { if (bot.hasTheBotJustStarted === true && bot.resumeExecution === false) { @@ -454,80 +454,10 @@ function dependencyLoopBody() { let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; + let file + getFile() - let previousFile; - let currentFile; - - if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { - getPreviousFile(); - } else { - previousFile = []; - getCurrentFile() - } - - function getPreviousFile() { - if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { - dependencyControlLoop(); - return - } - - if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { - dependencyControlLoop(); - return - } - } - - let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); - let filePath - - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - try { - if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { - /* - Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. - When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find - all the dependencies and the first run of the bot is successful. - - After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. - */ - logger.write(MODULE_NAME, "[WARN] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found."); - advanceTime(); - return; - } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - logger.write(MODULE_NAME, "[ERROR] The file " + filePath + '/' + fileName + ' does not exist and it is required to continue. This process will retry to read it in a while. In the meantime make yourself sure that the process that generates it has ran properly.'); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - previousFile = JSON.parse(text); - getCurrentFile(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - - function getCurrentFile() { + function getFile() { if (dependency.referenceParent.config.codeName !== "Multi-Period-Daily") { dependencyControlLoop(); return @@ -560,19 +490,18 @@ } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Not Ok -> err = " + err.code); + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Not Ok -> err = " + err.code); callBackFunction(err); return; } - currentFile = JSON.parse(text); - let dataFile = previousFile.concat(currentFile); - dataFiles.set(dependency.id, dataFile); + file = JSON.parse(text); + dataFiles.set(dependency.id, file); dependencyControlLoop(); } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -613,7 +542,7 @@ currentTimeFrame, currentTimeFrameLabel, bot.multiPeriodProcessDatetime, - variable, + simulationState, onBotFinished); function onBotFinished(err) { @@ -736,7 +665,7 @@ thisReport.file.lastExecution = bot.currentDaytime; thisReport.file.lastFile = lastFileDate; - thisReport.file.variable = variable; + thisReport.file.simulationState = simulationState; thisReport.save(callBack); bot.hasTheBotJustStarted = false; @@ -747,7 +676,7 @@ let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.processDatetime; - thisReport.file.variable = variable; + thisReport.file.simulationState = simulationState; thisReport.save(callBack); logger.newInternalLoop(bot.codeName, bot.process, bot.multiPeriodProcessDatetime); diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js index 37c791fc4..e4cd291ee 100644 --- a/Trading/TradingBot.js +++ b/Trading/TradingBot.js @@ -43,7 +43,7 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } } - function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, variable, callBackFunction) { + function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { try { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Entering function.') } @@ -128,7 +128,7 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR let tradingSystem = {} - if (bot.RESUME === true) { + if (s === true) { readFiles() } else { runSimulation() @@ -499,7 +499,7 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR timeFrame, timeFrameLabel, currentDay, - variable, + simulationState, exchangeAPI, recordsArray, conditionsArray, diff --git a/Trading/TradingEngine.js b/Trading/TradingEngine.js index 1a7c3344e..f88e031cd 100644 --- a/Trading/TradingEngine.js +++ b/Trading/TradingEngine.js @@ -4,7 +4,8 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let thisObject = { setCurrentCandle: setCurrentCandle, - updatePositionCounters: updatePositionCounters, + updateEpisodeCountersAndStatistics: updateEpisodeCountersAndStatistics, + updatePositionCountersAndStatistics: updatePositionCountersAndStatistics, updateDistanceToEventsCounters: updateDistanceToEventsCounters, getReadyToTakePosition: getReadyToTakePosition, takePosition: takePosition, @@ -128,7 +129,12 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } } - function updatePositionCounters() { + function updateEpisodeCountersAndStatistics() { + tradingEngine.episode.episodeCounters.periods++ + tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS + } + + function updatePositionCountersAndStatistics() { /* Keeping Position Counters Up-to-date */ if ( (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 946984692..d0983d6a0 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -25,7 +25,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT timeFrame, timeFrameLabel, currentDay, - variable, + s, exchangeAPI, recordsArray, conditionsArray, @@ -173,7 +173,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT return } - heartBeat() tradingEngineModule.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas. diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index 5d372763a..14564f53b 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -812,5 +812,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (value === undefined) return { DEFAULT_VALUE } return value } + + function isItInside(elementWithTimestamp, elementWithBeginEnd) { + /* + The function is to allow in Conditions and Formulas to easily know when an element with timestamp (like the ones of single files) are inside the time range + of an element with a time range, like a candle. + */ + if (elementWithTimestamp.timestamp >= elementWithBeginEnd.begin && elementWithTimestamp.timestamp <= elementWithBeginEnd.end) { + return true + } else { + return false + } + } + } From 7df9297c3ffae68fb18444761d1381ad6b1b07e7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Jun 2020 13:20:11 +0200 Subject: [PATCH 021/113] state saved --- Trading/TradingBot.js | 4 +++- Trading/TradingSimulation.js | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js index e4cd291ee..30eefe350 100644 --- a/Trading/TradingBot.js +++ b/Trading/TradingBot.js @@ -128,9 +128,11 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR let tradingSystem = {} - if (s === true) { + if (bot.RESUME === true) { readFiles() } else { + simulationState.tradingEngine = bot.SESSION.tradingEngine + simulationState.tradingSystem = bot.SESSION.tradingSystem runSimulation() } diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index d0983d6a0..dc71a3726 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -25,7 +25,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT timeFrame, timeFrameLabel, currentDay, - s, + simulationState, exchangeAPI, recordsArray, conditionsArray, @@ -42,8 +42,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT processingDailyFiles = true } - let tradingSystem = bot.TRADING_SYSTEM - let tradingEngine = bot.TRADING_ENGINE + let tradingSystem = simulationState.tradingSystem + let tradingEngine = simulationState.tradingEngine let sessionParameters = bot.SESSION.parameters if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } From 0ae546d1156f994d97da4fef9852a545bd967c1b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 8 Jun 2020 13:15:23 +0200 Subject: [PATCH 022/113] append to records implemented --- Trading/TradingBot.js | 421 +++----------------- MultiPeriod.js => Trading/TradingProcess.js | 13 +- Trading/TradingRecords.js | 195 +++------ Trading/TradingSimulation.js | 38 +- TradingBotProcessMainLoop.js | 8 +- 5 files changed, 132 insertions(+), 543 deletions(-) rename MultiPeriod.js => Trading/TradingProcess.js (98%) diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js index 30eefe350..d94f80e23 100644 --- a/Trading/TradingBot.js +++ b/Trading/TradingBot.js @@ -45,8 +45,13 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Entering function.') } + bot.processingDailyFiles + if (timeFrame > global.dailyFilePeriods[0][0]) { + bot.processingDailyFiles = false + } else { + bot.processingDailyFiles = true + } let chart = {} let mainDependency = {} @@ -89,7 +94,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } /* Single Files */ - let dataFiles = multiPeriodDataFiles.get('Single Files') let products = {} @@ -103,24 +107,12 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } /* Simulation */ - + const SNAPSHOTS_FOLDER_NAME = 'Snapshots' const TRADING_SIMULATION = require('./TradingSimulation.js') let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) - let market = bot.market - - const SIMULATED_RECORDS_FOLDER_NAME = 'Trading-Simulation' - const CONDITIONS_FOLDER_NAME = 'Simulation-Conditions' - const STRATEGIES_FOLDER_NAME = 'Simulation-Strategies' - const TRADES_FOLDER_NAME = 'Simulation-Trades' - const SNAPSHOTS_FOLDER_NAME = 'Snapshots' - - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 - - let recordsArray = [] - let conditionsArray = [] - let strategiesArray = [] - let positionsArray = [] + let totalFilesRead = 0 + let outputDatasetsMap = new Map() let snapshotHeaders let triggerOnSnapshot @@ -128,369 +120,78 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR let tradingSystem = {} - if (bot.RESUME === true) { - readFiles() - } else { + if (bot.RESUME !== true) { simulationState.tradingEngine = bot.SESSION.tradingEngine simulationState.tradingSystem = bot.SESSION.tradingSystem - runSimulation() } + readFiles() + return function readFiles() { - - if (timeFrame > global.dailyFilePeriods[0][0]) { - readMarketFiles(runSimulation) + /* + This bot have an output of files that it generates. At every call to the bot, it needs to read the previously generated + files in order to later append more information after the execution is over. Here in this function we are going to + read those output files and get them ready for appending content during the simulation. + */ + if (bot.processingDailyFiles) { + readDailyFiles() } else { - readDailyFiles(runSimulation) + readMarketFiles() } } - function readMarketFiles(callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> Entering function.') } + function readMarketFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent - readRecordsFile() - - function readRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readRecordsFile -> Entering function.') } + if (dataset.config.type === 'Market Files') { let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName + let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readRecordsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - recordsArray = [] - readConditionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - recordsArray = JSON.parse(text) - readConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) + readOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) } } + } - function readConditionsFile() { + function readOutputFile(fileName, filePath, productName) { + + filePath += '/' + fileName + + fileStorage.getTextFile(filePath, onFileRead, true) + + function onFileRead(err, text) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readConditionsFile -> Entering function.') } - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + CONDITIONS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readConditionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - conditionsArray = [] - readStrategiesFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - conditionsArray = JSON.parse(text) - conditionsArray = conditionsArray[1] // conditionsArray[0] = a snapshot of the trading system. - readStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } + if (err.message === 'File does not exist.') { + outputDatasetsMap.set(productName, []) + anotherFileRead() + return } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - function readStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readStrategiesFile -> Entering function.') } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> filePath = ' + filePath) + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - strategiesArray = [] - readPositionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - strategiesArray = JSON.parse(text) - readPositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } + callBackFunction(err) + return } + + outputDatasetsMap.set(productName, JSON.parse(text)) + anotherFileRead() } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readPositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readPositionsFile -> Entering function.') } - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readMarketFiles -> readPositionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - positionsArray = [] - callBack() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - positionsArray = JSON.parse(text) - callBack() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readPositionsFile -> err = ' + err.stack) + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } } - function readDailyFiles(callBack) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> Entering function.') } - - readRecordsFile() - - function readRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readRecordsFile -> Entering function.') } - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readRecordsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - recordsArray = [] - readConditionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - recordsArray = JSON.parse(text) - readConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readConditionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readConditionsFile -> Entering function.') } - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readConditionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - conditionsArray = [] - readStrategiesFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - conditionsArray = JSON.parse(text) - conditionsArray = conditionsArray[1] // conditionsArray[0] = a snapshot of the trading system. - readStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readStrategiesFile -> Entering function.') } - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - strategiesArray = [] - readPositionsFile() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - strategiesArray = JSON.parse(text) - readPositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function readPositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readPositionsFile -> Entering function.') } - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.getTextFile(filePath, onFileRead, true) - - function onFileRead(err, text) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> readDailyFiles -> readPositionsFile -> onFileRead -> Entering function.') } - - if (err.message === 'File does not exist.') { - positionsArray = [] - callBack() - return - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - positionsArray = JSON.parse(text) - callBack() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> onFileRead -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> readDailyFiles -> readPositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } + function anotherFileRead() { + totalFilesRead++ + if (totalFilesRead === outputDatasets.length) { + runSimulation() } } @@ -498,19 +199,15 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR tradingSimulation.runSimulation( chart, dataDependencies, - timeFrame, - timeFrameLabel, - currentDay, simulationState, exchangeAPI, - recordsArray, - conditionsArray, - strategiesArray, - positionsArray, + outputDatasets, + outputDatasetsMap, writeFiles, callBackFunction) } + function writeFiles(pTradingSystem, pSnapshotHeaders, pTriggerOnSnapshot, pTakePositionSnapshot) { tradingSystem = pTradingSystem @@ -518,10 +215,10 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR triggerOnSnapshot = pTriggerOnSnapshot takePositionSnapshot = pTakePositionSnapshot - if (timeFrame > global.dailyFilePeriods[0][0]) { - writeMarketFiles() - } else { + if (bot.processingDailyFiles) { writeDailyFiles() + } else { + writeMarketFiles() } } diff --git a/MultiPeriod.js b/Trading/TradingProcess.js similarity index 98% rename from MultiPeriod.js rename to Trading/TradingProcess.js index 2ffe071fd..725d66510 100644 --- a/MultiPeriod.js +++ b/Trading/TradingProcess.js @@ -1,8 +1,12 @@ -exports.newMultiPeriod = function newMultiPeriod(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS) { +exports.newTradingProcess = function newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS) { + /* + This Module will load all the process data dependencies from files and send them to the Trading Bot. + After execution, will save the time range and status report of the process. + */ const FULL_LOG = true; const LOG_FILE_CONTENT = false; - const MODULE_NAME = "Multi Period"; + const MODULE_NAME = "Trading Process"; const GMT_SECONDS = ':00.000 GMT+0000'; const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; @@ -21,7 +25,7 @@ let botInstance; - const FILE_STORAGE = require('./FileStorage.js'); + const FILE_STORAGE = require('../FileStorage.js'); let fileStorage = FILE_STORAGE.newFileStorage(logger); let processConfig; @@ -29,7 +33,6 @@ return thisObject; function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, exchangeAPI, callBackFunction) { - try { logger.fileName = MODULE_NAME; @@ -39,7 +42,7 @@ dataDependenciesModule = pDataDependencies; processConfig = pProcessConfig; - let USER_BOT_MODULE = require("./Trading/TradingBot") + let USER_BOT_MODULE = require("./TradingBot") botInstance = USER_BOT_MODULE.newTradingBot(bot, logger, UTILITIES, FILE_STORAGE); botInstance.initialize(exchangeAPI, callBackFunction); diff --git a/Trading/TradingRecords.js b/Trading/TradingRecords.js index fcdf8e415..3168f6097 100644 --- a/Trading/TradingRecords.js +++ b/Trading/TradingRecords.js @@ -3,166 +3,83 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { const MODULE_NAME = 'Trading Records' let thisObject = { - addSimulationRecord: addSimulationRecord, - addConditionsRecord: addConditionsRecord, - addStrategyRecord: addStrategyRecord, - addPositionRecord: addPositionRecord, + appendRecords: appendRecords, initialize: initialize, finalize: finalize } let tradingEngine + let tradingSystem + let outputDatasets + let outputDatasetsMap return thisObject - function initialize() { + function initialize(pOutputDatasets, pOutputDatasetsMap) { tradingEngine = bot.TRADING_ENGINE + tradingSystem = bot.TRADING_SYSTEM + outputDatasets = pOutputDatasets // These are the nodes defined at the UI + outputDatasetsMap = pOutputDatasetsMap // These are the files turned into arrays, stored in a Map by Product codeName. } function finalize() { tradingEngine = undefined + tradingSystem = undefined + outputDatasets = undefined + outputDatasetsMap = undefined } - function addSimulationRecord() { - /* Simulation Record */ - let simulationRecord - - if (tradingEngine.current.balance.baseAsset.value === Infinity) { - tradingEngine.current.balance.baseAsset.value = Number.MAX_SAFE_INTEGER - } - - if (tradingEngine.current.balance.quotedAsset.value === Infinity) { - tradingEngine.current.balance.quotedAsset.value = Number.MAX_SAFE_INTEGER - } - - simulationRecord = [ - candle.begin, - candle.end, - tradingEngine.current.balance.baseAsset.value, - tradingEngine.current.balance.quotedAsset.value, - tradingEngine.episode.episodeStatistics.profitLoss.value, - tradingEngine.last.position.profitLoss.value, - tradingEngine.current.position.stopLoss.value, - tradingEngine.episode.positionCounters.positions.value, - tradingEngine.episode.episodeCounters.hits.value, - tradingEngine.episode.episodeCounters.fails.value, - tradingEngine.episode.episodeStatistics.hitRatio.value, - tradingEngine.episode.episodeStatistics.ROI.value, - tradingEngine.episode.episodeCounters.periods, - tradingEngine.episode.episodeStatistics.days, - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value, - tradingEngine.current.position.rate.value, - tradingEngine.last.position.ROI.value, - tradingEngine.current.position.takeProfit.value, - tradingEngine.current.position.stopLoss.stopLossPhase.value, - tradingEngine.current.position.takeProfit.takeProfitPhase.value, - tradingEngine.current.position.size.value, - sessionParameters.sessionBaseAsset.config.initialBalance, - sessionParameters.sessionBaseAsset.config.minimumBalance, - sessionParameters.sessionBaseAsset.config.maximumBalance, - sessionParameters.sessionQuotedAsset.config.initialBalance, - sessionParameters.sessionQuotedAsset.config.minimumBalance, - sessionParameters.sessionQuotedAsset.config.maximumBalance, - '"' + sessionParameters.sessionBaseAsset.name + '"', - '"' + sessionParameters.sessionQuotedAsset.name + '"', - '"' + bot.market.marketBaseAsset + '"', - '"' + bot.market.marketQuotedAsset + '"', - tradingEngine.current.position.positionCounters.periods.value, - tradingEngine.current.position.positionStatistics.days.value - ] - - recordsArray.push(simulationRecord) - } - - function addConditionsRecord() { - /* Prepare the information for the Conditions File */ - let conditionsRecord = [ - candle.begin, - candle.end, - tradingEngine.current.strategy.index.value, - tradingEngine.current.position.stopLoss.stopLossPhase.value, - tradingEngine.current.position.takeProfit.takeProfitPhase.value, - conditionsValues, - formulasErrors, - formulasValues - ] - - conditionsArray.push(conditionsRecord) - } - - function addStrategyRecord() { + function appendRecords() { /* - Lets see if there will be an open strategy ... - Except if we are at the head of the market (remember we skipped the last candle for not being closed.) - */ - if ( - tradingEngine.current.strategy.begin.value !== 0 && - tradingEngine.current.strategy.end.value === 0 && - currentCandleIndex === candles.length - 2 && - lastCandle.end !== lastInstantOfTheDay - ) { - tradingEngine.current.strategy.status.value = 'Open' - tradingEngine.current.strategy.end.value = candle.end - } + Here we add records to the output files. At the product config property nodePath + we have a pointer to the node that have the information we need to extract. + Later, based on the product record definition we will extract each individual value. + */ + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + let product = dataset.parentNode + let record - /* Prepare the information for the Strategies File */ - if (tradingEngine.current.strategy.begin.value !== 0 && tradingEngine.current.strategy.end.value !== 0) { - let strategyRecord = [ - tradingEngine.current.strategy.begin.value, - tradingEngine.current.strategy.end.value, - tradingEngine.current.strategy.status.value, - tradingEngine.current.strategy.index.value, - tradingEngine.current.strategy.beginRate.value, - tradingEngine.current.strategy.endRate.value, - '"' + tradingEngine.current.strategy.situationName.value + '"', - '"' + tradingEngine.current.strategy.strategyName.value + '"' - ] - - strategiesArray.push(strategyRecord) - - inializeCurrentStrategy() - } - } - - function addPositionRecord() { - /* - Lets see if there will be an open trade ... - Except if we are at the head of the market (remember we skipped the last candle for not being closed.) - */ - if (tradingEngine.current.position.begin.value !== 0 && - tradingEngine.current.position.end.value === 0 && - currentCandleIndex === candles.length - 2 && - lastCandle.end !== lastInstantOfTheDay) { - - /* This means the trade is open */ - tradingEngine.current.position.status.value = 2 - tradingEngine.current.position.end.value = candle.end - tradingEngine.current.position.endRate.value = candle.close - - /* Here we will calculate the ongoing ROI */ - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - tradingEngine.current.position.positionStatistics.ROI.value = (tradingEngine.current.position.rate.value - candle.close) / tradingEngine.current.position.rate.value * 100 - } else { - tradingEngine.current.position.positionStatistics.ROI.value = (candle.close - tradingEngine.current.position.rate.value) / tradingEngine.current.position.rate.value * 100 + if (bot.processingDailyFiles === true && dataset.config.type === 'Daily Files') { + record = scanRecordDefinition(product) } + + if (bot.processingDailyFiles === false && dataset.config.type === 'Market Files') { + record = scanRecordDefinition(product) + } + + let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) + outputDatasetArray.push(record) } - /* Prepare the information for the Positions File */ - if (tradingEngine.current.position.begin.value !== 0 && tradingEngine.current.position.end.value !== 0) { - let positionRecord = [ - tradingEngine.current.position.begin.value, - tradingEngine.current.position.end.value, - tradingEngine.current.position.status.value, - tradingEngine.current.position.positionStatistics.ROI.value, - tradingEngine.current.position.beginRate.value, - tradingEngine.current.position.endRate.value, - tradingEngine.current.position.exitType.value, - '"' + tradingEngine.current.position.situationName.value + '"' - ] + function scanRecordDefinition(product) { + let rootNode = eval(product.config.nodePath) + let record = [] + for (let j = 0; j < product.recordDefinition.recordProperties.length; j++) { + let recordProperty = product.recordDefinition.recordProperties[j] + let value + if (recordProperty.config.nodePath !== undefined) { + let childNode = rootNode[recordProperty.config.childNode] + if (recordProperty.config.index !== undefined) { + value = childNode[recordProperty.config.codeName] // this returns an array + value = value[recordProperty.config.index].value // this return a node item of the array and its value + } else { + value = childNode[recordProperty.config.codeName].value // here we get the value of the childNode + } + } else { + value = rootNode[recordProperty.config.codeName].value // this is the standard way + } + if (value === Infinity) { + value = Number.MAX_SAFE_INTEGER + } + if (recordProperty.config.isString === true) { + value = '"' + value + '"' + } + record.push(value) + } - positionsArray.push(positionRecord) - - initializeCurrentPosition() + return record } } } \ No newline at end of file diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index dc71a3726..14929837c 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -1,11 +1,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILITIES) { const FULL_LOG = true - const LOG_FILE_CONTENT = false const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 const MODULE_NAME = 'Trading Simulation -> ' + bot.SESSION.name - const GMT_SECONDS = ':00.000 GMT+0000' - let thisObject = { finalize: finalize, runSimulation: runSimulation @@ -22,25 +19,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function runSimulation( chart, dataDependencies, - timeFrame, - timeFrameLabel, - currentDay, simulationState, exchangeAPI, - recordsArray, - conditionsArray, - strategiesArray, - positionsArray, + outputDatasets, + outputDatasetsMap, callback, callBackFunction) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> Entering function.') } - let processingDailyFiles - if (timeFrame > global.dailyFilePeriods[0][0]) { - processingDailyFiles = false - } else { - processingDailyFiles = true - } let tradingSystem = simulationState.tradingSystem let tradingEngine = simulationState.tradingEngine @@ -63,12 +48,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let takePositionNow = false let closePositionNow = false - /* In some cases we need to know if we are positioned at the last candle of the calendar day, for that we need these variables. */ - let lastInstantOfTheDay = 0 - if (currentDay) { - lastInstantOfTheDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1 - } - /* These are the Modules we will need to run the Simulation */ const TRADING_ENGINE_MODULE = require('./TradingEngine.js') let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) @@ -80,7 +59,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT const TRADING_RECORDS_MODULE = require('./TradingRecords.js') let tradingRecordsModule = TRADING_RECORDS_MODULE.newTradingRecords(bot, logger) - tradingRecordsModule.initialize() + tradingRecordsModule.initialize(outputDatasets, outputDatasetsMap) const SNAPSHOTS_MODULE = require('./Snapshots.js') let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) @@ -255,18 +234,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } snapshotsModule.manageSnapshots() - tradingRecordsModule.addSimulationRecord() - tradingRecordsModule.addConditionsRecord() - tradingRecordsModule.addStrategyRecord() - tradingRecordsModule.addPositionRecord() + tradingRecordsModule.appendRecords() annoucementsModule.makeAnnoucements() // After everything at the simulation level was done, we will do the annoucements that are pending. controlLoop() } function controlLoop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> controlLoop -> Entering function.') } - /* Checking if we should continue processing this loop or not. */ if (bot.STOP_SESSION === true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> controlLoop -> We are going to stop here bacause we were requested to stop processing this session.') } @@ -374,7 +348,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT */ /* Finding the Current Element on Market Files */ - if (processingDailyFiles) { + if (bot.processingDailyFiles) { for (let j = 0; j < global.dailyFilePeriods.length; j++) { let mapKey = dailyFilePeriods[j][1] let propertyName = 'at' + mapKey.replace('-', '') @@ -430,7 +404,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT We skip the candle at the head of the market because it has not closed yet. The procedure to determine if we are at the head of the market is different when we are processing Market Files than when we are processing Daily Files. TODO Check that the procedure is good for Beta 6 */ - if (processingDailyFiles) { // We are processing Daily Files + if (bot.processingDailyFiles) { // We are processing Daily Files let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value if (tradingEngine.current.candle.index.value === candles.length - 1) { if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index 73264fec4..ec15efa4d 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -8,9 +8,7 @@ let USER_BOT_MODULE; let USER_BOT_COMMONS; - const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket'); - const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily'); - const MULTI_PERIOD = require(ROOT_DIR + 'MultiPeriod'); + const TRADING_PROCESS_MODULE = require(ROOT_DIR + '/Trading/TradingProcess.js'); const FILE_STORAGE = require('./FileStorage.js'); const SESSION = require(ROOT_DIR + 'Session'); @@ -526,8 +524,8 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> onInizialized -> Execution finished well."); } switch (processConfig.framework.name) { - case 'Multi-Period': { - processFramework = MULTI_PERIOD.newMultiPeriod(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); + case 'Trading-Process': { + processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); intitializeProcessFramework(); break; } From d615a6244593077d50f61787cb2953369cdb6f0b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 8 Jun 2020 13:52:16 +0200 Subject: [PATCH 023/113] Update TradingSystem.js --- Trading/TradingSystem.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Trading/TradingSystem.js b/Trading/TradingSystem.js index 14564f53b..d82c83e41 100644 --- a/Trading/TradingSystem.js +++ b/Trading/TradingSystem.js @@ -27,18 +27,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let tradingEngine let conditions = new Map() - let conditionsValues = [] - let conditionsErrors = [] - let formulas = new Map() - let formulasValues = [] - let formulasErrors = [] return thisObject function initialize() { tradingSystem = bot.TRADING_SYSTEM tradingEngine = bot.TRADING_ENGINE + + tradingSystem.highlights = [] + tradingSystem.conditionsErrors = [] + tradingSystem.formulasValues = [] + tradingSystem.formulasErrors = [] } function finalize() { @@ -48,12 +48,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine = undefined conditions = undefined - conditionsValues = undefined - conditionsErrors = undefined + tradingSystem.highlights = undefined + tradingSystem.conditionsErrors = undefined formulas = undefined - formulasValues = undefined - formulasErrors = undefined + tradingSystem.formulasValues = undefined + tradingSystem.formulasErrors = undefined } function setChart(pChart) { @@ -151,12 +151,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { conditions.set(node.id, value) if (value === true) { - conditionsValues.push(1) + tradingSystem.highlights.push(1) } else { - conditionsValues.push(0) + tradingSystem.highlights.push(0) } - formulasErrors.push(error) + tradingSystem.conditionsErrors.push(error) if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> value = ' + value) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> error = ' + error) } @@ -196,7 +196,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { formulaValues.push(0) } - formulasErrors.push(error) + tradingSystem.formulasErrors.push(error) if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } From 963476d9d875d6cb6ae7b5a320bea2793d2e8a66 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 8 Jun 2020 14:55:12 +0200 Subject: [PATCH 024/113] Update TradingSimulation.js --- Trading/TradingSimulation.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 14929837c..3161270a9 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -401,9 +401,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function checkIfWeAreAtTheHeadOfTheMarket() { /* -We skip the candle at the head of the market because it has not closed yet. The procedure to determine if we are at the head of the market is different -when we are processing Market Files than when we are processing Daily Files. TODO Check that the procedure is good for Beta 6 -*/ + We skip the candle at the head of the market because it has not closed yet. The procedure to determine if we are at the head of the market is different + when we are processing Market Files than when we are processing Daily Files. TODO Check that the procedure is good for Beta 6 + */ if (bot.processingDailyFiles) { // We are processing Daily Files let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value if (tradingEngine.current.candle.index.value === candles.length - 1) { From 9be97215a644a6098e1d43d79e827139d0ca98ad Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 8 Jun 2020 15:20:19 +0200 Subject: [PATCH 025/113] writting output files --- FileStorage.js | 4 - Trading/TradingBot.js | 512 +++++------------------------------------- 2 files changed, 60 insertions(+), 456 deletions(-) diff --git a/FileStorage.js b/FileStorage.js index bec9ef69a..ed90f3721 100644 --- a/FileStorage.js +++ b/FileStorage.js @@ -198,9 +198,6 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious) function recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious) { - - logger.write(MODULE_NAME, '[INFO] FileStorage -> createTextFile -> Entering Function.') - /* Choose path for either logs or data */ let fileLocation if (filePath.indexOf("/Logs/") > 0) { @@ -210,7 +207,6 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { } try { - logger.write(MODULE_NAME, '[INFO] FileStorage -> createTextFile -> fileLocation: ' + fileLocation) /* If necesary a folder or folders are created before writing the file to disk. */ diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js index d94f80e23..6a2539469 100644 --- a/Trading/TradingBot.js +++ b/Trading/TradingBot.js @@ -1,8 +1,5 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STORAGE) { const FULL_LOG = true - const LOG_FILE_CONTENT = false - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 - const MODULE_NAME = 'Trading Bot' let thisObject = { @@ -112,6 +109,7 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) let market = bot.market let totalFilesRead = 0 + let totalFilesWritten = 0 let outputDatasetsMap = new Map() let snapshotHeaders @@ -155,6 +153,22 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } } + function readDailyFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Daily Files') { + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + let fileName = 'Data.json' + let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel + "/" + dateForPath + + readOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) + } + } + } + function readOutputFile(fileName, filePath, productName) { filePath += '/' + fileName @@ -207,14 +221,10 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR callBackFunction) } - - function writeFiles(pTradingSystem, pSnapshotHeaders, pTriggerOnSnapshot, pTakePositionSnapshot) { - tradingSystem = pTradingSystem - - snapshotHeaders = pSnapshotHeaders - triggerOnSnapshot = pTriggerOnSnapshot - takePositionSnapshot = pTakePositionSnapshot - + function writeFiles() { + /* + The output of files which were appended with information during the simulation execution, now needs to be saved. + */ if (bot.processingDailyFiles) { writeDailyFiles() } else { @@ -223,471 +233,69 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } function writeMarketFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> Entering function.') } + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent - writeRecordsFile() - - function writeRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeRecordsFile -> Entering function.') } - - let fileContent = JSON.stringify(recordsArray) + if (dataset.config.type === 'Market Files') { let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName + let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeConditionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeConditionsFile -> Entering function.') } - - let fileContent = JSON.stringify(conditionsArray) - - fileContent = '[' + JSON.stringify(tradingSystem) + ',' + fileContent + ']' - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + CONDITIONS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeStrategiesFile -> Entering function.') } - - let fileContent = JSON.stringify(strategiesArray) - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writePositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writePositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writePositionsFile -> Entering function.') } - - let fileContent = JSON.stringify(positionsArray) - - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeSnapshotFiles() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writePositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeSnapshotFiles() { - writeSnapshotFile(triggerOnSnapshot, 'Trigger-On', onFinish) - - function onFinish() { - writeSnapshotFile(takePositionSnapshot, 'Take-Position', callBackFunction) - } - } - - function writeSnapshotFile(snapshotArray, pFileName, callBack) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeSnapshotFile -> Entering function.') } - - let fileRecordCounter = 0 - - let fileContent = '' - let separator = '\r\n' - - parseRecord(snapshotHeaders) - - for (let i = 0; i < snapshotArray.length; i++) { - let record = snapshotArray[i] - parseRecord(record) - fileRecordCounter++ - } - - function parseRecord(record) { - for (let j = 0; j < record.length; j++) { - let property = record[j] - - fileContent = fileContent + '' + property - if (j !== record.length - 1) { - fileContent = fileContent + ',' - } - } - fileContent = fileContent + separator - } - - fileContent = '' + fileContent + '' - - let fileName = pFileName + '.csv' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SNAPSHOTS_FOLDER_NAME + '/' + 'Multi-Period-Market' + '/' + timeFrameLabel - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - callBack(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeMarketFiles -> writeSnapshotFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) + writeOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) } } } function writeDailyFiles() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> Entering function.') } + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent - writeRecordsFile() + if (dataset.config.type === 'Daily Files') { - function writeRecordsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeRecordsFile -> Entering function.') } - - let fileContent = JSON.stringify(recordsArray) - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SIMULATED_RECORDS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName + let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel + "/" + dateForPath - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeConditionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeRecordsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) + writeOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) } } + } - function writeConditionsFile() { + function writeOutputFile(fileName, filePath, productName) { + + filePath += '/' + fileName + let fileContent = JSON.stringify(outputDatasetsMap.get(productName)) + + fileStorage.createTextFile(filePath, fileContent, onFileCreated) + + function onFileCreated(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeConditionsFile -> Entering function.') } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> filePath = ' + filePath) + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> market = ' + market.baseAsset + '_' + market.quotedAsset) - let fileContent = JSON.stringify(conditionsArray) - - fileContent = '[' + JSON.stringify(tradingSystem) + ',' + fileContent + ']' - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + CONDITIONS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeStrategiesFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } + callBackFunction(err) + return } + + anotherFileWritten() } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeConditionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeStrategiesFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeStrategiesFile -> Entering function.') } - - let fileContent = JSON.stringify(strategiesArray) - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + STRATEGIES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writePositionsFile() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeStrategiesFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writePositionsFile() { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writePositionsFile -> Entering function.') } - - let fileContent = JSON.stringify(positionsArray) - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = 'Data.json' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + TRADES_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - writeSnapshotFiles() - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writePositionsFile -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - - function writeSnapshotFiles() { - writeSnapshotFile(triggerOnSnapshot, 'Trigger-On', onFinish) - - function onFinish() { - writeSnapshotFile(takePositionSnapshot, 'Take-Position', callBackFunction) - } - } - - function writeSnapshotFile(snapshotArray, pFileName, callBack) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeSnapshotFile -> Entering function.') } - - let fileRecordCounter = 0 - - let fileContent = '' - let separator = '\r\n' - - parseRecord(snapshotHeaders) - - for (let i = 0; i < snapshotArray.length; i++) { - let record = snapshotArray[i] - parseRecord(record) - fileRecordCounter++ - } - - function parseRecord(record) { - for (let j = 0; j < record.length; j++) { - let property = record[j] - - fileContent = fileContent + '' + property - if (j !== record.length - 1) { - fileContent = fileContent + ',' - } - } - fileContent = fileContent + separator - } - - fileContent = '' + fileContent + '' - - let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2) - let fileName = pFileName + '.csv' - let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + SNAPSHOTS_FOLDER_NAME + '/' + 'Multi-Period-Daily' + '/' + timeFrameLabel + '/' + dateForPath - filePath += '/' + fileName - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - - function onFileCreated(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> Entering function.') } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> fileContent = ' + fileContent) } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> filePath = ' + filePath) - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> market = ' + market.baseAsset + '_' + market.quotedAsset) - - callBackFunction(err) - return - } - - callBack(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> onFileCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] start -> writeDailyFiles -> writeSnapshotFile -> err = ' + err.stack) + logger.write(MODULE_NAME, '[ERROR] start -> readMarketFiles -> readOutputFile -> onFileRead -> err = ' + err.stack) callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } } + + function anotherFileWritten() { + totalFilesWritten++ + if (totalFilesWritten === outputDatasets.length) { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } + } + } catch (err) { logger.write(MODULE_NAME, '[ERROR] start -> err = ' + err.stack) callBackFunction(global.DEFAULT_FAIL_RESPONSE) From a0745fe1b955d87d4c00b3b96399fcb90f96fe09 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 9 Jun 2020 11:36:27 +0200 Subject: [PATCH 026/113] Code Cleaning --- ExchangeAPI.js | 24 ++--- Trading/Snapshots.js | 5 + Trading/TradingBot.js | 17 +-- Trading/TradingProcess.js | 6 +- Trading/TradingSimulation.js | 6 +- TradingBotProcessMainLoop.js | 204 ++++------------------------------- 6 files changed, 41 insertions(+), 221 deletions(-) diff --git a/ExchangeAPI.js b/ExchangeAPI.js index 54c3c52e7..617b5a5a8 100644 --- a/ExchangeAPI.js +++ b/ExchangeAPI.js @@ -1,5 +1,5 @@ exports.newExchangeAPI = function newExchangeAPI(logger, BOT) { - + let MODULE_NAME = "Exchange API"; let LOG_INFO = true; let apiClient; @@ -22,8 +22,7 @@ return thisObject; - function initialize(callBackFunction) { - try { + function initialize() { exchangeId = bot.exchange.toLowerCase() let key = process.env.KEY @@ -41,18 +40,11 @@ verbose: false, options: options } - exchange = new exchangeClass(exchangeConstructorParams) - - callBackFunction(global.DEFAULT_OK_RESPONSE) - } catch (err) { - logError("initialize -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } } function getMaxDecimalPositions() { - return 8 + return 8 } async function getTicker(market, callBackFunction) { @@ -62,13 +54,13 @@ let ticker if (exchange.has['fetchTicker']) { - ticker = await (exchange.fetchTicker(symbol)) + ticker = await (exchange.fetchTicker(symbol)) callBackFunction(global.DEFAULT_OK_RESPONSE, ticker) } else { logError("getTicker -> Exchange does not support fetchTicker command."); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } - + } catch (err) { logError("getTicker -> err = " + err.message); callBackFunction(global.DEFAULT_FAIL_RESPONSE); @@ -83,7 +75,7 @@ let openOrders if (exchange.has['fetchOpenOrders']) { - openOrders = await(exchange.fetchOpenOrders (symbol)) + openOrders = await (exchange.fetchOpenOrders(symbol)) callBackFunction(global.DEFAULT_OK_RESPONSE, openOrders) } else { logError("getOpenOrders -> Exchange does not support fetchOpenOrders command."); @@ -116,7 +108,7 @@ } } - async function createOrder(market, side, price, cost, amount, callBackFunction) { + async function createOrder(market, side, price, cost, amount, callBackFunction) { try { logInfo("createOrder -> Entering function."); @@ -137,7 +129,7 @@ let order if (exchange.has['createOrder']) { - order = await(exchange.createOrder(symbol, type, side, amount)) + order = await (exchange.createOrder(symbol, type, side, amount)) callBackFunction(global.DEFAULT_OK_RESPONSE, order) } else { logError("createOrder -> Exchange does not support createOrder command."); diff --git a/Trading/Snapshots.js b/Trading/Snapshots.js index 49faa2995..a8aa133f8 100644 --- a/Trading/Snapshots.js +++ b/Trading/Snapshots.js @@ -11,6 +11,11 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let tradingEngine + const SNAPSHOTS_FOLDER_NAME = 'Snapshots' + + let snapshotHeaders + let triggerOnSnapshot + let takePositionSnapshot /* Snapshots of Trigger On and Take Positions */ let snapshots = { diff --git a/Trading/TradingBot.js b/Trading/TradingBot.js index 6a2539469..b3eacbdef 100644 --- a/Trading/TradingBot.js +++ b/Trading/TradingBot.js @@ -14,8 +14,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR const COMMONS = require('../Commons.js') let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) - let exchangeAPI - return thisObject function finalize() { @@ -25,14 +23,11 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR commons = undefined } - function initialize(pExchangeAPI, callBackFunction) { + function initialize(callBackFunction) { try { logger.fileName = MODULE_NAME logger.initialize() - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> Entering function.') } - - exchangeAPI = pExchangeAPI callBackFunction(global.DEFAULT_OK_RESPONSE) } catch (err) { logger.write(MODULE_NAME, '[ERROR] initialize -> err = ' + err.stack) @@ -42,7 +37,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { try { - bot.processingDailyFiles if (timeFrame > global.dailyFilePeriods[0][0]) { bot.processingDailyFiles = false @@ -104,20 +98,14 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } /* Simulation */ - const SNAPSHOTS_FOLDER_NAME = 'Snapshots' const TRADING_SIMULATION = require('./TradingSimulation.js') let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) + let market = bot.market let totalFilesRead = 0 let totalFilesWritten = 0 let outputDatasetsMap = new Map() - let snapshotHeaders - let triggerOnSnapshot - let takePositionSnapshot - - let tradingSystem = {} - if (bot.RESUME !== true) { simulationState.tradingEngine = bot.SESSION.tradingEngine simulationState.tradingSystem = bot.SESSION.tradingSystem @@ -214,7 +202,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR chart, dataDependencies, simulationState, - exchangeAPI, outputDatasets, outputDatasetsMap, writeFiles, diff --git a/Trading/TradingProcess.js b/Trading/TradingProcess.js index 725d66510..1adfa6288 100644 --- a/Trading/TradingProcess.js +++ b/Trading/TradingProcess.js @@ -32,9 +32,8 @@ return thisObject; - function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, exchangeAPI, callBackFunction) { + function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { try { - logger.fileName = MODULE_NAME; logger.initialize(); @@ -43,9 +42,8 @@ processConfig = pProcessConfig; let USER_BOT_MODULE = require("./TradingBot") - botInstance = USER_BOT_MODULE.newTradingBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(exchangeAPI, callBackFunction); + botInstance.initialize(callBackFunction); } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); diff --git a/Trading/TradingSimulation.js b/Trading/TradingSimulation.js index 3161270a9..feaf5451e 100644 --- a/Trading/TradingSimulation.js +++ b/Trading/TradingSimulation.js @@ -20,7 +20,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT chart, dataDependencies, simulationState, - exchangeAPI, outputDatasets, outputDatasetsMap, callback, @@ -49,6 +48,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let closePositionNow = false /* These are the Modules we will need to run the Simulation */ + + const EXCHANGE_API = require(ROOT_DIR + 'ExchangeAPI'); + exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); + exchangeAPI.initialize(); + const TRADING_ENGINE_MODULE = require('./TradingEngine.js') let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index ec15efa4d..db448c00e 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -31,14 +31,9 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - /* This function is exactly the same in the 3 modules representing the 2 different bot types loops. */ - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - processConfig = pProcessConfig; - if (bot.definedByUI === true) { /* The code of the bot is defined at the UI. No need to load a file with the code. */ session.initialize(processConfig, onSessionInitialized) @@ -57,7 +52,6 @@ fileStorage.getTextFile(filePath, onBotDownloaded); function onBotDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); @@ -75,7 +69,6 @@ fileStorage.getTextFile(filePath, onCommonsDownloaded); function onCommonsDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -99,11 +92,7 @@ } function run(callBackFunction) { - try { - - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - /* Some initial values*/ bot.enableCheckLoopHealth = true; let fixedTimeLoopIntervalHandle; @@ -112,9 +101,7 @@ loop(); function loop() { - try { - processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { str = str.toString(); @@ -133,8 +120,6 @@ bot.loopCounter++; bot.loopStartTime = new Date().valueOf(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - let nextWaitTime; /* Heartbeats sent to the UI */ @@ -145,7 +130,6 @@ /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const EXCHANGE_API = require(ROOT_DIR + 'ExchangeAPI'); const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); @@ -198,21 +182,12 @@ initializeProcessExecutionEvents(); function initializeProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - processExecutionEvents.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); @@ -273,19 +248,11 @@ } function startProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - processExecutionEvents.start(onStarted); function onStarted(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); @@ -352,21 +319,12 @@ } function initializeStatusDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - statusDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); @@ -428,25 +386,33 @@ } function initializeDataDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } - dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); - dataDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } - initializeExchangeAPI(); + switch (processConfig.framework.name) { + case 'Trading-Process': { + processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); + intitializeProcessFramework(); + break; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } return; } case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. @@ -503,114 +469,12 @@ } } - function initializeExchangeAPI() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> Entering function."); } - - exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); - - exchangeAPI.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeContext -> onInizialized -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeExchangeAPI -> onInizialized -> Execution finished well."); } - - switch (processConfig.framework.name) { - case 'Trading-Process': { - processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); - intitializeProcessFramework(); - break; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Process Framework not Supported."); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeExchangeAPI -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeContext -> onInizialized -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeExchangeAPI -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - function intitializeProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } - - processFramework.initialize(processConfig, statusDependencies, dataDependencies, exchangeAPI, onInizialized); + processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); @@ -687,18 +551,11 @@ } function startProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } - processFramework.start(onFinished); function onFinished(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } processFramework.finalize() processFramework = undefined dataDependencies.finalize() @@ -795,11 +652,7 @@ } function finishProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - processExecutionEvents.finish(onFinished); function onFinished(err) { @@ -807,8 +660,6 @@ processExecutionEvents.finalize() processExecutionEvents = undefined - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); @@ -870,22 +721,15 @@ } function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ - processHeartBeat() /* Here we check if we must stop the loop gracefully. */ - shallWeStop(onStop, onContinue); function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } if (global.WRITE_LOGS_TO_FILES === 'true') { @@ -896,15 +740,10 @@ processStopped() callBackFunction(global.DEFAULT_OK_RESPONSE); return; - } function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - switch (nextWaitTime) { case 'Normal': { let waitTime @@ -990,13 +829,8 @@ } function shallWeStop(stopCallBack, continueCallBack) { - try { - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - if (!global.STOP_TASK_GRACEFULLY) { continueCallBack(); } else { From ad2a71da622564cc94b70bf3da9c127dff962519 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 10 Jun 2020 12:21:41 +0200 Subject: [PATCH 027/113] Debugging until Task Runs --- Root.js | 2 +- Session.js | 2 +- TradingBotProcessMainLoop.js | 40 ++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Root.js b/Root.js index f4f3910f3..f3411d1ba 100644 --- a/Root.js +++ b/Root.js @@ -144,7 +144,7 @@ processInstance.referenceParent.config.framework !== undefined && (processInstance.referenceParent.config.framework.name === 'Multi-Period-Market' || processInstance.referenceParent.config.framework.name === 'Multi-Period-Daily' || - processInstance.referenceParent.config.framework.name === 'Multi-Period') + processInstance.referenceParent.config.framework.name === 'Trading-Process') ) { botConfig = processInstance.referenceParent.parentNode.config botConfig.definedByUI = true diff --git a/Session.js b/Session.js index 2db5d9190..bda97ba8a 100644 --- a/Session.js +++ b/Session.js @@ -9,7 +9,7 @@ } const SOCIAL_BOTS_MODULE = require('./SocialBots.js') - let socialBotsModule = SOCIAL_BOTS_MODULE.newSocialBots(bot, logger) + let socialBotsModule = SOCIAL_BOTS_MODULE.newSocialBots(bot, parentLogger) return thisObject; diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index db448c00e..3d5207e7c 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -166,7 +166,7 @@ console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Waiting for " + bot.processNode.session.type + " " + bot.processNode.session.name + " to be ran. "); - nextWaitTime = 'Normal'; + nextWaitTime = 'Waiting for Session'; loopControl(nextWaitTime); return } @@ -745,12 +745,38 @@ function onContinue() { /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ switch (nextWaitTime) { + case 'Waiting for Session': { + let waitTime = processConfig.sessionRunWaitTime + nextLoopTimeoutHandle = setTimeout(loop, waitTime); + let waitingTime = waitTime / 1000 / 60 + let label = 'minute/s' + if (waitingTime < 1) { + waitingTime = waitTime / 1000 + label = 'seconds' + } + processHeartBeat(undefined, undefined, "Waiting " + waitingTime + " " + label + " for " + bot.processNode.session.type + " " + bot.processNode.session.name + " to be ran. ") + logger.persist(); + } + break case 'Normal': { let waitTime - if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { - waitTime = processConfig.liveWaitTime - } else { - waitTime = processConfig.normalWaitTime + switch (bot.SESSION.type) { + case 'Live Trading Session': { + waitTime = processConfig.liveTradingWaitTime + } + break + case 'Fordward Tessting Session': { + waitTime = processConfig.fordwardTestingWaitTime + } + break + case 'Paper Trading Session': { + waitTime = processConfig.paperTradingWaitTime + } + break + case 'Backtesting Session': { + waitTime = processConfig.backtestingWaitTime + } + break } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (waitTime / 1000 / 60) + " minute/s."); } @@ -765,9 +791,7 @@ label = 'seconds' } processHeartBeat(undefined, undefined, "Waiting " + waitingTime + " " + label + " for next execution.") - if (global.WRITE_LOGS_TO_FILES === 'true') { - logger.persist(); - } + logger.persist(); } break; case 'Retry': { From 15a73ef6ab7bdcbf78742c1a14ce6d5de7e5d5e6 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 10 Jun 2020 14:10:08 +0200 Subject: [PATCH 028/113] Session Errors Implemented --- Session.js | 32 +++++++++++++++++++++++--------- TradingBotProcessMainLoop.js | 25 ++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Session.js b/Session.js index bda97ba8a..088a4415a 100644 --- a/Session.js +++ b/Session.js @@ -60,7 +60,7 @@ bot.SESSION = JSON.parse(message.event.session) bot.DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter) bot.RESUME = message.event.resume - bot.SESSION.stop = sessionStop // stop function + bot.SESSION.stop = stopSession // stop function setUpAppSchema() setUpSessionFolderName() @@ -142,7 +142,9 @@ */ if (bot.SESSION.parameters === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Node with no Parameters."); } + let errorMessage = "Session Node with no Parameters." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION, errorMessage) return false } @@ -190,21 +192,29 @@ /* Time Frame */ if (bot.SESSION.parameters.timeFrame === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Time Frame."); } + let errorMessage = "Session Parameters Node with no Time Frame." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters, errorMessage) return false } - if (bot.SESSION.parameters.timeFrame.label === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Time Frame Label configuration."); } + if (bot.SESSION.parameters.timeFrame.config.label === undefined) { + let errorMessage = "Session Parameters Node with no Time Frame Label configuration." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters.timeFrame, errorMessage) return false } /* Session Base Asset */ if (bot.SESSION.parameters.sessionBaseAsset === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Session Base Asset."); } + let errorMessage = "Session Parameters Node with no Session Base Asset." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters, errorMessage) return false } if (bot.SESSION.parameters.sessionBaseAsset.config.initialBalance === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Session Base Asset with no initialBalance configuration."); } + let errorMessage = "Session Parameters Session Base Asset with no initialBalance configuration." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters.sessionBaseAsset, errorMessage) return false } if (bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance === undefined) { @@ -216,11 +226,15 @@ /* Session Quoted Asset */ if (bot.SESSION.parameters.sessionQuotedAsset === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Node with no Session Quoted Asset."); } + let errorMessage = "Session Parameters Node with no Session Quoted Asset." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters, errorMessage) return false } if (bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance === undefined) { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> Session Parameters Session Quoted Asset with no initialBalance configuration."); } + let errorMessage = "Session Parameters Session Quoted Asset with no initialBalance configuration." + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage) + bot.sessionError(bot.SESSION.parameters.sessionQuotedAsset, errorMessage) return false } if (bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance === undefined) { diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index 3d5207e7c..8b2d75d8b 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -98,6 +98,13 @@ let fixedTimeLoopIntervalHandle; bot.STOP_SESSION = true; + /* Errors sent to the UI */ + bot.sessionError = sessionError + + /* Heartbeats sent to the UI */ + bot.sessionHeartBeat = sessionHeartBeat + bot.processHeartBeat = processHeartBeat + loop(); function loop() { @@ -122,9 +129,7 @@ let nextWaitTime; - /* Heartbeats sent to the UI */ - bot.sessionHeartBeat = sessionHeartBeat - bot.processHeartBeat = processHeartBeat + /* We tell the UI that we are running. */ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ @@ -904,6 +909,20 @@ } } + function sessionError(node, errorMessage) { + let event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Error', event) + + if (global.STOP_TASK_GRACEFULLY === true) { + bot.STOP_SESSION = true + } + } + function processStopped() { global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') } From 32125b2d800a945303b4601ad5cd7f48a85e9ef2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 10 Jun 2020 15:15:21 +0200 Subject: [PATCH 029/113] processError implemented --- Session.js | 32 ++++++++++++++++++++++++++++++++ StatusReport.js | 28 ++++++++++++++-------------- TradingBotProcessMainLoop.js | 23 +++-------------------- server.js | 10 ++++++++++ 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/Session.js b/Session.js index 088a4415a..49ba30f62 100644 --- a/Session.js +++ b/Session.js @@ -45,6 +45,12 @@ global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Run Session', undefined, bot.sessionKey, undefined, runSession) global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, stopSession) + /* Errors sent to the UI */ + bot.sessionError = sessionError + + /* Heartbeats sent to the UI */ + bot.sessionHeartBeat = sessionHeartBeat + callBackFunction(global.DEFAULT_OK_RESPONSE) return @@ -372,6 +378,32 @@ } } + function sessionHeartBeat(processingDate) { + let event = { + seconds: (new Date()).getSeconds(), + processingDate: processingDate + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Heartbeat', event) + + if (global.STOP_TASK_GRACEFULLY === true) { + bot.STOP_SESSION = true + } + } + + function sessionError(node, errorMessage) { + let event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Error', event) + + if (global.STOP_TASK_GRACEFULLY === true) { + bot.STOP_SESSION = true + } + } + } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); diff --git a/StatusReport.js b/StatusReport.js index b9865452e..72fe4381d 100644 --- a/StatusReport.js +++ b/StatusReport.js @@ -48,47 +48,47 @@ /* Some very basic validations that we have all the information needed. */ if (statusDependencyNode.referenceParent === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Status Dependency without Reference Parent. Status Dependency = " + JSON.stringify(statusDependencyNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode, "Status Dependency without Reference Parent.") return } if (statusDependencyNode.referenceParent.parentNode === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Status Report not attached to a Process Definition. Status Report = " + JSON.stringify(statusDependencyNode.referenceParent)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent, "Status Report not attached to a Process Definition.") return } if (statusDependencyNode.referenceParent.parentNode.config.codeName === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Process Definition witn no codeName defined. Process Definition = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition witn no codeName defined.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Process Definition not attached to a Bot. Process Definition = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition not attached to a Bot.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Bot witn no codeName defined. Bot = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot with no codeName defined.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Bot not attached to a Data Mine. Bot = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot not attached to a Data Mine.") return } if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.config.codeName === undefined) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Data Mine witn no codeName defined. Data Mine = " + JSON.stringify(statusDependencyNode.referenceParent.parentNode.parentNode.parentNode)); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode.parentNode, "Data Mine witn no codeName defined.") return } + function validationFailed(errorInNode, errorMessage) { + let nodeString = JSON.stringify(errorInNode) + logger.write(MODULE_NAME, "[ERROR] initialize -> " + errorMessage + ' -> nodeString = ' + nodeString) + bot.processError(errorInNode, errorMessage) + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + /* Simplifying the access to basic info */ statusDependencyNode.bot = statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName statusDependencyNode.process = statusDependencyNode.referenceParent.parentNode.config.codeName diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index 8b2d75d8b..c2dbd8deb 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -99,10 +99,9 @@ bot.STOP_SESSION = true; /* Errors sent to the UI */ - bot.sessionError = sessionError + bot.processError = processError /* Heartbeats sent to the UI */ - bot.sessionHeartBeat = sessionHeartBeat bot.processHeartBeat = processHeartBeat loop(); @@ -897,30 +896,14 @@ global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) } - function sessionHeartBeat(processingDate) { - let event = { - seconds: (new Date()).getSeconds(), - processingDate: processingDate - } - global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Heartbeat', event) - - if (global.STOP_TASK_GRACEFULLY === true) { - bot.STOP_SESSION = true - } - } - - function sessionError(node, errorMessage) { + function processError(node, errorMessage) { let event = { nodeName: node.name, nodeType: node.type, nodeId: node.id, errorMessage: errorMessage } - global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Error', event) - - if (global.STOP_TASK_GRACEFULLY === true) { - bot.STOP_SESSION = true - } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) } function processStopped() { diff --git a/server.js b/server.js index e2840cc6d..5b48bf2a8 100644 --- a/server.js +++ b/server.js @@ -197,6 +197,16 @@ function bootLoader() { global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Heartbeat', event) } + global.taskError = function taskError(node, errorMessage) { + let event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Error', event) + } + for (let processIndex = 0; processIndex < global.TASK_NODE.bot.processes.length; processIndex++) { let config = global.TASK_NODE.bot.processes[processIndex].config From fe55b1008a485cdaed8962d95a87219ed1089881 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 10 Jun 2020 15:19:50 +0200 Subject: [PATCH 030/113] processError at inidicators and sensors --- IndicatorBotProcessMainLoop.js | 69 +++++++++++++++++----------------- SensorBotProcessMainLoop.js | 59 +++++++++++++++-------------- 2 files changed, 65 insertions(+), 63 deletions(-) diff --git a/IndicatorBotProcessMainLoop.js b/IndicatorBotProcessMainLoop.js index 90892dd72..dbf0c3b82 100644 --- a/IndicatorBotProcessMainLoop.js +++ b/IndicatorBotProcessMainLoop.js @@ -29,7 +29,7 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - + try { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } @@ -80,7 +80,7 @@ } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -95,14 +95,16 @@ let fixedTimeLoopIntervalHandle; + /* Errors sent to the UI */ + bot.processError = processError + + /* Heartbeats sent to the UI */ + bot.processHeartBeat = processHeartBeat + if (bot.runAtFixedInterval === true) { - fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); - } else { - loop(); - } function loop() { @@ -129,9 +131,8 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - /* Loop Heartbeat sent to the UI */ - bot.processHeartBeat = processHeartBeat - processHeartBeat() + /* We tell the UI that we are running. */ + processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ @@ -140,8 +141,8 @@ const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); const DATA_SET = require(ROOT_DIR + 'DataSet'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ @@ -372,7 +373,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -383,7 +384,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -476,7 +477,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -487,7 +488,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -566,7 +567,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -577,7 +578,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -593,7 +594,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } - usertBot.start(onFinished); + usertBot.start(onFinished); function onFinished(err) { @@ -666,7 +667,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -677,7 +678,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -754,7 +755,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -765,7 +766,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -859,7 +860,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -870,7 +871,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -974,7 +975,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - if(global.WRITE_LOGS_TO_FILES === 'true'){ + if (global.WRITE_LOGS_TO_FILES === 'true') { logger.persist(); } @@ -997,8 +998,8 @@ checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") - if(global.WRITE_LOGS_TO_FILES === 'true'){ + processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") + if (global.WRITE_LOGS_TO_FILES === 'true') { logger.persist(); } } @@ -1009,21 +1010,21 @@ checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); } break; case 'Sleep': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") logger.persist(); } break; case 'Coma': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") logger.persist(); } break; @@ -1047,7 +1048,7 @@ if (now - bot.loopStartTime > processConfig.deadWaitTime) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName,20) + " " + pad(bot.process,30) + " Loop # " + pad(Number(bot.loopCounter),5) + " found dead. Resurrecting it now.")); + console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); logger.persist(); // We persist the logs of the failed execution. clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. @@ -1055,7 +1056,7 @@ } else { - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName,20) + " " + pad(bot.process,30) + " Loop # " + pad(Number(bot.loopCounter),5) + " found delayed but still alive. No action taken.")); + console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); } } @@ -1086,7 +1087,7 @@ } } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); @@ -1110,7 +1111,7 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); diff --git a/SensorBotProcessMainLoop.js b/SensorBotProcessMainLoop.js index 558d9ffa4..5502fc340 100644 --- a/SensorBotProcessMainLoop.js +++ b/SensorBotProcessMainLoop.js @@ -85,7 +85,7 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -99,21 +99,23 @@ let fixedTimeLoopIntervalHandle; - if (bot.runAtFixedInterval === true) { + /* Errors sent to the UI */ + bot.processError = processError + /* Heartbeats sent to the UI */ + bot.processHeartBeat = processHeartBeat + + if (bot.runAtFixedInterval === true) { fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); loop(); // First run. - } else { - loop(); - } function loop() { try { - processHeartBeat(undefined, undefined, "Running...") + processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { str = str.toString(); return str.length < max ? pad(" " + str, max) : str; @@ -134,26 +136,25 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - /* Loop Heartbeat sent to the UI */ - bot.processHeartBeat = processHeartBeat - processHeartBeat() + /* We tell the UI that we are running. */ + processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. - /* High level log entry */ + /* High level log entry */ - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) - + " Main Loop # " + pad(Number(bot.loopCounter), 8) ) + console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + + " Main Loop # " + pad(Number(bot.loopCounter), 8)) /* We will prepare first the infraestructure needed for the bot to run. There are 4 modules we need to sucessfullly initialize first. */ @@ -374,7 +375,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -385,7 +386,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -464,7 +465,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -475,7 +476,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -564,7 +565,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -575,7 +576,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); @@ -598,7 +599,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } processExecutionEvents.finalize() - processExecutionEvents = undefined + processExecutionEvents = undefined switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { @@ -676,7 +677,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - if(global.WRITE_LOGS_TO_FILES === 'true'){ + if (global.WRITE_LOGS_TO_FILES === 'true') { logger.persist(); } @@ -703,8 +704,8 @@ checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") - if(global.WRITE_LOGS_TO_FILES === 'true'){ + processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") + if (global.WRITE_LOGS_TO_FILES === 'true') { logger.persist(); } } @@ -715,7 +716,7 @@ checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.retryWaitTime * 5, bot.loopCounter); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); } break; @@ -727,7 +728,7 @@ } else { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") logger.persist(); } } @@ -740,7 +741,7 @@ } else { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") logger.persist(); } } @@ -805,7 +806,7 @@ } } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); @@ -830,7 +831,7 @@ } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = "+ err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); From 5950471366d521eccc850345b11cfb43d0c12e1a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 10 Jun 2020 15:39:32 +0200 Subject: [PATCH 031/113] code cleaning --- Session.js | 8 +-- Trading/TradingProcess.js | 131 +++++++++++++++++++------------------- 2 files changed, 69 insertions(+), 70 deletions(-) diff --git a/Session.js b/Session.js index 49ba30f62..5b090ad0a 100644 --- a/Session.js +++ b/Session.js @@ -296,7 +296,7 @@ function startBackTesting(message) { bot.resumeExecution = false; // TODO change this for the new way of handling resuming bot.hasTheBotJustStarted = true // TODO see if this is necesary - bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // TODO see if this is necesary + bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // Here we initialize the date of the process. return true } @@ -308,7 +308,7 @@ } bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) return true @@ -323,7 +323,7 @@ } bot.resumeExecution = false; - bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true /* Reduce the balance */ @@ -352,7 +352,7 @@ bot.resumeExecution = false; bot.hasTheBotJustStarted = true - bot.multiPeriodProcessDatetime = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.label) return true } diff --git a/Trading/TradingProcess.js b/Trading/TradingProcess.js index 1adfa6288..a8dd2e4f6 100644 --- a/Trading/TradingProcess.js +++ b/Trading/TradingProcess.js @@ -79,7 +79,6 @@ }; let previousDay; // Holds the date of the previous day relative to the processing date. - let simulationState getContextVariables(); @@ -127,7 +126,7 @@ contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()); /* Validation that the data is not up-to-date. */ - if (bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS > contextVariables.dateEndOfMarket.valueOf() && bot.SESSION.type !== "Backtesting Session") { + if (bot.tradingProcessDate.valueOf() - ONE_DAY_IN_MILISECONDS > contextVariables.dateEndOfMarket.valueOf() && bot.SESSION.type !== "Backtesting Session") { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> " + "Head of the market is @ " + contextVariables.dateEndOfMarket); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> You need to UPDATE your datasets in order to run a " + bot.SESSION.type) } @@ -364,18 +363,18 @@ callTheBot() return } - bot.multiPeriodProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. + bot.tradingProcessDate = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. advanceTime(); function advanceTime() { - bot.multiPeriodProcessDatetime = new Date(bot.multiPeriodProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); - previousDay = new Date(bot.multiPeriodProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); + bot.tradingProcessDate = new Date(bot.tradingProcessDate.valueOf() + ONE_DAY_IN_MILISECONDS); + previousDay = new Date(bot.tradingProcessDate.valueOf() - ONE_DAY_IN_MILISECONDS); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.multiPeriodProcessDatetime = " + bot.multiPeriodProcessDatetime); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.tradingProcessDate = " + bot.tradingProcessDate); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } /* Validation that we are not going past the user defined end date. */ - if (bot.multiPeriodProcessDatetime.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + if (bot.tradingProcessDate.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } @@ -386,7 +385,7 @@ } /* Validation that we are not going past the head of the market. */ - if (bot.multiPeriodProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf() + ONE_DAY_IN_MILISECONDS - 1) { + if (bot.tradingProcessDate.valueOf() > contextVariables.dateEndOfMarket.valueOf() + ONE_DAY_IN_MILISECONDS - 1) { const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } @@ -418,7 +417,7 @@ function timeFramesLoop() { /* Telling the world we are alive and doing well */ - let processingDate = bot.multiPeriodProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); + let processingDate = bot.tradingProcessDate.getUTCFullYear() + '-' + utilities.pad(bot.tradingProcessDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.tradingProcessDate.getUTCDate(), 2); bot.processHeartBeat(processingDate) /* We will iterate through all posible timeFrames. @@ -471,7 +470,7 @@ } } - let dateForPath = bot.multiPeriodProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodProcessDatetime.getUTCDate(), 2); + let dateForPath = bot.tradingProcessDate.getUTCFullYear() + '/' + utilities.pad(bot.tradingProcessDate.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.tradingProcessDate.getUTCDate(), 2); let filePath if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; @@ -536,73 +535,73 @@ } } } + } - function callTheBot() { - botInstance.start( - multiPeriodDataFiles, - currentTimeFrame, - currentTimeFrameLabel, - bot.multiPeriodProcessDatetime, - simulationState, - onBotFinished); + function callTheBot() { + botInstance.start( + multiPeriodDataFiles, + currentTimeFrame, + currentTimeFrameLabel, + bot.tradingProcessDate, + simulationState, + onBotFinished); - function onBotFinished(err) { - try { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - callBackFunction(err); - return; - } + function onBotFinished(err) { + try { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err); + return; + } - botNeverRan = false - bot.RESUME = true // From here on, all other loops executions must resume from where we left at this current run. + botNeverRan = false + bot.RESUME = true // From here on, all other loops executions must resume from where we left at this current run. - if (currentTimeFrame > global.dailyFilePeriods[0][0]) { - writeMarketStatusReport(onMarketStatusReport) + if (currentTimeFrame > global.dailyFilePeriods[0][0]) { + writeMarketStatusReport(onMarketStatusReport) - } else { - writeDataRanges(currentTimeFrameLabel, onWritten); - } + } else { + writeDataRanges(currentTimeFrameLabel, onWritten); + } - function onWritten(err) { - try { + function onWritten(err) { + try { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); - callBackFunction(err); - return; - } - - writeDailyStatusReport(bot.multiPeriodProcessDatetime, onDailyStatusReport); - } catch (err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function onDailyStatusReport() { - /* The next run we need the process to continue at the date it finished. */ - processConfig.framework.startDate.resumeExecution = true; - advanceTime(); - } - - function onMarketStatusReport() { - /* This is where we check if we need reached the user defined end datetime. */ - const now = new Date() - if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { - const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> " + logText); } - - bot.STOP_SESSION = true - callBackFunction(global.DEFAULT_OK_RESPONSE); + callBackFunction(err); return; } - callBackFunction(global.DEFAULT_OK_RESPONSE); + + writeDailyStatusReport(bot.tradingProcessDate, onDailyStatusReport); + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + function onDailyStatusReport() { + /* The next run we need the process to continue at the date it finished. */ + processConfig.framework.startDate.resumeExecution = true; + advanceTime(); } + + function onMarketStatusReport() { + /* This is where we check if we need reached the user defined end datetime. */ + const now = new Date() + if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> " + logText); } + + bot.STOP_SESSION = true + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } @@ -613,7 +612,7 @@ function productLoopBody() { let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; - writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodProcessDatetime, productCodeName, currentTimeFrameLabel, controlLoop); + writeDataRange(contextVariables.dateBeginOfMarket, bot.tradingProcessDate, productCodeName, currentTimeFrameLabel, controlLoop); } function controlLoop() { @@ -680,7 +679,7 @@ thisReport.file.simulationState = simulationState; thisReport.save(callBack); - logger.newInternalLoop(bot.codeName, bot.process, bot.multiPeriodProcessDatetime); + logger.newInternalLoop(bot.codeName, bot.process, bot.tradingProcessDate); } } catch (err) { From fc8ae1573622d4fe3caaba34cb6a25912ce85888 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 10 Jun 2020 21:37:25 +0200 Subject: [PATCH 032/113] Huge Refactoring --- IndicatorBot.js | 1202 +++++++++++++++-- IndicatorBotProcessMainLoop.js | 1122 --------------- IndicatorOutput.js | 168 +++ .../Announcements.js | 0 {Trading => LowFrequencyTrading}/Snapshots.js | 0 .../TradingEngine.js | 0 .../TradingExecution.js | 0 .../TradingOutput.js | 62 +- .../TradingProcess.js | 87 +- .../TradingRecords.js | 0 .../TradingSimulation.js | 0 .../TradingSystem.js | 0 MultiPeriodDaily.js | 28 +- MultiPeriodMarket.js | 28 +- Root.js | 26 +- SensorBotProcessMainLoop.js => SensorBot.js | 12 +- TradingBotProcessMainLoop.js => TradingBot.js | 6 +- 17 files changed, 1378 insertions(+), 1363 deletions(-) delete mode 100644 IndicatorBotProcessMainLoop.js create mode 100644 IndicatorOutput.js rename {Trading => LowFrequencyTrading}/Announcements.js (100%) rename {Trading => LowFrequencyTrading}/Snapshots.js (100%) rename {Trading => LowFrequencyTrading}/TradingEngine.js (100%) rename {Trading => LowFrequencyTrading}/TradingExecution.js (100%) rename Trading/TradingBot.js => LowFrequencyTrading/TradingOutput.js (76%) rename {Trading => LowFrequencyTrading}/TradingProcess.js (90%) rename {Trading => LowFrequencyTrading}/TradingRecords.js (100%) rename {Trading => LowFrequencyTrading}/TradingSimulation.js (100%) rename {Trading => LowFrequencyTrading}/TradingSystem.js (100%) rename SensorBotProcessMainLoop.js => SensorBot.js (99%) rename TradingBotProcessMainLoop.js => TradingBot.js (99%) diff --git a/IndicatorBot.js b/IndicatorBot.js index fc28afeae..3d2ab599f 100644 --- a/IndicatorBot.js +++ b/IndicatorBot.js @@ -1,167 +1,1131 @@ -exports.newIndicatorBot = function newIndicatorBot(bot, logger, UTILITIES, FILE_STORAGE) { +exports.newIndicatorBot = function newIndicatorBot(bot, parentLogger) { + const ROOT_DIR = './'; + + const MODULE_NAME = "Indicator Process Main Loop"; const FULL_LOG = true; - const LOG_FILE_CONTENT = false; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; - const MODULE_NAME = "Indicator Bot"; + let USER_BOT_MODULE; + let COMMONS_MODULE; + + const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket'); + const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily'); + const FILE_STORAGE = require('./FileStorage.js'); + let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); + + const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. + + let nextLoopTimeoutHandle; + let checkLoopHealthHandle; let thisObject = { initialize: initialize, - finalize: finalize, - start: start + run: run }; - let utilities = UTILITIES.newCloudUtilities(logger); - let fileStorage = FILE_STORAGE.newFileStorage(logger); - - const COMMONS = require('./Commons.js'); - let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE); + let processConfig; return thisObject; - function finalize() { - thisObject = undefined - utilities = undefined - fileStorage = undefined - commons = undefined - } - - function initialize(callBackFunction) { + function initialize(pProcessConfig, callBackFunction) { try { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - logger.fileName = MODULE_NAME; - logger.initialize(); + processConfig = pProcessConfig; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } + if (bot.definedByUI === true) { + /* The code of the bot is defined at the UI. No need to load a file with the code. */ + callBackFunction(global.DEFAULT_OK_RESPONSE); + return + } - callBackFunction(global.DEFAULT_OK_RESPONSE); + /* This bot is not ready for taking its code from the UI, then we need to load it from its repo. */ + let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName + filePath += "/User.Bot.js" + + fileStorage.getTextFile(filePath, onBotDownloaded); + + function onBotDownloaded(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> filePath = " + filePath); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + USER_BOT_MODULE = {} + USER_BOT_MODULE.newUserBot = eval(text); // TODO This needs to be changed function + + filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo; + filePath += "/Commons.js" + + fileStorage.getTextFile(filePath, onCommonsDownloaded); + + function onCommonsDownloaded(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + COMMONS_MODULE = {} + COMMONS_MODULE.newCommons = eval(text); // TODO This needs to be changed function + + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - function start(dataFiles, timeFrame, timeFrameLabel, currentDay, interExecutionMemory, callBackFunction) { + function run(callBackFunction) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - let products = {} - let mainDependency = {} - let processingDailyFiles + bot.enableCheckLoopHealth = true; - if (currentDay !== undefined) { - processingDailyFiles = true + let fixedTimeLoopIntervalHandle; + + /* Errors sent to the UI */ + bot.processError = processError + + /* Heartbeats sent to the UI */ + bot.processHeartBeat = processHeartBeat + + if (bot.runAtFixedInterval === true) { + fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); } else { - processingDailyFiles = false + loop(); } - /* The first phase here is about checking that we have everything we need at the definition level. */ - let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies - if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } + function loop() { - let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets - if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } + try { - /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - /* During the third phase, we need to generate the data of the different products this process produces an output */ - for (let i = 0; i < outputDatasets.length; i++) { - - let outputDatasetNode = outputDatasets[i] - let jsonData // Just build data as Json objects - let outputData // Data built as a result of applying user defined code and formulas at the Data Building Procedure - let singularVariableName // Name of the variable for this product - let recordDefinition // Record as defined by the user at the UI - let fileContent // Here we store the contents of the new data built just before writing it to a file. - let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. - - /* - For each outputDatasetNode in our process output, we will build the information based on our input products. - */ - - recordDefinition = outputDatasetNode.referenceParent.parentNode.record - singularVariableName = outputDatasetNode.referenceParent.parentNode.config.singularVariableName - - /* Check Irregular Periods */ - - if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode !== undefined) { - if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { - resultsWithIrregularPeriods = true + function pad(str, max) { + str = str.toString(); + return str.length < max ? pad(" " + str, max) : str; } - } - /* Build the data */ - jsonData = commons.dataBuildingProcedure( - products, - mainDependency, - recordDefinition, - outputDatasetNode.referenceParent.parentNode.dataBuilding, - singularVariableName, - outputDatasetNode.referenceParent.parentNode.config.codeName, - timeFrame, - timeFrameLabel, - resultsWithIrregularPeriods, - interExecutionMemory, - processingDailyFiles, - currentDay - ) + /* For each loop we want to create a new log file. */ - /* Add the calculated properties */ - if (outputDatasetNode.referenceParent.parentNode.calculations !== undefined) { - outputData = commons.calculationsProcedure(jsonData, recordDefinition, outputDatasetNode.referenceParent.parentNode.calculations, singularVariableName, timeFrame) - } else { - outputData = jsonData - } - products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] = outputData - } + if (logger !== undefined) { + logger.finalize() + } + logger = DEBUG_MODULE.newDebugLog(); + global.LOGGER_MAP.set(MODULE_NAME, logger) + logger.bot = bot; + logger.initialize(); - /*At the fourth and last phase, we will save the new information generated into files corresponding to each output outputDatasetNode.*/ - let totalFilesWritten = 0 - for (let i = 0; i < outputDatasets.length; i++) { - let outputDatasetNode = outputDatasets[i] - let outputData = products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] - let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. - let contextSummary = {} + bot.loopCounter++; + bot.loopStartTime = new Date().valueOf(); - /* Check Irregular Periods */ - if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { - resultsWithIrregularPeriods = true - } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - /* Simplifying the access to basic info */ + /* We tell the UI that we are running. */ + processHeartBeat() - contextSummary.dataset = outputDatasetNode.referenceParent.config.codeName - contextSummary.product = outputDatasetNode.referenceParent.parentNode.config.codeName - contextSummary.bot = outputDatasetNode.referenceParent.parentNode.parentNode.config.codeName - contextSummary.dataMine = outputDatasetNode.referenceParent.parentNode.parentNode.parentNode.config.codeName + /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - /* This stuff is still hardcoded and unresolved. */ - contextSummary.botVersion = { - "major": 1, - "minor": 0 - } - contextSummary.dataSetVersion = "dataSet.V1" + const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); + const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); + const DATA_SET = require(ROOT_DIR + 'DataSet'); + const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); - let fileContent = commons.generateFileContent(outputData, outputDatasetNode.referenceParent.parentNode.record, resultsWithIrregularPeriods, processingDailyFiles, currentDay, callBackFunction) - commons.writeFile(contextSummary, fileContent, anotherFileWritten, processingDailyFiles, timeFrameLabel, currentDay, callBackFunction) - } + /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ + bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. - function anotherFileWritten() { - totalFilesWritten++ - if (totalFilesWritten === outputDatasets.length) { - callBackFunction(global.DEFAULT_OK_RESPONSE); + /* High level log entry */ + + console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + + " Main Loop # " + pad(Number(bot.loopCounter), 8)) + + /* We will prepare first the infraestructure needed for the bot to run. There are 3 modules we need to sucessfullly initialize first. */ + + let processExecutionEvents + let userBot; + let processFramework; + let statusDependencies; + let dataDependencies; + + let nextWaitTime; + + initializeProcessExecutionEvents(); + + function initializeProcessExecutionEvents() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } + + processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) + + processExecutionEvents.initialize(onInizialized); + + function onInizialized(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); + startProcessExecutionEvents() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function startProcessExecutionEvents() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } + + processExecutionEvents.start(onStarted); + + function onStarted(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); + + if (global.STOP_TASK_GRACEFULLY === true) { + loopControl() + return + } + + initializeStatusDependencies(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessExecutionEvents -> onStarted -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function initializeStatusDependencies() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } + + statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); + + statusDependencies.initialize(onInizialized); + + function onInizialized(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); + initializeDataDependencies(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeStatusDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function initializeDataDependencies() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } + + dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); + + dataDependencies.initialize(onInizialized); + + function onInizialized(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } + + /* If the process is configured to run inside a framework, we continue there, otherwise we run the bot directly. */ + if (processConfig.framework === undefined) { + initializeUserBot(); + return; + } + + switch (processConfig.framework.name) { + case 'Multi-Period-Market': { + processFramework = MULTI_PERIOD_MARKET.newMultiPeriodMarket(bot, logger, UTILITIES, FILE_STORAGE); + intitializeProcessFramework(); + break; + } + case 'Multi-Period-Daily': { + processFramework = MULTI_PERIOD_DAILY.newMultiPeriodDaily(bot, logger, UTILITIES, FILE_STORAGE); + intitializeProcessFramework(); + break; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeDataDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function initializeUserBot() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> Entering function."); } + + usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE); + + usertBot.initialize(statusDependencies, onInizialized); + + function onInizialized(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Execution finished well."); + startUserBot(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeUserBot -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function startUserBot() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } + + usertBot.start(onFinished); + + function onFinished(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Execution finished well."); + finishProcessExecutionEvents() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + case "Dependency does not exist.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency does not exist. This Loop will go to sleep."); + nextWaitTime = 'Sleep'; + loopControl(nextWaitTime); + return; + } + case "Dependency not ready.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency not ready. Will Retry Later."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled custom response received. -> err = " + err.message); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function intitializeProcessFramework() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } + + processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); + + function onInizialized(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); + startProcessFramework(); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> intitializeProcessFramework -> onInizialized -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function startProcessFramework() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } + + processFramework.start(onFinished); + + function onFinished(err) { + + try { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } + processFramework.finalize() + processFramework = undefined + dataDependencies.finalize() + dataDependencies = undefined + statusDependencies.finalize() + statusDependencies = undefined + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Execution finished well."); + finishProcessExecutionEvents() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + case global.CUSTOM_OK_RESPONSE.result: { + + switch (err.message) { + case "Dependency does not exist.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency does not exist. This Loop will go to sleep."); + nextWaitTime = 'Sleep'; + loopControl(nextWaitTime); + return; + } + case "Dependency not ready.": { + logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency not ready. This Loop will go to sleep."); + nextWaitTime = 'Sleep'; + loopControl(nextWaitTime); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + } + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function finishProcessExecutionEvents() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } + + processExecutionEvents.finish(onFinished); + + function onFinished(err) { + try { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } + + processExecutionEvents.finalize() + processExecutionEvents = undefined + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); + nextWaitTime = 'Normal'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write(MODULE_NAME, "[WARN] run -> loop -> finishProcessExecutionEvents -> onFinished -> Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + processStopped() + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + return; + } + default: { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); + + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); + } + } + + function loopControl(nextWaitTime) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } + + /* We show we reached the end of the loop. */ + + processHeartBeat() + + /* Here we check if we must stop the loop gracefully. */ + + shallWeStop(onStop, onContinue); + + function onStop() { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } + + if (global.WRITE_LOGS_TO_FILES === 'true') { + logger.persist(); + } + + processStopped() + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + + } + + function onContinue() { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } + + /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ + + switch (nextWaitTime) { + case 'Normal': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } + if (processConfig.deadWaitTime > 0) { + checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); + } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); + processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") + if (global.WRITE_LOGS_TO_FILES === 'true') { + logger.persist(); + } + } + break; + case 'Retry': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } + if (processConfig.deadWaitTime > 0) { + checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); + } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); + processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") + logger.persist(); + } + break; + case 'Sleep': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); + processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") + logger.persist(); + } + break; + case 'Coma': { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } + nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); + processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") + logger.persist(); + } + break; + } + } + } + + function checkLoopHealth(pLastLoop) { + + if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { + + logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); + + return; + } // This gets disabled anytime the Main Loop is shut down by any condition. + + if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. + + let now = new Date().valueOf(); + + if (now - bot.loopStartTime > processConfig.deadWaitTime) { + + logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); + console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); + + logger.persist(); // We persist the logs of the failed execution. + clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. + loop(); // We restart the loop so that the processing can continue. + + } else { + + console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); + + } + } + } + + function shallWeStop(stopCallBack, continueCallBack) { + + try { + + /* IMPORTANT: This function is exactly the same on the 3 modules. */ + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } + + if (!global.STOP_TASK_GRACEFULLY) { + continueCallBack(); + } else { + stopCallBack(); + } + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + } catch (err) { + parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + callBackFunction(err); } } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + + function processHeartBeat(processingDate, percentage, status) { + let event = { + seconds: (new Date()).getSeconds(), + processingDate: processingDate, + percentage: percentage, + status: status + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) + } + + function processError(node, errorMessage) { + let event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) + } + + function processStopped() { + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + } + + } catch (err) { + parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/IndicatorBotProcessMainLoop.js b/IndicatorBotProcessMainLoop.js deleted file mode 100644 index dbf0c3b82..000000000 --- a/IndicatorBotProcessMainLoop.js +++ /dev/null @@ -1,1122 +0,0 @@ -exports.newIndicatorBotProcessMainLoop = function newIndicatorBotProcessMainLoop(bot, parentLogger) { - - const ROOT_DIR = './'; - - const MODULE_NAME = "Indicator Process Main Loop"; - const FULL_LOG = true; - - let USER_BOT_MODULE; - let COMMONS_MODULE; - - const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket'); - const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily'); - const FILE_STORAGE = require('./FileStorage.js'); - let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); - - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); - let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. - - let nextLoopTimeoutHandle; - let checkLoopHealthHandle; - - let thisObject = { - initialize: initialize, - run: run - }; - - let processConfig; - - return thisObject; - - function initialize(pProcessConfig, callBackFunction) { - - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - processConfig = pProcessConfig; - - if (bot.definedByUI === true) { - /* The code of the bot is defined at the UI. No need to load a file with the code. */ - callBackFunction(global.DEFAULT_OK_RESPONSE); - return - } - - /* This bot is not ready for taking its code from the UI, then we need to load it from its repo. */ - - let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName - filePath += "/User.Bot.js" - - fileStorage.getTextFile(filePath, onBotDownloaded); - - function onBotDownloaded(err, text) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> filePath = " + filePath); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - USER_BOT_MODULE = {} - USER_BOT_MODULE.newUserBot = eval(text); // TODO This needs to be changed function - - filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo; - filePath += "/Commons.js" - - fileStorage.getTextFile(filePath, onCommonsDownloaded); - - function onCommonsDownloaded(err, text) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - COMMONS_MODULE = {} - COMMONS_MODULE.newCommons = eval(text); // TODO This needs to be changed function - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function run(callBackFunction) { - - try { - - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - - bot.enableCheckLoopHealth = true; - - let fixedTimeLoopIntervalHandle; - - /* Errors sent to the UI */ - bot.processError = processError - - /* Heartbeats sent to the UI */ - bot.processHeartBeat = processHeartBeat - - if (bot.runAtFixedInterval === true) { - fixedTimeLoopIntervalHandle = setInterval(loop, bot.fixedInterval); - } else { - loop(); - } - - function loop() { - - try { - - function pad(str, max) { - str = str.toString(); - return str.length < max ? pad(" " + str, max) : str; - } - - /* For each loop we want to create a new log file. */ - - if (logger !== undefined) { - logger.finalize() - } - logger = DEBUG_MODULE.newDebugLog(); - global.LOGGER_MAP.set(MODULE_NAME, logger) - logger.bot = bot; - logger.initialize(); - - bot.loopCounter++; - bot.loopStartTime = new Date().valueOf(); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - - /* We tell the UI that we are running. */ - processHeartBeat() - - /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); - const DATA_SET = require(ROOT_DIR + 'DataSet'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); - - /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. - - /* High level log entry */ - - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) - + " Main Loop # " + pad(Number(bot.loopCounter), 8)) - - /* We will prepare first the infraestructure needed for the bot to run. There are 3 modules we need to sucessfullly initialize first. */ - - let processExecutionEvents - let userBot; - let processFramework; - let statusDependencies; - let dataDependencies; - - let nextWaitTime; - - initializeProcessExecutionEvents(); - - function initializeProcessExecutionEvents() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - - processExecutionEvents.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); - startProcessExecutionEvents() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function startProcessExecutionEvents() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - - processExecutionEvents.start(onStarted); - - function onStarted(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); - - if (global.STOP_TASK_GRACEFULLY === true) { - loopControl() - return - } - - initializeStatusDependencies(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessExecutionEvents -> onStarted -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeStatusDependencies() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - - statusDependencies.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); - initializeDataDependencies(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeStatusDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeDataDependencies() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } - - dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); - - dataDependencies.initialize(onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } - - /* If the process is configured to run inside a framework, we continue there, otherwise we run the bot directly. */ - if (processConfig.framework === undefined) { - initializeUserBot(); - return; - } - - switch (processConfig.framework.name) { - case 'Multi-Period-Market': { - processFramework = MULTI_PERIOD_MARKET.newMultiPeriodMarket(bot, logger, UTILITIES, FILE_STORAGE); - intitializeProcessFramework(); - break; - } - case 'Multi-Period-Daily': { - processFramework = MULTI_PERIOD_DAILY.newMultiPeriodDaily(bot, logger, UTILITIES, FILE_STORAGE); - intitializeProcessFramework(); - break; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeDataDependencies -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function initializeUserBot() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> Entering function."); } - - usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE); - - usertBot.initialize(statusDependencies, onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Execution finished well."); - startUserBot(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> initializeUserBot -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function startUserBot() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } - - usertBot.start(onFinished); - - function onFinished(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Execution finished well."); - finishProcessExecutionEvents() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - case "Dependency does not exist.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency does not exist. This Loop will go to sleep."); - nextWaitTime = 'Sleep'; - loopControl(nextWaitTime); - return; - } - case "Dependency not ready.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startUserBot -> onFinished -> Dependency not ready. Will Retry Later."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function intitializeProcessFramework() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } - - processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); - - function onInizialized(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); - startProcessFramework(); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> intitializeProcessFramework -> onInizialized -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function startProcessFramework() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } - - processFramework.start(onFinished); - - function onFinished(err) { - - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } - processFramework.finalize() - processFramework = undefined - dataDependencies.finalize() - dataDependencies = undefined - statusDependencies.finalize() - statusDependencies = undefined - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Execution finished well."); - finishProcessExecutionEvents() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - case "Dependency does not exist.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency does not exist. This Loop will go to sleep."); - nextWaitTime = 'Sleep'; - loopControl(nextWaitTime); - return; - } - case "Dependency not ready.": { - logger.write(MODULE_NAME, "[WARN] run -> loop -> startProcessFramework -> onFinished -> Dependency not ready. This Loop will go to sleep."); - nextWaitTime = 'Sleep'; - loopControl(nextWaitTime); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function finishProcessExecutionEvents() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - - processExecutionEvents.finish(onFinished); - - function onFinished(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - - processExecutionEvents.finalize() - processExecutionEvents = undefined - - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Execution finished well."); - nextWaitTime = 'Normal'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write(MODULE_NAME, "[WARN] run -> loop -> finishProcessExecutionEvents -> onFinished -> Retry Later. Requesting Execution Retry."); - nextWaitTime = 'Retry'; - loopControl(nextWaitTime); - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } - - /* We show we reached the end of the loop. */ - - processHeartBeat() - - /* Here we check if we must stop the loop gracefully. */ - - shallWeStop(onStop, onContinue); - - function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - - if (global.WRITE_LOGS_TO_FILES === 'true') { - logger.persist(); - } - - processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - - switch (nextWaitTime) { - case 'Normal': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") - if (global.WRITE_LOGS_TO_FILES === 'true') { - logger.persist(); - } - } - break; - case 'Retry': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); - processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") - logger.persist(); - } - break; - case 'Sleep': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.sleepWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.sleepWaitTime / 60000 + " minutes for next execution.") - logger.persist(); - } - break; - case 'Coma': { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } - nextLoopTimeoutHandle = setTimeout(loop, processConfig.comaWaitTime); - processHeartBeat(undefined, undefined, "Waiting " + processConfig.comaWaitTime / 3600000 + " hours for next execution.") - logger.persist(); - } - break; - } - } - } - - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); - - } - } - } - - function shallWeStop(stopCallBack, continueCallBack) { - - try { - - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - - if (!global.STOP_TASK_GRACEFULLY) { - continueCallBack(); - } else { - stopCallBack(); - } - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - } - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - } - } - - function processHeartBeat(processingDate, percentage, status) { - let event = { - seconds: (new Date()).getSeconds(), - processingDate: processingDate, - percentage: percentage, - status: status - } - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) - } - - function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') - } - - } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } -}; diff --git a/IndicatorOutput.js b/IndicatorOutput.js new file mode 100644 index 000000000..1f1e62182 --- /dev/null +++ b/IndicatorOutput.js @@ -0,0 +1,168 @@ +exports.newIndicatorOutput = function newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) { + + const FULL_LOG = true; + const LOG_FILE_CONTENT = false; + const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; + + const MODULE_NAME = "Indicator Bot"; + + let thisObject = { + initialize: initialize, + finalize: finalize, + start: start + }; + + let utilities = UTILITIES.newCloudUtilities(logger); + let fileStorage = FILE_STORAGE.newFileStorage(logger); + + const COMMONS = require('./Commons.js'); + let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE); + + return thisObject; + + function finalize() { + thisObject = undefined + utilities = undefined + fileStorage = undefined + commons = undefined + } + + function initialize(callBackFunction) { + + try { + + logger.fileName = MODULE_NAME; + logger.initialize(); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function start(dataFiles, timeFrame, timeFrameLabel, currentDay, interExecutionMemory, callBackFunction) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } + + let products = {} + let mainDependency = {} + let processingDailyFiles + + if (currentDay !== undefined) { + processingDailyFiles = true + } else { + processingDailyFiles = false + } + + /* The first phase here is about checking that we have everything we need at the definition level. */ + let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies + if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } + + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets + if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } + + /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + + /* During the third phase, we need to generate the data of the different products this process produces an output */ + for (let i = 0; i < outputDatasets.length; i++) { + + let outputDatasetNode = outputDatasets[i] + let jsonData // Just build data as Json objects + let outputData // Data built as a result of applying user defined code and formulas at the Data Building Procedure + let singularVariableName // Name of the variable for this product + let recordDefinition // Record as defined by the user at the UI + let fileContent // Here we store the contents of the new data built just before writing it to a file. + let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. + + /* + For each outputDatasetNode in our process output, we will build the information based on our input products. + */ + + recordDefinition = outputDatasetNode.referenceParent.parentNode.record + singularVariableName = outputDatasetNode.referenceParent.parentNode.config.singularVariableName + + /* Check Irregular Periods */ + + if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode !== undefined) { + if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { + resultsWithIrregularPeriods = true + } + } + + /* Build the data */ + jsonData = commons.dataBuildingProcedure( + products, + mainDependency, + recordDefinition, + outputDatasetNode.referenceParent.parentNode.dataBuilding, + singularVariableName, + outputDatasetNode.referenceParent.parentNode.config.codeName, + timeFrame, + timeFrameLabel, + resultsWithIrregularPeriods, + interExecutionMemory, + processingDailyFiles, + currentDay + ) + + /* Add the calculated properties */ + if (outputDatasetNode.referenceParent.parentNode.calculations !== undefined) { + outputData = commons.calculationsProcedure(jsonData, recordDefinition, outputDatasetNode.referenceParent.parentNode.calculations, singularVariableName, timeFrame) + } else { + outputData = jsonData + } + products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] = outputData + } + + /*At the fourth and last phase, we will save the new information generated into files corresponding to each output outputDatasetNode.*/ + let totalFilesWritten = 0 + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let outputData = products[outputDatasetNode.referenceParent.parentNode.config.pluralVariableName] + let resultsWithIrregularPeriods // A product will have irregular periods when the User Code inserts new result records at will, in contrast with normal procedure where the platform insert one record per loop execution. + let contextSummary = {} + + /* Check Irregular Periods */ + if (outputDatasetNode.referenceParent.parentNode.dataBuilding.loop.javascriptCode.code.indexOf('results.push') >= 0) { + resultsWithIrregularPeriods = true + } + + /* Simplifying the access to basic info */ + + contextSummary.dataset = outputDatasetNode.referenceParent.config.codeName + contextSummary.product = outputDatasetNode.referenceParent.parentNode.config.codeName + contextSummary.bot = outputDatasetNode.referenceParent.parentNode.parentNode.config.codeName + contextSummary.dataMine = outputDatasetNode.referenceParent.parentNode.parentNode.parentNode.config.codeName + + /* This stuff is still hardcoded and unresolved. */ + contextSummary.botVersion = { + "major": 1, + "minor": 0 + } + contextSummary.dataSetVersion = "dataSet.V1" + + let fileContent = commons.generateFileContent(outputData, outputDatasetNode.referenceParent.parentNode.record, resultsWithIrregularPeriods, processingDailyFiles, currentDay, callBackFunction) + commons.writeFile(contextSummary, fileContent, anotherFileWritten, processingDailyFiles, timeFrameLabel, currentDay, callBackFunction) + } + + + function anotherFileWritten() { + totalFilesWritten++ + if (totalFilesWritten === outputDatasets.length) { + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } +}; diff --git a/Trading/Announcements.js b/LowFrequencyTrading/Announcements.js similarity index 100% rename from Trading/Announcements.js rename to LowFrequencyTrading/Announcements.js diff --git a/Trading/Snapshots.js b/LowFrequencyTrading/Snapshots.js similarity index 100% rename from Trading/Snapshots.js rename to LowFrequencyTrading/Snapshots.js diff --git a/Trading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js similarity index 100% rename from Trading/TradingEngine.js rename to LowFrequencyTrading/TradingEngine.js diff --git a/Trading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js similarity index 100% rename from Trading/TradingExecution.js rename to LowFrequencyTrading/TradingExecution.js diff --git a/Trading/TradingBot.js b/LowFrequencyTrading/TradingOutput.js similarity index 76% rename from Trading/TradingBot.js rename to LowFrequencyTrading/TradingOutput.js index b3eacbdef..1d526efaa 100644 --- a/Trading/TradingBot.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -1,4 +1,4 @@ -exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STORAGE) { +exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FILE_STORAGE) { const FULL_LOG = true const MODULE_NAME = 'Trading Bot' @@ -11,9 +11,6 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR let utilities = UTILITIES.newCloudUtilities(logger) let fileStorage = FILE_STORAGE.newFileStorage(logger) - const COMMONS = require('../Commons.js') - let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) - return thisObject function finalize() { @@ -35,7 +32,7 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } } - function start(multiPeriodDataFiles, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { + function start(chart, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { try { bot.processingDailyFiles if (timeFrame > global.dailyFilePeriods[0][0]) { @@ -43,61 +40,8 @@ exports.newTradingBot = function newTradingBot(bot, logger, UTILITIES, FILE_STOR } else { bot.processingDailyFiles = true } - let chart = {} - let mainDependency = {} - /* The first phase here is about checking that we have everything we need at the definition level. */ - let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies - if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } - - let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets - if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } - - /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ - for (let j = 0; j < global.marketFilesPeriods.length; j++) { - let timeFrameLabel = marketFilesPeriods[j][1] - let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) - let products = {} - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Inflating Data Files for timeFrame = ' + timeFrameLabel) } - - if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - let propertyName = 'at' + timeFrameLabel.replace('-', '') - chart[propertyName] = products - } - } - - for (let j = 0; j < global.dailyFilePeriods.length; j++) { - let timeFrameLabel = global.dailyFilePeriods[j][1] - let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) - let products = {} - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Inflating Data Files for timeFrame = ' + timeFrameLabel) } - - if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - let propertyName = 'at' + timeFrameLabel.replace('-', '') - chart[propertyName] = products - } - } - - /* Single Files */ - let dataFiles = multiPeriodDataFiles.get('Single Files') - let products = {} - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Inflating Data Files from Single Files.') } - - if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) - - let propertyName = 'atAnyTimeFrame' - chart[propertyName] = products - } - - /* Simulation */ + /* Preparing everything for the Simulation */ const TRADING_SIMULATION = require('./TradingSimulation.js') let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) diff --git a/Trading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js similarity index 90% rename from Trading/TradingProcess.js rename to LowFrequencyTrading/TradingProcess.js index a8dd2e4f6..8c0c259cf 100644 --- a/Trading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -1,4 +1,4 @@ -exports.newTradingProcess = function newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS) { +exports.newTradingProcess = function newTradingProcess(bot, logger, UTILITIES, TRADING_OUTPUT_MODULE, USER_BOT_COMMONS) { /* This Module will load all the process data dependencies from files and send them to the Trading Bot. @@ -23,7 +23,7 @@ let dataFiles = new Map(); let multiPeriodDataFiles = new Map(); - let botInstance; + let tradingOutputModule; const FILE_STORAGE = require('../FileStorage.js'); let fileStorage = FILE_STORAGE.newFileStorage(logger); @@ -41,9 +41,9 @@ dataDependenciesModule = pDataDependencies; processConfig = pProcessConfig; - let USER_BOT_MODULE = require("./TradingBot") - botInstance = USER_BOT_MODULE.newTradingBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(callBackFunction); + let TRADING_OUTPUT_MODULE = require("./TradingOutput") + tradingOutputModule = TRADING_OUTPUT_MODULE.newTradingOutput(bot, logger, UTILITIES, FILE_STORAGE); + tradingOutputModule.initialize(callBackFunction); } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); @@ -56,8 +56,8 @@ multiPeriodDataFiles = undefined statusDependencies = undefined dataDependenciesModule = undefined - botInstance.finalize() - botInstance = undefined + tradingOutputModule.finalize() + tradingOutputModule = undefined fileStorage = undefined processConfig = undefined thisObject = undefined @@ -360,7 +360,7 @@ function processDailyFiles() { let n = 0; if (currentTimeFrame) { - callTheBot() + buildCharts() return } bot.tradingProcessDate = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. @@ -528,7 +528,7 @@ } else { n = 0; if (currentTimeFrame !== undefined) { - callTheBot(); + buildCharts(); } else { logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> Time Frame not Recognized. Can not continue."); callBackFunction(global.DEFAULT_FAIL_RESPONSE); @@ -537,16 +537,67 @@ } } - function callTheBot() { - botInstance.start( - multiPeriodDataFiles, + function buildCharts() { + const COMMONS = require('../Commons.js') + let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) + + let chart = {} + let mainDependency = {} + + /* The first phase here is about checking that we have everything we need at the definition level. */ + let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies + if (commons.validateDataDependencies(dataDependencies, callBackFunction) !== true) { return } + + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets + if (commons.validateOutputDatasets(outputDatasets, callBackFunction) !== true) { return } + + /* The second phase is about transforming the inputs into a format that can be used to apply the user defined code. */ + for (let j = 0; j < global.marketFilesPeriods.length; j++) { + let timeFrameLabel = marketFilesPeriods[j][1] + let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) + let products = {} + + if (dataFiles !== undefined) { + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + + let propertyName = 'at' + timeFrameLabel.replace('-', '') + chart[propertyName] = products + } + } + + for (let j = 0; j < global.dailyFilePeriods.length; j++) { + let timeFrameLabel = global.dailyFilePeriods[j][1] + let dataFiles = multiPeriodDataFiles.get(timeFrameLabel) + let products = {} + + if (dataFiles !== undefined) { + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + + let propertyName = 'at' + timeFrameLabel.replace('-', '') + chart[propertyName] = products + } + } + + /* Single Files */ + let dataFiles = multiPeriodDataFiles.get('Single Files') + let products = {} + + if (dataFiles !== undefined) { + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + + let propertyName = 'atAnyTimeFrame' + chart[propertyName] = products + } + + tradingOutputModule.start( + chart, currentTimeFrame, currentTimeFrameLabel, bot.tradingProcessDate, simulationState, - onBotFinished); + onOutputGenerated); - function onBotFinished(err) { + function onOutputGenerated(err) { try { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { callBackFunction(err); @@ -567,14 +618,14 @@ try { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onWritten -> err = " + err.stack); callBackFunction(err); return; } writeDailyStatusReport(bot.tradingProcessDate, onDailyStatusReport); } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> onWritten -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onWritten -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -590,7 +641,7 @@ const now = new Date() if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> callTheBot -> onBotFinished -> onMarketStatusReport -> " + logText); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onMarketStatusReport -> " + logText); } bot.STOP_SESSION = true callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -600,7 +651,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> callTheBot -> onBotFinished -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/Trading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js similarity index 100% rename from Trading/TradingRecords.js rename to LowFrequencyTrading/TradingRecords.js diff --git a/Trading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js similarity index 100% rename from Trading/TradingSimulation.js rename to LowFrequencyTrading/TradingSimulation.js diff --git a/Trading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js similarity index 100% rename from Trading/TradingSystem.js rename to LowFrequencyTrading/TradingSystem.js diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index 096df4c62..da6266000 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -18,7 +18,7 @@ let dataDependenciesModule; let dataFiles = new Map; - let botInstance; + let indicatorOutputModule; let fileStorage = FILE_STORAGE.newFileStorage(logger); @@ -42,10 +42,10 @@ dataDependenciesModule = pDataDependencies; processConfig = pProcessConfig; - let USER_BOT_MODULE = require("./IndicatorBot") + let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - botInstance = USER_BOT_MODULE.newIndicatorBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(callBackFunction); + indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE); + indicatorOutputModule.initialize(callBackFunction); } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); @@ -57,7 +57,7 @@ dataFiles = undefined statusDependencies = undefined dataDependenciesModule = undefined - botInstance = undefined + indicatorOutputModule = undefined fileStorage = undefined processConfig = undefined thisObject = undefined @@ -574,7 +574,7 @@ } else { - callTheBot(); + generateOutput(); } } @@ -584,28 +584,28 @@ } } - function callTheBot() { + function generateOutput() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> Entering function."); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> Entering function."); } const timeFrame = global.dailyFilePeriods[n][0]; const timeFrameLabel = global.dailyFilePeriods[n][1]; - botInstance.start( + indicatorOutputModule.start( dataFiles, timeFrame, timeFrameLabel, bot.multiPeriodDailyProcessDatetime, interExecutionMemoryArray[n], - onBotFinished); + onOutputGenerated); - function onBotFinished(err) { + function onOutputGenerated(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> Entering function."); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> Entering function."); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { @@ -617,13 +617,13 @@ periodsControlLoop(); } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js index 11bc7ae40..0464b5d30 100644 --- a/MultiPeriodMarket.js +++ b/MultiPeriodMarket.js @@ -16,7 +16,7 @@ let dataDependenciesModule; let dataFiles = new Map(); - let botInstance; + let indicatorOutputModule; let processConfig; @@ -35,10 +35,10 @@ dataDependenciesModule = pDataDependencies; processConfig = pProcessConfig; - let USER_BOT_MODULE = require("./IndicatorBot") + let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - botInstance = USER_BOT_MODULE.newIndicatorBot(bot, logger, UTILITIES, FILE_STORAGE); - botInstance.initialize(callBackFunction); + indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE); + indicatorOutputModule.initialize(callBackFunction); } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); @@ -50,7 +50,7 @@ dataFiles = undefined statusDependencies = undefined dataDependenciesModule = undefined - botInstance = undefined + indicatorOutputModule = undefined processConfig = undefined thisObject = undefined } @@ -200,7 +200,7 @@ } else { - callTheBot(); + generateOutput(); } } @@ -210,28 +210,28 @@ } } - function callTheBot() { + function generateOutput() { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> Entering function."); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> Entering function."); } const timeFrame = global.marketFilesPeriods[n][0]; const timeFrameLabel = global.marketFilesPeriods[n][1]; - botInstance.start( + indicatorOutputModule.start( dataFiles, timeFrame, timeFrameLabel, undefined, undefined, - onBotFinished); + onOutputGenerated); - function onBotFinished(err) { + function onOutputGenerated(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> Entering function."); } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> Entering function."); } if (err.result !== global.DEFAULT_OK_RESPONSE.result) { @@ -242,13 +242,13 @@ periodsControlLoop(); } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> onBotFinished -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> callTheBot -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/Root.js b/Root.js index f3411d1ba..329f2eda7 100644 --- a/Root.js +++ b/Root.js @@ -132,9 +132,9 @@ /* Process Loops Declarations. */ - const INDICATOR_BOT_MAIN_LOOP_MODULE = require('./IndicatorBotProcessMainLoop'); - const SENSOR_BOT_MAIN_LOOP_MODULE = require('./SensorBotProcessMainLoop'); - const TRADING_ENGINE_MAIN_LOOP_MODULE = require('./TradingBotProcessMainLoop'); + const INDICATOR_BOT_MODULE = require('./IndicatorBot'); + const SENSOR_BOT = require('./SensorBot'); + const TRADING_BOT_MODULE = require('./TradingBot'); let botConfig; let processInstance = global.TASK_NODE.bot.processes[processIndex] @@ -144,7 +144,7 @@ processInstance.referenceParent.config.framework !== undefined && (processInstance.referenceParent.config.framework.name === 'Multi-Period-Market' || processInstance.referenceParent.config.framework.name === 'Multi-Period-Daily' || - processInstance.referenceParent.config.framework.name === 'Trading-Process') + processInstance.referenceParent.config.framework.name === 'Low-Frequency-Trading-Process') ) { botConfig = processInstance.referenceParent.parentNode.config botConfig.definedByUI = true @@ -396,14 +396,14 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> Entering function."); } - let extractionBotMainLoop = SENSOR_BOT_MAIN_LOOP_MODULE.newSensorBotProcessMainLoop(pBotConfig, logger); - extractionBotMainLoop.initialize(pProcessConfig, onInitializeReady); + let sensorBot = SENSOR_BOT.newSensorBot(pBotConfig, logger); + sensorBot.initialize(pProcessConfig, onInitializeReady); function onInitializeReady(err) { if (err.result === global.DEFAULT_OK_RESPONSE.result) { - extractionBotMainLoop.run(whenRunFinishes); + sensorBot.run(whenRunFinishes); function whenRunFinishes(err) { @@ -461,14 +461,14 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runIndicatorBot -> Entering function."); } - let indicatorBotMainLoop = INDICATOR_BOT_MAIN_LOOP_MODULE.newIndicatorBotProcessMainLoop(pBotConfig, logger); - indicatorBotMainLoop.initialize(pProcessConfig, onInitializeReady); + let indicatorBot = INDICATOR_BOT_MODULE.newIndicatorBot(pBotConfig, logger); + indicatorBot.initialize(pProcessConfig, onInitializeReady); function onInitializeReady(err) { if (err.result === global.DEFAULT_OK_RESPONSE.result) { - indicatorBotMainLoop.run(whenRunFinishes); + indicatorBot.run(whenRunFinishes); function whenRunFinishes(err) { @@ -524,14 +524,14 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runTradingBot -> Entering function."); } - let tradingBotMainLoop = TRADING_ENGINE_MAIN_LOOP_MODULE.newTradingBotProcessMainLoop(pBotConfig, logger); - tradingBotMainLoop.initialize(pProcessConfig, onInitializeReady); + let tradingBot = TRADING_BOT_MODULE.newTradingBot(pBotConfig, logger); + tradingBot.initialize(pProcessConfig, onInitializeReady); function onInitializeReady(err) { if (err.result === global.DEFAULT_OK_RESPONSE.result) { - tradingBotMainLoop.run(whenRunFinishes); + tradingBot.run(whenRunFinishes); function whenRunFinishes(err) { diff --git a/SensorBotProcessMainLoop.js b/SensorBot.js similarity index 99% rename from SensorBotProcessMainLoop.js rename to SensorBot.js index 5502fc340..a40fc6203 100644 --- a/SensorBotProcessMainLoop.js +++ b/SensorBot.js @@ -1,4 +1,4 @@ -exports.newSensorBotProcessMainLoop = function newSensorBotProcessMainLoop(bot, parentLogger) { +exports.newSensorBot = function newSensorBot(bot, parentLogger) { const ROOT_DIR = './'; @@ -825,6 +825,16 @@ global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Heartbeat', event) } + function processError(node, errorMessage) { + let event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) + } + function processStopped() { global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') } diff --git a/TradingBotProcessMainLoop.js b/TradingBot.js similarity index 99% rename from TradingBotProcessMainLoop.js rename to TradingBot.js index c2dbd8deb..0431e9bc3 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBot.js @@ -1,4 +1,4 @@ -exports.newTradingBotProcessMainLoop = function newTradingBotProcessMainLoop(bot, parentLogger) { +exports.newTradingBot = function newTradingBot(bot, parentLogger) { const ROOT_DIR = './'; @@ -8,7 +8,7 @@ let USER_BOT_MODULE; let USER_BOT_COMMONS; - const TRADING_PROCESS_MODULE = require(ROOT_DIR + '/Trading/TradingProcess.js'); + const TRADING_PROCESS_MODULE = require(ROOT_DIR + '/LowFrequencyTrading/TradingProcess.js'); const FILE_STORAGE = require('./FileStorage.js'); const SESSION = require(ROOT_DIR + 'Session'); @@ -400,7 +400,7 @@ case global.DEFAULT_OK_RESPONSE.result: { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } switch (processConfig.framework.name) { - case 'Trading-Process': { + case 'Low-Frequency-Trading-Process': { processFramework = TRADING_PROCESS_MODULE.newTradingProcess(bot, logger, UTILITIES, USER_BOT_MODULE, USER_BOT_COMMONS); intitializeProcessFramework(); break; From 17a15c1501af1950b54b39d4614ec0346e9e366a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 11 Jun 2020 13:38:44 +0200 Subject: [PATCH 033/113] VALUE TO USE erradicated --- LowFrequencyTrading/TradingEngine.js | 10 +++++----- LowFrequencyTrading/TradingProcess.js | 14 +++++++------- LowFrequencyTrading/TradingSystem.js | 4 ++-- Session.js | 8 ++++++-- TelegramBot.js | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index f88e031cd..3fa6fdd64 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -187,7 +187,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.current.position.rate.value = getPositionRate() /* We take what was calculated at the formula and apply the slippage. */ - let slippageAmount = tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.slippage.positionRate / 100 + let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount @@ -213,12 +213,12 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let feePaid = 0 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value } else { - feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value @@ -240,13 +240,13 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid tradingEngine.current.balance.quotedAsset.value = 0 } else { - feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.VALUES_TO_USE.feeStructure.taker / 100 + feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid tradingEngine.current.balance.baseAsset.value = 0 diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 8c0c259cf..ae13fc7b0 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -90,7 +90,7 @@ let statusReport; /* We are going to use the start date as beging of market date. */ - contextVariables.dateBeginOfMarket = bot.VALUES_TO_USE.timeRange.initialDatetime + contextVariables.dateBeginOfMarket = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime) /* Here we get the status report from the bot who knows which is the end of the market. @@ -281,7 +281,7 @@ let dependencyIndex = 0; dataFiles = new Map(); - if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { + if (bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel) { currentTimeFrame = global.marketFilesPeriods[n][0]; currentTimeFrameLabel = global.marketFilesPeriods[n][1]; } @@ -299,7 +299,7 @@ } if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + if (!(bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { dependencyControlLoop(); return } @@ -374,7 +374,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } /* Validation that we are not going past the user defined end date. */ - if (bot.tradingProcessDate.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + if (bot.tradingProcessDate.valueOf() >= bot.SESSION.parameters.timeRange.config.finalDatetime) { const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } @@ -442,7 +442,7 @@ } } - if (bot.VALUES_TO_USE.timeFrame === timeFrameLabel) { + if (bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel) { currentTimeFrame = global.dailyFilePeriods[n][0]; currentTimeFrameLabel = global.dailyFilePeriods[n][1]; } @@ -464,7 +464,7 @@ } if (dataDependenciesModule.isItADepenency(timeFrameLabel, datasetModule.node.parentNode.config.singularVariableName) !== true) { - if (!(bot.VALUES_TO_USE.timeFrame === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { + if (!(bot.SESSION.parameters.timeFrame.config.label === timeFrameLabel && datasetModule.node.parentNode.config.pluralVariableName === 'candles')) { dependencyControlLoop(); return } @@ -639,7 +639,7 @@ function onMarketStatusReport() { /* This is where we check if we need reached the user defined end datetime. */ const now = new Date() - if (now.valueOf() >= bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf()) { + if (now.valueOf() >= bot.SESSION.parameters.timeRange.config.finalDatetime) { const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onMarketStatusReport -> " + logText); } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index d82c83e41..195b67999 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -704,7 +704,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let slippedStopLoss = tradingEngine.current.position.stopLoss.value /* Apply the Slippage */ - let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 + let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { slippedStopLoss = slippedStopLoss + slippageAmount @@ -752,7 +752,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let slippedTakeProfit = tradingEngine.current.position.takeProfit.value /* Apply the Slippage */ - let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 + let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { slippedTakeProfit = slippedTakeProfit + slippageAmount diff --git a/Session.js b/Session.js index 5b090ad0a..28ff62883 100644 --- a/Session.js +++ b/Session.js @@ -167,9 +167,13 @@ } if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) { // if the initialDatetime is missing we create a default one. bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date()).valueOf() + } else { + bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf() } if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { // if the finalDatetime is missing we create a default one. bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date()).valueOf() + } else { + bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf() } /* Session Type Forced Values */ switch (bot.SESSION.type) { @@ -310,7 +314,7 @@ bot.resumeExecution = false; bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true - pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) + pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) return true } @@ -343,7 +347,7 @@ bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100 bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100 - pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame) + pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) return true } diff --git a/TelegramBot.js b/TelegramBot.js index d0677c46d..262511f1f 100644 --- a/TelegramBot.js +++ b/TelegramBot.js @@ -30,7 +30,7 @@ exports.newTelegramBot = function newTelegramBot(bot, logger) { thisObject.telegramAPI = new Telegram(botToken) - const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started with an initial balance of " + " " + bot.VALUES_TO_USE.initialBalanceA + " " + bot.VALUES_TO_USE.baseAsset + "." + const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started." thisObject.telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeTelegramBot -> Telegram API error -> err = " + err)) } catch (err) { From c21684fde69956fadeb9c577a4dd7b5bcc1d6487 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 11 Jun 2020 13:46:07 +0200 Subject: [PATCH 034/113] bug fixed --- Session.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Session.js b/Session.js index 28ff62883..d65f2c2d7 100644 --- a/Session.js +++ b/Session.js @@ -300,7 +300,7 @@ function startBackTesting(message) { bot.resumeExecution = false; // TODO change this for the new way of handling resuming bot.hasTheBotJustStarted = true // TODO see if this is necesary - bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // Here we initialize the date of the process. + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // Here we initialize the date of the process. return true } @@ -312,7 +312,7 @@ } bot.resumeExecution = false; - bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) return true @@ -327,7 +327,7 @@ } bot.resumeExecution = false; - bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true /* Reduce the balance */ @@ -356,7 +356,7 @@ bot.resumeExecution = false; bot.hasTheBotJustStarted = true - bot.tradingProcessDatee = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) + bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.label) return true } From e404047a07935b3da7e9be1672e08f12483bac95 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 11 Jun 2020 18:39:19 +0200 Subject: [PATCH 035/113] Simulation being called --- LowFrequencyTrading/TradingOutput.js | 14 +++++++------- LowFrequencyTrading/TradingProcess.js | 6 +++--- LowFrequencyTrading/TradingRecords.js | 4 +--- LowFrequencyTrading/TradingSimulation.js | 9 ++++----- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index 1d526efaa..cd5c60f2b 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -45,8 +45,11 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL const TRADING_SIMULATION = require('./TradingSimulation.js') let tradingSimulation = TRADING_SIMULATION.newTradingSimulation(bot, logger, UTILITIES) + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets let market = bot.market + let totalFilesToBeRead = 0 let totalFilesRead = 0 + let totalFilesToBeWritten = 0 let totalFilesWritten = 0 let outputDatasetsMap = new Map() @@ -102,9 +105,8 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL } function readOutputFile(fileName, filePath, productName) { - + totalFilesToBeRead++ filePath += '/' + fileName - fileStorage.getTextFile(filePath, onFileRead, true) function onFileRead(err, text) { @@ -136,7 +138,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL function anotherFileRead() { totalFilesRead++ - if (totalFilesRead === outputDatasets.length) { + if (totalFilesRead === totalFilesToBeRead) { runSimulation() } } @@ -144,9 +146,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL function runSimulation() { tradingSimulation.runSimulation( chart, - dataDependencies, simulationState, - outputDatasets, outputDatasetsMap, writeFiles, callBackFunction) @@ -195,7 +195,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL } function writeOutputFile(fileName, filePath, productName) { - + totalFilesToBeWritten++ filePath += '/' + fileName let fileContent = JSON.stringify(outputDatasetsMap.get(productName)) @@ -222,7 +222,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL function anotherFileWritten() { totalFilesWritten++ - if (totalFilesWritten === outputDatasets.length) { + if (totalFilesWritten === totalFilesToBeWritten) { callBackFunction(global.DEFAULT_OK_RESPONSE) } } diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index ae13fc7b0..aedb17798 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -558,7 +558,7 @@ let products = {} if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, currentTimeFrame) let propertyName = 'at' + timeFrameLabel.replace('-', '') chart[propertyName] = products @@ -571,7 +571,7 @@ let products = {} if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, currentTimeFrame) let propertyName = 'at' + timeFrameLabel.replace('-', '') chart[propertyName] = products @@ -583,7 +583,7 @@ let products = {} if (dataFiles !== undefined) { - commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, timeFrame) + commons.inflateDatafiles(dataFiles, dataDependencies, products, mainDependency, currentTimeFrame) let propertyName = 'atAnyTimeFrame' chart[propertyName] = products diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 3168f6097..fbbe6cb45 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -10,7 +10,6 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let tradingEngine let tradingSystem - let outputDatasets let outputDatasetsMap return thisObject @@ -18,14 +17,12 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { function initialize(pOutputDatasets, pOutputDatasetsMap) { tradingEngine = bot.TRADING_ENGINE tradingSystem = bot.TRADING_SYSTEM - outputDatasets = pOutputDatasets // These are the nodes defined at the UI outputDatasetsMap = pOutputDatasetsMap // These are the files turned into arrays, stored in a Map by Product codeName. } function finalize() { tradingEngine = undefined tradingSystem = undefined - outputDatasets = undefined outputDatasetsMap = undefined } @@ -35,6 +32,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { we have a pointer to the node that have the information we need to extract. Later, based on the product record definition we will extract each individual value. */ + let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets for (let i = 0; i < outputDatasets.length; i++) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index feaf5451e..83fa02e24 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -18,9 +18,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function runSimulation( chart, - dataDependencies, simulationState, - outputDatasets, outputDatasetsMap, callback, callBackFunction) { @@ -57,13 +55,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() - const TRADING_ENGINE_MODULE = require('./TradingExecution.js') - let tradingExecutionModule = TRADING_ENGINE_MODULE.newTradingExecution(bot, logger) + const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') + let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger) tradingExecutionModule.initialize() const TRADING_RECORDS_MODULE = require('./TradingRecords.js') let tradingRecordsModule = TRADING_RECORDS_MODULE.newTradingRecords(bot, logger) - tradingRecordsModule.initialize(outputDatasets, outputDatasetsMap) + tradingRecordsModule.initialize(outputDatasetsMap) const SNAPSHOTS_MODULE = require('./Snapshots.js') let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) @@ -350,6 +348,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT easy access to the current candle for instance, or the current bollinger band, meaning the one the Simulation is currently standing at. For that reason we do the following processing, to have at the chart data structure the current objects of each indicator / time frame. */ + let dataDependencies = bot.processNode.referenceParent.processDependencies.dataDependencies /* Finding the Current Element on Market Files */ if (bot.processingDailyFiles) { From 1f11ad549cfe89e60742a89b3a9fe93a8f75f272 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 11 Jun 2020 21:53:59 +0200 Subject: [PATCH 036/113] Task Error event working --- DebugLog.js | 2 +- FileStorage.js | 16 ++- IndicatorBot.js | 19 ++- SensorBot.js | 19 ++- Session.js | 17 ++- TradingBot.js | 370 +++++++++++++++++------------------------------- server.js | 22 ++- 7 files changed, 194 insertions(+), 271 deletions(-) diff --git a/DebugLog.js b/DebugLog.js index f3372242d..52b56bde4 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -124,7 +124,7 @@ exports.newDebugLog = function newDebugLog() { function writeLog() { - fileStorage.createTextFile(filePath + '/' + fileName, contentToPersist + '\r\n' + "]", onFileCreated); + fileStorage.createTextFile(filePath + '/' + fileName, contentToPersist + '\r\n' + "]", onFileCreated, undefined, true); function onFileCreated(err) { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> err = " + err.message); diff --git a/FileStorage.js b/FileStorage.js index ed90f3721..1b55b9af7 100644 --- a/FileStorage.js +++ b/FileStorage.js @@ -191,13 +191,13 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { } } - function createTextFile(filePath, fileContent, callBackFunction, keepPrevious) { + function createTextFile(filePath, fileContent, callBackFunction, keepPrevious, noTemp) { let currentRetryWriteTextFile = 0 - recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious) + recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious, noTemp) - function recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious) { + function recursiveCreateTextFile(filePath, fileContent, callBackFunction, keepPrevious, noTemp) { /* Choose path for either logs or data */ let fileLocation if (filePath.indexOf("/Logs/") > 0) { @@ -218,7 +218,11 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { Then we delete the original file, if exists, and finally we rename the temporary into the original name. */ const fs = require('fs') - fs.writeFile(fileLocation + '.tmp', fileContent, onFileWritenn) + if (noTemp === true) { + fs.writeFile(fileLocation, fileContent, onFileWritenn) + } else { + fs.writeFile(fileLocation + '.tmp', fileContent, onFileWritenn) + } function onFileWritenn(err) { let retryTimeToUse = FAST_RETRY_TIME_IN_MILISECONDS @@ -231,6 +235,10 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { setTimeout(retry, retryTimeToUse) } else { + if (noTemp === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE) + return + } if (keepPrevious === true) { /* In some cases, we are going to keep a copy of the previous version of the file being written. This will be usefull to recover from crashes diff --git a/IndicatorBot.js b/IndicatorBot.js index 3d2ab599f..0c4499399 100644 --- a/IndicatorBot.js +++ b/IndicatorBot.js @@ -2,7 +2,7 @@ const ROOT_DIR = './'; - const MODULE_NAME = "Indicator Process Main Loop"; + const MODULE_NAME = "Indicator Bot"; const FULL_LOG = true; let USER_BOT_MODULE; @@ -1107,11 +1107,18 @@ } function processError(node, errorMessage) { - let event = { - nodeName: node.name, - nodeType: node.type, - nodeId: node.id, - errorMessage: errorMessage + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } } global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) } diff --git a/SensorBot.js b/SensorBot.js index a40fc6203..491db724e 100644 --- a/SensorBot.js +++ b/SensorBot.js @@ -2,7 +2,7 @@ const ROOT_DIR = './'; - const MODULE_NAME = "Sensor Process Main Loop"; + const MODULE_NAME = "Sensor Bot"; const FULL_LOG = true; let USER_BOT_MODULE; @@ -826,11 +826,18 @@ } function processError(node, errorMessage) { - let event = { - nodeName: node.name, - nodeType: node.type, - nodeId: node.id, - errorMessage: errorMessage + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } } global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) } diff --git a/Session.js b/Session.js index d65f2c2d7..0a928a0cf 100644 --- a/Session.js +++ b/Session.js @@ -395,11 +395,18 @@ } function sessionError(node, errorMessage) { - let event = { - nodeName: node.name, - nodeType: node.type, - nodeId: node.id, - errorMessage: errorMessage + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } } global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Error', event) diff --git a/TradingBot.js b/TradingBot.js index 0431e9bc3..6a38c724e 100644 --- a/TradingBot.js +++ b/TradingBot.js @@ -2,7 +2,7 @@ const ROOT_DIR = './'; - const MODULE_NAME = "Trading Process Main Loop"; + const MODULE_NAME = "Trading Bot"; const FULL_LOG = true; let USER_BOT_MODULE; @@ -205,49 +205,33 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -278,47 +262,32 @@ case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -342,50 +311,34 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return return; } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -408,13 +361,9 @@ default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } return; @@ -426,50 +375,33 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -492,65 +424,39 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { - - switch (err.message) { - default: { - logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; - } - } + logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -581,16 +487,10 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - sessionStopped() - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { @@ -609,49 +509,34 @@ } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -678,49 +563,33 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -866,23 +735,17 @@ } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -897,17 +760,36 @@ } function processError(node, errorMessage) { - let event = { - nodeName: node.name, - nodeType: node.type, - nodeId: node.id, - errorMessage: errorMessage + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } } global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Error', event) } function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + if (global.unexpectedError !== undefined) { + processError(undefined, "An unexpected error caused the Process to stop.") + } else { + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + } + sessionStopped() + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + clearTimeout(checkLoopHealthHandle); + bot.enableCheckLoopHealth = false; + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } function sessionStopped() { diff --git a/server.js b/server.js index 5b48bf2a8..eed0c81f7 100644 --- a/server.js +++ b/server.js @@ -91,6 +91,10 @@ process.on('message', message => { let shuttingDownProcess = false global.EXIT_NODE_PROCESS = function exitProcess() { + if (global.unexpectedError !== undefined) { + global.taskError(undefined, "An unexpected error caused the Task to stop.") + } + if (shuttingDownProcess === true) { return } shuttingDownProcess = true @@ -198,12 +202,20 @@ function bootLoader() { } global.taskError = function taskError(node, errorMessage) { - let event = { - nodeName: node.name, - nodeType: node.type, - nodeId: node.id, - errorMessage: errorMessage + let event + if (node !== undefined) { + event = { + nodeName: node.name, + nodeType: node.type, + nodeId: node.id, + errorMessage: errorMessage + } + } else { + event = { + errorMessage: errorMessage + } } + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Error', event) } From ef254e056eb82e9d8331df9f9b793875a14c0bde Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 11 Jun 2020 22:35:55 +0200 Subject: [PATCH 037/113] Code Cleaning --- IndicatorBot.js | 611 +++++++++++------------------------------------- SensorBot.js | 432 ++++++++-------------------------- TradingBot.js | 63 ----- 3 files changed, 231 insertions(+), 875 deletions(-) diff --git a/IndicatorBot.js b/IndicatorBot.js index 0c4499399..9e3d17574 100644 --- a/IndicatorBot.js +++ b/IndicatorBot.js @@ -17,7 +17,6 @@ let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; - let checkLoopHealthHandle; let thisObject = { initialize: initialize, @@ -29,10 +28,7 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - processConfig = pProcessConfig; if (bot.definedByUI === true) { @@ -42,7 +38,6 @@ } /* This bot is not ready for taking its code from the UI, then we need to load it from its repo. */ - let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName filePath += "/User.Bot.js" @@ -67,7 +62,6 @@ fileStorage.getTextFile(filePath, onCommonsDownloaded); function onCommonsDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -86,13 +80,7 @@ } function run(callBackFunction) { - try { - - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - - bot.enableCheckLoopHealth = true; - let fixedTimeLoopIntervalHandle; /* Errors sent to the UI */ @@ -108,16 +96,13 @@ } function loop() { - try { - function pad(str, max) { str = str.toString(); return str.length < max ? pad(" " + str, max) : str; } /* For each loop we want to create a new log file. */ - if (logger !== undefined) { logger.finalize() } @@ -129,13 +114,10 @@ bot.loopCounter++; bot.loopStartTime = new Date().valueOf(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - /* We tell the UI that we are running. */ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); @@ -145,16 +127,13 @@ const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. /* High level log entry */ - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + " Main Loop # " + pad(Number(bot.loopCounter), 8)) /* We will prepare first the infraestructure needed for the bot to run. There are 3 modules we need to sucessfullly initialize first. */ - let processExecutionEvents let userBot; let processFramework; @@ -166,21 +145,12 @@ initializeProcessExecutionEvents(); function initializeProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - processExecutionEvents.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); @@ -194,66 +164,40 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - processExecutionEvents.start(onStarted); function onStarted(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); @@ -273,68 +217,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeStatusDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - statusDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); @@ -348,72 +265,43 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeDataDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> Entering function."); } - dataDependencies = DATA_DEPENDENCIES.newDataDependencies(bot, logger, DATA_SET); - dataDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeDataDependencies -> onInizialized -> Execution finished well."); } - /* If the process is configured to run inside a framework, we continue there, otherwise we run the bot directly. */ if (processConfig.framework === undefined) { initializeUserBot(); @@ -434,13 +322,9 @@ default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework not Supported."); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Process Framework Name = " + processConfig.framework.name); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } return; @@ -454,66 +338,40 @@ case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeUserBot() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> Entering function."); } - usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE); - usertBot.initialize(statusDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Execution finished well."); @@ -527,81 +385,51 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { switch (err.message) { default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startUserBot() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } - usertBot.start(onFinished); function onFinished(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Execution finished well."); @@ -615,15 +443,10 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { @@ -642,66 +465,41 @@ } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function intitializeProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); } - processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> onInizialized -> Execution finished well."); @@ -715,79 +513,51 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { switch (err.message) { default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startProcessFramework() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> Entering function."); } - processFramework.start(onFinished); function onFinished(err) { - try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessFramework -> onFinished -> Entering function."); } processFramework.finalize() processFramework = undefined dataDependencies.finalize() @@ -808,15 +578,10 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { @@ -835,64 +600,41 @@ } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function finishProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - processExecutionEvents.finish(onFinished); function onFinished(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - processExecutionEvents.finalize() processExecutionEvents = undefined @@ -910,93 +652,54 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ - processHeartBeat() /* Here we check if we must stop the loop gracefully. */ - shallWeStop(onStop, onContinue); function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - - if (global.WRITE_LOGS_TO_FILES === 'true') { - logger.persist(); - } - processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); return; - } function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - switch (nextWaitTime) { case 'Normal': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") if (global.WRITE_LOGS_TO_FILES === 'true') { @@ -1006,9 +709,6 @@ break; case 'Retry': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); @@ -1032,44 +732,9 @@ } } - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); - - } - } - } - function shallWeStop(stopCallBack, continueCallBack) { - try { - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - if (!global.STOP_TASK_GRACEFULLY) { continueCallBack(); } else { @@ -1077,22 +742,16 @@ } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -1124,15 +783,21 @@ } function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + if (global.unexpectedError !== undefined) { + processError(undefined, "An unexpected error caused the Process to stop.") + } else { + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + } + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/SensorBot.js b/SensorBot.js index 491db724e..bea8a6256 100644 --- a/SensorBot.js +++ b/SensorBot.js @@ -15,7 +15,6 @@ let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; - let checkLoopHealthHandle; let thisObject = { initialize: initialize, @@ -27,12 +26,8 @@ return thisObject; function initialize(pProcessConfig, callBackFunction) { - /* This function is exactly the same in the 3 modules representing the 2 different bot types loops. */ - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - processConfig = pProcessConfig; if (bot.definedByUI === true) { @@ -47,7 +42,6 @@ fileStorage.getTextFile(filePath, onBotDownloaded); function onBotDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onInizialized -> onBotDownloaded -> err = " + err.message); @@ -70,7 +64,6 @@ fileStorage.getTextFile(filePath, onCommonsDownloaded); function onCommonsDownloaded(err, text) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> onBotDownloaded -> onCommonsDownloaded -> Commons not found: " + err.code || err.message); callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -91,12 +84,7 @@ } function run(callBackFunction) { - try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); } - - bot.enableCheckLoopHealth = true; - let fixedTimeLoopIntervalHandle; /* Errors sent to the UI */ @@ -113,7 +101,6 @@ } function loop() { - try { processHeartBeat(undefined, undefined, "Running...") function pad(str, max) { @@ -122,7 +109,6 @@ } /* For each loop we want to create a new log file. */ - if (logger !== undefined) { logger.finalize() } @@ -134,13 +120,10 @@ bot.loopCounter++; bot.loopStartTime = new Date().valueOf(); - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } - /* We tell the UI that we are running. */ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); @@ -148,16 +131,13 @@ const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. /* High level log entry */ - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + " Main Loop # " + pad(Number(bot.loopCounter), 8)) /* We will prepare first the infraestructure needed for the bot to run. There are 4 modules we need to sucessfullly initialize first. */ - let processExecutionEvents let userBot; let statusDependencies; @@ -168,21 +148,12 @@ initializeProcessExecutionEvents(); function initializeProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> Entering function."); } - processExecutionEvents = PROCESS_EXECUTION_EVENTS.newProcessExecutionEvents(bot, logger) - processExecutionEvents.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Execution finished well."); @@ -196,66 +167,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> Entering function."); } - processExecutionEvents.start(onStarted); function onStarted(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startProcessExecutionEvents -> onStarted -> Execution finished well."); @@ -275,68 +221,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeStatusDependencies() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> Entering function."); } - statusDependencies = STATUS_DEPENDENCIES.newStatusDependencies(bot, logger, STATUS_REPORT, UTILITIES, PROCESS_OUTPUT); - statusDependencies.initialize(onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeStatusDependencies -> onInizialized -> Execution finished well."); @@ -350,68 +269,41 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function initializeUserBot() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> Entering function."); } - usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE, STATUS_REPORT, exchangeAPI); - usertBot.initialize(statusDependencies, onInizialized); function onInizialized(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeUserBot -> onInizialized > Execution finished well."); @@ -425,81 +317,51 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { switch (err.message) { default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized > Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> onInizialized -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeUserBot -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function startUserBot() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> Entering function."); } - usertBot.start(onFinished); function onFinished(err) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished -> Entering function."); } - switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write(MODULE_NAME, "[INFO] run -> loop -> startUserBot -> onFinished > Execution finished well."); @@ -513,15 +375,10 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } case global.CUSTOM_OK_RESPONSE.result: { @@ -540,64 +397,41 @@ } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Unhandled custom response received. -> err = " + err.message); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } } } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished > Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startUserBot -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function finishProcessExecutionEvents() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> Entering function."); } - processExecutionEvents.finish(onFinished); function onFinished(err) { try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> finishProcessExecutionEvents -> onFinished -> Entering function."); } - processExecutionEvents.finalize() processExecutionEvents = undefined @@ -615,94 +449,52 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Operation Failed. Aborting the process."); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); - return; + global.unexpectedError = err.message + processStopped() + return } default: { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err.result = " + err.result); logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> Unhandled err.result received. -> err = " + err.message); - - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } function loopControl(nextWaitTime) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Entering function."); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ /* Here we check if we must stop the loop gracefully. */ - shallWeStop(onStop, onContinue); function onStop() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onStop -> Stopping the Loop Gracefully. See you next time!"); } - - if (global.WRITE_LOGS_TO_FILES === 'true') { - logger.persist(); - } - - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); return; - } function onContinue() { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> onContinue -> Entering function."); } - /* Sensor bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ - switch (nextWaitTime) { case 'Normal': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.normalWaitTime); processHeartBeat(undefined, undefined, "Waiting " + processConfig.normalWaitTime / 1000 + " seconds for next execution.") if (global.WRITE_LOGS_TO_FILES === 'true') { @@ -712,9 +504,6 @@ break; case 'Retry': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.retryWaitTime * 5, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); @@ -750,44 +539,9 @@ } } - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); - - } - } - } - function shallWeStop(stopCallBack, continueCallBack) { - try { - /* IMPORTANT: This function is exactly the same on the 3 modules. */ - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> shallWeStop -> Entering function. "); } - if (!global.STOP_TASK_GRACEFULLY) { continueCallBack(); } else { @@ -795,23 +549,17 @@ } } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> shallWeStop -> err = " + err.stack); - logger.persist(); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + global.unexpectedError = err.message + processStopped() + return } } } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] run -> loop -> err = " + err.stack); - clearInterval(fixedTimeLoopIntervalHandle); - clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(err); + global.unexpectedError = err.message + processStopped() + return } } @@ -843,7 +591,15 @@ } function processStopped() { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + if (global.unexpectedError !== undefined) { + processError(undefined, "An unexpected error caused the Process to stop.") + } else { + global.EVENT_SERVER_CLIENT.raiseEvent(bot.processKey, 'Stopped') + } + logger.persist(); + clearInterval(fixedTimeLoopIntervalHandle); + clearTimeout(nextLoopTimeoutHandle); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -851,8 +607,6 @@ parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/TradingBot.js b/TradingBot.js index 6a38c724e..c31de711f 100644 --- a/TradingBot.js +++ b/TradingBot.js @@ -19,7 +19,6 @@ let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; - let checkLoopHealthHandle; let thisObject = { initialize: initialize, @@ -94,7 +93,6 @@ function run(callBackFunction) { try { /* Some initial values*/ - bot.enableCheckLoopHealth = true; let fixedTimeLoopIntervalHandle; bot.STOP_SESSION = true; @@ -132,7 +130,6 @@ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); @@ -142,16 +139,13 @@ const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. /* High level log entry */ - console.log(new Date().toISOString() + " " + pad(bot.exchange, 20) + " " + pad(bot.market.baseAsset + '/' + bot.market.quotedAsset, 10) + " " + pad(bot.codeName, 30) + " " + pad(bot.process, 30) + " Main Loop # " + pad(Number(bot.loopCounter), 8) + " " + bot.processNode.session.type + " " + bot.processNode.session.name) /* Checking if we need to need to emit any event */ - if (bot.SESSION_STATUS === 'Idle' && bot.STOP_SESSION === false) { global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Running') bot.SESSION_STATUS = 'Running' @@ -218,7 +212,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> onInizialized -> err = " + err.stack); global.unexpectedError = err.message @@ -226,7 +219,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeProcessExecutionEvents -> err = " + err.stack); global.unexpectedError = err.message @@ -260,7 +252,6 @@ return; } case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - processStopped() logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> Operation Failed. Aborting the process."); global.unexpectedError = err.message processStopped() @@ -274,7 +265,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> onStarted -> err = " + err.stack); global.unexpectedError = err.message @@ -282,7 +272,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessExecutionEvents -> err = " + err.stack); global.unexpectedError = err.message @@ -325,7 +314,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> onInizialized -> err = " + err.stack); global.unexpectedError = err.message @@ -333,7 +321,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeStatusDependencies -> err = " + err.stack); global.unexpectedError = err.message @@ -388,7 +375,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> onInizialized -> err = " + err.stack); global.unexpectedError = err.message @@ -396,7 +382,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> initializeDataDependencies -> err = " + err.stack); global.unexpectedError = err.message @@ -443,7 +428,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> onInizialized -> err = " + err.stack); global.unexpectedError = err.message @@ -451,7 +435,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> intitializeProcessFramework -> err = " + err.stack); global.unexpectedError = err.message @@ -523,7 +506,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> onFinished -> err = " + err.stack); global.unexpectedError = err.message @@ -531,7 +513,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> startProcessFramework -> err = " + err.stack); global.unexpectedError = err.message @@ -576,7 +557,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> onFinished -> err = " + err.stack); global.unexpectedError = err.message @@ -584,7 +564,6 @@ return } } - } catch (err) { logger.write(MODULE_NAME, "[ERROR] run -> loop -> finishProcessExecutionEvents -> err = " + err.stack); global.unexpectedError = err.message @@ -611,7 +590,6 @@ global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Stopped') processStopped() - callBackFunction(global.DEFAULT_OK_RESPONSE); return; } @@ -653,9 +631,6 @@ } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (waitTime / 1000 / 60) + " minute/s."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, waitTime); let waitingTime = waitTime / 1000 / 60 let label = 'minute/s' @@ -669,9 +644,6 @@ break; case 'Retry': { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - if (processConfig.deadWaitTime > 0) { - checkLoopHealthHandle = setTimeout(checkLoopHealth, processConfig.deadWaitTime, bot.loopCounter); - } nextLoopTimeoutHandle = setTimeout(loop, processConfig.retryWaitTime); processHeartBeat(undefined, undefined, "Trying to recover from some problem. Waiting " + processConfig.retryWaitTime / 1000 + " seconds for next execution.") logger.persist(); @@ -695,36 +667,6 @@ } } - function checkLoopHealth(pLastLoop) { - - if (bot.enableCheckLoopHealth === false || global.STOP_TASK_GRACEFULLY === true) { - - logger.write(MODULE_NAME, "[WARN] run -> loop -> checkLoopHealth -> bot.enableCheckLoopHealth = " + bot.enableCheckLoopHealth); - - return; - } // This gets disabled anytime the Main Loop is shut down by any condition. - - if (bot.loopCounter <= pLastLoop + 1) { // This means that the next loop started but also stopped executing abruptally. - - let now = new Date().valueOf(); - - if (now - bot.loopStartTime > processConfig.deadWaitTime) { - - logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop); - console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now.")); - - logger.persist(); // We persist the logs of the failed execution. - clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute. - loop(); // We restart the loop so that the processing can continue. - - } else { - - console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken.")); - - } - } - } - function shallWeStop(stopCallBack, continueCallBack) { try { /* IMPORTANT: This function is exactly the same on the 3 modules. */ @@ -786,9 +728,6 @@ logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; - callBackFunction(global.DEFAULT_FAIL_RESPONSE); } @@ -803,8 +742,6 @@ parentLogger.write(MODULE_NAME, "[ERROR] run -> err = " + err.stack); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - clearTimeout(checkLoopHealthHandle); - bot.enableCheckLoopHealth = false; callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } From 7c279e46dca4b362e97023e5ac090bc01190e187 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 09:22:08 +0200 Subject: [PATCH 038/113] global.ROOT_DIR --- DebugLog.js | 1 - IndicatorBot.js | 22 ++++---- LowFrequencyTrading/TradingSimulation.js | 71 +++++++++++------------- Root.js | 7 +-- SensorBot.js | 14 ++--- TradingBot.js | 22 ++++---- server.js | 2 + 7 files changed, 63 insertions(+), 76 deletions(-) diff --git a/DebugLog.js b/DebugLog.js index 52b56bde4..1832d53db 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -1,6 +1,5 @@ exports.newDebugLog = function newDebugLog() { - const ROOT_DIR = './'; const MODULE_NAME = "DebugLog"; let executionDatetime = "D." + global.EXECUTION_DATETIME.getUTCFullYear() + diff --git a/IndicatorBot.js b/IndicatorBot.js index 9e3d17574..30537cb6f 100644 --- a/IndicatorBot.js +++ b/IndicatorBot.js @@ -1,19 +1,17 @@ exports.newIndicatorBot = function newIndicatorBot(bot, parentLogger) { - const ROOT_DIR = './'; - const MODULE_NAME = "Indicator Bot"; const FULL_LOG = true; let USER_BOT_MODULE; let COMMONS_MODULE; - const MULTI_PERIOD_MARKET = require(ROOT_DIR + 'MultiPeriodMarket'); - const MULTI_PERIOD_DAILY = require(ROOT_DIR + 'MultiPeriodDaily'); + const MULTI_PERIOD_MARKET = require(global.ROOT_DIR + 'MultiPeriodMarket'); + const MULTI_PERIOD_DAILY = require(global.ROOT_DIR + 'MultiPeriodDaily'); const FILE_STORAGE = require('./FileStorage.js'); let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; @@ -118,13 +116,13 @@ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); - const DATA_SET = require(ROOT_DIR + 'DataSet'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(global.ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(global.ROOT_DIR + 'StatusDependencies'); + const DATA_DEPENDENCIES = require(global.ROOT_DIR + 'DataDependencies'); + const DATA_SET = require(global.ROOT_DIR + 'DataSet'); + const PROCESS_EXECUTION_EVENTS = require(global.ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(global.ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 83fa02e24..51cc0d8ed 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -47,7 +47,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* These are the Modules we will need to run the Simulation */ - const EXCHANGE_API = require(ROOT_DIR + 'ExchangeAPI'); + const EXCHANGE_API = require(global.ROOT_DIR + 'ExchangeAPI'); exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); exchangeAPI.initialize(); @@ -280,44 +280,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT callback(tradingSystem, snapshots.headers, snapshots.triggerOn, snapshots.takePosition) } - function getElement(pArray, currentCandle, datasetName) { - if (pArray === undefined) { return } - try { - let element - for (let i = 0; i < pArray.length; i++) { - element = pArray[i] - - if (currentCandle.end === element.end) { // when there is an exact match at the end we take that element - return element - } else { - if ( - i > 0 && - element.end > currentCandle.end - ) { - let previousElement = pArray[i - 1] - if (previousElement.end < currentCandle.end) { - return previousElement // If one elements goes into the future of currentCandle, then we stop and take the previous element. - } else { - return - } - } - if ( - i === pArray.length - 1 // If we reach the end of the array, then we return the last element. - && - element.end < currentCandle.end - ) { - return element - } - } - } - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> getElement -> datasetName = ' + datasetName) - logger.write(MODULE_NAME, '[ERROR] runSimulation -> getElement -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - } - } - function heartBeat() { /* We will produce a simulation level heartbeat in order to inform the user this is running. */ heartBeatDate = new Date(Math.trunc(candle.begin / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) @@ -402,6 +364,37 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } + function getElement(pArray, currentCandle, datasetName) { + if (pArray === undefined) { return } + try { + let element + for (let i = 0; i < pArray.length; i++) { + element = pArray[i] + + if (currentCandle.end === element.end) { // when there is an exact match at the end we take that element + return element + } else { + if ( + i > 0 && + element.end > currentCandle.end + ) { + let previousElement = pArray[i - 1] + if (previousElement.end < currentCandle.end) { + return previousElement // If one elements goes into the future of currentCandle, then we stop and take the previous element. + } else { + return + } + } + } + } + return + } catch (err) { + logger.write(MODULE_NAME, '[ERROR] runSimulation -> getElement -> datasetName = ' + datasetName) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> getElement -> err = ' + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } + function checkIfWeAreAtTheHeadOfTheMarket() { /* We skip the candle at the head of the market because it has not closed yet. The procedure to determine if we are at the head of the market is different diff --git a/Root.js b/Root.js index 329f2eda7..e31c5ab48 100644 --- a/Root.js +++ b/Root.js @@ -35,7 +35,6 @@ global.dailyFilePeriods = JSON.parse(global.dailyFilePeriods); - const ROOT_DIR = './'; const MODULE_NAME = "Root"; const WAIT_TIME_FOR_ALL_PROCESS_INSTANCES_TO_START = 10000 // This avoid a race condition that could happen if one process finished before all the other even started. @@ -387,7 +386,7 @@ try { global.TOTAL_PROCESS_INSTANCES_CREATED++ - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; logger = DEBUG_MODULE.newDebugLog(); @@ -452,7 +451,7 @@ try { global.TOTAL_PROCESS_INSTANCES_CREATED++ - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; logger = DEBUG_MODULE.newDebugLog(); @@ -515,7 +514,7 @@ global.TOTAL_PROCESS_INSTANCES_CREATED++ try { - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; logger = DEBUG_MODULE.newDebugLog(); diff --git a/SensorBot.js b/SensorBot.js index bea8a6256..c53f95071 100644 --- a/SensorBot.js +++ b/SensorBot.js @@ -1,7 +1,5 @@ exports.newSensorBot = function newSensorBot(bot, parentLogger) { - const ROOT_DIR = './'; - const MODULE_NAME = "Sensor Bot"; const FULL_LOG = true; @@ -11,7 +9,7 @@ const FILE_STORAGE = require('./FileStorage.js'); let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; @@ -124,11 +122,11 @@ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(global.ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(global.ROOT_DIR + 'StatusDependencies'); + const PROCESS_EXECUTION_EVENTS = require(global.ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(global.ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. diff --git a/TradingBot.js b/TradingBot.js index c31de711f..a99df360a 100644 --- a/TradingBot.js +++ b/TradingBot.js @@ -1,21 +1,19 @@ exports.newTradingBot = function newTradingBot(bot, parentLogger) { - const ROOT_DIR = './'; - const MODULE_NAME = "Trading Bot"; const FULL_LOG = true; let USER_BOT_MODULE; let USER_BOT_COMMONS; - const TRADING_PROCESS_MODULE = require(ROOT_DIR + '/LowFrequencyTrading/TradingProcess.js'); + const TRADING_PROCESS_MODULE = require(global.ROOT_DIR + '/LowFrequencyTrading/TradingProcess.js'); const FILE_STORAGE = require('./FileStorage.js'); - const SESSION = require(ROOT_DIR + 'Session'); + const SESSION = require(global.ROOT_DIR + 'Session'); let fileStorage = FILE_STORAGE.newFileStorage(parentLogger); let session = SESSION.newSession(bot, parentLogger) - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); + const DEBUG_MODULE = require(global.ROOT_DIR + 'DebugLog'); let logger; // We need this here in order for the loopHealth function to work and be able to rescue the loop when it gets in trouble. let nextLoopTimeoutHandle; @@ -130,13 +128,13 @@ processHeartBeat() /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ - const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); - const STATUS_REPORT = require(ROOT_DIR + 'StatusReport'); - const STATUS_DEPENDENCIES = require(ROOT_DIR + 'StatusDependencies'); - const DATA_DEPENDENCIES = require(ROOT_DIR + 'DataDependencies'); - const DATA_SET = require(ROOT_DIR + 'DataSet'); - const PROCESS_EXECUTION_EVENTS = require(ROOT_DIR + 'ProcessExecutionEvents'); - const PROCESS_OUTPUT = require(ROOT_DIR + 'ProcessOutput'); + const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); + const STATUS_REPORT = require(global.ROOT_DIR + 'StatusReport'); + const STATUS_DEPENDENCIES = require(global.ROOT_DIR + 'StatusDependencies'); + const DATA_DEPENDENCIES = require(global.ROOT_DIR + 'DataDependencies'); + const DATA_SET = require(global.ROOT_DIR + 'DataSet'); + const PROCESS_EXECUTION_EVENTS = require(global.ROOT_DIR + 'ProcessExecutionEvents'); + const PROCESS_OUTPUT = require(global.ROOT_DIR + 'ProcessOutput'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. diff --git a/server.js b/server.js index eed0c81f7..b40e410e0 100644 --- a/server.js +++ b/server.js @@ -25,6 +25,8 @@ global.CUSTOM_FAIL_RESPONSE = { message: "Custom Message" }; +global.ROOT_DIR = './'; + const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; global.LOGGER_MAP = new Map() global.SESSION_MAP = new Map() From 07a971223938ecd5a64ed79f7e6d7c48187af767 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 09:48:20 +0200 Subject: [PATCH 039/113] bot.simulationState.tradingSystem --- LowFrequencyTrading/Announcements.js | 2 +- LowFrequencyTrading/Snapshots.js | 2 +- LowFrequencyTrading/TradingEngine.js | 2 +- LowFrequencyTrading/TradingExecution.js | 2 +- LowFrequencyTrading/TradingOutput.js | 7 +++---- LowFrequencyTrading/TradingProcess.js | 10 ++++------ LowFrequencyTrading/TradingRecords.js | 4 ++-- LowFrequencyTrading/TradingSimulation.js | 7 +++---- LowFrequencyTrading/TradingSystem.js | 8 ++++---- 9 files changed, 20 insertions(+), 24 deletions(-) diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js index 4f9d92f5a..99fdb57b3 100644 --- a/LowFrequencyTrading/Announcements.js +++ b/LowFrequencyTrading/Announcements.js @@ -14,7 +14,7 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { return thisObject function initialize() { - tradingEngine = bot.TRADING_ENGINE + tradingEngine = bot.simulationState.tradingEngine } function finalize() { diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index a8aa133f8..37c8ddf9b 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -39,7 +39,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { return thisObject function initialize() { - tradingEngine = bot.TRADING_ENGINE + tradingEngine = bot.simulationState.tradingEngine } function finalize() { diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 3fa6fdd64..4f61a7282 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -22,7 +22,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { function initialize() { /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed */ - tradingEngine = bot.TRADING_ENGINE + tradingEngine = bot.simulationState.tradingEngine if (tradingEngine.isInitialized !== true || bot.RESUME === false) { tradingEngine.isInitialized = true diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 4aa191a79..b8343c0d7 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -15,7 +15,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { function initialize() { - tradingEngine = bot.TRADING_ENGINE + tradingEngine = bot.simulationState.tradingEngine } diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index cd5c60f2b..158758c7d 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -32,7 +32,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL } } - function start(chart, timeFrame, timeFrameLabel, currentDay, simulationState, callBackFunction) { + function start(chart, timeFrame, timeFrameLabel, currentDay, callBackFunction) { try { bot.processingDailyFiles if (timeFrame > global.dailyFilePeriods[0][0]) { @@ -54,8 +54,8 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let outputDatasetsMap = new Map() if (bot.RESUME !== true) { - simulationState.tradingEngine = bot.SESSION.tradingEngine - simulationState.tradingSystem = bot.SESSION.tradingSystem + bot.simulationState.tradingEngine = bot.simulationState.tradingEngine + bot.simulationState.tradingSystem = bot.simulationState.tradingSystem } readFiles() return @@ -146,7 +146,6 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL function runSimulation() { tradingSimulation.runSimulation( chart, - simulationState, outputDatasetsMap, writeFiles, callBackFunction) diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index aedb17798..188b93060 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -79,7 +79,6 @@ }; let previousDay; // Holds the date of the previous day relative to the processing date. - let simulationState getContextVariables(); @@ -152,8 +151,8 @@ } thisReport = statusReport.file; - simulationState = thisReport.simulationState; - if (simulationState === undefined) { simulationState = {} } // This should happen only when there is no status report + bot.simulationState = thisReport.simulationState; + if (bot.simulationState === undefined) { bot.simulationState = {} } // This should happen only when there is no status report if (thisReport.lastFile !== undefined) { if (bot.hasTheBotJustStarted === true && bot.resumeExecution === false) { @@ -594,7 +593,6 @@ currentTimeFrame, currentTimeFrameLabel, bot.tradingProcessDate, - simulationState, onOutputGenerated); function onOutputGenerated(err) { @@ -716,7 +714,7 @@ thisReport.file.lastExecution = bot.currentDaytime; thisReport.file.lastFile = lastFileDate; - thisReport.file.simulationState = simulationState; + thisReport.file.simulationState = bot.simulationState; thisReport.save(callBack); bot.hasTheBotJustStarted = false; @@ -727,7 +725,7 @@ let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.processDatetime; - thisReport.file.simulationState = simulationState; + thisReport.file.simulationState = bot.simulationState; thisReport.save(callBack); logger.newInternalLoop(bot.codeName, bot.process, bot.tradingProcessDate); diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index fbbe6cb45..92d5c70c7 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -15,8 +15,8 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { return thisObject function initialize(pOutputDatasets, pOutputDatasetsMap) { - tradingEngine = bot.TRADING_ENGINE - tradingSystem = bot.TRADING_SYSTEM + tradingEngine = bot.simulationState.tradingEngine + tradingSystem = bot.simulationState.tradingSystem outputDatasetsMap = pOutputDatasetsMap // These are the files turned into arrays, stored in a Map by Product codeName. } diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 51cc0d8ed..b3e79419f 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -18,14 +18,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function runSimulation( chart, - simulationState, outputDatasetsMap, callback, callBackFunction) { try { - let tradingSystem = simulationState.tradingSystem - let tradingEngine = simulationState.tradingEngine + let tradingSystem = bot.simulationState.tradingSystem + let tradingEngine = bot.simulationState.tradingEngine let sessionParameters = bot.SESSION.parameters if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } @@ -47,7 +46,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* These are the Modules we will need to run the Simulation */ - const EXCHANGE_API = require(global.ROOT_DIR + 'ExchangeAPI'); + const EXCHANGE_API = require('../ExchangeAPI'); exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); exchangeAPI.initialize(); diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 195b67999..02732f1d9 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -32,8 +32,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { return thisObject function initialize() { - tradingSystem = bot.TRADING_SYSTEM - tradingEngine = bot.TRADING_ENGINE + tradingSystem = bot.simulationState.tradingSystem + tradingEngine = bot.simulationState.tradingEngine tradingSystem.highlights = [] tradingSystem.conditionsErrors = [] @@ -61,11 +61,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function evalConditions() { - evalNode(bot.TRADING_SYSTEM, 'Conditions') + evalNode(bot.simulationState.tradingSystem, 'Conditions') } function evalFormulas() { - evalNode(bot.TRADING_SYSTEM, 'Formulas') + evalNode(bot.simulationState.tradingSystem, 'Formulas') } function evalNode(node, evaluating) { From 59ab3e06c524964b683f8211e93c6267427736c8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 10:06:28 +0200 Subject: [PATCH 040/113] Update TradingOutput.js --- LowFrequencyTrading/TradingOutput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index 158758c7d..bf2ba4679 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -54,8 +54,8 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let outputDatasetsMap = new Map() if (bot.RESUME !== true) { - bot.simulationState.tradingEngine = bot.simulationState.tradingEngine - bot.simulationState.tradingSystem = bot.simulationState.tradingSystem + bot.simulationState.tradingEngine = bot.TRADING_ENGINE + bot.simulationState.tradingSystem = bot.TRADING_SYSTEM } readFiles() return From aabf49dccb4643935a2113df78edffdb64e5e6e6 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 10:25:40 +0200 Subject: [PATCH 041/113] fixes --- LowFrequencyTrading/TradingSimulation.js | 10 +++++----- Session.js | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index b3e79419f..39e8f5cb7 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -138,6 +138,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle Begin @ ' + (new Date(candle.begin)).toLocaleString()) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle End @ ' + (new Date(candle.end)).toLocaleString()) } + tradingEngineModule.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas. + if (checkIfWeAreAtTheHeadOfTheMarket() === false) { afterLoop() return @@ -154,8 +156,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } heartBeat() - - tradingEngineModule.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas. positionChartAtCurrentCandle() tradingEngineModule.updateEpisodeCountersAndStatistics() @@ -422,11 +422,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function checkInitialAndFinalDatetime() { /* Here we check that the current candle is not outside of user-defined time range at the session parameters.*/ - if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { + if (tradingEngine.current.candle.end.value < sessionParameters.timeRange.config.initialDatetime.valueOf()) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } return false } - if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { + if (tradingEngine.current.candle.begin.value > sessionParameters.timeRange.config.finalDatetime.valueOf()) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle after the sessionParameters.timeRange.config.finalDatetime.') } return false } @@ -457,7 +457,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT maximumBalance = sessionParameters.sessionQuotedAsset.config.maximumBalance } - let stopRunningDate = new Date(candle.begin) + let stopRunningDate = new Date(tradingEngine.current.candle.begin.value) if (balance <= minimumBalance) { tradingSystem.error = 'Min Balance @ ' + stopRunningDate.toLocaleString() if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkMinimunAndMaximunBalance -> ' + tradingSystem.error) } diff --git a/Session.js b/Session.js index 0a928a0cf..0297863f1 100644 --- a/Session.js +++ b/Session.js @@ -213,6 +213,7 @@ bot.sessionError(bot.SESSION.parameters.timeFrame, errorMessage) return false } + bot.SESSION.parameters.timeFrame.config.value = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) /* Session Base Asset */ if (bot.SESSION.parameters.sessionBaseAsset === undefined) { @@ -314,7 +315,7 @@ bot.resumeExecution = false; bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true - pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) + pProcessConfig.liveWaitTime = sessionParameters.timeFrame.config.value return true } @@ -347,7 +348,7 @@ bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100 bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100 - pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label) + pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value return true } @@ -357,7 +358,7 @@ bot.resumeExecution = false; bot.hasTheBotJustStarted = true bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) - pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.label) + pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value return true } From c3a5a051e4e85f6bd279cffeb08490b6eddaeec7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 11:19:40 +0200 Subject: [PATCH 042/113] Simulation cycle complete --- LowFrequencyTrading/TradingProcess.js | 8 +++++--- LowFrequencyTrading/TradingSimulation.js | 7 +------ LowFrequencyTrading/TradingSystem.js | 6 +++--- Root.js | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 188b93060..712d9d35a 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -607,7 +607,6 @@ if (currentTimeFrame > global.dailyFilePeriods[0][0]) { writeMarketStatusReport(onMarketStatusReport) - } else { writeDataRanges(currentTimeFrameLabel, onWritten); } @@ -645,6 +644,9 @@ callBackFunction(global.DEFAULT_OK_RESPONSE); return; } + if (bot.SESSION.type === 'Backtesting Session') { + bot.STOP_SESSION = true + } callBackFunction(global.DEFAULT_OK_RESPONSE); } } @@ -709,7 +711,7 @@ } function writeDailyStatusReport(lastFileDate, callBack) { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName + "-" + "dataSet.V1"; let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.currentDaytime; @@ -721,7 +723,7 @@ } function writeMarketStatusReport(callBack) { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period" + "-" + "dataSet.V1"; + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName + "-" + "dataSet.V1"; let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.processDatetime; diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 39e8f5cb7..77a0d7e63 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -88,8 +88,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT initializeLoop() function initializeLoop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initializeLoop -> Entering function.') } - if (bot.RESUME === true && tradingEngine.last.candle !== undefined) { /* Estimate the Initial Candle based on the last candle saved at tradingEngine */ let firstBegin = candles[0].begin @@ -266,8 +264,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } function afterLoop() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> afterLoop -> Entering function.') } - tradingEngineModule.finalize() tradingExecutionModule.finalize() tradingSystemModule.finalize() @@ -275,8 +271,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT snapshotsModule.finalize() announcementsModule.finalize() - - callback(tradingSystem, snapshots.headers, snapshots.triggerOn, snapshots.takePosition) + callback() } function heartBeat() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 02732f1d9..558683b3b 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -44,9 +44,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function finalize() { chart = undefined - tradingSystem = undefined - tradingEngine = undefined - conditions = undefined tradingSystem.highlights = undefined tradingSystem.conditionsErrors = undefined @@ -54,6 +51,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { formulas = undefined tradingSystem.formulasValues = undefined tradingSystem.formulasErrors = undefined + + tradingSystem = undefined + tradingEngine = undefined } function setChart(pChart) { diff --git a/Root.js b/Root.js index e31c5ab48..279601849 100644 --- a/Root.js +++ b/Root.js @@ -252,7 +252,7 @@ let processConfig = global.TASK_NODE.bot.processes[processIndex].referenceParent.config if (processConfig.framework !== undefined) { - if (processConfig.framework.name === "Multi-Period-Daily" || processConfig.framework.name === "Multi-Period-Market" || processConfig.framework.name === "Multi-Period") { + if (processConfig.framework.name === "Multi-Period-Daily" || processConfig.framework.name === "Multi-Period-Market" || processConfig.framework.name === "Low-Frequency-Trading-Process") { if (processConfig.framework.startDate !== undefined) { processConfig.framework.startDate.resumeExecution = true; if (processConfig.startMode.noTime !== undefined) { From 202af8692760ce37f46dc9fd28a05d44b125740f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 12:16:16 +0200 Subject: [PATCH 043/113] Code Mantainance --- DataSet.js | 4 ++-- IndicatorOutput.js | 7 ------- LowFrequencyTrading/TradingProcess.js | 13 ++++--------- MultiPeriodDaily.js | 4 +--- MultiPeriodMarket.js | 2 +- Root.js | 9 +-------- Session.js | 13 ------------- StatusDependencies.js | 12 ++++++------ StatusReport.js | 15 ++++----------- TradingBot.js | 2 -- 10 files changed, 19 insertions(+), 62 deletions(-) diff --git a/DataSet.js b/DataSet.js index 1a6364b7a..bb7f789e3 100644 --- a/DataSet.js +++ b/DataSet.js @@ -204,8 +204,8 @@ exports.newDataSet = function newDataSet(BOT, logger) { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createTextFile -> pFolderPath = " + pFolderPath); } if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createTextFile -> pFileName = " + pFileName); } - let ownerId = thisObject.node.dataMine + "-" + thisObject.node.bot + "-" + thisObject.node.botVersion.major + "-" + thisObject.node.botVersion.minor + "-" + thisObject.node.dataSetVersion; - let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.dataSetVersion; + let ownerId = thisObject.node.dataMine + "-" + thisObject.node.bot + let botId = bot.dataMine + "-" + bot.codeName if (ownerId !== botId) { diff --git a/IndicatorOutput.js b/IndicatorOutput.js index 1f1e62182..9a1a9b95b 100644 --- a/IndicatorOutput.js +++ b/IndicatorOutput.js @@ -141,13 +141,6 @@ contextSummary.bot = outputDatasetNode.referenceParent.parentNode.parentNode.config.codeName contextSummary.dataMine = outputDatasetNode.referenceParent.parentNode.parentNode.parentNode.config.codeName - /* This stuff is still hardcoded and unresolved. */ - contextSummary.botVersion = { - "major": 1, - "minor": 0 - } - contextSummary.dataSetVersion = "dataSet.V1" - let fileContent = commons.generateFileContent(outputData, outputDatasetNode.referenceParent.parentNode.record, resultsWithIrregularPeriods, processingDailyFiles, currentDay, callBackFunction) commons.writeFile(contextSummary, fileContent, anotherFileWritten, processingDailyFiles, timeFrameLabel, currentDay, callBackFunction) } diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 712d9d35a..ffe9cb149 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -155,8 +155,7 @@ if (bot.simulationState === undefined) { bot.simulationState = {} } // This should happen only when there is no status report if (thisReport.lastFile !== undefined) { - if (bot.hasTheBotJustStarted === true && bot.resumeExecution === false) { - + if (bot.RESUME !== true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining because bot has just started and resume execution was true."); } startFromBegining(); return; @@ -628,8 +627,6 @@ } function onDailyStatusReport() { - /* The next run we need the process to continue at the date it finished. */ - processConfig.framework.startDate.resumeExecution = true; advanceTime(); } @@ -711,26 +708,24 @@ } function writeDailyStatusReport(lastFileDate, callBack) { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName + "-" + "dataSet.V1"; + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.currentDaytime; thisReport.file.lastFile = lastFileDate; thisReport.file.simulationState = bot.simulationState; thisReport.save(callBack); - - bot.hasTheBotJustStarted = false; } function writeMarketStatusReport(callBack) { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName + "-" + "dataSet.V1"; + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + bot.processNode.referenceParent.config.codeName let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.processDatetime; thisReport.file.simulationState = bot.simulationState; - thisReport.save(callBack); logger.newInternalLoop(bot.codeName, bot.process, bot.tradingProcessDate); + thisReport.save(callBack); } } catch (err) { diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index da6266000..b26ff1f15 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -772,8 +772,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> lastFileDate = " + lastFileDate); } try { - - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Daily" + "-" + "dataSet.V1"; + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Daily" let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.currentDaytime; @@ -783,7 +782,6 @@ thisReport.save(callBack); bot.hasTheBotJustStarted = false; - } catch (err) { logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js index 0464b5d30..a0b4d41ed 100644 --- a/MultiPeriodMarket.js +++ b/MultiPeriodMarket.js @@ -296,7 +296,7 @@ try { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Market" + "-" + "dataSet.V1"; + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Market" let thisReport = statusDependencies.statusReports.get(reportKey); thisReport.file.lastExecution = bot.processDatetime; diff --git a/Root.js b/Root.js index 279601849..0070316cf 100644 --- a/Root.js +++ b/Root.js @@ -179,7 +179,7 @@ botConfig.repo = global.TASK_NODE.bot.config.repo; bootingBot(processIndex); } catch (err) { - console.log(logDisplace + "Root : [ERROR] start -> getBotConfigFromFile -> onInizialized -> onFileReceived -> err = " + JSON.stringify(err)); + console.log(logDisplace + "Root : [ERROR] start -> getBotConfigFromFile -> onInizialized -> onFileReceived -> err = " + err.stack); return; } } @@ -213,13 +213,6 @@ botConfig.uiStartDate = global.TASK_NODE.bot.config.startDate botConfig.config = global.TASK_NODE.bot.config - /* This stuff is still hardcoded and unresolved. */ - botConfig.version = { - "major": 1, - "minor": 0 - } - botConfig.dataSetVersion = "dataSet.V1" - /* Loop Counter */ botConfig.loopCounter = 0; diff --git a/Session.js b/Session.js index 0297863f1..64fe9f403 100644 --- a/Session.js +++ b/Session.js @@ -55,7 +55,6 @@ return function runSession(message) { - try { if (bot.SESSION_STATUS === 'Idle' || bot.SESSION_STATUS === 'Running') { return } // This happens when the UI is reloaded, the session was running and tries to run it again. @@ -299,40 +298,31 @@ } function startBackTesting(message) { - bot.resumeExecution = false; // TODO change this for the new way of handling resuming - bot.hasTheBotJustStarted = true // TODO see if this is necesary bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // Here we initialize the date of the process. return true } function startLiveTrading(message) { - if (process.env.KEY === undefined || process.env.SECRET === undefined) { // TODO Remove this from Process .env if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> Key 'codeName' or 'secret' not provided. Plese check that and try again."); } return false } - bot.resumeExecution = false; bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) - bot.hasTheBotJustStarted = true pProcessConfig.liveWaitTime = sessionParameters.timeFrame.config.value return true } function startFordwardTesting(message) { - if (process.env.KEY === undefined || process.env.SECRET === undefined) { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startFordwardTesting -> Key name or Secret not provided, not possible to run the process in Forward Testing mode."); } console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.") return false } - bot.resumeExecution = false; bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) - bot.hasTheBotJustStarted = true /* Reduce the balance */ - let balancePercentage = 1 // This is the default value if (bot.SESSION.config.balancePercentage !== undefined) { @@ -354,9 +344,6 @@ } function startPaperTrading(message) { - - bot.resumeExecution = false; - bot.hasTheBotJustStarted = true bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value return true diff --git a/StatusDependencies.js b/StatusDependencies.js index 48b5659a9..21314c4c8 100644 --- a/StatusDependencies.js +++ b/StatusDependencies.js @@ -4,7 +4,7 @@ const LOG_FILE_CONTENT = false; const MODULE_NAME = "Status Dependencies"; - let bot = BOT + let bot = BOT let thisObject = { nodeArray: undefined, @@ -47,7 +47,7 @@ let statusDependency = { dependency: thisObject.nodeArray[i] } - + dependenciesToProcess.push(statusDependency) } @@ -124,7 +124,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> onLoad -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] initialize -> onLoad -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -137,14 +137,14 @@ loadCount++; logger.write(MODULE_NAME, "[INFO] initialize -> addReport -> Total Added = " + loadCount); - let key = statusDependency.dependency.dataMine + "-" + statusDependency.dependency.bot + "-" + statusDependency.dependency.process + "-" + statusDependency.dependency.dataSetVersion; + let key = statusDependency.dependency.dataMine + "-" + statusDependency.dependency.bot + "-" + statusDependency.dependency.process thisObject.keys.push(key); thisObject.statusReports.set(key, statusReportModule); if (statusReportModule.mainUtility !== undefined) { thisObject.reportsByMainUtility.set(statusReportModule.mainUtility, statusReportModule) - } + } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> addReport -> Report added to Map. -> key = " + key); } @@ -161,7 +161,7 @@ } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack); + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/StatusReport.js b/StatusReport.js index 72fe4381d..ff40b4e74 100644 --- a/StatusReport.js +++ b/StatusReport.js @@ -102,13 +102,6 @@ } } - /* This stuff is still hardcoded and unresolved. */ - statusDependencyNode.botVersion = { - "major": 1, - "minor": 0 - } - statusDependencyNode.dataSetVersion = "dataSet.V1" - if (bot.SESSION !== undefined && statusDependencyNode.bottype === "Trading Bot") { sessionPath = bot.SESSION.folderName + "/" } @@ -204,8 +197,8 @@ let fileName = "Status.Report.json"; let filePath; - let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.botVersion.major + "-" + statusDependencyNode.botVersion.minor + "-" + statusDependencyNode.process + "-" + statusDependencyNode.dataSetVersion; - let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.process + "-" + bot.dataSetVersion; + let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process + let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.process if (ownerId !== botId) { @@ -301,8 +294,8 @@ if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] save -> Entering function."); } - let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.botVersion.major + "-" + statusDependencyNode.botVersion.minor + "-" + statusDependencyNode.process + "-" + statusDependencyNode.dataSetVersion; - let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.process + "-" + bot.dataSetVersion; + let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process + let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.process if (ownerId !== botId && statusDependencyNode.process !== "Context") { // Context is a special case where the report is created by the Context.js module itself. diff --git a/TradingBot.js b/TradingBot.js index a99df360a..c62a11434 100644 --- a/TradingBot.js +++ b/TradingBot.js @@ -41,8 +41,6 @@ return } - bot.resumeExecution = processConfig.framework.startDate.resumeExecution // We are inherating this from root, but from here we need it at bot - let filePath = bot.dataMine + "/" + "bots" + "/" + bot.repo + "/" + pProcessConfig.codeName filePath += "/User.Bot.js" From 8e19a5ac246a1f7b75e0fb6263a2437990cf18dd Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 12:45:29 +0200 Subject: [PATCH 044/113] fix on stop --- IndicatorBot.js | 6 +++++- Root.js | 3 --- SensorBot.js | 6 +++++- TradingBot.js | 6 +++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/IndicatorBot.js b/IndicatorBot.js index 30537cb6f..dbe4b844f 100644 --- a/IndicatorBot.js +++ b/IndicatorBot.js @@ -789,7 +789,11 @@ logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + if (global.unexpectedError !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } } } catch (err) { diff --git a/Root.js b/Root.js index 0070316cf..cecc40636 100644 --- a/Root.js +++ b/Root.js @@ -404,14 +404,11 @@ let botId = pBotConfig.dataMine + "." + pBotConfig.codeName + "." + pBotConfig.process; if (err.result === global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Bot execution finished sucessfully."); logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Bot Id = " + botId); logger.persist(); - } else { - logger.write(MODULE_NAME, "[ERROR] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> err = " + err.message); logger.write(MODULE_NAME, "[ERROR] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Execution will be stopped. "); logger.write(MODULE_NAME, "[ERROR] start -> bootingBot -> runSensorBot -> onInitializeReady -> whenStartFinishes -> Bye."); diff --git a/SensorBot.js b/SensorBot.js index c53f95071..d8f8aaa32 100644 --- a/SensorBot.js +++ b/SensorBot.js @@ -597,7 +597,11 @@ logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + if (global.unexpectedError !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } } } diff --git a/TradingBot.js b/TradingBot.js index c62a11434..8b16f955e 100644 --- a/TradingBot.js +++ b/TradingBot.js @@ -724,7 +724,11 @@ logger.persist(); clearInterval(fixedTimeLoopIntervalHandle); clearTimeout(nextLoopTimeoutHandle); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + if (global.unexpectedError !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } } function sessionStopped() { From f8b110e28a58c6ae4edba1cdd37a6089328ddda7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jun 2020 21:43:58 +0200 Subject: [PATCH 045/113] cleaning --- DebugLog.js | 2 -- LowFrequencyTrading/TradingProcess.js | 2 -- 2 files changed, 4 deletions(-) diff --git a/DebugLog.js b/DebugLog.js index 1832d53db..e7c874e9b 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -141,9 +141,7 @@ exports.newDebugLog = function newDebugLog() { function write(pModule, pMessage) { - try { - if (disableLogging === true) { return; } if (thisObject.bot === undefined) { return; } diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index ffe9cb149..e900ade5c 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -176,12 +176,10 @@ } function startFromBegining() { - contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS); if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile); } processSingleFiles(); - } } catch (err) { From 83d54fefe2839096295706ad6325b5e687cd1c62 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 13 Jun 2020 13:22:53 +0200 Subject: [PATCH 046/113] Code Cleaning --- Commons.js | 12 ++++----- IndicatorOutput.js | 4 --- LowFrequencyTrading/TradingEngine.js | 7 ++++-- LowFrequencyTrading/TradingExecution.js | 8 +++--- LowFrequencyTrading/TradingProcess.js | 13 +++++----- LowFrequencyTrading/TradingSimulation.js | 31 ++++++++++++------------ MultiPeriodDaily.js | 1 - server.js | 13 +++++----- 8 files changed, 42 insertions(+), 47 deletions(-) diff --git a/Commons.js b/Commons.js index bea993f51..5037502fe 100644 --- a/Commons.js +++ b/Commons.js @@ -2,8 +2,6 @@ const FULL_LOG = true; const LOG_FILE_CONTENT = false; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; - const ONE_MIN_IN_MILISECONDS = 60 * 1000; const MODULE_NAME = "Commons"; @@ -199,7 +197,7 @@ let system = { // These are the available system variables to be used in User Code and Formulas timeFrame: timeFrame, - ONE_DAY_IN_MILISECONDS: ONE_DAY_IN_MILISECONDS + ONE_DAY_IN_MILISECONDS: global.ONE_DAY_IN_MILISECONDS } let variable = {} // This is the structure where the user will define its own variables that will be shared across different code blocks and formulas. let results = [] @@ -290,8 +288,8 @@ let yesterday = {} let system = { // These are the available system variables to be used in User Code and Formulas timeFrame: timeFrame, - ONE_DAY_IN_MILISECONDS: ONE_DAY_IN_MILISECONDS, - ONE_MIN_IN_MILISECONDS: ONE_MIN_IN_MILISECONDS + ONE_DAY_IN_MILISECONDS: global.ONE_DAY_IN_MILISECONDS, + ONE_MIN_IN_MILISECONDS: global.ONE_MIN_IN_MILISECONDS } let variable = {} // This is the structure where the user will define its own variables that will be shared across different code blocks and formulas. let results = [] @@ -340,7 +338,7 @@ if (processingDailyFiles) { /* Initialization of Last Instance */ - lastInstantOfTheDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1; + lastInstantOfTheDay = currentDay.valueOf() + global.ONE_DAY_IN_MILISECONDS - 1; if (interExecutionMemory[productName] === undefined) { /* The first time the intialization variables goes to the Inter Execution Memory. */ @@ -457,7 +455,7 @@ at the next day, in whole, even if it starts in the previous day. */ - let lastInstantOdDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1; + let lastInstantOdDay = currentDay.valueOf() + global.ONE_DAY_IN_MILISECONDS - 1; if (record.end < currentDay.valueOf() - 1) { continue; } if (record.end === lastInstantOdDay) { continue; } diff --git a/IndicatorOutput.js b/IndicatorOutput.js index 9a1a9b95b..3faa4052e 100644 --- a/IndicatorOutput.js +++ b/IndicatorOutput.js @@ -1,9 +1,5 @@ exports.newIndicatorOutput = function newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) { - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; - const MODULE_NAME = "Indicator Bot"; let thisObject = { diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 4f61a7282..1c35bd47c 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -16,6 +16,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } let tradingEngine + let sessionParameters return thisObject @@ -23,6 +24,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { /* Here we will go through all the nodes in the Trading Engine hiriarchy and apply the initial value to the value property when needed */ tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters if (tradingEngine.isInitialized !== true || bot.RESUME === false) { tradingEngine.isInitialized = true @@ -32,6 +34,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { function finalize() { tradingEngine = undefined + sessionParameters = undefined } function setCurrentCandle(candle) { @@ -131,7 +134,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { function updateEpisodeCountersAndStatistics() { tradingEngine.episode.episodeCounters.periods++ - tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS + tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS } function updatePositionCountersAndStatistics() { @@ -144,7 +147,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } tradingEngine.current.position.positionCounters.periods.value++ - tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / ONE_DAY_IN_MILISECONDS + tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS } else { tradingEngine.current.position.positionCounters.periods.value = 0 tradingEngine.current.position.positionStatistics.days.value = 0 diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index b8343c0d7..169b1f3b6 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -86,7 +86,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* We wont take a position unless we are withing the sessionParameters.timeRange.config.initialDatetime and the sessionParameters.timeRange.config.finalDatetime range */ if (sessionParameters.timeRange.config.initialDatetime !== undefined) { - if (candle.end < sessionParameters.timeRange.config.initialDatetime.valueOf()) { + if (candle.end < sessionParameters.timeRange.config.initialDatetime) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } takePositionAtSimulation() return @@ -95,7 +95,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* We wont take a position if we are past the final datetime */ if (sessionParameters.timeRange.config.finalDatetime !== undefined) { - if (candle.begin > sessionParameters.timeRange.config.finalDatetime.valueOf()) { + if (candle.begin > sessionParameters.timeRange.config.finalDatetime) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } takePositionAtSimulation() return @@ -115,8 +115,8 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* We are not going to place orders based on outdated information. The next filter prevents firing orders when backtesting. */ if (currentDay) { - let today = new Date(Math.trunc((new Date().valueOf()) / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - let processDay = new Date(Math.trunc(currentDay.valueOf() / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) + let today = new Date(Math.trunc((new Date().valueOf()) / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) + let processDay = new Date(Math.trunc(currentDay.valueOf() / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) if (today.valueOf() !== processDay.valueOf()) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) } diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index e900ade5c..b9a1c10f0 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -8,7 +8,6 @@ const LOG_FILE_CONTENT = false; const MODULE_NAME = "Trading Process"; const GMT_SECONDS = ':00.000 GMT+0000'; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; thisObject = { initialize: initialize, @@ -125,7 +124,7 @@ contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()); /* Validation that the data is not up-to-date. */ - if (bot.tradingProcessDate.valueOf() - ONE_DAY_IN_MILISECONDS > contextVariables.dateEndOfMarket.valueOf() && bot.SESSION.type !== "Backtesting Session") { + if (bot.tradingProcessDate.valueOf() - global.ONE_DAY_IN_MILISECONDS > contextVariables.dateEndOfMarket.valueOf() && bot.SESSION.type !== "Backtesting Session") { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> " + "Head of the market is @ " + contextVariables.dateEndOfMarket); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> You need to UPDATE your datasets in order to run a " + bot.SESSION.type) } @@ -359,12 +358,12 @@ buildCharts() return } - bot.tradingProcessDate = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. + bot.tradingProcessDate = new Date(contextVariables.lastFile.valueOf() - global.ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. advanceTime(); function advanceTime() { - bot.tradingProcessDate = new Date(bot.tradingProcessDate.valueOf() + ONE_DAY_IN_MILISECONDS); - previousDay = new Date(bot.tradingProcessDate.valueOf() - ONE_DAY_IN_MILISECONDS); + bot.tradingProcessDate = new Date(bot.tradingProcessDate.valueOf() + global.ONE_DAY_IN_MILISECONDS); + previousDay = new Date(bot.tradingProcessDate.valueOf() - global.ONE_DAY_IN_MILISECONDS); if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> bot.tradingProcessDate = " + bot.tradingProcessDate); } if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> previousDay = " + previousDay); } @@ -381,7 +380,7 @@ } /* Validation that we are not going past the head of the market. */ - if (bot.tradingProcessDate.valueOf() > contextVariables.dateEndOfMarket.valueOf() + ONE_DAY_IN_MILISECONDS - 1) { + if (bot.tradingProcessDate.valueOf() > contextVariables.dateEndOfMarket.valueOf() + global.ONE_DAY_IN_MILISECONDS - 1) { const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } @@ -674,7 +673,7 @@ function writeDataRange(pBegin, pEnd, productCodeName, currentTimeFrameLabel, callBack) { let dataRange = { begin: pBegin.valueOf(), - end: pEnd.valueOf() + ONE_DAY_IN_MILISECONDS + end: pEnd.valueOf() + global.ONE_DAY_IN_MILISECONDS }; let fileContent = JSON.stringify(dataRange); diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 77a0d7e63..59192b43d 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -1,6 +1,5 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILITIES) { const FULL_LOG = true - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 const MODULE_NAME = 'Trading Simulation -> ' + bot.SESSION.name let thisObject = { @@ -110,7 +109,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } else { /* Estimate Initial Candle based on the timeRage configured for the session. */ let firstEnd = candles[0].end - let targetEnd = sessionParameters.timeRange.config.initialDatetime.valueOf() + let targetEnd = sessionParameters.timeRange.config.initialDatetime let diff = targetEnd - firstEnd let amount = diff / sessionParameters.timeFrame.config.value @@ -143,13 +142,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT return } - if (checkInitialAndFinalDatetime() === false) { + if (checkMinimunAndMaximunBalance() === false) { afterLoop() return } - if (checkMinimunAndMaximunBalance() === false) { - afterLoop() + if (checkInitialAndFinalDatetime() === false) { + controlLoop() return } @@ -276,15 +275,15 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function heartBeat() { /* We will produce a simulation level heartbeat in order to inform the user this is running. */ - heartBeatDate = new Date(Math.trunc(candle.begin / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) + heartBeatDate = new Date(Math.trunc(tradingEngine.current.candle.begin.value / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) if (heartBeatDate.valueOf() !== previousheartBeatDate) { let processingDate = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + tradingEngine.current.candle.index.value + ' @ ' + processingDate) } /* Telling the world we are alive and doing well */ - let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime.valueOf()) - let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime.valueOf()) + let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime) + let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime) let currentDateString = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) let currentDate = new Date(heartBeatDate) @@ -335,7 +334,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Market') { continue } let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let currentElement = getElement(thisChart[pluralVariableName], candle, 'Market' + '-' + mapKey + '-' + pluralVariableName) + let currentElement = getElement(thisChart[pluralVariableName], 'Market' + '-' + mapKey + '-' + pluralVariableName) thisChart[singularVariableName] = currentElement } } @@ -358,22 +357,22 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } } - function getElement(pArray, currentCandle, datasetName) { + function getElement(pArray, datasetName) { if (pArray === undefined) { return } try { let element for (let i = 0; i < pArray.length; i++) { element = pArray[i] - if (currentCandle.end === element.end) { // when there is an exact match at the end we take that element + if (tradingEngine.current.candle.end.value === element.end) { // when there is an exact match at the end we take that element return element } else { if ( i > 0 && - element.end > currentCandle.end + element.end > tradingEngine.current.candle.end.value ) { let previousElement = pArray[i - 1] - if (previousElement.end < currentCandle.end) { + if (previousElement.end < tradingEngine.current.candle.end.value) { return previousElement // If one elements goes into the future of currentCandle, then we stop and take the previous element. } else { return @@ -395,7 +394,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT when we are processing Market Files than when we are processing Daily Files. TODO Check that the procedure is good for Beta 6 */ if (bot.processingDailyFiles) { // We are processing Daily Files - let candlesPerDay = ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value + let candlesPerDay = global.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value if (tradingEngine.current.candle.index.value === candles.length - 1) { if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkIfWeAreAtTheHeadOfTheMarket -> Skipping Candle because it is the last one and has not been closed yet.') } @@ -417,11 +416,11 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function checkInitialAndFinalDatetime() { /* Here we check that the current candle is not outside of user-defined time range at the session parameters.*/ - if (tradingEngine.current.candle.end.value < sessionParameters.timeRange.config.initialDatetime.valueOf()) { + if (tradingEngine.current.candle.end.value < sessionParameters.timeRange.config.initialDatetime) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle before the sessionParameters.timeRange.config.initialDatetime.') } return false } - if (tradingEngine.current.candle.begin.value > sessionParameters.timeRange.config.finalDatetime.valueOf()) { + if (tradingEngine.current.candle.begin.value > sessionParameters.timeRange.config.finalDatetime) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> checkInitialAndFinalDatetime -> Skipping Candle after the sessionParameters.timeRange.config.finalDatetime.') } return false } diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index b26ff1f15..66e2f586d 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -4,7 +4,6 @@ const LOG_FILE_CONTENT = false; const MODULE_NAME = "Multi Period Daily"; const GMT_SECONDS = ':00.000 GMT+0000'; - const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; thisObject = { initialize: initialize, diff --git a/server.js b/server.js index b40e410e0..50f2d662c 100644 --- a/server.js +++ b/server.js @@ -27,7 +27,8 @@ global.CUSTOM_FAIL_RESPONSE = { global.ROOT_DIR = './'; -const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; +global.ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 +global.ONE_MIN_IN_MILISECONDS = 60 * 1000 global.LOGGER_MAP = new Map() global.SESSION_MAP = new Map() @@ -316,9 +317,9 @@ function startRoot(processIndex) { } global.getPercentage = function (fromDate, currentDate, lastDate) { - let fromDays = Math.trunc(fromDate.valueOf() / ONE_DAY_IN_MILISECONDS) - let currentDays = Math.trunc(currentDate.valueOf() / ONE_DAY_IN_MILISECONDS) - let lastDays = Math.trunc(lastDate.valueOf() / ONE_DAY_IN_MILISECONDS) + let fromDays = Math.trunc(fromDate.valueOf() / global.ONE_DAY_IN_MILISECONDS) + let currentDays = Math.trunc(currentDate.valueOf() / global.ONE_DAY_IN_MILISECONDS) + let lastDays = Math.trunc(lastDate.valueOf() / global.ONE_DAY_IN_MILISECONDS) let percentage = (currentDays - fromDays) * 100 / (lastDays - fromDays) if ((lastDays - fromDays) === 0) { percentage = 100 @@ -327,8 +328,8 @@ global.getPercentage = function (fromDate, currentDate, lastDate) { } global.areEqualDates = function (date1, date2) { - let day1Days = Math.trunc(date1.valueOf() / ONE_DAY_IN_MILISECONDS) - let day2Days = Math.trunc(date2.valueOf() / ONE_DAY_IN_MILISECONDS) + let day1Days = Math.trunc(date1.valueOf() / global.ONE_DAY_IN_MILISECONDS) + let day2Days = Math.trunc(date2.valueOf() / global.ONE_DAY_IN_MILISECONDS) if (day1Days === day2Days) { return true From 5e6f946f60a48eebeb32b751717c3642b7908d3a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 13 Jun 2020 19:51:41 +0200 Subject: [PATCH 047/113] Fixed Transaction Records --- LowFrequencyTrading/Announcements.js | 6 +- LowFrequencyTrading/TradingEngine.js | 8 +-- LowFrequencyTrading/TradingExecution.js | 12 ++-- LowFrequencyTrading/TradingOutput.js | 8 +-- LowFrequencyTrading/TradingRecords.js | 86 +++++++++++++++++++----- LowFrequencyTrading/TradingSimulation.js | 2 +- 6 files changed, 87 insertions(+), 35 deletions(-) diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js index 99fdb57b3..cb52abb0a 100644 --- a/LowFrequencyTrading/Announcements.js +++ b/LowFrequencyTrading/Announcements.js @@ -45,7 +45,7 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { } } - if (tradingEngine.episode.episodeCounters.periods > lastPeriodAnnounced) { + if (tradingEngine.episode.episodeCounters.periods.value > lastPeriodAnnounced) { if (isNaN(value) === false) { /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ let valueVariation @@ -72,12 +72,12 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ if (newAnnouncementRecord.periods !== undefined) { - newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods + newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods.value newAnnouncementRecord.value = value } else { newAnnouncementRecord = { key: key, - periods: tradingEngine.episode.episodeCounters.periods, + periods: tradingEngine.episode.episodeCounters.periods.value, value: value } variable.announcements.push(newAnnouncementRecord) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 1c35bd47c..12765c8a0 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -133,8 +133,8 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } function updateEpisodeCountersAndStatistics() { - tradingEngine.episode.episodeCounters.periods++ - tradingEngine.episode.episodeStatistics.days = tradingEngine.episode.episodeCounters.periods * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + tradingEngine.episode.episodeCounters.periods.value++ + tradingEngine.episode.episodeStatistics.days.value = tradingEngine.episode.episodeCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS } function updatePositionCountersAndStatistics() { @@ -278,11 +278,11 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } else { tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days * 365 + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } addRecords() diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 169b1f3b6..385792450 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -105,7 +105,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ if (variable.executionContext !== undefined) { if (variable.executionContext.periods !== undefined) { - if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { + if (tradingEngine.episode.episodeCounters.periods.value <= variable.executionContext.periods) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') } takePositionAtSimulation() return @@ -149,7 +149,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { variable.executionContext = { status: 'Taking Position', - periods: tradingEngine.episode.episodeCounters.periods + periods: tradingEngine.episode.episodeCounters.periods.value } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') } @@ -164,7 +164,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } variable.executionContext = { status: 'In a Position', - periods: tradingEngine.episode.episodeCounters.periods, + periods: tradingEngine.episode.episodeCounters.periods.value, amountA: amountA, amountB: amountB, orderId: order.id @@ -244,7 +244,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ if (variable.executionContext !== undefined) { if (variable.executionContext.periods !== undefined) { - if (tradingEngine.episode.episodeCounters.periods <= variable.executionContext.periods) { + if (tradingEngine.episode.episodeCounters.periods.value <= variable.executionContext.periods) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') } closePositionAtSimulation() return @@ -275,7 +275,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { variable.executionContext = { status: 'Closing Position', - periods: tradingEngine.episode.episodeCounters.periods + periods: tradingEngine.episode.episodeCounters.periods.value } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') } @@ -290,7 +290,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } variable.executionContext = { status: 'Position Closed', - periods: tradingEngine.episode.episodeCounters.periods, + periods: tradingEngine.episode.episodeCounters.periods.value, amountA: amountA, amountB: amountB, orderId: order.id diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index bf2ba4679..bc43286f7 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -83,7 +83,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel - readOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) + readOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } @@ -99,7 +99,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel + "/" + dateForPath - readOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) + readOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } @@ -172,7 +172,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel - writeOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) + writeOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } @@ -188,7 +188,7 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let fileName = 'Data.json' let filePath = bot.filePathRoot + '/Output/' + bot.SESSION.folderName + '/' + dataset.parentNode.config.codeName + '/' + dataset.config.codeName + '/' + timeFrameLabel + "/" + dateForPath - writeOutputFile(fileName, filePath, dataset.parentNode.config.pluralVariableName) + writeOutputFile(fileName, filePath, dataset.parentNode.config.codeName) } } } diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 92d5c70c7..c26cc1623 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -14,7 +14,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { return thisObject - function initialize(pOutputDatasets, pOutputDatasetsMap) { + function initialize(pOutputDatasetsMap) { tradingEngine = bot.simulationState.tradingEngine tradingSystem = bot.simulationState.tradingSystem outputDatasetsMap = pOutputDatasetsMap // These are the files turned into arrays, stored in a Map by Product codeName. @@ -52,32 +52,84 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } function scanRecordDefinition(product) { - let rootNode = eval(product.config.nodePath) + /* + The Product Root Node is the root of the node hiriarchy branch that we need to look at + in order to fnd the node where we are going to extract the value. + */ + let productRootNode = eval(product.config.nodePath) let record = [] - for (let j = 0; j < product.recordDefinition.recordProperties.length; j++) { - let recordProperty = product.recordDefinition.recordProperties[j] - let value + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + /* + The Property Root Node is the Root of the Hiriarchy branch we must find in order + to get the node where we are going to extract the value. Initially + we point it to the Product Root Node, because that is the default in case + a property does not have at its configuration a different nodePath configured + pointing to an specific Root for the property. + */ + let propertyRootNode = productRootNode + /* + If we find at the configuration a nodePath, then we take this path to find + the Root Node specifically for this property only. + */ if (recordProperty.config.nodePath !== undefined) { - let childNode = rootNode[recordProperty.config.childNode] - if (recordProperty.config.index !== undefined) { - value = childNode[recordProperty.config.codeName] // this returns an array - value = value[recordProperty.config.index].value // this return a node item of the array and its value - } else { - value = childNode[recordProperty.config.codeName].value // here we get the value of the childNode - } - } else { - value = rootNode[recordProperty.config.codeName].value // this is the standard way + propertyRootNode = eval(recordProperty.config.nodePath) } - if (value === Infinity) { - value = Number.MAX_SAFE_INTEGER + /* + The Target Node is the node from where we are going to exctract the value. + We will use the codeName of the Record Property to match it with + the any of the properties of the Root Node to get the Target Node. + */ + let targetNode = propertyRootNode[recordProperty.config.codeName] + /* + If the codeName of the Record Property can not match the name of the property at + the target node, the user can explicitly specify the property name at the configuration, + and in those cases we need to use that. This happens when there are many Record Properties + pointing to the same property at the Target Node. + */ + if (recordProperty.config.childProperty !== undefined) { + targetNode = propertyRootNode[recordProperty.config.childProperty] } + /* + It can happen that intead of having a Node in targetNode what we have is an + array of nodes. We need to pick one of the elements of the array and for that + we use the Index value we find at the configuration of the Record Property. + */ + if (recordProperty.config.index !== undefined) { + targetNode = targetNode[recordProperty.config.index] + } + /* + By Default the value is extracted from the value property of the Target Node. + But it might happen the the Target Node does not exist for example when there is an Array + of Nodes defined in the Record Properties but not all of them exist at the Root Node. + We filter out those cases by not extracting the value from the value property. + */ + let value + if (targetNode !== undefined) { + value = targetNode.value + } + + value = safeValue(value) if (recordProperty.config.isString === true) { value = '"' + value + '"' } record.push(value) } - return record } } + + function safeValue(value) { + /* + The purpose of this function is to check that value variable does not have a value that + will later break the JSON format of files where this is going to be stored at. + */ + if (value === Infinity) { + value = Number.MAX_SAFE_INTEGER + } + if (value === undefined) { + value = 0 + } + return value + } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 59192b43d..d1eede93d 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -233,7 +233,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT snapshotsModule.manageSnapshots() tradingRecordsModule.appendRecords() - annoucementsModule.makeAnnoucements() // After everything at the simulation level was done, we will do the annoucements that are pending. + announcementsModule.makeAnnoucements() // After everything at the simulation level was done, we will do the annoucements that are pending. controlLoop() } From da115112a6d18a8d4e5083b2e53bd3e38ae42111 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 14 Jun 2020 11:31:45 +0200 Subject: [PATCH 048/113] fix --- LowFrequencyTrading/Announcements.js | 1 + LowFrequencyTrading/Snapshots.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js index cb52abb0a..751836a78 100644 --- a/LowFrequencyTrading/Announcements.js +++ b/LowFrequencyTrading/Announcements.js @@ -10,6 +10,7 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { } let tradingEngine + let announcementsToBeMade = [] return thisObject diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 37c8ddf9b..7a47a6db7 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -34,7 +34,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { saveAsLastTakePositionSnapshot = false addToSnapshots = false - let announcementsToBeMade = [] + return thisObject From 4088d631a821e962af0863dbccefa676589782d1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 14 Jun 2020 13:09:20 +0200 Subject: [PATCH 049/113] initialBalance --- LowFrequencyTrading/TradingEngine.js | 5 ++-- LowFrequencyTrading/TradingOutput.js | 36 +++++++++++++++++++++++- LowFrequencyTrading/TradingRecords.js | 9 ++++-- LowFrequencyTrading/TradingSimulation.js | 3 -- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 12765c8a0..2ccba67eb 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -26,9 +26,10 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine = bot.simulationState.tradingEngine sessionParameters = bot.SESSION.parameters - if (tradingEngine.isInitialized !== true || bot.RESUME === false) { - tradingEngine.isInitialized = true + if (bot.RESUME === false) { initializeNode(tradingEngine) + tradingEngine.current.balance.baseAsset.value = sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.current.balance.quotedAsset.value = sessionParameters.sessionQuotedAsset.config.initialBalance } } diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index bc43286f7..59ada325b 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -56,10 +56,44 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL if (bot.RESUME !== true) { bot.simulationState.tradingEngine = bot.TRADING_ENGINE bot.simulationState.tradingSystem = bot.TRADING_SYSTEM + initializeOutputs() + } else { + readFiles() } - readFiles() return + function initializeOutputs() { + if (bot.processingDailyFiles) { + initializeDailyFiles() + } else { + initializeMarketFiles() + } + } + + function initializeDailyFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Daily Files') { + outputDatasetsMap.set(dataset.parentNode.config.codeName, []) + } + } + runSimulation() + } + + function initializeMarketFiles() { + for (let i = 0; i < outputDatasets.length; i++) { + let outputDatasetNode = outputDatasets[i] + let dataset = outputDatasetNode.referenceParent + + if (dataset.config.type === 'Market Files') { + outputDatasetsMap.set(dataset.parentNode.config.codeName, []) + } + } + runSimulation() + } + function readFiles() { /* This bot have an output of files that it generates. At every call to the bot, it needs to read the previously generated diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index c26cc1623..af897c42a 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -38,17 +38,17 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let dataset = outputDatasetNode.referenceParent let product = dataset.parentNode let record + let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) if (bot.processingDailyFiles === true && dataset.config.type === 'Daily Files') { record = scanRecordDefinition(product) + outputDatasetArray.push(record) } if (bot.processingDailyFiles === false && dataset.config.type === 'Market Files') { record = scanRecordDefinition(product) + outputDatasetArray.push(record) } - - let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) - outputDatasetArray.push(record) } function scanRecordDefinition(product) { @@ -113,6 +113,9 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { if (recordProperty.config.isString === true) { value = '"' + value + '"' } + if (recordProperty.config.decimals !== undefined) { + value = value.toFixed(decimals) + } record.push(value) } return record diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index d1eede93d..36f92621e 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -77,9 +77,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let propertyName = 'at' + sessionParameters.timeFrame.config.label.replace('-', '') let candles = chart[propertyName].candles - /* Last Candle */ - let lastCandle = candles[candles.length - 1] // TODO Maybe remove this - /* Variables needed for heartbeat functionality */ let heartBeatDate let previousheartBeatDate From fb3cdde61a1ab75cd2bc98462f8cf01c6c39cc5a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 15 Jun 2020 16:15:01 +0200 Subject: [PATCH 050/113] Bug Fixed --- LowFrequencyTrading/TradingRecords.js | 2 +- LowFrequencyTrading/TradingSimulation.js | 3 ++- LowFrequencyTrading/TradingSystem.js | 9 ++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index af897c42a..8865f092c 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -114,7 +114,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { value = '"' + value + '"' } if (recordProperty.config.decimals !== undefined) { - value = value.toFixed(decimals) + value = Number(value.toFixed(recordProperty.config.decimals)) } record.push(value) } diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 36f92621e..f6aee62d9 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -151,9 +151,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT heartBeat() positionChartAtCurrentCandle() + tradingSystemModule.setChart(chart) tradingEngineModule.updateEpisodeCountersAndStatistics() - tradingSystemModule.setChart(chart) + tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() tradingSystemModule.checkTriggerOn() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 558683b3b..cf066bc8f 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -100,7 +100,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { case 'node': { if (property.name !== previousPropertyName) { if (node[property.name] !== undefined) { - evalNode(node[property.name]) + evalNode(node[property.name], evaluating) } previousPropertyName = property.name } @@ -109,9 +109,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { case 'array': { if (node[property.name] !== undefined) { let nodePropertyArray = node[property.name] - object[property.name] = [] for (let m = 0; m < nodePropertyArray.length; m++) { - evalNode(nodePropertyArray[m]) + evalNode(nodePropertyArray[m], evaluating) } } break @@ -191,9 +190,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { formulas.set(node.id, value) if (value === true) { - formulaValues.push(1) + tradingSystem.formulasValues.push(1) } else { - formulaValues.push(0) + tradingSystem.formulasValues.push(0) } tradingSystem.formulasErrors.push(error) From 83da1c32aaf3ba6fb710072e0b5a643fb118093a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 16 Jun 2020 19:21:53 +0200 Subject: [PATCH 051/113] hightlights and values working --- LowFrequencyTrading/TradingRecords.js | 29 +++++++++---- LowFrequencyTrading/TradingSimulation.js | 2 +- LowFrequencyTrading/TradingSystem.js | 52 ++++++++++++------------ 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 8865f092c..e74116bbc 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -106,15 +106,26 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { */ let value if (targetNode !== undefined) { - value = targetNode.value - } - - value = safeValue(value) - if (recordProperty.config.isString === true) { - value = '"' + value + '"' - } - if (recordProperty.config.decimals !== undefined) { - value = Number(value.toFixed(recordProperty.config.decimals)) + if (targetNode.type !== undefined) { + /* + In this case the Target Node is really a node (since it has a type), so we extract the value + from its value property. + */ + value = targetNode.value + value = safeValue(value) + if (recordProperty.config.isString === true) { + value = '"' + value + '"' + } + if (recordProperty.config.decimals !== undefined) { + value = Number(value.toFixed(recordProperty.config.decimals)) + } + } else { + /* + In this case the Target Node is not really node, but the value itself. Se we return this as the + value of the Record Property. + */ + value = targetNode + } } record.push(value) } diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index f6aee62d9..da931ff07 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -151,7 +151,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT heartBeat() positionChartAtCurrentCandle() - tradingSystemModule.setChart(chart) + tradingSystemModule.reset(chart) tradingEngineModule.updateEpisodeCountersAndStatistics() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index cf066bc8f..4abd7dacc 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -4,7 +4,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const FULL_LOG = true let thisObject = { - setChart: setChart, + reset: reset, evalConditions: evalConditions, evalFormulas: evalFormulas, checkTriggerOn: checkTriggerOn, @@ -34,30 +34,33 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function initialize() { tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine - - tradingSystem.highlights = [] - tradingSystem.conditionsErrors = [] - tradingSystem.formulasValues = [] - tradingSystem.formulasErrors = [] } function finalize() { chart = undefined conditions = undefined - tradingSystem.highlights = undefined - tradingSystem.conditionsErrors = undefined - formulas = undefined - tradingSystem.formulasValues = undefined - tradingSystem.formulasErrors = undefined + + tradingSystem.highlights = undefined + tradingSystem.errors = undefined + tradingSystem.values = undefined + tradingSystem.status = undefined + tradingSystem.progress = undefined + tradingSystem.running = undefined tradingSystem = undefined tradingEngine = undefined } - function setChart(pChart) { + function reset(pChart) { chart = pChart // We need chat to be a local object accessible from conditions and formulas. + tradingSystem.highlights = [] + tradingSystem.errors = [] + tradingSystem.values = [] + tradingSystem.status = [] + tradingSystem.progress = [] + tradingSystem.running = [] } function evalConditions() { @@ -143,19 +146,20 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { */ } else { error = err.message - node.error = error } } conditions.set(node.id, value) if (value === true) { - tradingSystem.highlights.push(1) - } else { - tradingSystem.highlights.push(0) + tradingSystem.highlights.push([node.id, '']) + } + if (error !== undefined) { + tradingSystem.errors.push([node.id, error]) + } + if (value !== undefined) { + tradingSystem.values.push([node.id, value]) } - - tradingSystem.conditionsErrors.push(error) if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> value = ' + value) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> error = ' + error) } @@ -183,19 +187,17 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { */ } else { error = err.message - node.error = error } } formulas.set(node.id, value) - if (value === true) { - tradingSystem.formulasValues.push(1) - } else { - tradingSystem.formulasValues.push(0) + if (error !== undefined) { + tradingSystem.errors.push([node.id, error]) + } + if (value !== undefined) { + tradingSystem.values.push([node.id, value]) } - - tradingSystem.formulasErrors.push(error) if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } From 4015879faf7f49dcea30f976279da6453ce5f56b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 16 Jun 2020 20:41:01 +0200 Subject: [PATCH 052/113] All Plotting Working --- LowFrequencyTrading/TradingSystem.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 4abd7dacc..401c85da0 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -153,6 +153,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (value === true) { tradingSystem.highlights.push([node.id, '']) + tradingSystem.errors.push([node.id, 'La concha del mono']) + tradingSystem.progress.push([node.id, 85]) + tradingSystem.status.push([node.id, 'Waiting for la concha del mono']) + tradingSystem.running.push([node.id, true]) } if (error !== undefined) { tradingSystem.errors.push([node.id, error]) From d5b1aaded5b3b00283c86acfa4ce2891354bf912 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 17 Jun 2020 17:20:59 +0200 Subject: [PATCH 053/113] Trading System Reviewed --- LowFrequencyTrading/TradingSimulation.js | 78 +-- LowFrequencyTrading/TradingSystem.js | 710 ++++++++++++----------- 2 files changed, 409 insertions(+), 379 deletions(-) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index da931ff07..1e25b3c63 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -40,8 +40,10 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER /* Variables to know when we need to open or close a position. */ - let takePositionNow = false - let closePositionNow = false + let triggeringOn + let triggeringOff + let takingPosition + let closingPosition /* These are the Modules we will need to run the Simulation */ @@ -79,7 +81,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Variables needed for heartbeat functionality */ let heartBeatDate - let previousheartBeatDate + let previousHeartBeatDate initializeLoop() @@ -154,28 +156,27 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.reset(chart) tradingEngineModule.updateEpisodeCountersAndStatistics() - + /* + The next step is about evaluationg Conditions and Formulas of the Trading System. + */ tradingSystemModule.evalConditions() tradingSystemModule.evalFormulas() - tradingSystemModule.checkTriggerOn() - tradingSystemModule.checkTriggerOff() - takePositionNow = tradingSystemModule.checkTakePosition() + /* + Next we check if we will be triggering on, off or taking position. + */ + triggeringOn = tradingSystemModule.checkTriggerOn() + triggeringOff = tradingSystemModule.checkTriggerOff() + takingPosition = tradingSystemModule.checkTakePosition() /* Stop Loss Management */ - if ( - (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && - takePositionNow !== true - ) { - tradingSystemModule.checkStopPhases() + if (takingPosition !== true) { + tradingSystemModule.checkStopPhasesEvents() tradingSystemModule.calculateStopLoss() } /* Take Profit Management */ - if ( - (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && - takePositionNow !== true - ) { - tradingSystemModule.checkTakeProfitPhases() + if (takingPosition !== true) { + tradingSystemModule.checkTakeProfitPhaseEvents() tradingSystemModule.calculateTakeProfit() } @@ -183,51 +184,28 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingEngineModule.updateDistanceToEventsCounters() /* Checking if Stop or Take Profit were hit */ - if ( - (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') && - takePositionNow !== true - ) { - closePositionNow = tradingSystemModule.checkStopLossOrTakeProfitWasHit() + if (takingPosition !== true) { + closingPosition = tradingSystemModule.checkStopLossOrTakeProfitWasHit() } /* Taking a Position */ - if ( - takePositionNow === true - ) { - takePositionNow = false + if (takingPosition === true) { tradingEngineModule.getReadyToTakePosition(candle) tradingExecutionModule.takePosition() tradingEngineModule.takePosition() + takingPosition = false } /* Closing a Position */ - if ( - closePositionNow === true - ) { - closePositionNow = false + if (closingPosition === true) { tradingEngineModule.getReadyToClosePosition(candle) tradingExecutionModule.closePosition() tradingEngineModule.closePosition() + closingPosition = false } - /* Closing the Closing Stage TODO This should die once we have execution working. */ - if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { - tradingEngine.current.strategy.end.value = candle.end - tradingEngine.current.strategy.endRate.value = candle.min - tradingEngine.current.strategy.status.value = 'Closed' - - tradingEngine.current.strategy.index.value = -1 - tradingEngine.current.strategy.stageType.value = 'No Stage' - - timerToCloseStage = 0 - tradingEngine.current.distanceToEvent.triggerOff.value = 1 - - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing the Closing Stage -> Exiting Close Stage.') } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing the Closing Stage -> Waiting for timer.') } - } - } + /* After a position was closed, we need to close the strategy. */ + tradingSystem.exitStrategyAfterPosition() snapshotsModule.manageSnapshots() tradingRecordsModule.appendRecords() @@ -274,7 +252,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function heartBeat() { /* We will produce a simulation level heartbeat in order to inform the user this is running. */ heartBeatDate = new Date(Math.trunc(tradingEngine.current.candle.begin.value / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) - if (heartBeatDate.valueOf() !== previousheartBeatDate) { + if (heartBeatDate.valueOf() !== previousHeartBeatDate) { let processingDate = heartBeatDate.getUTCFullYear() + '-' + utilities.pad(heartBeatDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(heartBeatDate.getUTCDate(), 2) if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Simulation ' + bot.sessionKey + ' Loop # ' + tradingEngine.current.candle.index.value + ' @ ' + processingDate) } @@ -292,7 +270,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) } } - previousheartBeatDate = heartBeatDate.valueOf() + previousHeartBeatDate = heartBeatDate.valueOf() } function positionChartAtCurrentCandle() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 401c85da0..dbad30cfe 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -10,11 +10,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { checkTriggerOn: checkTriggerOn, checkTriggerOff: checkTriggerOff, checkTakePosition: checkTakePosition, - checkStopPhases: checkStopPhases, + checkStopPhasesEvents: checkStopPhasesEvents, calculateStopLoss: calculateStopLoss, - checkTakeProfitPhases: checkTakeProfitPhases, + checkTakeProfitPhaseEvents: checkTakeProfitPhaseEvents, calculateTakeProfit: calculateTakeProfit, checkStopLossOrTakeProfitWasHit: checkStopLossOrTakeProfitWasHit, + exitStrategyAfterPosition: exitStrategyAfterPosition, getPositionSize: getPositionSize, getPositionRate: getPositionRate, initialize: initialize, @@ -152,7 +153,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { conditions.set(node.id, value) if (value === true) { - tradingSystem.highlights.push([node.id, '']) + tradingSystem.highlights.push(node.id) tradingSystem.errors.push([node.id, 'La concha del mono']) tradingSystem.progress.push([node.id, 85]) tradingSystem.status.push([node.id, 'Waiting for la concha del mono']) @@ -208,26 +209,19 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function checkTriggerOn() { - /* Trigger On Conditions */ if ( - tradingEngine.current.strategy.stageType.value === 'No Stage' && - tradingEngine.current.strategy.index.value === -1 + tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue ) { - /* - Here we need to pick a strategy, or if there is not suitable strategy for the current - market conditions, we pass until the next period. - - To pick a new strategy we will evaluate what we call the trigger on. Once we enter - into one strategy, we will ignore market conditions for others. However there is also + To pick a new strategy we will check that any of the situations of the trigger on is true. + Once we enter into one strategy, we will ignore market conditions for others. However there is also a strategy trigger off which can be hit before taking a position. If hit, we would - be outside a strategy again and looking for the condition to enter all over again. + be outside a strategy again and looking for the conditions to enter all over again. */ for (let j = 0; j < tradingSystem.strategies.length; j++) { - if ( // If a strategy was already picked during the loop, we exit it - tradingEngine.current.strategy.stageType.value !== 'No Stage' || - tradingEngine.current.strategy.index.value !== -1 + if ( // If a strategy was already picked during the loop, we exit the loop + tradingEngine.current.strategy.index.value !== tradingEngine.current.strategy.index.config.initialValue ) { continue } let strategy = tradingSystem.strategies[j] @@ -243,45 +237,50 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } 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).value } - if (value === false) { passed = false } } + tradingSystem.values.push([situation.id, passed]) if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(triggerStage.triggerOn.id) + tradingSystem.highlights.push(triggerStage.id) tradingEngine.current.strategy.stageType.value = 'Trigger Stage' - checkAnnouncements(triggerStage) - + tradingEngine.current.strategy.status.value = 'Open' 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 // In case the strategy does not get exited + 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 tradingEngine.current.distanceToEvent.triggerOn.value = 1 + /* TODO See what to do with this: checkAnnouncements(triggerStage.triggerOn) + checkAnnouncements(triggerStage) saveAsLastTriggerOnSnapshot = true + */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Entering into Strategy: ' + strategy.name) } - break + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) } + return true // This Means that we have just met the conditions to trigger on. } } } } } } + return false // This Means that we have not met the conditions to trigger on. } function checkTriggerOff() { - /* Trigger Off Condition */ if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage @@ -301,36 +300,41 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (conditions.get(condition.id) !== undefined) { value = conditions.get(condition.id).value } - if (value === false) { passed = false } } + tradingSystem.values.push([situation.id, passed]) if (passed) { + tradingSystem.highlights.push(situation.id) + 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' tradingEngine.current.strategy.stageType.value = 'No Stage' - tradingEngine.current.strategy.index.value = -1 + tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue tradingEngine.current.distanceToEvent.triggerOff.value = 1 + /* TODO See what to do with this: checkAnnouncements(triggerStage.triggerOff) + */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Exiting Strategy: ' + strategy.name) } - break + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) } + return true // This Means that we have just met the conditions to trigger off. } } } } } - + return false // This Means that we have not met the conditions to trigger off. } function checkTakePosition() { /* Take Position Condition */ if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -352,10 +356,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (value === false) { passed = false } } + tradingSystem.values.push([situation.id, passed]) if (passed) { - tradingEngine.current.strategy.stageType.value = 'Open Stage' - checkAnnouncements(strategy.openStage) + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(triggerStage.takePosition.id) + tradingSystem.highlights.push(triggerStage.id) + tradingEngine.current.strategy.stageType.value = 'Open Stage' tradingEngine.current.position.stopLossStage.value = 'Open Stage' tradingEngine.current.position.takeProfitStage.value = 'Open Stage' tradingEngine.current.position.stopLossPhase.value = 0 @@ -363,125 +370,138 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.situationName.value = situation.name + /* TODO See what to do with this: checkAnnouncements(triggerStage.takePosition) + checkAnnouncements(strategy.openStage) saveAsLastTakePositionSnapshot = true + */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Conditions at the Take Position Event were met.') } - return true + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') } + return true // This Means that we have just met the conditions to take position. } } } } } - return false + return false // This Means that we have not met the conditions to take position. } - function checkStopPhases() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + function checkStopPhasesEvents() { + if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let p - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let parentNode - let j = tradingEngine.current.strategy.index.value - let p - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - parentNode = openStage.initialDefinition - p = tradingEngine.current.position.stopLoss.stopLossPhase.value - } - } - } - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - parentNode = manageStage - p = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 - } - } - - if (parentNode !== undefined) { - let phase = parentNode.stopLoss.phases[p] - - /* Check the next Phase Event. */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let situation = nextPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - 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).value - } - - if (value === false) { passed = false } - } - - if (passed) { - tradingEngine.current.position.stopLoss.stopLossPhase.value++ - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' - if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' - checkAnnouncements(manageStage, 'Take Profit') - } - - checkAnnouncements(nextPhaseEvent) - return + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.stopLoss !== undefined) { + parentNode = openStage.initialDefinition + p = tradingEngine.current.position.stopLoss.stopLossPhase.value } } } - /* Check the Move to Phase Events. */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - let situation = moveToPhaseEvent.situations[k] + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + parentNode = manageStage + p = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 + } + } + + if (parentNode !== undefined) { + let phase = parentNode.stopLoss.phases[p] + + /* Check the next Phase Event. */ + let nextPhaseEvent = phase.nextPhaseEvent + if (nextPhaseEvent !== undefined) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + let situation = nextPhaseEvent.situations[k] let passed if (situation.conditions.length > 0) { 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).value } - if (value === false) { passed = false } } + tradingSystem.values.push([situation.id, passed]) if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(nextPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(parentNode.stopLoss.id) + tradingSystem.highlights.push(parentNode.id) - let moveToPhase = moveToPhaseEvent.referenceParent - if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { - if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { - tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 - } - } - } else { - moveToPhaseEvent.error = 'This Node needs to reference a Phase.' - continue - } - + tradingEngine.current.position.stopLoss.stopLossPhase.value++ tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Take Profit') } - checkAnnouncements(moveToPhaseEvent) - return + checkAnnouncements(nextPhaseEvent) + return // only one event can pass at the time + } + } + } + + /* Check the Move to Phase Events. */ + for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { + let moveToPhaseEvent = phase.moveToPhaseEvents[n] + if (moveToPhaseEvent !== undefined) { + for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { + let situation = moveToPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + if (value === false) { passed = false } + } + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(moveToPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(parentNode.stopLoss.id) + tradingSystem.highlights.push(parentNode.id) + + let moveToPhase = moveToPhaseEvent.referenceParent + if (moveToPhase !== undefined) { + for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { + if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { + tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 + } + } + } else { + tradingSystem.errors.push([moveToPhaseEvent.id, 'This Node needs to reference a Phase.']) + continue + } + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' + if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { + tradingEngine.current.strategy.stageType.value = 'Manage Stage' + checkAnnouncements(manageStage, 'Take Profit') + } + checkAnnouncements(moveToPhaseEvent) + return // only one event can pass at the time + } } } } @@ -490,148 +510,160 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function calculateStopLoss() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - let key + if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - phase = openStage.initialDefinition.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value) + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.stopLoss !== undefined) { + phase = openStage.initialDefinition.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value) + } } } - } - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - phase = manageStage.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value - 1) + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + phase = manageStage.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value - 1) + } } - } - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.stopLoss.value + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.stopLoss.value + tradingEngine.current.position.stopLoss.value = formulas.get(key) - tradingEngine.current.position.stopLoss.value = formulas.get(key) - - if (tradingEngine.current.position.stopLoss.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) + if (tradingEngine.current.position.stopLoss.value !== previousValue) { + checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) + } } } } } - function checkTakeProfitPhases() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + function checkTakeProfitPhaseEvents() { + if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let p - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let parentNode - let j = tradingEngine.current.strategy.index.value - let p - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - parentNode = openStage.initialDefinition - p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - } - } - } - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - parentNode = manageStage - p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 - } - } - - if (parentNode !== undefined) { - let phase = parentNode.takeProfit.phases[p] - if (phase === undefined) { return } // trying to jump to a phase that does not exists. - - /* Check the next Phase Event. */ - let nextPhaseEvent = phase.nextPhaseEvent - if (nextPhaseEvent !== undefined) { - for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let situation = nextPhaseEvent.situations[k] - let passed - if (situation.conditions.length > 0) { - 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).value - } - - if (value === false) { passed = false } - } - - if (passed) { - tradingEngine.current.position.takeProfit.takeProfitPhase.value++ - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' - if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' - checkAnnouncements(manageStage, 'Stop') - } - - checkAnnouncements(nextPhaseEvent) - return + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.takeProfit !== undefined) { + parentNode = openStage.initialDefinition + p = tradingEngine.current.position.takeProfit.takeProfitPhase.value } } } - /* Check the Move to Phase Events. */ - for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { - let moveToPhaseEvent = phase.moveToPhaseEvents[n] - if (moveToPhaseEvent !== undefined) { - for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { - let situation = moveToPhaseEvent.situations[k] + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + parentNode = manageStage + p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 + } + } + + if (parentNode !== undefined) { + let phase = parentNode.takeProfit.phases[p] + if (phase === undefined) { return } // trying to jump to a phase that does not exists. + + /* Check the next Phase Event. */ + let nextPhaseEvent = phase.nextPhaseEvent + if (nextPhaseEvent !== undefined) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + let situation = nextPhaseEvent.situations[k] let passed if (situation.conditions.length > 0) { 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).value } - if (value === false) { passed = false } } + tradingSystem.values.push([situation.id, passed]) if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(nextPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(parentNode.takeProfit.id) + tradingSystem.highlights.push(parentNode.id) - let moveToPhase = moveToPhaseEvent.referenceParent - if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { - if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { - tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 - } - } - } else { - moveToPhaseEvent.error = 'This Node needs to reference a Phase.' - continue - } - + tradingEngine.current.position.takeProfit.takeProfitPhase.value++ tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' checkAnnouncements(manageStage, 'Stop') } - checkAnnouncements(moveToPhaseEvent) - return + checkAnnouncements(nextPhaseEvent) + return // only one event can pass at the time + } + } + } + + /* Check the Move to Phase Events. */ + for (let n = 0; n < phase.moveToPhaseEvents.length; n++) { + let moveToPhaseEvent = phase.moveToPhaseEvents[n] + if (moveToPhaseEvent !== undefined) { + for (let k = 0; k < moveToPhaseEvent.situations.length; k++) { + let situation = moveToPhaseEvent.situations[k] + let passed + if (situation.conditions.length > 0) { + 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).value + } + if (value === false) { passed = false } + } + + tradingSystem.values.push([situation.id, passed]) + if (passed) { + tradingSystem.highlights.push(situation.id) + tradingSystem.highlights.push(nextPhaseEvent.id) + tradingSystem.highlights.push(phase.id) + tradingSystem.highlights.push(parentNode.takeProfit.id) + tradingSystem.highlights.push(parentNode.id) + + let moveToPhase = moveToPhaseEvent.referenceParent + if (moveToPhase !== undefined) { + for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { + if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { + tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 + } + } + } else { + tradingSystem.errors.push([moveToPhaseEvent.id, 'This Node needs to reference a Phase.']) + continue + } + + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' + if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { + tradingEngine.current.strategy.stageType.value = 'Manage Stage' + checkAnnouncements(manageStage, 'Stop') + } + + checkAnnouncements(moveToPhaseEvent) + return // only one event can pass at the time + } } } } @@ -640,36 +672,37 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function calculateTakeProfit() { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - let key + if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - phase = openStage.initialDefinition.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value) + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.takeProfit !== undefined) { + phase = openStage.initialDefinition.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] + key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value) + } } } - } - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - phase = manageStage.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1) + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + phase = manageStage.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] + key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1) + } } - } - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.stopLoss.value + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.stopLoss.value + tradingEngine.current.position.takeProfit.value = formulas.get(key) - tradingEngine.current.position.takeProfit.value = formulas.get(key) - - if (tradingEngine.current.position.takeProfit.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) + if (tradingEngine.current.position.takeProfit.value !== previousValue) { + checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) + } } } } @@ -677,111 +710,130 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkStopLossOrTakeProfitWasHit() { { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] - - /* Checking what happened since the last execution. We need to know if the Stop Loss - or our Take Profit were hit. */ - - /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - - if ( - (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) - ) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Stop Loss was hit.') } - /* - Hit Point Validation - - This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. + if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + /* + Checking what happened since the last execution. We need to know if the Stop Loss + or our Take Profit were hit. */ - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { - tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value + /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ + if ( + (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) + ) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') } + /* + Hit Point Validation + + This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value + } + } else { + if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.max.value + } } - } else { - if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.candle.max.value) { - tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.max.value + + let slippedStopLoss = tradingEngine.current.position.stopLoss.value + + /* Apply the Slippage */ + let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 + + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + slippedStopLoss = slippedStopLoss + slippageAmount + } else { + slippedStopLoss = slippedStopLoss - slippageAmount } + + tradingEngine.current.position.endRate.value = slippedStopLoss + + tradingEngine.current.strategy.stageType.value = 'Close Stage' + checkAnnouncements(strategy.closeStage, 'Stop') + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.position.status.value = 1 + tradingEngine.current.position.exitType.value = 1 + + return true // This means that the STOP was hit. } - let slippedStopLoss = tradingEngine.current.position.stopLoss.value + /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ + if ( + (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) + ) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') } + /* + Hit Point Validation: + + This prevents misscalculations when a formula places the take profit in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value + } + } else { + if (tradingEngine.current.position.takeProfit.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.min.value + } + } - /* Apply the Slippage */ - let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 + let slippedTakeProfit = tradingEngine.current.position.takeProfit.value + /* Apply the Slippage */ + let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - slippedStopLoss = slippedStopLoss + slippageAmount - } else { - slippedStopLoss = slippedStopLoss - slippageAmount + if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + slippedTakeProfit = slippedTakeProfit + slippageAmount + } else { + slippedTakeProfit = slippedTakeProfit - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedTakeProfit + + tradingEngine.current.strategy.stageType.value = 'Close Stage' + checkAnnouncements(strategy.closeStage, 'Take Profit') + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.position.status.value = 1 + tradingEngine.current.position.exitType.value = 2 + + addToSnapshots = true + return true // This means that the Take Profit was hit. } - - tradingEngine.current.position.endRate.value = slippedStopLoss - - tradingEngine.current.strategy.stageType.value = 'Close Stage' - checkAnnouncements(strategy.closeStage, 'Stop') - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value - tradingEngine.current.position.status.value = 1 - tradingEngine.current.position.exitType.value = 1 - - return true } + return false // This means that the neither the STOP nor the Take Profit were hit. + } + } - /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ + function exitStrategyAfterPosition() { + if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { + if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { + tradingEngine.current.strategy.end.value = candle.end + tradingEngine.current.strategy.endRate.value = candle.min + tradingEngine.current.strategy.status.value = 'Closed' - if ( - (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) - ) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Take Profit was hit.') } - /* - Hit Point Validation: + tradingEngine.current.strategy.index.value = -1 + tradingEngine.current.strategy.stageType.value = 'No Stage' - This prevents misscalculations when a formula places the take profit in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ + timerToCloseStage = 0 + tradingEngine.current.distanceToEvent.triggerOff.value = 1 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { - tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value - } - } else { - if (tradingEngine.current.position.takeProfit.value < tradingEngine.current.candle.min.value) { - tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.min.value - } - } - - let slippedTakeProfit = tradingEngine.current.position.takeProfit.value - /* Apply the Slippage */ - let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 - - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { - slippedTakeProfit = slippedTakeProfit + slippageAmount - } else { - slippedTakeProfit = slippedTakeProfit - slippageAmount - } - - tradingEngine.current.position.endRate.value = slippedTakeProfit - - tradingEngine.current.strategy.stageType.value = 'Close Stage' - checkAnnouncements(strategy.closeStage, 'Take Profit') - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - - tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value - tradingEngine.current.position.status.value = 1 - tradingEngine.current.position.exitType.value = 2 - - addToSnapshots = true - return true + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Exiting the Strategy.') } + } else { + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Waiting for timer.') } } } - return false } function getPositionSize() { From 4281ec7ae97a2a282dc61453e564eab292b5414b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 18 Jun 2020 11:37:08 +0200 Subject: [PATCH 054/113] bug fixed --- LowFrequencyTrading/TradingEngine.js | 10 +++++----- LowFrequencyTrading/TradingExecution.js | 4 ++-- LowFrequencyTrading/TradingSimulation.js | 2 +- LowFrequencyTrading/TradingSystem.js | 18 +++++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 2ccba67eb..43e8ad95b 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -193,7 +193,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { /* We take what was calculated at the formula and apply the slippage. */ let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount } else { tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount @@ -216,7 +216,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let feePaid = 0 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid @@ -242,7 +242,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let feePaid = 0 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 @@ -256,7 +256,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.current.balance.baseAsset.value = 0 } - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } @@ -276,7 +276,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.episode.episodeCounters.fails.value++ } - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 385792450..44d92a1fb 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -131,7 +131,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { let amountB let orderSide - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { orderSide = 'sell' orderPrice = tradingEngine.current.position.rate.value @@ -257,7 +257,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { let amountB let orderSide - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { orderSide = 'buy' orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 1e25b3c63..05d753b2b 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -417,7 +417,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let maximumBalance let balance - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { balance = tradingEngine.current.balance.baseAsset.value minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index dbad30cfe..d48911f65 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -719,8 +719,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ if ( - (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) + (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) ) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') } /* @@ -729,7 +729,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value } @@ -744,7 +744,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Apply the Slippage */ let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { slippedStopLoss = slippedStopLoss + slippageAmount } else { slippedStopLoss = slippedStopLoss - slippageAmount @@ -766,8 +766,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ if ( - (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.marketBaseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) + (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || + (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) ) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') } /* @@ -776,7 +776,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { This prevents misscalculations when a formula places the take profit in this case way beyond the market price. If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value } @@ -790,7 +790,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Apply the Slippage */ let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { slippedTakeProfit = slippedTakeProfit + slippageAmount } else { slippedTakeProfit = slippedTakeProfit - slippageAmount @@ -838,7 +838,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function getPositionSize() { let balance - if (sessionParameters.sessionBaseAsset.name === bot.market.marketBaseAsset) { + if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { balance = tradingEngine.current.balance.baseAsset.value } else { balance = tradingEngine.current.balance.quotedAsset.value From b42fcf0038c2ea9b8b17ab81ddbb070fd4350431 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 18 Jun 2020 12:19:20 +0200 Subject: [PATCH 055/113] debugging --- LowFrequencyTrading/Snapshots.js | 2 +- LowFrequencyTrading/TradingEngine.js | 12 ++++---- LowFrequencyTrading/TradingExecution.js | 4 +-- LowFrequencyTrading/TradingSimulation.js | 6 ++-- LowFrequencyTrading/TradingSystem.js | 36 ++++++++++++------------ 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 7a47a6db7..64a77e85c 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -62,7 +62,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let closeValues = [ tradingEngine.episode.positionCounters.positions.value, // Position Number (new Date(candle.begin)).toISOString(), // Datetime - tradingSystem.strategies[tradingEngine.current.strategy.index.value].name, // Strategy Name + tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].name, // Strategy Name tradingEngine.current.strategy.situationName.value, // Trigger On Situation tradingEngine.current.position.situationName.value, // Take Position Situation hitOrFial(), // Result diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 43e8ad95b..a755b5cd8 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -193,7 +193,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { /* We take what was calculated at the formula and apply the slippage. */ let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount } else { tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount @@ -216,7 +216,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let feePaid = 0 - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid @@ -234,7 +234,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.current.distanceToEvent.closePosition.value = 1 /* Position size and rate */ - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] } function closePosition() { @@ -242,7 +242,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let feePaid = 0 - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 @@ -256,7 +256,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.current.balance.baseAsset.value = 0 } - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } @@ -276,7 +276,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.episode.episodeCounters.fails.value++ } - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 44d92a1fb..b2cfdf148 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -131,7 +131,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { let amountB let orderSide - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { orderSide = 'sell' orderPrice = tradingEngine.current.position.rate.value @@ -257,7 +257,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { let amountB let orderSide - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { orderSide = 'buy' orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 05d753b2b..3e5105861 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -46,7 +46,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let closingPosition /* These are the Modules we will need to run the Simulation */ - const EXCHANGE_API = require('../ExchangeAPI'); exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); exchangeAPI.initialize(); @@ -83,6 +82,9 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let heartBeatDate let previousHeartBeatDate + /* This determines where we are standing */ + bot.sessionAndMarketBaseAssetsAreEqual = (sessionParameters.sessionBaseAsset.referenceParent.referenceParent.config.codeName === bot.market.baseAsset) + initializeLoop() function initializeLoop() { @@ -417,7 +419,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let maximumBalance let balance - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { balance = tradingEngine.current.balance.baseAsset.value minimumBalance = sessionParameters.sessionBaseAsset.config.minimumBalance maximumBalance = sessionParameters.sessionBaseAsset.config.maximumBalance diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index d48911f65..b0564d527 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -219,12 +219,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { be outside a strategy again and looking for the conditions to enter all over again. */ - for (let j = 0; j < tradingSystem.strategies.length; j++) { + for (let j = 0; j < tradingSystem.tradingStrategies.length; j++) { if ( // If a strategy was already picked during the loop, we exit the loop tradingEngine.current.strategy.index.value !== tradingEngine.current.strategy.index.config.initialValue ) { continue } - let strategy = tradingSystem.strategies[j] + let strategy = tradingSystem.tradingStrategies[j] let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -282,7 +282,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkTriggerOff() { if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -334,7 +334,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkTakePosition() { /* Take Position Condition */ if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -388,7 +388,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkStopPhasesEvents() { if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode @@ -511,7 +511,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function calculateStopLoss() { if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let phase @@ -548,7 +548,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkTakeProfitPhaseEvents() { if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode @@ -673,7 +673,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function calculateTakeProfit() { if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage let phase @@ -711,7 +711,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkStopLossOrTakeProfitWasHit() { { if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] /* Checking what happened since the last execution. We need to know if the Stop Loss or our Take Profit were hit. @@ -719,7 +719,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ if ( - (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || + (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) ) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') } @@ -729,7 +729,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value } @@ -744,7 +744,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Apply the Slippage */ let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { slippedStopLoss = slippedStopLoss + slippageAmount } else { slippedStopLoss = slippedStopLoss - slippageAmount @@ -766,7 +766,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ if ( - (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || + (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) ) { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') } @@ -776,7 +776,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { This prevents misscalculations when a formula places the take profit in this case way beyond the market price. If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value } @@ -790,7 +790,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Apply the Slippage */ let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { slippedTakeProfit = slippedTakeProfit + slippageAmount } else { slippedTakeProfit = slippedTakeProfit - slippageAmount @@ -838,13 +838,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function getPositionSize() { let balance - if (sessionParameters.sessionBaseAsset.name === bot.market.baseAsset) { + if (bot.sessionAndMarketBaseAssetsAreEqual) { balance = tradingEngine.current.balance.baseAsset.value } else { balance = tradingEngine.current.balance.quotedAsset.value } const DEFAULT_VALUE = balance - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] if (strategy.openStage === undefined) return { DEFAULT_VALUE } if (strategy.openStage.initialDefinition === undefined) return { DEFAULT_VALUE } @@ -858,7 +858,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function getPositionRate() { const DEFAULT_VALUE = tradingEngine.current.candle.close.value - let strategy = tradingSystem.strategies[tradingEngine.current.strategy.index.value] + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] if (strategy.openStage === undefined) return { DEFAULT_VALUE } if (strategy.openStage.initialDefinition === undefined) return { DEFAULT_VALUE } From 083788d36c83e768662ce7babb947abc7bbdabc6 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 18 Jun 2020 15:21:22 +0200 Subject: [PATCH 056/113] saving objects --- LowFrequencyTrading/TradingRecords.js | 33 +++++++++++++-- LowFrequencyTrading/TradingSimulation.js | 4 +- LowFrequencyTrading/TradingSystem.js | 51 +++++++++++++----------- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index e74116bbc..2ff28155a 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -37,16 +37,41 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let outputDatasetNode = outputDatasets[i] let dataset = outputDatasetNode.referenceParent let product = dataset.parentNode - let record let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) if (bot.processingDailyFiles === true && dataset.config.type === 'Daily Files') { - record = scanRecordDefinition(product) - outputDatasetArray.push(record) + persistRecord() } if (bot.processingDailyFiles === false && dataset.config.type === 'Market Files') { - record = scanRecordDefinition(product) + persistRecord() + } + + function persistRecord() { + 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) { + outputDatasetArray.splice(outputDatasetArray.length - 1, 1) + + } + } + } + } + } + /* We add every record to the existing file. */ outputDatasetArray.push(record) } } diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 3e5105861..3ea233ffd 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -82,7 +82,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let heartBeatDate let previousHeartBeatDate - /* This determines where we are standing */ + /* This determines where we are standing */ bot.sessionAndMarketBaseAssetsAreEqual = (sessionParameters.sessionBaseAsset.referenceParent.referenceParent.config.codeName === bot.market.baseAsset) initializeLoop() @@ -207,7 +207,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT } /* After a position was closed, we need to close the strategy. */ - tradingSystem.exitStrategyAfterPosition() + tradingSystemModule.exitStrategyAfterPosition() snapshotsModule.manageSnapshots() tradingRecordsModule.appendRecords() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index b0564d527..bef5179ae 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -77,10 +77,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Here we check if there is a codition to be evaluated */ if (node.type === 'Condition' && evaluating === 'Conditions') { - if (node.code !== undefined) { - /* We will eval this condition */ - evalCondition(node) - } + /* We will eval this condition */ + evalCondition(node) } /* Here we check if there is a formula to be evaluated */ @@ -127,11 +125,21 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function evalCondition(node) { let value let error + /* + The code can be at the condition node if it was done with the Conditions Editor, or it can also be + at a Javascript Code node. If there is a Javascript object we will give it preference and take the code + from there. Otherwise we will take the code from the Condition node. + */ + let code = node.code + if (node.javascriptCode !== undefined) { + if (node.javascriptCode.code !== undefined) { + code = node.javascriptCode.code + } + } try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> code = ' + node.code) } - - value = eval(node.code) + if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> code = ' + code) } + value = eval(code) } catch (err) { /* One possible error is that the conditions references a .previous that is undefined. This @@ -139,8 +147,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { */ value = false - if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || - node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 + if (code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || + code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 ) { /* We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. @@ -154,10 +162,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (value === true) { tradingSystem.highlights.push(node.id) - tradingSystem.errors.push([node.id, 'La concha del mono']) - tradingSystem.progress.push([node.id, 85]) - tradingSystem.status.push([node.id, 'Waiting for la concha del mono']) - tradingSystem.running.push([node.id, true]) } if (error !== undefined) { tradingSystem.errors.push([node.id, error]) @@ -240,9 +244,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) @@ -298,9 +302,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) @@ -350,10 +354,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) @@ -363,11 +367,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.id) tradingEngine.current.strategy.stageType.value = 'Open Stage' - tradingEngine.current.position.stopLossStage.value = 'Open Stage' - tradingEngine.current.position.takeProfitStage.value = 'Open Stage' - tradingEngine.current.position.stopLossPhase.value = 0 - tradingEngine.current.position.takeProfitPhase.value = 0 - + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Open Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Open Stage' + tradingEngine.current.position.stopLoss.stopLossPhase.value = 0 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = 0 tradingEngine.current.position.situationName.value = situation.name /* TODO See what to do with this: From 7e142a01dc51e5a6af11b03e6cb163cfbd46eb8c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 18 Jun 2020 16:02:50 +0200 Subject: [PATCH 057/113] cleaning --- LowFrequencyTrading/TradingEngine.js | 143 ++---------------- LowFrequencyTrading/TradingExecution.js | 183 +++++++++++------------ LowFrequencyTrading/TradingSimulation.js | 9 +- LowFrequencyTrading/TradingSystem.js | 153 +++++++++++++++++-- 4 files changed, 240 insertions(+), 248 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index a755b5cd8..3e231918a 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -1,16 +1,11 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { - const MODULE_NAME = 'Trading Engine' - let thisObject = { setCurrentCandle: setCurrentCandle, updateEpisodeCountersAndStatistics: updateEpisodeCountersAndStatistics, + resetPositionCountersAndStatistics: resetPositionCountersAndStatistics, updatePositionCountersAndStatistics: updatePositionCountersAndStatistics, updateDistanceToEventsCounters: updateDistanceToEventsCounters, - getReadyToTakePosition: getReadyToTakePosition, - takePosition: takePosition, - getReadyToClosePosition: getReadyToClosePosition, - closePosition: closePosition, initialize: initialize, finalize: finalize } @@ -138,20 +133,23 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.episode.episodeStatistics.days.value = tradingEngine.episode.episodeCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS } + function resetPositionCountersAndStatistics() { + /* Keeping Position Counters Up-to-date */ + if ( + (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') + ) { return } + + tradingEngine.current.position.positionCounters.periods.value = tradingEngine.current.position.positionCounters.periods.config.initialValue + tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionStatistics.days.config.initialValue + } + function updatePositionCountersAndStatistics() { /* Keeping Position Counters Up-to-date */ if ( (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') ) { - if (takePositionNow === true) { - tradingEngine.current.position.positionCounters.periods.value = 0 - } - tradingEngine.current.position.positionCounters.periods.value++ tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS - } else { - tradingEngine.current.position.positionCounters.periods.value = 0 - tradingEngine.current.position.positionStatistics.days.value = 0 } } @@ -182,124 +180,5 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } } - function getReadyToTakePosition(candle) { - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.takePosition.value = 1 - - /* Position size and rate */ - tradingEngine.current.position.size.value = getPositionSize() - tradingEngine.current.position.rate.value = getPositionRate() - - /* We take what was calculated at the formula and apply the slippage. */ - let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount - } else { - tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount - } - - /* Update the trade record information. */ - tradingEngine.current.position.begin.value = candle.begin - tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value - } - - function takePosition() { - calculateTakeProfit() // TODO: Check if this is really necesary - calculateStopLoss() // TODO: Check if this is really necesary - - tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - - tradingEngine.last.position.profitLoss.value = 0 - tradingEngine.last.position.ROI.value = 0 - - let feePaid = 0 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value - } else { - feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value - } - } - - function getReadyToClosePosition() { - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.closePosition.value = 1 - - /* Position size and rate */ - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - } - - function closePosition() { - tradingEngine.episode.positionCounters.positions.value++ - - let feePaid = 0 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - - feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = 0 - } else { - - feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.baseAsset.value = 0 - } - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value - tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance - } else { - tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value - tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance - } - - tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.ROI.value - - if (tradingEngine.last.position.profitLoss.value > 0) { - tradingEngine.episode.episodeCounters.hits.value++ - } else { - tradingEngine.episode.episodeCounters.fails.value++ - } - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 - } else { - tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 - } - - addRecords() - - tradingEngine.current.position.stopLoss.value = 0 - tradingEngine.current.position.takeProfit.value = 0 - - tradingEngine.current.position.rate.value = 0 - tradingEngine.current.position.size.value = 0 - - timerToCloseStage = candle.begin - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 - tradingEngine.current.position.takeProfit.takeProfitPhase.value = -1 - - } } diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index b2cfdf148..1016cb64b 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -24,70 +24,65 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { } function takePosition() { - /* Check if we need to execute. */ - if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ - /* Check that we are in LIVE 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) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) + /* Check that we are in LIVE 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) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) + logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // We need to put the order because It was not put yet. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } + /* We see if we need to put the actual order at the exchange. */ + if (variable.executionContext !== undefined) { + switch (variable.executionContext.status) { + case 'Without a Position': { // We need to put the order because It was not put yet. + if (strategy.openStage !== undefined) { + if (strategy.openStage.openExecution !== undefined) { + putOpeningOrder() + return } - break - } - case 'Position Closed': { // Waiting for a confirmation that the position was closed. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return - } - } - break - } - case 'Taking Position': { // Waiting for a confirmation that the position was taken. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is Taking Position.') } - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is In a Position.') } - break } + break } - } else { // The context does not exist so it means we are not in a position. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return + case 'Position Closed': { // Waiting for a confirmation that the position was closed. + if (strategy.openStage !== undefined) { + if (strategy.openStage.openExecution !== undefined) { + putOpeningOrder() + return + } } + break + } + case 'Taking Position': { // Waiting for a confirmation that the position was taken. + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is Taking Position.') + break + } + case 'In a Position': { // This should mean that we already put the order at the exchange. + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is In a Position.') + break + } + } + } else { // The context does not exist so it means we are not in a position. + if (strategy.openStage !== undefined) { + if (strategy.openStage.openExecution !== undefined) { + putOpeningOrder() + return } } - } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not trading live.') } } } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not the last closed candle.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not trading live.') } } function putOpeningOrder() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Entering function.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Entering function.') /* We wont take a position unless we are withing the sessionParameters.timeRange.config.initialDatetime and the sessionParameters.timeRange.config.finalDatetime range */ if (sessionParameters.timeRange.config.initialDatetime !== undefined) { if (candle.end < sessionParameters.timeRange.config.initialDatetime) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) takePositionAtSimulation() return } @@ -96,7 +91,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { /* We wont take a position if we are past the final datetime */ if (sessionParameters.timeRange.config.finalDatetime !== undefined) { if (candle.begin > sessionParameters.timeRange.config.finalDatetime) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } + logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) takePositionAtSimulation() return } @@ -106,7 +101,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { if (variable.executionContext !== undefined) { if (variable.executionContext.periods !== undefined) { if (tradingEngine.episode.episodeCounters.periods.value <= variable.executionContext.periods) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') takePositionAtSimulation() return } @@ -118,9 +113,9 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { let today = new Date(Math.trunc((new Date().valueOf()) / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) let processDay = new Date(Math.trunc(currentDay.valueOf() / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) if (today.valueOf() !== processDay.valueOf()) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> processDay = ' + processDay) } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> processDay = ' + processDay) takePositionAtSimulation() return } @@ -152,16 +147,16 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { periods: tradingEngine.episode.episodeCounters.periods.value } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) function onOrderCreated(err, order) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Entering function.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Entering function.') try { switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') variable.executionContext = { status: 'In a Position', periods: tradingEngine.episode.episodeCounters.periods.value, @@ -173,21 +168,21 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { return } case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') + logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) strategy.openStage.openExecution.error = err.message afterLoop() return } case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') + logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) strategy.openStage.openExecution.error = err.message afterLoop() return } } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } + logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) callBackFunction(global.DEFAULT_FAIL_RESPONSE) return } catch (err) { @@ -199,39 +194,37 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { } function closePosition() { - if (currentCandleIndex > candles.length - 10) { /* Only at the last candles makes sense to check if we are in live mode or not. */ - /* Check that we are in LIVE MODE */ - if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // No way to close anything at the exchange. - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Without a Position.') } - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } + /* Check that we are in LIVE MODE */ + if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { + /* We see if we need to put the actual order at the exchange. */ + if (variable.executionContext !== undefined) { + switch (variable.executionContext.status) { + case 'Without a Position': { // No way to close anything at the exchange. + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Without a Position.') + break + } + case 'In a Position': { // This should mean that we already put the order at the exchange. + if (strategy.closeStage !== undefined) { + if (strategy.closeStage.closeExecution !== undefined) { + putClosingOrder() + return } - break } - case 'Closing Position': { // Waiting for a confirmation that the position was taken. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } + break + } + case 'Closing Position': { // Waiting for a confirmation that the position was taken. + if (strategy.closeStage !== undefined) { + if (strategy.closeStage.closeExecution !== undefined) { + putClosingOrder() + return } - break } + break + } - case 'Position Closed': { // - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Position Closed.') } - break - } + case 'Position Closed': { // + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Position Closed.') + break } } } @@ -239,13 +232,11 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { } function putClosingOrder() { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> putClosingOrder -> Entering function.') } - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ if (variable.executionContext !== undefined) { if (variable.executionContext.periods !== undefined) { if (tradingEngine.episode.episodeCounters.periods.value <= variable.executionContext.periods) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') closePositionAtSimulation() return } @@ -278,16 +269,16 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { periods: tradingEngine.episode.episodeCounters.periods.value } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) function onOrderCreated(err, order) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Entering function.') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Entering function.') try { switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') variable.executionContext = { status: 'Position Closed', periods: tradingEngine.episode.episodeCounters.periods.value, @@ -299,20 +290,20 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { return } case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') /* We will assume that the problem is temporary, and expect that it will work at the next execution. */ strategy.closeStage.closeExecution.error = err.message afterLoop() return } case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') } + logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') strategy.closeStage.closeExecution.error = err.message afterLoop() return } } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) } + logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) callBackFunction(global.DEFAULT_FAIL_RESPONSE) return } catch (err) { diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 3ea233ffd..7060f0a9f 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -182,6 +182,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.calculateTakeProfit() } + tradingEngineModule.resetPositionCountersAndStatistics() tradingEngineModule.updatePositionCountersAndStatistics() tradingEngineModule.updateDistanceToEventsCounters() @@ -192,17 +193,17 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Taking a Position */ if (takingPosition === true) { - tradingEngineModule.getReadyToTakePosition(candle) + tradingSystemModule.getReadyToTakePosition() tradingExecutionModule.takePosition() - tradingEngineModule.takePosition() + tradingSystemModule.takePosition() takingPosition = false } /* Closing a Position */ if (closingPosition === true) { - tradingEngineModule.getReadyToClosePosition(candle) + tradingSystemModule.getReadyToClosePosition() tradingExecutionModule.closePosition() - tradingEngineModule.closePosition() + tradingSystemModule.closePosition() closingPosition = false } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index bef5179ae..0d2f92d4f 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -1,8 +1,5 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { - const MODULE_NAME = 'Trading System' - const FULL_LOG = true - let thisObject = { reset: reset, evalConditions: evalConditions, @@ -15,6 +12,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { checkTakeProfitPhaseEvents: checkTakeProfitPhaseEvents, calculateTakeProfit: calculateTakeProfit, checkStopLossOrTakeProfitWasHit: checkStopLossOrTakeProfitWasHit, + getReadyToTakePosition: getReadyToTakePosition, + takePosition: takePosition, + getReadyToClosePosition: getReadyToClosePosition, + closePosition: closePosition, exitStrategyAfterPosition: exitStrategyAfterPosition, getPositionSize: getPositionSize, getPositionRate: getPositionRate, @@ -138,7 +139,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> code = ' + code) } + logger.write(MODULE_NAME, '[INFO] evalCondition -> code = ' + code) value = eval(code) } catch (err) { /* @@ -170,8 +171,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.values.push([node.id, value]) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> value = ' + value) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalCondition -> error = ' + error) } + logger.write(MODULE_NAME, '[INFO] evalCondition -> value = ' + value) + logger.write(MODULE_NAME, '[INFO] evalCondition -> error = ' + error) } function evalFormula(node) { @@ -179,7 +180,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let error try { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> code = ' + node.code) } + logger.write(MODULE_NAME, '[INFO] evalFormula -> code = ' + node.code) value = eval(node.code) } catch (err) { /* @@ -208,8 +209,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.values.push([node.id, value]) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) } - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } + logger.write(MODULE_NAME, '[INFO] evalFormula -> value = ' + value) + logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } function checkTriggerOn() { @@ -273,7 +274,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { saveAsLastTriggerOnSnapshot = true */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) } + logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) return true // This Means that we have just met the conditions to trigger on. } } @@ -325,7 +326,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { checkAnnouncements(triggerStage.triggerOff) */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) } + logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) return true // This Means that we have just met the conditions to trigger off. } } @@ -379,7 +380,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { saveAsLastTakePositionSnapshot = true */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') } + logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') return true // This Means that we have just met the conditions to take position. } } @@ -725,7 +726,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) ) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') } + logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') /* Hit Point Validation @@ -772,7 +773,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) ) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') } + logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') /* Hit Point Validation: @@ -819,6 +820,126 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } + function getReadyToTakePosition() { + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.takePosition.value = 1 + + /* Position size and rate */ + tradingEngine.current.position.size.value = getPositionSize() + tradingEngine.current.position.rate.value = getPositionRate() + + /* We take what was calculated at the formula and apply the slippage. */ + let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount + } else { + tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount + } + + /* Update the position information. */ + tradingEngine.current.position.begin.value = tradingEngine.current.candle.begin.value + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value + } + + function takePosition() { + calculateTakeProfit() // TODO: Check if this is really necesary + calculateStopLoss() // TODO: Check if this is really necesary + + tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + + tradingEngine.last.position.profitLoss.value = 0 + tradingEngine.last.position.ROI.value = 0 + + let feePaid = 0 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value + } else { + feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value + } + } + + function getReadyToClosePosition() { + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.closePosition.value = 1 + + /* Position size and rate */ + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + } + + function closePosition() { + tradingEngine.episode.positionCounters.positions.value++ + + let feePaid = 0 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + + feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = 0 + } else { + + feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid + tradingEngine.current.balance.baseAsset.value = 0 + } + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value + tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance + } else { + tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value + tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance + } + + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.ROI.value + + if (tradingEngine.last.position.profitLoss.value > 0) { + tradingEngine.episode.episodeCounters.hits.value++ + } else { + tradingEngine.episode.episodeCounters.fails.value++ + } + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + } else { + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + } + + addRecords() + + tradingEngine.current.position.stopLoss.value = 0 + tradingEngine.current.position.takeProfit.value = 0 + + tradingEngine.current.position.rate.value = 0 + tradingEngine.current.position.size.value = 0 + + timerToCloseStage = candle.begin + tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' + tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = -1 + + } + function exitStrategyAfterPosition() { if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { @@ -832,9 +953,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { timerToCloseStage = 0 tradingEngine.current.distanceToEvent.triggerOff.value = 1 - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Exiting the Strategy.') } + logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Exiting the Strategy.') } else { - if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Waiting for timer.') } + logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Waiting for timer.') } } } From ce71e3332a36960bcbbc12d0634eec8cee830724 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 18 Jun 2020 17:02:47 +0200 Subject: [PATCH 058/113] Simulation Runing Again --- LowFrequencyTrading/Snapshots.js | 4 ++-- LowFrequencyTrading/TradingSystem.js | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 64a77e85c..6904dc2e4 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -66,12 +66,12 @@ exports.newSnapshots = function newSnapshots(bot, logger) { tradingEngine.current.strategy.situationName.value, // Trigger On Situation tradingEngine.current.position.situationName.value, // Take Position Situation hitOrFial(), // Result - tradingEngine.last.position.ROI.value, // ROI + tradingEngine.last.position.positionStatistics.ROI.value, // ROI exitType() // Exit Type ] function hitOrFial() { - if (tradingEngine.last.position.ROI.value > 0) { return 'HIT' } else { return 'FAIL' } + if (tradingEngine.last.position.positionStatistics.ROI.value > 0) { return 'HIT' } else { return 'FAIL' } } function exitType() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 0d2f92d4f..aa50c7ad5 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -27,6 +27,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let tradingSystem let tradingEngine + let sessionParameters let conditions = new Map() let formulas = new Map() @@ -36,6 +37,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function initialize() { tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters } function finalize() { @@ -53,6 +55,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem = undefined tradingEngine = undefined + sessionParameters = undefined } function reset(pChart) { @@ -724,7 +727,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ if ( (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.stopLoss.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) + (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) ) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') /* @@ -771,7 +774,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ if ( (bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.takeProfit.value) || - (sessionParameters.sessionBaseAsset.name !== bot.market.baseAsset && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) + (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) ) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') /* @@ -849,8 +852,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.last.position.profitLoss.value = 0 - tradingEngine.last.position.ROI.value = 0 + tradingEngine.last.position.positionStatistics.profitLoss.value = 0 + tradingEngine.last.position.positionStatistics.ROI.value = 0 let feePaid = 0 @@ -895,20 +898,20 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value - tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.last.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value + tradingEngine.last.position.positionStatistics.ROI.value = tradingEngine.last.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.positionStatistics.ROI.value)) { tradingEngine.last.position.positionStatistics.ROI.value = 0 } tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance } else { - tradingEngine.last.position.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value - tradingEngine.last.position.ROI.value = tradingEngine.last.position.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.ROI.value)) { tradingEngine.last.position.ROI.value = 0 } + tradingEngine.last.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value + tradingEngine.last.position.positionStatistics.ROI.value = tradingEngine.last.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value + if (isNaN(tradingEngine.last.position.positionStatistics.ROI.value)) { tradingEngine.last.position.positionStatistics.ROI.value = 0 } tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance } - tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.ROI.value + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.positionStatistics.ROI.value - if (tradingEngine.last.position.profitLoss.value > 0) { + if (tradingEngine.last.position.positionStatistics.profitLoss.value > 0) { tradingEngine.episode.episodeCounters.hits.value++ } else { tradingEngine.episode.episodeCounters.fails.value++ From 7ba3d51c708db5db37b18df4f7c3e7ba6e21eb35 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 20 Jun 2020 14:50:05 +0200 Subject: [PATCH 059/113] Update Commons.js --- Commons.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Commons.js b/Commons.js index 5037502fe..f3b4cc853 100644 --- a/Commons.js +++ b/Commons.js @@ -409,7 +409,7 @@ results.push(product[variableName]); } - /* While we are positioned at Yesterday, we keey updating this data structure. */ + /* While we are positioned at Yesterday, we keep updating this data structure. */ if (processingDailyFiles) { if (positionedAtYesterday) { yesterday.variable = JSON.parse(JSON.stringify(variable)) From 5217343f408bf5af5e305964a2062b9359a05738 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 20 Jun 2020 14:59:32 +0200 Subject: [PATCH 060/113] Some module descrptions --- LowFrequencyTrading/Snapshots.js | 4 +++- LowFrequencyTrading/TradingEngine.js | 4 ++++ LowFrequencyTrading/TradingExecution.js | 4 +++- LowFrequencyTrading/TradingOutput.js | 4 ++++ LowFrequencyTrading/TradingProcess.js | 3 +-- LowFrequencyTrading/TradingRecords.js | 4 +++- LowFrequencyTrading/TradingSimulation.js | 4 ++++ LowFrequencyTrading/TradingSystem.js | 4 ++++ 8 files changed, 26 insertions(+), 5 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 6904dc2e4..85c7329ed 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -1,5 +1,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { - + /* + This module encapsulates the snapshots functionality. + */ const MODULE_NAME = 'Snapshots' let thisObject = { diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 3e231918a..c122735f5 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -1,4 +1,8 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { + /* + We call the Trading Engine to the data structure that is needed in order to exevute the + trading protocol with the specific rules defined at the Trading System. + */ const MODULE_NAME = 'Trading Engine' let thisObject = { setCurrentCandle: setCurrentCandle, diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 1016cb64b..edf84a9d0 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -1,5 +1,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { - + /* + The Trading Execution modules manages the execution of orders against the exchanges. + */ const MODULE_NAME = 'Trading Execution' let thisObject = { diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index 59ada325b..5c99087ff 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -1,4 +1,8 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FILE_STORAGE) { + /* + This module will load if necesary all the data outputs so that they can be appended with new + records if needed. After running the simulation, it will save all the data outputs. + */ const FULL_LOG = true const MODULE_NAME = 'Trading Bot' diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index b9a1c10f0..33eede41a 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -1,7 +1,6 @@ exports.newTradingProcess = function newTradingProcess(bot, logger, UTILITIES, TRADING_OUTPUT_MODULE, USER_BOT_COMMONS) { - /* - This Module will load all the process data dependencies from files and send them to the Trading Bot. + This Module will load all the process data dependencies from files and send them downstream. After execution, will save the time range and status report of the process. */ const FULL_LOG = true; diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 2ff28155a..0dd78993b 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -1,5 +1,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { - + /* + This module facilitates the appending of records to the output of the process. + */ const MODULE_NAME = 'Trading Records' let thisObject = { diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 7060f0a9f..36797dfa7 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -1,4 +1,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILITIES) { + /* + This Module represents the trading simulacion. Escentially a loop through a set of candles and + the execution at each loop cycle of the Trading System Protocol. + */ const FULL_LOG = true const MODULE_NAME = 'Trading Simulation -> ' + bot.SESSION.name diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index aa50c7ad5..401eab5c0 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -1,4 +1,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { + /* + The Trading System is the user defined set of rules compliant with the Trading Protocol that + defines the trading logic to be applied during each cycle of the Simulation. + */ const MODULE_NAME = 'Trading System' let thisObject = { reset: reset, From 11417851f6d2fb35a5683f9046d35442bd091f17 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 20 Jun 2020 15:55:37 +0200 Subject: [PATCH 061/113] no more invalid numbers in files --- LowFrequencyTrading/TradingRecords.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 0dd78993b..863ba8806 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -131,7 +131,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { of Nodes defined in the Record Properties but not all of them exist at the Root Node. We filter out those cases by not extracting the value from the value property. */ - let value + let value = 0 // This is a default value, since we do not want null in files because it breakes JSON format. if (targetNode !== undefined) { if (targetNode.type !== undefined) { /* @@ -139,10 +139,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { from its value property. */ value = targetNode.value - value = safeValue(value) - if (recordProperty.config.isString === true) { - value = '"' + value + '"' - } + if (recordProperty.config.decimals !== undefined) { value = Number(value.toFixed(recordProperty.config.decimals)) } @@ -154,13 +151,16 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { value = targetNode } } + if (recordProperty.config.isString !== true) { + value = safeNumericValue(value) + } record.push(value) } return record } } - function safeValue(value) { + function safeNumericValue(value) { /* The purpose of this function is to check that value variable does not have a value that will later break the JSON format of files where this is going to be stored at. @@ -171,6 +171,12 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { if (value === undefined) { value = 0 } + if (value === null) { + value = 0 + } + if (isNaN(value)) { + value = 0 + } return value } } \ No newline at end of file From 2aef400f5088a3d6515c26b04c43ae1b4387785a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 20 Jun 2020 18:27:46 +0200 Subject: [PATCH 062/113] Simulation Running Again with multiple Trades --- LowFrequencyTrading/Snapshots.js | 10 +- LowFrequencyTrading/TradingSimulation.js | 2 - LowFrequencyTrading/TradingSystem.js | 164 +++++++++++++---------- 3 files changed, 101 insertions(+), 75 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 85c7329ed..5de1c735f 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -12,6 +12,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } let tradingEngine + let tradingSystem const SNAPSHOTS_FOLDER_NAME = 'Snapshots' @@ -36,19 +37,20 @@ exports.newSnapshots = function newSnapshots(bot, logger) { saveAsLastTakePositionSnapshot = false addToSnapshots = false - - return thisObject function initialize() { tradingEngine = bot.simulationState.tradingEngine + tradingSystem = bot.simulationState.tradingSystem } function finalize() { tradingEngine = undefined + tradingSystem = undefined } function manageSnapshots() { + return // TODO enable this to run /* Snapshots Management (before we generate the trade record and delete that info) */ if (saveAsLastTriggerOnSnapshot === true) { snapshots.lastTriggerOn = snapshotDataRecord @@ -62,8 +64,8 @@ exports.newSnapshots = function newSnapshots(bot, logger) { if (addToSnapshots === true) { let closeValues = [ - tradingEngine.episode.positionCounters.positions.value, // Position Number - (new Date(candle.begin)).toISOString(), // Datetime + tradingEngine.episode.episodeCounters.positions.value, // Position Number + (new Date(tradingEngine.current.candle.begin.value)).toISOString(), // Datetime tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].name, // Strategy Name tradingEngine.current.strategy.situationName.value, // Trigger On Situation tradingEngine.current.position.situationName.value, // Take Position Situation diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 36797dfa7..4a0a5f154 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -33,8 +33,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) } if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) } - let timerToCloseStage = 0 // TODO - /* Stop Loss Management TODO */ const MIN_STOP_LOSS_VALUE = 0.0000000001 // We can not let the stop be zero to avoid division by 0 error or infinity numbers as a result. const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 401eab5c0..06858a74f 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -194,7 +194,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { One possible error is that the formula references a .previous that is undefined. This will not be considered an error. */ - value = false + value = 0 if (node.code.indexOf('previous') > -1 && err.message.indexOf('of undefined') > -1 || node.code.indexOf('chart') > -1 && err.message.indexOf('of undefined') > -1 @@ -403,26 +403,35 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode - let p + let phaseIndex + let phase + let stopLoss + let stage if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + stage = openStage if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { + if (openStage.initialDefinition.initialStopLoss !== undefined) { parentNode = openStage.initialDefinition - p = tradingEngine.current.position.stopLoss.stopLossPhase.value + phaseIndex = tradingEngine.current.position.stopLoss.stopLossPhase.value + stopLoss = openStage.initialDefinition.initialStopLoss + phase = stopLoss.phases[phaseIndex] } } } if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { + stage = manageStage + if (manageStage.managedStopLoss !== undefined) { parentNode = manageStage - p = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 + phaseIndex = tradingEngine.current.position.stopLoss.stopLossPhase.value - 1 + stopLoss = manageStage.managedStopLoss + phase = stopLoss.phases[phaseIndex] } } if (parentNode !== undefined) { - let phase = parentNode.stopLoss.phases[p] + if (phase === undefined) { return } // trying to jump to a phase that does not exists. /* Check the next Phase Event. */ let nextPhaseEvent = phase.nextPhaseEvent @@ -448,17 +457,22 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(situation.id) tradingSystem.highlights.push(nextPhaseEvent.id) tradingSystem.highlights.push(phase.id) - tradingSystem.highlights.push(parentNode.stopLoss.id) + tradingSystem.highlights.push(stopLoss.id) tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) tradingEngine.current.position.stopLoss.stopLossPhase.value++ tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' + /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Take Profit') + */ } + /* TODO ANNOUNCEMENT checkAnnouncements(nextPhaseEvent) + */ return // only one event can pass at the time } } @@ -490,13 +504,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(situation.id) tradingSystem.highlights.push(moveToPhaseEvent.id) tradingSystem.highlights.push(phase.id) - tradingSystem.highlights.push(parentNode.stopLoss.id) + tradingSystem.highlights.push(stopLoss.id) tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) let moveToPhase = moveToPhaseEvent.referenceParent if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.stopLoss.phases.length; q++) { - if (parentNode.stopLoss.phases[q].id === moveToPhase.id) { + for (let q = 0; q < stopLoss.phases.length; q++) { + if (stopLoss.phases[q].id === moveToPhase.id) { tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 } } @@ -508,9 +523,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' + /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Take Profit') + */ } + /* checkAnnouncements(moveToPhaseEvent) + */ return // only one event can pass at the time } } @@ -526,32 +545,31 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let openStage = strategy.openStage let manageStage = strategy.manageStage let phase - let key if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.stopLoss !== undefined) { - phase = openStage.initialDefinition.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value) + if (openStage.initialDefinition.initialStopLoss !== undefined) { + phase = openStage.initialDefinition.initialStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] } } } if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.stopLoss !== undefined) { - phase = manageStage.stopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (tradingEngine.current.position.stopLoss.stopLossPhase.value - 1) + if (manageStage.managedStopLoss !== undefined) { + phase = manageStage.managedStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] } } if (phase !== undefined) { if (phase.formula !== undefined) { let previousValue = tradingEngine.current.position.stopLoss.value - tradingEngine.current.position.stopLoss.value = formulas.get(key) + tradingEngine.current.position.stopLoss.value = formulas.get(phase.formula.id) + /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.stopLoss.value !== previousValue) { checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) } + */ } } } @@ -563,26 +581,34 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let openStage = strategy.openStage let manageStage = strategy.manageStage let parentNode - let p + let phaseIndex + let phase + let takeProfit + let stage if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + stage = openStage if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { + if (openStage.initialDefinition.initialTakeProfit !== undefined) { parentNode = openStage.initialDefinition - p = tradingEngine.current.position.takeProfit.takeProfitPhase.value + phaseIndex = tradingEngine.current.position.takeProfit.takeProfitPhase.value + takeProfit = openStage.initialDefinition.initialTakeProfit + phase = takeProfit.phases[phaseIndex] } } } if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { + stage = manageStage + if (manageStage.managedTakeProfit !== undefined) { parentNode = manageStage - p = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 + phaseIndex = tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1 + takeProfit = manageStage.managedTakeProfit + phase = takeProfit.phases[phaseIndex] } } if (parentNode !== undefined) { - let phase = parentNode.takeProfit.phases[p] if (phase === undefined) { return } // trying to jump to a phase that does not exists. /* Check the next Phase Event. */ @@ -609,17 +635,21 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(situation.id) tradingSystem.highlights.push(nextPhaseEvent.id) tradingSystem.highlights.push(phase.id) - tradingSystem.highlights.push(parentNode.takeProfit.id) + tradingSystem.highlights.push(takeProfit.id) tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) tradingEngine.current.position.takeProfit.takeProfitPhase.value++ tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' + /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Stop') + */ } - + /* TODO ANNOUNCEMENT checkAnnouncements(nextPhaseEvent) + */ return // only one event can pass at the time } } @@ -651,13 +681,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(situation.id) tradingSystem.highlights.push(nextPhaseEvent.id) tradingSystem.highlights.push(phase.id) - tradingSystem.highlights.push(parentNode.takeProfit.id) + tradingSystem.highlights.push(takeProfit.id) tradingSystem.highlights.push(parentNode.id) + tradingSystem.highlights.push(stage.id) let moveToPhase = moveToPhaseEvent.referenceParent if (moveToPhase !== undefined) { - for (let q = 0; q < parentNode.takeProfit.phases.length; q++) { - if (parentNode.takeProfit.phases[q].id === moveToPhase.id) { + for (let q = 0; q < takeProfit.phases.length; q++) { + if (takeProfit.phases[q].id === moveToPhase.id) { tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 } } @@ -669,10 +700,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingEngine.current.strategy.stageType.value = 'Manage Stage' + /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Stop') + */ } - + /* TODO ANNOUNCEMENT checkAnnouncements(moveToPhaseEvent) + */ return // only one event can pass at the time } } @@ -688,32 +722,31 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let openStage = strategy.openStage let manageStage = strategy.manageStage let phase - let key if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.takeProfit !== undefined) { - phase = openStage.initialDefinition.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] - key = tradingEngine.current.strategy.index.value + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value) + if (openStage.initialDefinition.initialTakeProfit !== undefined) { + phase = openStage.initialDefinition.initialTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] } } } if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.takeProfit !== undefined) { - phase = manageStage.takeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] - key = tradingEngine.current.strategy.index.value + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1) + if (manageStage.managedTakeProfit !== undefined) { + phase = manageStage.managedTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] } } if (phase !== undefined) { if (phase.formula !== undefined) { let previousValue = tradingEngine.current.position.stopLoss.value - tradingEngine.current.position.takeProfit.value = formulas.get(key) + tradingEngine.current.position.takeProfit.value = formulas.get(phase.formula.id) + /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.takeProfit.value !== previousValue) { checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) } + */ } } } @@ -764,8 +797,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.endRate.value = slippedStopLoss tradingEngine.current.strategy.stageType.value = 'Close Stage' + /* TODO ANNOUNCEMENT checkAnnouncements(strategy.closeStage, 'Stop') - + */ tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value @@ -810,8 +844,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.endRate.value = slippedTakeProfit tradingEngine.current.strategy.stageType.value = 'Close Stage' + /* TODO ANNOUNCEMENT checkAnnouncements(strategy.closeStage, 'Take Profit') - + */ tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' @@ -883,7 +918,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function closePosition() { - tradingEngine.episode.positionCounters.positions.value++ + tradingEngine.episode.episodeCounters.positions.value++ let feePaid = 0 @@ -923,23 +958,20 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (bot.sessionAndMarketBaseAssetsAreEqual) { tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } else { tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.positionCounters.positions.value + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } - addRecords() - tradingEngine.current.position.stopLoss.value = 0 tradingEngine.current.position.takeProfit.value = 0 tradingEngine.current.position.rate.value = 0 tradingEngine.current.position.size.value = 0 - timerToCloseStage = candle.begin tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 @@ -949,21 +981,15 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function exitStrategyAfterPosition() { if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - if (candle.begin - 5 * 60 * 1000 > timerToCloseStage) { - tradingEngine.current.strategy.end.value = candle.end - tradingEngine.current.strategy.endRate.value = candle.min - tradingEngine.current.strategy.status.value = 'Closed' - tradingEngine.current.strategy.index.value = -1 - tradingEngine.current.strategy.stageType.value = 'No 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' - timerToCloseStage = 0 - tradingEngine.current.distanceToEvent.triggerOff.value = 1 + tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue + tradingEngine.current.strategy.stageType.value = 'No Stage' - logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Exiting the Strategy.') - } else { - logger.write(MODULE_NAME, '[INFO] exitStrategyAfterPosition -> Waiting for timer.') - } + tradingEngine.current.distanceToEvent.triggerOff.value = 1 } } @@ -977,13 +1003,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const DEFAULT_VALUE = balance let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - if (strategy.openStage === undefined) return { DEFAULT_VALUE } - if (strategy.openStage.initialDefinition === undefined) return { DEFAULT_VALUE } - if (strategy.openStage.initialDefinition.positionSize === undefined) return { DEFAULT_VALUE } - if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return { DEFAULT_VALUE } + if (strategy.openStage === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionSize === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return DEFAULT_VALUE let value = formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) - if (value === undefined) return { DEFAULT_VALUE } + if (value === undefined) return DEFAULT_VALUE return value } @@ -991,13 +1017,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const DEFAULT_VALUE = tradingEngine.current.candle.close.value let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - if (strategy.openStage === undefined) return { DEFAULT_VALUE } - if (strategy.openStage.initialDefinition === undefined) return { DEFAULT_VALUE } - if (strategy.openStage.initialDefinition.positionRate === undefined) return { DEFAULT_VALUE } - if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return { DEFAULT_VALUE } + if (strategy.openStage === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionRate === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return DEFAULT_VALUE let value = formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) - if (value === undefined) return { DEFAULT_VALUE } + if (value === undefined) return DEFAULT_VALUE return value } From 7dc1aa648f09830dbf90823db6fc175ed08a321f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 20 Jun 2020 20:14:42 +0200 Subject: [PATCH 063/113] 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 + } } From 2f7b02b02797c3a5f191a36b73a79e50bb1db630 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 21 Jun 2020 11:25:18 +0200 Subject: [PATCH 064/113] Strategy Objects working well --- LowFrequencyTrading/TradingStrategy.js | 70 ++++++++++++++++++++++++++ LowFrequencyTrading/TradingSystem.js | 62 ++++++++--------------- 2 files changed, 91 insertions(+), 41 deletions(-) create mode 100644 LowFrequencyTrading/TradingStrategy.js diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js new file mode 100644 index 000000000..a2693244a --- /dev/null +++ b/LowFrequencyTrading/TradingStrategy.js @@ -0,0 +1,70 @@ +exports.newTradingStrategy = function newTradingStrategy(bot, logger) { + /* + This module packages all functions related to Strategies. + */ + const MODULE_NAME = 'Trading Strategy' + let thisObject = { + openStrategy: openStrategy, + closeStrategy: closeStrategy, + updateStageType: updateStageType, + resetStrategy: resetStrategy, + updateStatus: updateStatus, + updateCounters: updateCounters, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + + return thisObject + + function initialize() { + tradingEngine = bot.simulationState.tradingEngine + } + + function finalize() { + tradingEngine = undefined + } + + function openStrategy(stageType, index, situationName, strategyName) { + 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 + + tradingEngine.current.strategy.stageType.value = stageType + tradingEngine.current.strategy.index.value = index + tradingEngine.current.strategy.situationName.value = situationName + tradingEngine.current.strategy.strategyName.value = strategyName + } + + function closeStrategy() { + 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 resetStrategy() { + tradingEngine.current.strategy.status.value = tradingEngine.current.strategy.status.config.initialValue + tradingEngine.current.strategy.stageType.value = tradingEngine.current.strategy.stageType.config.initialValue + tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue + tradingEngine.current.strategy.strategyCounters.periods.value = tradingEngine.current.strategy.strategyCounters.periods.config.initialValue + } + + function updateStageType(stageType) { + tradingEngine.current.strategy.stageType.value = stageType + } + + function updateStatus() { + if (tradingEngine.current.strategy.status.value === 'Closed') { + resetStrategy() + } + } + + function updateCounters() { + if (tradingEngine.current.strategy.status.value === 'Open') { + tradingEngine.current.strategy.strategyCounters.periods.value++ + } + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 6087d3a91..e0e9ef9c9 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -38,15 +38,23 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let conditions = new Map() let formulas = new Map() + const TRADING_STRATEGY_MODULE = require('./TradingStrategy.js') + let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger) + return thisObject function initialize() { tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine sessionParameters = bot.SESSION.parameters + + tradingStrategyModule.initialize() } function finalize() { + tradingStrategyModule.finalize() + tradingStrategyModule = undefined + chart = undefined conditions = undefined @@ -75,9 +83,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function mantainStrategies() { - if (tradingEngine.current.strategy.status.value === 'Closed') { - resetStrategy() - } + tradingStrategyModule.updateStatus() + tradingStrategyModule.updateCounters() } function mantainPositions() { @@ -275,11 +282,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.triggerOn.id) tradingSystem.highlights.push(triggerStage.id) - tradingEngine.current.strategy.stageType.value = 'Trigger Stage' - openStrategy() - tradingEngine.current.strategy.index.value = j - tradingEngine.current.strategy.situationName.value = situation.name - tradingEngine.current.strategy.strategyName.value = strategy.name + tradingStrategyModule.openStrategy('Trigger Stage', j, situation.name, strategy.name) tradingEngine.current.distanceToEvent.triggerOn.value = 1 @@ -329,9 +332,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.triggerOff.id) tradingSystem.highlights.push(triggerStage.id) - closeTrateggy() - tradingEngine.current.strategy.stageType.value = 'No Stage' - tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue + tradingStrategyModule.closeStrategy() tradingEngine.current.distanceToEvent.triggerOff.value = 1 @@ -380,7 +381,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.takePosition.id) tradingSystem.highlights.push(triggerStage.id) - tradingEngine.current.strategy.stageType.value = 'Open Stage' + tradingStrategyModule.updateStageType('Open Stage') + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Open Stage' tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Open Stage' tradingEngine.current.position.stopLoss.stopLossPhase.value = 0 @@ -470,7 +472,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.stopLoss.stopLossPhase.value++ tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' + tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Take Profit') */ @@ -528,7 +530,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' + tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Take Profit') */ @@ -648,7 +650,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.takeProfit.takeProfitPhase.value++ tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' + tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Stop') */ @@ -705,7 +707,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingEngine.current.strategy.stageType.value = 'Manage Stage' + tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT checkAnnouncements(manageStage, 'Stop') */ @@ -802,7 +804,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.endRate.value = slippedStopLoss - tradingEngine.current.strategy.stageType.value = 'Close Stage' + tradingStrategyModule.updateStageType('Close Stage') /* TODO ANNOUNCEMENT checkAnnouncements(strategy.closeStage, 'Stop') */ @@ -849,7 +851,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.position.endRate.value = slippedTakeProfit - tradingEngine.current.strategy.stageType.value = 'Close Stage' + tradingStrategyModule.updateStageType('Close Stage') /* TODO ANNOUNCEMENT checkAnnouncements(strategy.closeStage, 'Take Profit') */ @@ -987,11 +989,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function exitStrategyAfterPosition() { if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - - closeTrateggy() - tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue - tradingEngine.current.strategy.stageType.value = 'No Stage' - + tradingStrategyModule.closeStrategy() tradingEngine.current.distanceToEvent.triggerOff.value = 1 } } @@ -1041,23 +1039,5 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { return false } } - - 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 - } } From 3badcfb2a5b5db36fac8ab9067fd9ba4f452cffc Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 21 Jun 2020 11:32:18 +0200 Subject: [PATCH 065/113] updateEnds --- LowFrequencyTrading/TradingStrategy.js | 14 +++++++++++--- LowFrequencyTrading/TradingSystem.js | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index a2693244a..e3e588b6f 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -6,10 +6,11 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger) { let thisObject = { openStrategy: openStrategy, closeStrategy: closeStrategy, + updateEnds: updateEnds, updateStageType: updateStageType, - resetStrategy: resetStrategy, updateStatus: updateStatus, updateCounters: updateCounters, + resetStrategy: resetStrategy, initialize: initialize, finalize: finalize } @@ -29,9 +30,9 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger) { function openStrategy(stageType, index, situationName, strategyName) { 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.end.value = tradingEngine.current.candle.end.value 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.endRate.value = tradingEngine.current.candle.min.value tradingEngine.current.strategy.stageType.value = stageType tradingEngine.current.strategy.index.value = index @@ -52,6 +53,13 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger) { tradingEngine.current.strategy.strategyCounters.periods.value = tradingEngine.current.strategy.strategyCounters.periods.config.initialValue } + function updateEnds() { + if (tradingEngine.current.strategy.status.value === 'Open') { + tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value + } + } + function updateStageType(stageType) { tradingEngine.current.strategy.stageType.value = stageType } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index e0e9ef9c9..d1f90f394 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -85,6 +85,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function mantainStrategies() { tradingStrategyModule.updateStatus() tradingStrategyModule.updateCounters() + tradingStrategyModule.updateEnds() } function mantainPositions() { From 03b69d0a0c9b9f83c25abdf350033ab9d3c2b3e3 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 22 Jun 2020 19:20:34 +0200 Subject: [PATCH 066/113] Take Profit and Stop Loss being Plotted --- LowFrequencyTrading/TradingEngine.js | 23 +-- LowFrequencyTrading/TradingPosition.js | 192 +++++++++++++++++++++++ LowFrequencyTrading/TradingRecords.js | 24 ++- LowFrequencyTrading/TradingSimulation.js | 12 +- LowFrequencyTrading/TradingStrategy.js | 8 +- LowFrequencyTrading/TradingSystem.js | 183 ++++++--------------- 6 files changed, 272 insertions(+), 170 deletions(-) create mode 100644 LowFrequencyTrading/TradingPosition.js diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index c122735f5..3189af917 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -7,8 +7,6 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let thisObject = { setCurrentCandle: setCurrentCandle, updateEpisodeCountersAndStatistics: updateEpisodeCountersAndStatistics, - resetPositionCountersAndStatistics: resetPositionCountersAndStatistics, - updatePositionCountersAndStatistics: updatePositionCountersAndStatistics, updateDistanceToEventsCounters: updateDistanceToEventsCounters, initialize: initialize, finalize: finalize @@ -98,6 +96,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { node.value = node.config.initialValue } } + node.initialize = initializeNode // This will allow anyone to initialize this node and its children. /* Now we go down through all this node children */ let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) @@ -137,26 +136,6 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.episode.episodeStatistics.days.value = tradingEngine.episode.episodeCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS } - function resetPositionCountersAndStatistics() { - /* Keeping Position Counters Up-to-date */ - if ( - (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') - ) { return } - - tradingEngine.current.position.positionCounters.periods.value = tradingEngine.current.position.positionCounters.periods.config.initialValue - tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionStatistics.days.config.initialValue - } - - function updatePositionCountersAndStatistics() { - /* Keeping Position Counters Up-to-date */ - if ( - (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') - ) { - tradingEngine.current.position.positionCounters.periods.value++ - tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS - } - } - function updateDistanceToEventsCounters() { /* Keeping Distance Counters Up-to-date */ if ( diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js new file mode 100644 index 000000000..f613c9f1c --- /dev/null +++ b/LowFrequencyTrading/TradingPosition.js @@ -0,0 +1,192 @@ +exports.newTradingPosition = function newTradingPosition(bot, logger) { + /* + This module packages all functions related to Positions. + */ + const MODULE_NAME = 'Trading Position' + let thisObject = { + openPosition: openPosition, + closePosition: closePosition, + applyStopLossFormula: applyStopLossFormula, + applyTakeProfitFormula: applyTakeProfitFormula, + preventStopLossDistortion: preventStopLossDistortion, + applySlippageToStopLoss: applySlippageToStopLoss, + preventTakeProfitDistortion: preventTakeProfitDistortion, + applySlippageToTakeProfit: applySlippageToTakeProfit, + updateStopLoss: updateStopLoss, + updateTakeProfit: updateTakeProfit, + updateSizeAndRate: updateSizeAndRate, + applySlippageToRate: applySlippageToRate, + updateEnds: updateEnds, + updateStatus: updateStatus, + updateCounters: updateCounters, + updateStatistics: updateStatistics, + resetPosition: resetPosition, + initialize: initialize, + finalize: finalize + } + + let tradingEngine + let sessionParameters + + return thisObject + + function initialize() { + tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters + } + + function finalize() { + tradingEngine = undefined + sessionParameters = undefined + } + + function openPosition(situationName) { + tradingEngine.current.position.status.value = 'Open' + tradingEngine.current.position.begin.value = tradingEngine.current.candle.begin.value + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + + tradingEngine.current.position.situationName.value = situationName + + tradingEngine.current.position.stopLoss.stopLossStage.value = 'Open Stage' + tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Open Stage' + tradingEngine.current.position.stopLoss.stopLossPhase.value = 0 + tradingEngine.current.position.takeProfit.takeProfitPhase.value = 0 + } + + function closePosition(exitType) { + tradingEngine.current.position.status.value = 'Closed' + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + tradingEngine.current.position.exitType.value = exitType + } + + function resetPosition() { + tradingEngine.current.position.initialize(tradingEngine.current.position) + } + + function applyStopLossFormula(formulas, formulaId) { + tradingEngine.current.position.stopLoss.value = formulas.get(formulaId) + } + + function applyTakeProfitFormula(formulas, formulaId) { + tradingEngine.current.position.takeProfit.value = formulas.get(formulaId) + } + + function updateStopLoss(phase, stage) { + tradingEngine.current.position.stopLoss.stopLossPhase.value = phase + tradingEngine.current.position.stopLoss.stopLossStage.value = stage + } + + function updateTakeProfit(phase, stage) { + tradingEngine.current.position.takeProfit.takeProfitPhase.value = phase + tradingEngine.current.position.takeProfit.takeProfitStage.value = stage + } + + function updateSizeAndRate(size, rate) { + tradingEngine.current.position.size.value = size + tradingEngine.current.position.rate.value = rate + } + + function applySlippageToRate() { + let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value - slippageAmount + } else { + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value + slippageAmount + } + } + + function preventStopLossDistortion() { + /* + Hit Point Validation + + This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + if (bot.sessionAndMarketBaseAssetsAreEqual) { + if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value + } + } else { + if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.max.value + } + } + } + + function applySlippageToStopLoss() { + let slippedStopLoss = tradingEngine.current.position.stopLoss.value + + /* Apply the Slippage */ + let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + slippedStopLoss = slippedStopLoss + slippageAmount + } else { + slippedStopLoss = slippedStopLoss - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedStopLoss + } + + function preventTakeProfitDistortion() { + /* + Hit Point Validation: + + This prevents misscalculations when a formula places the take profit in this case way beyond the market price. + If we take the stop loss value at those situation would be a huge distortion of facts. + */ + if (bot.sessionAndMarketBaseAssetsAreEqual) { + if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value + } + } else { + if (tradingEngine.current.position.takeProfit.value < tradingEngine.current.candle.min.value) { + tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.min.value + } + } + } + + function applySlippageToTakeProfit() { + let slippedTakeProfit = tradingEngine.current.position.takeProfit.value + /* Apply the Slippage */ + let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 + + if (bot.sessionAndMarketBaseAssetsAreEqual) { + slippedTakeProfit = slippedTakeProfit + slippageAmount + } else { + slippedTakeProfit = slippedTakeProfit - slippageAmount + } + + tradingEngine.current.position.endRate.value = slippedTakeProfit + } + + function updateEnds() { + if (tradingEngine.current.position.status.value === 'Open') { + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value + } + } + + function updateStatus() { + if (tradingEngine.current.position.status.value === 'Closed') { + resetPosition() + } + } + + function updateCounters() { + if (tradingEngine.current.position.status.value === 'Open') { + tradingEngine.current.position.positionCounters.periods.value++ + } + } + + function updateStatistics() { + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.current.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.current.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value + } else { + tradingEngine.current.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value + tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.current.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value + } + tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + } +} \ No newline at end of file diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 95af633d7..742272708 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -41,10 +41,6 @@ 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() } @@ -91,6 +87,9 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } function scanRecordDefinition(product) { + if (product.config.codeName === 'Position-Objects') { + let a = 1 + } /* The Product Root Node is the root of the node hiriarchy branch that we need to look at in order to fnd the node where we are going to extract the value. @@ -166,6 +165,9 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { if (recordProperty.config.isString !== true && Array.isArray(value) !== true) { value = safeNumericValue(value) } + if (recordProperty.config.isString === true && Array.isArray(value) !== true) { + value = safeStringValue(value) + } record.push(value) } return record @@ -191,4 +193,18 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } return value } + + function safeStringValue(value) { + /* + The purpose of this function is to check that value variable does not have a value that + will later break the JSON format of files where this is going to be stored at. + */ + if (value === undefined) { + value = '' + } + if (value === null) { + value = '' + } + return value + } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index b7516706a..a8a7cc891 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -186,8 +186,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.calculateTakeProfit() } - tradingEngineModule.resetPositionCountersAndStatistics() - tradingEngineModule.updatePositionCountersAndStatistics() tradingEngineModule.updateDistanceToEventsCounters() /* Checking if Stop or Take Profit were hit */ @@ -198,6 +196,16 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* Taking a Position */ if (takingPosition === true) { tradingSystemModule.getReadyToTakePosition() + /* + Whem we calculated all the formulas at the begining of the loop, we didn't know + we were going to take position in this same loop cycle. Some might formulas depends on + calculations that are triggered only when we are taking position. For that reason + we will calculate all formulas again, and after that the Stop Loss and Take Profit. + */ + tradingSystemModule.evalFormulas() + tradingSystemModule.calculateTakeProfit() + tradingSystemModule.calculateStopLoss() + tradingExecutionModule.takePosition() tradingSystemModule.takePosition() takingPosition = false diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index e3e588b6f..0cd221649 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -40,17 +40,15 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger) { tradingEngine.current.strategy.strategyName.value = strategyName } - function closeStrategy() { + function closeStrategy(exitType) { tradingEngine.current.strategy.status.value = 'Closed' + tradingEngine.current.strategy.exitType.value = exitType tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value } function resetStrategy() { - tradingEngine.current.strategy.status.value = tradingEngine.current.strategy.status.config.initialValue - tradingEngine.current.strategy.stageType.value = tradingEngine.current.strategy.stageType.config.initialValue - tradingEngine.current.strategy.index.value = tradingEngine.current.strategy.index.config.initialValue - tradingEngine.current.strategy.strategyCounters.periods.value = tradingEngine.current.strategy.strategyCounters.periods.config.initialValue + tradingEngine.current.strategy.initialize(tradingEngine.current.strategy) } function updateEnds() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index d1f90f394..aea6f75d6 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -41,6 +41,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const TRADING_STRATEGY_MODULE = require('./TradingStrategy.js') let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger) + const TRADING_POSITION_MODULE = require('./TradingPosition.js') + let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger) + return thisObject function initialize() { @@ -49,12 +52,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { sessionParameters = bot.SESSION.parameters tradingStrategyModule.initialize() + tradingPositionModule.initialize() } function finalize() { tradingStrategyModule.finalize() tradingStrategyModule = undefined + tradingPositionModule.finalize() + tradingPositionModule = undefined + chart = undefined conditions = undefined @@ -89,7 +96,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function mantainPositions() { - + tradingPositionModule.updateStatus() + tradingPositionModule.updateCounters() + tradingPositionModule.updateEnds() } function evalConditions() { @@ -333,7 +342,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.triggerOff.id) tradingSystem.highlights.push(triggerStage.id) - tradingStrategyModule.closeStrategy() + tradingStrategyModule.closeStrategy('Trigger Off') tradingEngine.current.distanceToEvent.triggerOff.value = 1 @@ -353,7 +362,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkTakePosition() { /* Take Position Condition */ - if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + if ( + tradingEngine.current.strategy.stageType.value === 'Trigger Stage' && + tradingEngine.current.strategy.status.value === 'Open' + ) { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage @@ -383,12 +395,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(triggerStage.id) tradingStrategyModule.updateStageType('Open Stage') - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Open Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Open Stage' - tradingEngine.current.position.stopLoss.stopLossPhase.value = 0 - tradingEngine.current.position.takeProfit.takeProfitPhase.value = 0 - tradingEngine.current.position.situationName.value = situation.name + tradingPositionModule.openPosition(situation.name) /* TODO See what to do with this: checkAnnouncements(triggerStage.takePosition) @@ -470,8 +477,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(parentNode.id) tradingSystem.highlights.push(stage.id) - tradingEngine.current.position.stopLoss.stopLossPhase.value++ - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' + tradingPositionModule.updateStopLoss(tradingEngine.current.position.stopLoss.stopLossPhase.value + 1, 'Manage Stage') + if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT @@ -521,7 +528,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (moveToPhase !== undefined) { for (let q = 0; q < stopLoss.phases.length; q++) { if (stopLoss.phases[q].id === moveToPhase.id) { - tradingEngine.current.position.stopLoss.stopLossPhase.value = q + 1 + tradingPositionModule.updateStopLoss(q + 1, 'Manage Stage') } } } else { @@ -529,7 +536,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { continue } - tradingEngine.current.position.stopLoss.stopLossStage.value = 'Manage Stage' if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT @@ -572,7 +578,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (phase !== undefined) { if (phase.formula !== undefined) { let previousValue = tradingEngine.current.position.stopLoss.value - tradingEngine.current.position.stopLoss.value = formulas.get(phase.formula.id) + tradingPositionModule.applyStopLossFormula(formulas, phase.formula.id) /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.stopLoss.value !== previousValue) { @@ -648,8 +654,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.highlights.push(parentNode.id) tradingSystem.highlights.push(stage.id) - tradingEngine.current.position.takeProfit.takeProfitPhase.value++ - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' + tradingPositionModule.updateTakeProfit(tradingEngine.current.position.takeProfit.takeProfitPhase.value + 1, 'Manage Stage') + if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT @@ -698,7 +704,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (moveToPhase !== undefined) { for (let q = 0; q < takeProfit.phases.length; q++) { if (takeProfit.phases[q].id === moveToPhase.id) { - tradingEngine.current.position.takeProfit.takeProfitPhase.value = q + 1 + tradingPositionModule.updateTakeProfit(q + 1, 'Manage Stage') } } } else { @@ -706,7 +712,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { continue } - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'Manage Stage' if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') /* TODO ANNOUNCEMENT @@ -748,8 +753,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (phase !== undefined) { if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.stopLoss.value - tradingEngine.current.position.takeProfit.value = formulas.get(phase.formula.id) + let previousValue = tradingEngine.current.position.takeProfit.value + tradingPositionModule.applyTakeProfitFormula(formulas, phase.formula.id) /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.takeProfit.value !== previousValue) { @@ -776,45 +781,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.min.value <= tradingEngine.current.position.stopLoss.value) ) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Stop Loss was hit.') - /* - Hit Point Validation - - This prevents misscalculations when a formula places the stop loss in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ - if (bot.sessionAndMarketBaseAssetsAreEqual) { - if (tradingEngine.current.position.stopLoss.value < tradingEngine.current.candle.min.value) { - tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.min.value - } - } else { - if (tradingEngine.current.position.stopLoss.value > tradingEngine.current.candle.max.value) { - tradingEngine.current.position.stopLoss.value = tradingEngine.current.candle.max.value - } - } - let slippedStopLoss = tradingEngine.current.position.stopLoss.value - - /* Apply the Slippage */ - let slippageAmount = slippedStopLoss * bot.SESSION.parameters.slippage.config.stopLoss / 100 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - slippedStopLoss = slippedStopLoss + slippageAmount - } else { - slippedStopLoss = slippedStopLoss - slippageAmount - } - - tradingEngine.current.position.endRate.value = slippedStopLoss + tradingPositionModule.preventStopLossDistortion() + tradingPositionModule.applySlippageToStopLoss() + tradingPositionModule.closePosition('Stop Loss') tradingStrategyModule.updateStageType('Close Stage') + /* TODO ANNOUNCEMENT checkAnnouncements(strategy.closeStage, 'Stop') */ - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value - tradingEngine.current.position.status.value = 1 - tradingEngine.current.position.exitType.value = 1 - return true // This means that the STOP was hit. } @@ -824,46 +800,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { (!bot.sessionAndMarketBaseAssetsAreEqual && tradingEngine.current.candle.max.value >= tradingEngine.current.position.takeProfit.value) ) { logger.write(MODULE_NAME, '[INFO] checkStopLossOrTakeProfitWasHit -> Take Profit was hit.') - /* - Hit Point Validation: - - This prevents misscalculations when a formula places the take profit in this case way beyond the market price. - If we take the stop loss value at those situation would be a huge distortion of facts. - */ - if (bot.sessionAndMarketBaseAssetsAreEqual) { - if (tradingEngine.current.position.takeProfit.value > tradingEngine.current.candle.max.value) { - tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.max.value - } - } else { - if (tradingEngine.current.position.takeProfit.value < tradingEngine.current.candle.min.value) { - tradingEngine.current.position.takeProfit.value = tradingEngine.current.candle.min.value - } - } - let slippedTakeProfit = tradingEngine.current.position.takeProfit.value - /* Apply the Slippage */ - let slippageAmount = slippedTakeProfit * bot.SESSION.parameters.slippage.config.takeProfit / 100 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - slippedTakeProfit = slippedTakeProfit + slippageAmount - } else { - slippedTakeProfit = slippedTakeProfit - slippageAmount - } - - tradingEngine.current.position.endRate.value = slippedTakeProfit + tradingPositionModule.preventTakeProfitDistortion() + tradingPositionModule.applySlippageToTakeProfit() + tradingPositionModule.closePosition('Take Profit') tradingStrategyModule.updateStageType('Close Stage') + /* TODO ANNOUNCEMENT checkAnnouncements(strategy.closeStage, 'Take Profit') */ - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - - tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value - tradingEngine.current.position.status.value = 1 - tradingEngine.current.position.exitType.value = 2 - - addToSnapshots = true return true // This means that the Take Profit was hit. } } @@ -872,31 +818,20 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function getReadyToTakePosition() { + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.positions.value++ + /* Inicializing this counter */ tradingEngine.current.distanceToEvent.takePosition.value = 1 /* Position size and rate */ - tradingEngine.current.position.size.value = getPositionSize() - tradingEngine.current.position.rate.value = getPositionRate() + tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) - /* We take what was calculated at the formula and apply the slippage. */ - let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value - slippageAmount - } else { - tradingEngine.current.position.rate.value = tradingEngine.current.position.rate.value + slippageAmount - } - - /* Update the position information. */ - tradingEngine.current.position.begin.value = tradingEngine.current.candle.begin.value - tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value + /* We take what was calculated at the formula of position rate and apply the slippage. */ + tradingPositionModule.applySlippageToRate() } function takePosition() { - calculateTakeProfit() // TODO: Check if this is really necesary - calculateStopLoss() // TODO: Check if this is really necesary - tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value @@ -927,70 +862,44 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function closePosition() { - tradingEngine.episode.episodeCounters.positions.value++ - + /* We calculate the new balances after the position is closed. */ let feePaid = 0 - if (bot.sessionAndMarketBaseAssetsAreEqual) { - feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid tradingEngine.current.balance.quotedAsset.value = 0 } else { - feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid tradingEngine.current.balance.baseAsset.value = 0 } - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.last.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.baseAsset.value - tradingEngine.last.position.positionStatistics.ROI.value = tradingEngine.last.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.positionStatistics.ROI.value)) { tradingEngine.last.position.positionStatistics.ROI.value = 0 } - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance - } else { - tradingEngine.last.position.positionStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.previous.balance.quotedAsset.value - tradingEngine.last.position.positionStatistics.ROI.value = tradingEngine.last.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value - if (isNaN(tradingEngine.last.position.positionStatistics.ROI.value)) { tradingEngine.last.position.positionStatistics.ROI.value = 0 } - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance - } - - tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.last.position.positionStatistics.ROI.value + tradingPositionModule.updateStatistics() + /* Recalculating Episode Counters */ if (tradingEngine.last.position.positionStatistics.profitLoss.value > 0) { tradingEngine.episode.episodeCounters.hits.value++ } else { tradingEngine.episode.episodeCounters.fails.value++ } + /* Recalculating Episode Statistics */ if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } else { + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } - - tradingEngine.current.position.stopLoss.value = 0 - tradingEngine.current.position.takeProfit.value = 0 - - tradingEngine.current.position.rate.value = 0 - tradingEngine.current.position.size.value = 0 - - tradingEngine.current.position.stopLoss.stopLossStage.value = 'No Stage' - tradingEngine.current.position.takeProfit.takeProfitStage.value = 'No Stage' - tradingEngine.current.position.stopLoss.stopLossPhase.value = -1 - tradingEngine.current.position.takeProfit.takeProfitPhase.value = -1 - } function exitStrategyAfterPosition() { if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - tradingStrategyModule.closeStrategy() + tradingStrategyModule.closeStrategy('Position Closed') tradingEngine.current.distanceToEvent.triggerOff.value = 1 } } From ad5936b972010f3ac9d5c17f5f0b3ffb4a290837 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 23 Jun 2020 12:07:01 +0200 Subject: [PATCH 067/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index aea6f75d6..b00278431 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -463,9 +463,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) @@ -510,9 +510,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) @@ -640,9 +640,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) @@ -686,9 +686,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let condition = situation.conditions[m] let value = false if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id).value + value = conditions.get(condition.id) } - if (value === false) { passed = false } + if (value !== true) { passed = false } } tradingSystem.values.push([situation.id, passed]) From 092a6e8a02fd2c7c7a3c4a35871b357e79dfc1d1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 23 Jun 2020 12:40:15 +0200 Subject: [PATCH 068/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index b00278431..2e7b5d980 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -694,7 +694,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.values.push([situation.id, passed]) if (passed) { tradingSystem.highlights.push(situation.id) - tradingSystem.highlights.push(nextPhaseEvent.id) + tradingSystem.highlights.push(moveToPhaseEvent.id) tradingSystem.highlights.push(phase.id) tradingSystem.highlights.push(takeProfit.id) tradingSystem.highlights.push(parentNode.id) From b968fd3609a31b69cc3747265d5baa6fcc86dda1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 23 Jun 2020 14:47:47 +0200 Subject: [PATCH 069/113] Update TradingProcess.js --- LowFrequencyTrading/TradingProcess.js | 1 + 1 file changed, 1 insertion(+) diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 33eede41a..177e78bc5 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -729,6 +729,7 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } + function pad(str, max) { str = str.toString(); return str.length < max ? pad(" " + str, max) : str; From ff922bf7970951c66ef4184f63b3719cde305cc9 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 24 Jun 2020 20:48:39 +0200 Subject: [PATCH 070/113] Announcements working again --- LowFrequencyTrading/Announcements.js | 94 +++++------------------- LowFrequencyTrading/TradingSimulation.js | 6 -- LowFrequencyTrading/TradingSystem.js | 72 ++++++------------ SocialBots.js | 15 +--- 4 files changed, 48 insertions(+), 139 deletions(-) diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js index 751836a78..b9112dd7e 100644 --- a/LowFrequencyTrading/Announcements.js +++ b/LowFrequencyTrading/Announcements.js @@ -3,7 +3,6 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { const MODULE_NAME = 'Announcements' let thisObject = { - checkAnnouncements: checkAnnouncements, makeAnnoucements: makeAnnoucements, initialize: initialize, finalize: finalize @@ -20,93 +19,40 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { function finalize() { tradingEngine = undefined + announcementsToBeMade = undefined } - function checkAnnouncements(node, value) { - /* - Value is an optional parameter that represents the value that the announcement is monitoring for change (for numeric values only). - If we do receive this value, we will only make the annoucement if the variance is grater than the user pre-defined value - for this variance. - */ - + function makeAnnoucements(node) { if (node.announcements !== undefined) { for (let i = 0; i < node.announcements.length; i++) { let announcement = node.announcements[i] - let key = node.type + '-' + announcement.name + '-' + announcement.id - - let lastPeriodAnnounced = -1 - let newAnnouncementRecord = {} - - for (let j = 0; j < variable.announcements.length; j++) { - let announcementRecord = variable.announcements[j] - if (announcementRecord.key === key) { - lastPeriodAnnounced = announcementRecord.periods - newAnnouncementRecord = announcementRecord - break + let canAnnounce = true + if (announcement.announcementCondition !== undefined) { + let conditionValue + try { + conditionValue = eval(announcement.announcementCondition.code) + } catch (err) { + tradingSystem.errors.push([announcement.announcementCondition.id, err.message]) } + if (conditionValue !== true) { canAnnounce = false } } - - if (tradingEngine.episode.episodeCounters.periods.value > lastPeriodAnnounced) { - if (isNaN(value) === false) { - /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ - let valueVariation - - let config = announcement.config - valueVariation = config.valueVariation - - if (newAnnouncementRecord.value !== undefined && valueVariation !== undefined) { - let upperLimit = newAnnouncementRecord.value + newAnnouncementRecord.value * valueVariation / 100 - let lowerLimit = newAnnouncementRecord.value - newAnnouncementRecord.value * valueVariation / 100 - if (value > lowerLimit && value < upperLimit) { - /* There is not enough variation to announce this again. */ - return - } + if (canAnnounce === true) { + let text = announcement.config.text + if (announcement.formula !== undefined) { + try { + text = eval(announcement.formula.code) + } catch (err) { + tradingSystem.errors.push([announcement.formula.id, err.message]) } } - /* - We store the announcement temporarily at an Array to differ its execution, becasue we need to evaulate its formula - and at this point in time the potential variables used at the formula are still not set. - */ - announcement.value = value - announcementsToBeMade.push(announcement) - - /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ - if (newAnnouncementRecord.periods !== undefined) { - newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods.value - newAnnouncementRecord.value = value + if (bot.SESSION.socialBots !== undefined) { + bot.SESSION.socialBots.announce(text) } else { - newAnnouncementRecord = { - key: key, - periods: tradingEngine.episode.episodeCounters.periods.value, - value: value - } - variable.announcements.push(newAnnouncementRecord) + tradingSystem.errors.push([announcement.id, 'Could not announce because session does not have Social Bots.']) } } } } } - - function makeAnnoucements() { - /* Here we go through all the annoucements that need to be done during this loop, and we just do them. */ - for (let i = 0; i < announcementsToBeMade.length; i++) { - announcement = announcementsToBeMade[i] - /* Here we check if there is a formula attached to the annoucement, we evaluate it to get the annoucement text. */ - let formulaValue - if (announcement.formula !== undefined) { - try { - let value = announcement.value - formulaValue = eval(announcement.formula.code) - } catch (err) { - announcement.formula.error = err.message - } - } - announcement.formulaValue = formulaValue - - if (bot.SESSION.socialBots !== undefined) { - bot.SESSION.socialBots.announce(announcement) - } - } - } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index a8a7cc891..1cde9f2a0 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -68,10 +68,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) snapshotsModule.initialize() - const ANNOUNCEMENTS_MODULE = require('./Announcements.js') - let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) - announcementsModule.initialize() - const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) tradingSystemModule.initialize() @@ -224,7 +220,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT snapshotsModule.manageSnapshots() tradingRecordsModule.appendRecords() - announcementsModule.makeAnnoucements() // After everything at the simulation level was done, we will do the annoucements that are pending. controlLoop() } @@ -259,7 +254,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.finalize() tradingRecordsModule.finalize() snapshotsModule.finalize() - announcementsModule.finalize() callback() } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 2e7b5d980..27463eee4 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -44,6 +44,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const TRADING_POSITION_MODULE = require('./TradingPosition.js') let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger) + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') + let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + return thisObject function initialize() { @@ -53,6 +56,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingStrategyModule.initialize() tradingPositionModule.initialize() + announcementsModule.initialize() } function finalize() { @@ -62,6 +66,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.finalize() tradingPositionModule = undefined + announcementsModule.finalize() + announcementsModule = undefined + chart = undefined conditions = undefined @@ -296,9 +303,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.distanceToEvent.triggerOn.value = 1 + announcementsModule.makeAnnoucements(triggerStage.triggerOn) + announcementsModule.makeAnnoucements(triggerStage) /* TODO See what to do with this: - checkAnnouncements(triggerStage.triggerOn) - checkAnnouncements(triggerStage) saveAsLastTriggerOnSnapshot = true */ @@ -346,9 +353,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.distanceToEvent.triggerOff.value = 1 - /* TODO See what to do with this: - checkAnnouncements(triggerStage.triggerOff) - */ + announcementsModule.makeAnnoucements(triggerStage.triggerOff) logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) return true // This Means that we have just met the conditions to trigger off. @@ -397,9 +402,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingStrategyModule.updateStageType('Open Stage') tradingPositionModule.openPosition(situation.name) + announcementsModule.makeAnnoucements(triggerStage.takePosition) + announcementsModule.makeAnnoucements(strategy.openStage) /* TODO See what to do with this: - checkAnnouncements(triggerStage.takePosition) - checkAnnouncements(strategy.openStage) saveAsLastTakePositionSnapshot = true */ @@ -481,14 +486,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Take Profit') - */ + announcementsModule.makeAnnoucements(manageStage) } - - /* TODO ANNOUNCEMENT - checkAnnouncements(nextPhaseEvent) - */ + announcementsModule.makeAnnoucements(nextPhaseEvent) return // only one event can pass at the time } } @@ -538,13 +538,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Take Profit') - */ + announcementsModule.makeAnnoucements(manageStage) } - /* - checkAnnouncements(moveToPhaseEvent) - */ + announcementsModule.makeAnnoucements(moveToPhaseEvent) return // only one event can pass at the time } } @@ -580,11 +576,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let previousValue = tradingEngine.current.position.stopLoss.value tradingPositionModule.applyStopLossFormula(formulas, phase.formula.id) - /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.stopLoss.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) + announcementsModule.makeAnnoucements(phase) } - */ } } } @@ -658,13 +652,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Stop') - */ + announcementsModule.makeAnnoucements(manageStage) } - /* TODO ANNOUNCEMENT - checkAnnouncements(nextPhaseEvent) - */ + announcementsModule.makeAnnoucements(nextPhaseEvent) return // only one event can pass at the time } } @@ -714,13 +704,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Stop') - */ + announcementsModule.makeAnnoucements(manageStage) } - /* TODO ANNOUNCEMENT - checkAnnouncements(moveToPhaseEvent) - */ + announcementsModule.makeAnnoucements(moveToPhaseEvent) return // only one event can pass at the time } } @@ -756,11 +742,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let previousValue = tradingEngine.current.position.takeProfit.value tradingPositionModule.applyTakeProfitFormula(formulas, phase.formula.id) - /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.takeProfit.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) + announcementsModule.makeAnnoucements(phase) } - */ } } } @@ -785,12 +769,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.preventStopLossDistortion() tradingPositionModule.applySlippageToStopLoss() tradingPositionModule.closePosition('Stop Loss') - tradingStrategyModule.updateStageType('Close Stage') - - /* TODO ANNOUNCEMENT - checkAnnouncements(strategy.closeStage, 'Stop') - */ + announcementsModule.makeAnnoucements(strategy.closeStage) return true // This means that the STOP was hit. } @@ -804,12 +784,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.preventTakeProfitDistortion() tradingPositionModule.applySlippageToTakeProfit() tradingPositionModule.closePosition('Take Profit') - tradingStrategyModule.updateStageType('Close Stage') - - /* TODO ANNOUNCEMENT - checkAnnouncements(strategy.closeStage, 'Take Profit') - */ + announcementsModule.makeAnnoucements(strategy.closeStage) return true // This means that the Take Profit was hit. } } diff --git a/SocialBots.js b/SocialBots.js index 736f50fd9..9ae190727 100644 --- a/SocialBots.js +++ b/SocialBots.js @@ -19,29 +19,22 @@ exports.newSocialBots = function newSocialBots(bot, logger) { let socialBot = bot.SESSION.socialBots.bots[i] if (socialBot.type === "Telegram Bot") { let config = socialBot.config - socialBot.botInstance = SOCIAL_BOTS_MODULE.newTelegramBot(bot, logger) + socialBot.botInstance = TELEGRAM_BOT_MODULE.newTelegramBot(bot, logger) socialBot.botInstance.initialize(config.botToken, config.chatId) } } } - function announce(announcement) { + function announce(text) { if (bot.SESSION.socialBots.bots !== undefined) { for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { let socialBot = bot.SESSION.socialBots.bots[i] try { - let config = announcement.config - if (socialBot.type === "Telegram Bot") { - if (announcement.formulaValue !== undefined) { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } else { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, config.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, text).catch(err => logger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) } - } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); + logger.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); } } } From 3241a646cc954458b790b834bade8d14e5de84b5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 24 Jun 2020 22:32:22 +0200 Subject: [PATCH 071/113] Announcements working --- LowFrequencyTrading/TradingProcess.js | 6 +++--- Session.js | 8 ++++---- TelegramBot.js | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 177e78bc5..63535929b 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -373,7 +373,7 @@ const logText = "User Defined End Datetime reached @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> advanceTime -> " + logText); } - bot.STOP_SESSION = true + bot.SESSION.stop(logText) callBackFunction(global.DEFAULT_OK_RESPONSE); return; } @@ -633,12 +633,12 @@ const logText = "User Defined End Datetime reached @ " + now.getUTCFullYear() + "/" + (now.getUTCMonth() + 1) + "/" + now.getUTCDate() + "."; if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processDailyFiles -> buildCharts -> onOutputGenerated -> onMarketStatusReport -> " + logText); } - bot.STOP_SESSION = true + bot.SESSION.stop('because it just finished.') callBackFunction(global.DEFAULT_OK_RESPONSE); return; } if (bot.SESSION.type === 'Backtesting Session') { - bot.STOP_SESSION = true + bot.SESSION.stop('Backtesting Session over Market Files Finished.') } callBackFunction(global.DEFAULT_OK_RESPONSE); } diff --git a/Session.js b/Session.js index 64fe9f403..0564ae8dc 100644 --- a/Session.js +++ b/Session.js @@ -100,7 +100,7 @@ bot.STOP_SESSION = true } - socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' was started.") + socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is starting.") } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> runSession -> err = " + err.stack); } @@ -108,10 +108,10 @@ function stopSession(commandOrigin) { try { - if (reason === undefined) { commandOrigin = ' from the User Interface.' } - socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' was stopped " + commandOrigin) - bot.STOP_SESSION = true + if (commandOrigin === undefined) { commandOrigin = ' from the User Interface.' } + socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is stopping " + commandOrigin) socialBotsModule.finalize() + bot.STOP_SESSION = true } catch (err) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> stopSession -> err = " + err.stack); } diff --git a/TelegramBot.js b/TelegramBot.js index 262511f1f..9e2383ed1 100644 --- a/TelegramBot.js +++ b/TelegramBot.js @@ -30,8 +30,8 @@ exports.newTelegramBot = function newTelegramBot(bot, logger) { thisObject.telegramAPI = new Telegram(botToken) - const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started." - thisObject.telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeTelegramBot -> Telegram API error -> err = " + err)) + const message = "Telegram bot is starting." + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeTelegramBot -> Telegram API error -> err = " + err)) } catch (err) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> err = " + err.stack); @@ -40,12 +40,12 @@ exports.newTelegramBot = function newTelegramBot(bot, logger) { } function finalize() { - const messge = "I have been ordered to shut down. Goodbye!" - telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] finalize -> Telegram API error -> err = " + err)) + const message = "Telegram bot is signing off." + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] finalize -> Telegram API error -> err = " + err)) } function sendMessage(message) { - thisObject.telegramAPI.sendMessage(thisObject.chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] announce -> Telegram API error -> err = " + err)) + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] announce -> Telegram API error -> err = " + err)) } } \ No newline at end of file From aacc90c72131de9a3875613713322f2450b6a82e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 27 Jun 2020 13:30:05 +0200 Subject: [PATCH 072/113] one year --- Session.js | 10 +++------- server.js | 1 + 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Session.js b/Session.js index 0564ae8dc..8b362bd64 100644 --- a/Session.js +++ b/Session.js @@ -2,7 +2,6 @@ const MODULE_NAME = "Session" const FULL_LOG = true; - const ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000 let thisObject = { initialize: initialize @@ -15,10 +14,7 @@ function initialize(pProcessConfig, callBackFunction) { try { - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - /* Initialize this info so that everything is logged propeerly */ - bot.SESSION = { name: bot.processNode.session.name, id: bot.processNode.session.id @@ -184,17 +180,17 @@ } case 'Live Trading Session': { bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS) break } case 'Fordward Testing Session': { bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS) break } case 'Paper Trading Session': { bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS) + bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS) break } } diff --git a/server.js b/server.js index 50f2d662c..56f923cae 100644 --- a/server.js +++ b/server.js @@ -27,6 +27,7 @@ global.CUSTOM_FAIL_RESPONSE = { global.ROOT_DIR = './'; +global.ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000 global.ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000 global.ONE_MIN_IN_MILISECONDS = 60 * 1000 global.LOGGER_MAP = new Map() From b35bade968ba7d32e3deaf79e0d437b461edef44 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 27 Jun 2020 14:52:45 +0200 Subject: [PATCH 073/113] Announcements Plotted at Trading System --- LowFrequencyTrading/Announcements.js | 7 +++++-- LowFrequencyTrading/TradingRecords.js | 3 +++ LowFrequencyTrading/TradingSystem.js | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js index b9112dd7e..ec8c25d75 100644 --- a/LowFrequencyTrading/Announcements.js +++ b/LowFrequencyTrading/Announcements.js @@ -8,18 +8,20 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { finalize: finalize } + let tradingSystem let tradingEngine - let announcementsToBeMade = [] return thisObject function initialize() { + tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine } function finalize() { + tradingSystem = undefined tradingEngine = undefined - announcementsToBeMade = undefined + bot.SESSION.messagesSent = undefined } function makeAnnoucements(node) { @@ -48,6 +50,7 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { if (bot.SESSION.socialBots !== undefined) { bot.SESSION.socialBots.announce(text) + tradingSystem.announcements.push([announcement.id, text]) } else { tradingSystem.errors.push([announcement.id, 'Could not announce because session does not have Social Bots.']) } diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 742272708..993c75edd 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -12,6 +12,7 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let tradingEngine let tradingSystem + let sessionParameters let outputDatasetsMap return thisObject @@ -19,12 +20,14 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { function initialize(pOutputDatasetsMap) { tradingEngine = bot.simulationState.tradingEngine tradingSystem = bot.simulationState.tradingSystem + sessionParameters = bot.SESSION.parameters outputDatasetsMap = pOutputDatasetsMap // These are the files turned into arrays, stored in a Map by Product codeName. } function finalize() { tradingEngine = undefined tradingSystem = undefined + sessionParameters = undefined outputDatasetsMap = undefined } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 27463eee4..d79a43da7 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -87,6 +87,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function reset(pChart) { + /* This function helps reset data structures at every cycle of the simulation loop/=. */ chart = pChart // We need chat to be a local object accessible from conditions and formulas. tradingSystem.highlights = [] tradingSystem.errors = [] @@ -94,6 +95,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.status = [] tradingSystem.progress = [] tradingSystem.running = [] + tradingSystem.announcements = [] } function mantainStrategies() { From e0f95a32d0cb4e7c9fdb1e5d17ee506d53d15184 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 28 Jun 2020 15:29:00 +0200 Subject: [PATCH 074/113] Snaphost re implemented --- LowFrequencyTrading/Snapshots.js | 219 ++++++++++++++++------- LowFrequencyTrading/TradingPosition.js | 5 + LowFrequencyTrading/TradingSimulation.js | 7 - LowFrequencyTrading/TradingSystem.js | 18 +- 4 files changed, 176 insertions(+), 73 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 5de1c735f..839b78108 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -5,7 +5,10 @@ exports.newSnapshots = function newSnapshots(bot, logger) { const MODULE_NAME = 'Snapshots' let thisObject = { - manageSnapshots: manageSnapshots, + strategyEntry: strategyEntry, + strategyExit: strategyExit, + positionEntry: positionEntry, + positionExit: positionExit, addCodeToSnapshot: addCodeToSnapshot, initialize: initialize, finalize: finalize @@ -13,6 +16,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let tradingEngine let tradingSystem + let sessionParameters const SNAPSHOTS_FOLDER_NAME = 'Snapshots' @@ -20,90 +24,153 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let triggerOnSnapshot let takePositionSnapshot - /* Snapshots of Trigger On and Take Positions */ + /* Snapshots of Strategies and Positions */ let snapshots = { - headers: undefined, - triggerOn: [], - takePosition: [], - lastTriggerOn: undefined, - lastTakePosition: undefined + headers: [], + strategies: [], + positions: [] } - /* We are going to take snapshots of the values of indicators at key moments of the sumulation. */ - let snapshotKeys = new Map() - snapshotLoopHeaders = [] - snapshotDataRecord = [] - saveAsLastTriggerOnSnapshot = false - saveAsLastTakePositionSnapshot = false - addToSnapshots = false + let snapshotKeys = new Map() // This is to prevent adding the same indicator / variable to the output collection. + let createHeaders = true // This is going to be true only once + let createCloseHeaders = true // This is going to be true only once + let closeValues + let strategyValues + let addToStrategyValues + let positionValues + let addToPositionValues return thisObject function initialize() { tradingEngine = bot.simulationState.tradingEngine tradingSystem = bot.simulationState.tradingSystem + sessionParameters = bot.SESSION.parameters } function finalize() { tradingEngine = undefined tradingSystem = undefined + sessionParameters = undefined } - function manageSnapshots() { - return // TODO enable this to run - /* Snapshots Management (before we generate the trade record and delete that info) */ - if (saveAsLastTriggerOnSnapshot === true) { - snapshots.lastTriggerOn = snapshotDataRecord - saveAsLastTriggerOnSnapshot = false + function strategyEntry() { + /* + We will record here a snapshot of all values used in conditions and formulas within the Trading System. + If the strategy exists with a trade, this information will be added to the snapshots file. + */ + + strategyValues = [] + addToStrategyValues = true + + evalNode(tradingSystem) + + /* Turn off this */ + createHeaders = false + addToStrategyValues = false + } + + function strategyExit() { + getResults() + let valuesArray = closeValues.concat(strategyValues) + snapshots.strategies.push(valuesArray) + } + + function positionEntry() { + positionValues = [] + addToPositionValues = true + + evalNode(tradingSystem) + + /* Turn off this */ + createHeaders = false + addToPositionValues = false + } + + function positionExit() { + getResults() + let valuesArray = closeValues.concat(positionValues) + snapshots.positions.push(valuesArray) + } + + function evalNode(node) { + if (node === undefined) { return } + + /* Here we check if there is a codition to be evaluated */ + if (node.type === 'Condition') { + /* We will eval this condition */ + evalCondition(node) } - if (saveAsLastTakePositionSnapshot === true) { - snapshots.lastTakePosition = snapshotDataRecord - saveAsLastTakePositionSnapshot = false - } - - if (addToSnapshots === true) { - let closeValues = [ - tradingEngine.episode.episodeCounters.positions.value, // Position Number - (new Date(tradingEngine.current.candle.begin.value)).toISOString(), // Datetime - tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].name, // Strategy Name - tradingEngine.current.strategy.situationName.value, // Trigger On Situation - tradingEngine.current.position.situationName.value, // Take Position Situation - hitOrFial(), // Result - tradingEngine.last.position.positionStatistics.ROI.value, // ROI - exitType() // Exit Type - ] - - function hitOrFial() { - if (tradingEngine.last.position.positionStatistics.ROI.value > 0) { return 'HIT' } else { return 'FAIL' } + /* Here we check if there is a formula to be evaluated */ + if (node.type === 'Formula') { + if (node.code !== undefined) { + /* We will eval this formula */ + evalFormula(node) } + } - function exitType() { - switch (tradingEngine.current.position.exitType.value) { - case 1: return 'Stop' - case 2: return 'Take Profit' + /* Now we go down through all this node children */ + let nodeDefinition = bot.APP_SCHEMA_MAP.get(node.type) + if (nodeDefinition === undefined) { return } + + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (node[property.name] !== undefined) { + evalNode(node[property.name]) + } + previousPropertyName = property.name + } + break + } + case 'array': { + if (node[property.name] !== undefined) { + let nodePropertyArray = node[property.name] + for (let m = 0; m < nodePropertyArray.length; m++) { + evalNode(nodePropertyArray[m]) + } + } + break + } } } - - snapshots.triggerOn.push(closeValues.concat(snapshots.lastTriggerOn)) - snapshots.takePosition.push(closeValues.concat(snapshots.lastTakePosition)) - - snapshots.lastTriggerOn = undefined - snapshots.lastTakePosition = undefined - addToSnapshots = false - } - - let closeHeaders = ['Trade Number', 'Close Datetime', 'Strategy', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] - if (snapshots.headers === undefined) { - snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshotLoopHeaders))) } } - function addCodeToSnapshot(code) { - if (code === undefined) { return } + function evalCondition(node) { + + /* + The code can be at the condition node if it was done with the Conditions Editor, or it can also be + at a Javascript Code node. If there is a Javascript object we will give it preference and take the code + from there. Otherwise we will take the code from the Condition node. + */ + + let nodeWithCode = node + if (node.javascriptCode !== undefined) { + if (node.javascriptCode.code !== undefined) { + nodeWithCode = node.javascriptCode + } + } + addCodeToSnapshot(nodeWithCode) + } + + function evalFormula(node) { + let nodeWithCode = node + addCodeToSnapshot(nodeWithCode) + } + + function addCodeToSnapshot(nodeWithCode) { + if (nodeWithCode === undefined) { return } + if (nodeWithCode.code === undefined) { return } try { - let instructionsArray = code.split(' ') + let instructionsArray = nodeWithCode.code.split(' ') for (let i = 0; i < instructionsArray.length; i++) { let instruction = instructionsArray[i] instruction = instruction.replace('(', '') @@ -133,7 +200,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } // Example: chart.at01hs.popularSMA.sma200 - chart.at01hs.popularSMA.sma100 < 10 - if (timeFrame !== 'atAnyvariable.timeFrame') { + if (timeFrame !== 'atAnyTimeFrame') { timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) } let key = timeFrame + '-' + product + '-' + property @@ -141,17 +208,45 @@ exports.newSnapshots = function newSnapshots(bot, logger) { if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. snapshotKeys.set(key, key) - snapshotLoopHeaders.push(key) + if (createHeaders === true) { + snapshots.headers.push(key) + } let value = eval(instruction) - snapshotDataRecord.push(value) + if (addToStrategyValues === true) { + strategyValues.push(value) + } + + if (addToPositionValues === true) { + positionValues.push(value) + } } } } } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> code = ' + code) + tradingSystem.errors.push(nodeWithCode.id, err.message) + logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> nodeWithCode.code = ' + nodeWithCode.code) logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } + + function getResults() { + + let closeHeaders = ['Trade Number', 'Close Datetime', 'Strategy Name', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] + closeValues = [ + tradingEngine.episode.episodeCounters.positions.value, // Position Number + (new Date(tradingEngine.current.candle.begin.value)).toISOString(), // Datetime + tradingEngine.last.strategy.strategyName.value, // Strategy Name + tradingEngine.last.strategy.situationName.value, // Trigger On Situation + tradingEngine.last.position.situationName.value, // Take Position Situation + tradingEngine.last.position.positionStatistics.hitFail.value, // Result + tradingEngine.last.position.positionStatistics.ROI.value, // ROI + tradingEngine.last.position.exitType.value // Exit Type + ] + + if (createCloseHeaders === true) { + snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshots.headers))) + } + } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index f613c9f1c..51389abc8 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -188,5 +188,10 @@ exports.newTradingPosition = function newTradingPosition(bot, logger) { tradingEngine.current.position.positionStatistics.ROI.value = tradingEngine.current.position.positionStatistics.profitLoss.value * 100 / tradingEngine.current.position.size.value } tradingEngine.current.position.positionStatistics.days.value = tradingEngine.current.position.positionCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + if (tradingEngine.current.position.positionStatistics.ROI.value > 0) { + tradingEngine.current.position.positionStatistics.hitFail.value = 'HIT' + } else { + tradingEngine.current.position.positionStatistics.hitFail.value = 'FAIL' + } } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 1cde9f2a0..7cc119c78 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -64,10 +64,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingRecordsModule = TRADING_RECORDS_MODULE.newTradingRecords(bot, logger) tradingRecordsModule.initialize(outputDatasetsMap) - const SNAPSHOTS_MODULE = require('./Snapshots.js') - let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) - snapshotsModule.initialize() - const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) tradingSystemModule.initialize() @@ -217,8 +213,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT /* After a position was closed, we need to close the strategy. */ tradingSystemModule.exitStrategyAfterPosition() - - snapshotsModule.manageSnapshots() tradingRecordsModule.appendRecords() controlLoop() @@ -253,7 +247,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingExecutionModule.finalize() tradingSystemModule.finalize() tradingRecordsModule.finalize() - snapshotsModule.finalize() callback() } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index d79a43da7..f0efbd14c 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -47,6 +47,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const ANNOUNCEMENTS_MODULE = require('./Announcements.js') let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + const SNAPSHOTS_MODULE = require('./Snapshots.js') + let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) + return thisObject function initialize() { @@ -57,6 +60,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingStrategyModule.initialize() tradingPositionModule.initialize() announcementsModule.initialize() + snapshotsModule.initialize() } function finalize() { @@ -69,6 +73,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { announcementsModule.finalize() announcementsModule = undefined + snapshotsModule.finalize() + snapshotsModule = undefined + chart = undefined conditions = undefined @@ -130,7 +137,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { /* Here we check if there is a formula to be evaluated */ if (node.type === 'Formula' && evaluating === 'Formulas') { if (node.code !== undefined) { - /* We will eval this condition */ + /* We will eval this formula */ evalFormula(node) } } @@ -307,9 +314,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { announcementsModule.makeAnnoucements(triggerStage.triggerOn) announcementsModule.makeAnnoucements(triggerStage) - /* TODO See what to do with this: - saveAsLastTriggerOnSnapshot = true - */ + + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyEntry() + } + } logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) return true // This Means that we have just met the conditions to trigger on. From 590d127718edbbad13317faae136018e71db6026 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 28 Jun 2020 16:49:59 +0200 Subject: [PATCH 075/113] Snapshot Fully Implemented --- LowFrequencyTrading/Snapshots.js | 93 +++++++++++++++++++++++----- LowFrequencyTrading/TradingSystem.js | 35 +++++++++-- 2 files changed, 106 insertions(+), 22 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 839b78108..69a9bcac0 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -20,10 +20,6 @@ exports.newSnapshots = function newSnapshots(bot, logger) { const SNAPSHOTS_FOLDER_NAME = 'Snapshots' - let snapshotHeaders - let triggerOnSnapshot - let takePositionSnapshot - /* Snapshots of Strategies and Positions */ let snapshots = { headers: [], @@ -49,6 +45,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } function finalize() { + writeSnapshotFiles() tradingEngine = undefined tradingSystem = undefined sessionParameters = undefined @@ -71,7 +68,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } function strategyExit() { - getResults() + getResults(tradingEngine.current.strategy.begin.value) let valuesArray = closeValues.concat(strategyValues) snapshots.strategies.push(valuesArray) } @@ -88,7 +85,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } function positionExit() { - getResults() + getResults(tradingEngine.current.position.begin.value) let valuesArray = closeValues.concat(positionValues) snapshots.positions.push(valuesArray) } @@ -231,22 +228,86 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } } - function getResults() { + function getResults(openDatetime) { - let closeHeaders = ['Trade Number', 'Close Datetime', 'Strategy Name', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] + let closeHeaders = ['Trade Number', 'Open Datetime', 'Close Datetime', 'Strategy Name', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] closeValues = [ - tradingEngine.episode.episodeCounters.positions.value, // Position Number - (new Date(tradingEngine.current.candle.begin.value)).toISOString(), // Datetime - tradingEngine.last.strategy.strategyName.value, // Strategy Name - tradingEngine.last.strategy.situationName.value, // Trigger On Situation - tradingEngine.last.position.situationName.value, // Take Position Situation - tradingEngine.last.position.positionStatistics.hitFail.value, // Result - tradingEngine.last.position.positionStatistics.ROI.value, // ROI - tradingEngine.last.position.exitType.value // Exit Type + tradingEngine.episode.episodeCounters.positions.value, // Position Number + (new Date(openDatetime)).toISOString(), // Open Datetime + tradingEngine.current.strategy.strategyName.value, // Strategy Name + tradingEngine.current.strategy.situationName.value, // Trigger On Situation + tradingEngine.current.position.situationName.value, // Take Position Situation + tradingEngine.current.position.positionStatistics.hitFail.value, // Result + tradingEngine.current.position.positionStatistics.ROI.value, // ROI + tradingEngine.current.position.exitType.value // Exit Type ] if (createCloseHeaders === true) { snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshots.headers))) } } + + function writeSnapshotFiles() { + + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + writeSnapshotFile(snapshots.strategies, 'Strategies') + } + } + + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + writeSnapshotFile(snapshots.positions, 'Positions') + } + } + } + + function writeSnapshotFile(snapshotArray, pFileName) { + try { + const FILE_STORAGE = require('../FileStorage.js'); + let fileStorage = FILE_STORAGE.newFileStorage(logger); + + let fileContent = ""; + let separator = "\r\n"; + + parseRecord(snapshots.headers) + + for (let i = 0; i < snapshotArray.length; i++) { + let record = snapshotArray[i]; + parseRecord(record) + fileRecordCounter++; + } + + function parseRecord(record) { + for (let j = 0; j < record.length; j++) { + let property = record[j] + + fileContent = fileContent + '' + property + if (j !== record.length - 1) { + fileContent = fileContent + "," + } + } + fileContent = fileContent + separator + } + + fileContent = "" + fileContent + ""; + + let fileName = pFileName + '.csv'; + let filePath = bot.filePathRoot + "/Output/" + bot.SESSION.folderName + "/" + SNAPSHOTS_FOLDER_NAME; + filePath += '/' + fileName + + fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> onFileCreated -> market = " + market.baseAsset + "_" + market.quotedAsset); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] writeSnapshotFile -> err = " + err.stack); + } + } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index f0efbd14c..f8a36d0ee 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -315,9 +315,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { announcementsModule.makeAnnoucements(triggerStage.triggerOn) announcementsModule.makeAnnoucements(triggerStage) - if (sessionParameters.snapshots !== undefined) { - if (sessionParameters.snapshots.config.strategy === true) { - snapshotsModule.strategyEntry() + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyEntry() + } } } @@ -416,9 +418,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { announcementsModule.makeAnnoucements(triggerStage.takePosition) announcementsModule.makeAnnoucements(strategy.openStage) - /* TODO See what to do with this: - saveAsLastTakePositionSnapshot = true - */ + + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionEntry() + } + } + } logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') return true // This Means that we have just met the conditions to take position. @@ -890,6 +897,22 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingStrategyModule.closeStrategy('Position Closed') tradingEngine.current.distanceToEvent.triggerOff.value = 1 } + + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyExit() + } + } + } + + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionExit() + } + } + } } function getPositionSize() { From 326f9fab0d93037b00a7454d8050b8c369d7db2d Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 29 Jun 2020 13:21:05 +0200 Subject: [PATCH 076/113] Snapshots working --- LowFrequencyTrading/Snapshots.js | 59 ++++++++++++++++-------- LowFrequencyTrading/TradingSimulation.js | 1 - LowFrequencyTrading/TradingSystem.js | 10 ++-- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index 69a9bcac0..d36073854 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -5,6 +5,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { const MODULE_NAME = 'Snapshots' let thisObject = { + reset: reset, strategyEntry: strategyEntry, strategyExit: strategyExit, positionEntry: positionEntry, @@ -27,7 +28,8 @@ exports.newSnapshots = function newSnapshots(bot, logger) { positions: [] } - let snapshotKeys = new Map() // This is to prevent adding the same indicator / variable to the output collection. + let positionKeys = new Map() // This is to prevent adding the same indicator / variable to the output collection. + let strategyKeys = new Map() let createHeaders = true // This is going to be true only once let createCloseHeaders = true // This is going to be true only once let closeValues @@ -36,6 +38,8 @@ exports.newSnapshots = function newSnapshots(bot, logger) { let positionValues let addToPositionValues + let chart + return thisObject function initialize() { @@ -49,6 +53,16 @@ exports.newSnapshots = function newSnapshots(bot, logger) { tradingEngine = undefined tradingSystem = undefined sessionParameters = undefined + + positionKeys = undefined + strategyKeys = undefined + + chart = undefined + } + + function reset(pChart) { + /* This function helps reset data structures at every cycle of the simulation loop/=. */ + chart = pChart // We need chat to be a local object accessible from conditions and formulas. } function strategyEntry() { @@ -57,6 +71,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { If the strategy exists with a trade, this information will be added to the snapshots file. */ + strategyKeys = new Map() strategyValues = [] addToStrategyValues = true @@ -68,12 +83,14 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } function strategyExit() { - getResults(tradingEngine.current.strategy.begin.value) + getResults(tradingEngine.current.strategy.begin.value, tradingEngine.current.strategy.end.value) let valuesArray = closeValues.concat(strategyValues) snapshots.strategies.push(valuesArray) } function positionEntry() { + + positionKeys = new Map() positionValues = [] addToPositionValues = true @@ -85,7 +102,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } function positionExit() { - getResults(tradingEngine.current.position.begin.value) + getResults(tradingEngine.current.position.begin.value, tradingEngine.current.position.end.value) let valuesArray = closeValues.concat(positionValues) snapshots.positions.push(valuesArray) } @@ -201,21 +218,27 @@ exports.newSnapshots = function newSnapshots(bot, logger) { timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) } let key = timeFrame + '-' + product + '-' + property - let existingKey = snapshotKeys.get(key) + let existingKey - if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. - snapshotKeys.set(key, key) - if (createHeaders === true) { - snapshots.headers.push(key) - } + if (addToStrategyValues === true) { + addValues(strategyKeys, strategyValues) + } - let value = eval(instruction) - if (addToStrategyValues === true) { - strategyValues.push(value) - } + if (addToPositionValues === true) { + addValues(positionKeys, positionValues) + } - if (addToPositionValues === true) { - positionValues.push(value) + function addValues(keys, values) { + existingKey = keys.get(key) + + if (existingKey === undefined) { // means that at the current loop this property of this product was not used before. + keys.set(key, key) + if (createHeaders === true) { + snapshots.headers.push(key) + } + + let value = eval(instruction) + values.push(value) } } } @@ -224,16 +247,16 @@ exports.newSnapshots = function newSnapshots(bot, logger) { tradingSystem.errors.push(nodeWithCode.id, err.message) logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> nodeWithCode.code = ' + nodeWithCode.code) logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } - function getResults(openDatetime) { + function getResults(openDatetime, closeDatetime) { let closeHeaders = ['Trade Number', 'Open Datetime', 'Close Datetime', 'Strategy Name', 'Trigger On Situation', 'Take Position Situation', 'Result', 'ROI', 'Exit Type'] closeValues = [ tradingEngine.episode.episodeCounters.positions.value, // Position Number (new Date(openDatetime)).toISOString(), // Open Datetime + (new Date(closeDatetime)).toISOString(), // Open Datetime tradingEngine.current.strategy.strategyName.value, // Strategy Name tradingEngine.current.strategy.situationName.value, // Trigger On Situation tradingEngine.current.position.situationName.value, // Take Position Situation @@ -244,6 +267,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { if (createCloseHeaders === true) { snapshots.headers = closeHeaders.concat(JSON.parse(JSON.stringify(snapshots.headers))) + createCloseHeaders = false } } @@ -275,7 +299,6 @@ exports.newSnapshots = function newSnapshots(bot, logger) { for (let i = 0; i < snapshotArray.length; i++) { let record = snapshotArray[i]; parseRecord(record) - fileRecordCounter++; } function parseRecord(record) { diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 7cc119c78..5ddc2d633 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -124,7 +124,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function loop() { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + tradingEngine.current.candle.index.value) } - let candle = candles[tradingEngine.current.candle.index.value] // This is the current candle the Simulation is working at. if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle Begin @ ' + (new Date(candle.begin)).toLocaleString()) } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index f8a36d0ee..c027c2612 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -96,6 +96,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function reset(pChart) { /* This function helps reset data structures at every cycle of the simulation loop/=. */ chart = pChart // We need chat to be a local object accessible from conditions and formulas. + snapshotsModule.reset(chart) + tradingSystem.highlights = [] tradingSystem.errors = [] tradingSystem.values = [] @@ -893,10 +895,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function exitStrategyAfterPosition() { - if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - tradingStrategyModule.closeStrategy('Position Closed') - tradingEngine.current.distanceToEvent.triggerOff.value = 1 - } + if (tradingEngine.current.strategy.stageType.value !== 'Close Stage') { return } + + tradingStrategyModule.closeStrategy('Position Closed') + tradingEngine.current.distanceToEvent.triggerOff.value = 1 if (bot.SESSION.type === 'Backtesting Session') { if (sessionParameters.snapshots !== undefined) { From 4d363e3683689f9f4199acfe77ca9b7598bbd46c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 29 Jun 2020 19:09:08 +0200 Subject: [PATCH 077/113] Performance Optimization --- LowFrequencyTrading/TradingProcess.js | 30 ++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 63535929b..5e790d354 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -317,7 +317,8 @@ } let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); + let trimmedDataFile = trimDataFile(dataFile, datasetModule.node.parentNode.record) + dataFiles.set(dependency.id, trimmedDataFile); dependencyControlLoop(); } @@ -723,6 +724,33 @@ logger.newInternalLoop(bot.codeName, bot.process, bot.tradingProcessDate); thisReport.save(callBack); } + + function trimDataFile(dataFile, recordDefinition) { + /* + Here we will discard all the records in a file that are outside of the current time range. + */ + let beginIndex + let endIndex + let result = [] + for (let i = 0; i < recordDefinition.properties.length; i++) { + let property = recordDefinition.properties[i] + if (property.config.codeName === 'begin') { + beginIndex = i + } + if (property.config.codeName === 'end') { + endIndex = i + } + } + for (let i = 0; i < dataFile.length; i++) { + let dataRecord = dataFile[i] + let begin = dataRecord[beginIndex] + let end = dataRecord[endIndex] + if (end < bot.SESSION.parameters.timeRange.config.initialDatetime) { continue } + if (begin > bot.SESSION.parameters.timeRange.config.finalDatetime) { continue } + result.push(dataRecord) + } + return result + } } catch (err) { logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); From cac21220d5ece3d1b413841d9bdaeb5a9dffad43 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 5 Jul 2020 18:51:34 +0200 Subject: [PATCH 078/113] Daily Files working --- LowFrequencyTrading/TradingEngine.js | 2 +- LowFrequencyTrading/TradingOutput.js | 5 +- LowFrequencyTrading/TradingProcess.js | 12 +++-- LowFrequencyTrading/TradingRecords.js | 69 ++++++++++++++++++++---- LowFrequencyTrading/TradingSimulation.js | 4 +- Session.js | 1 + 6 files changed, 75 insertions(+), 18 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 3189af917..62f0889b5 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -23,7 +23,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine = bot.simulationState.tradingEngine sessionParameters = bot.SESSION.parameters - if (bot.RESUME === false) { + if (bot.FIRST_EXECUTION === true) { initializeNode(tradingEngine) tradingEngine.current.balance.baseAsset.value = sessionParameters.sessionBaseAsset.config.initialBalance tradingEngine.current.balance.quotedAsset.value = sessionParameters.sessionQuotedAsset.config.initialBalance diff --git a/LowFrequencyTrading/TradingOutput.js b/LowFrequencyTrading/TradingOutput.js index 5c99087ff..0abe8c40a 100644 --- a/LowFrequencyTrading/TradingOutput.js +++ b/LowFrequencyTrading/TradingOutput.js @@ -57,7 +57,10 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, UTILITIES, FIL let totalFilesWritten = 0 let outputDatasetsMap = new Map() - if (bot.RESUME !== true) { + if (bot.FIRST_EXECUTION === true && bot.RESUME === false) { + /* + Here is where the Trading Engine and Trading Systems received are moved to the simulation state. + */ bot.simulationState.tradingEngine = bot.TRADING_ENGINE bot.simulationState.tradingSystem = bot.TRADING_SYSTEM initializeOutputs() diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index 5e790d354..eb23e6ae6 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -66,7 +66,6 @@ let currentTimeFrame let currentTimeFrameLabel let market = bot.market; - let botNeverRan = true; /* Context Variables */ @@ -256,6 +255,10 @@ } } + /* + We do market files first since if the simulation is run on daily files, there will be a loop to getch each daus files and we do not need + that loop to reload market files. + */ function processMarketFiles() { let n; timeFramesLoop(); @@ -523,7 +526,7 @@ } else { n = 0; if (currentTimeFrame !== undefined) { - buildCharts(); + buildCharts(advanceTime); } else { logger.write(MODULE_NAME, "[ERROR] start -> processDailyFiles -> timeFramesControlLoop -> Time Frame not Recognized. Can not continue."); callBackFunction(global.DEFAULT_FAIL_RESPONSE); @@ -532,7 +535,7 @@ } } - function buildCharts() { + function buildCharts(advanceTime) { const COMMONS = require('../Commons.js') let commons = COMMONS.newCommons(bot, logger, UTILITIES, FILE_STORAGE) @@ -598,8 +601,7 @@ return; } - botNeverRan = false - bot.RESUME = true // From here on, all other loops executions must resume from where we left at this current run. + bot.FIRST_EXECUTION = false // From here on, all other loops executions wont be the first execution. if (currentTimeFrame > global.dailyFilePeriods[0][0]) { writeMarketStatusReport(onMarketStatusReport) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index 993c75edd..f15526649 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -53,9 +53,11 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } function persistRecord() { + pruneOutputFile(product, outputDatasetArray) let record = scanRecordDefinition(product) if (product.config.saveAsObjects === true) { + /* For saving objects we need to take care of a different set of rules. */ for (let j = 0; j < product.record.properties.length; j++) { let recordProperty = product.record.properties[j] if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { @@ -73,26 +75,50 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { 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) + if (bot.processingDailyFiles) { + /* + When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, + since the object will be duplicated on the next day. How do we know we are positioned at the last candle + of the day? Easy: the end of the candle must be 1 millisecod before the next day. That happens at any + time frame. + */ + let currentDay = new Date(tradingEngine.current.candle.end.value) + let nextDay = new Date(tradingEngine.current.candle.end.value + 1) + if (currentDay.getUTCDate() !== nextDay.getUTCDate()) { + /* + We will save the object only if it is closed, becasuse we are at the last candle of the day. + */ + if (propertyValue === product.config.propertyValueThatClosesObject) { + outputDatasetArray.push(record) + } + } else { + /* + When we are not at the end of the day, we will save the object normally, like in market files. + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + } + } + else { + /* + For Market Files 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. */ + /* When we are not dealing with objects, we add every record to the existing file. */ outputDatasetArray.push(record) } } } function scanRecordDefinition(product) { - if (product.config.codeName === 'Position-Objects') { - let a = 1 - } /* The Product Root Node is the root of the node hiriarchy branch that we need to look at in order to fnd the node where we are going to extract the value. @@ -210,4 +236,29 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } return value } + + function pruneOutputFile(product, outputFile) { + if (outputFile.isPrunned === true) { return } + /* + When a session is resumed, we will be potentially reading output files belonging to a previous session execution. + For that reason we need to prune all the records that are beyond the current candle. We do not delete everything + because we might be resuming a stopped session, which is fine. + */ + for (let i = 0; i < outputFile.length; i++) { + let record = outputFile[i] + + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === 'end') { + let end = record[j] + if (end >= tradingEngine.current.candle.end.value) { + outputFile.splice(i, 1) + pruneOutputFile(product, outputFile) + return + } + } + } + } + outputFile.isPrunned = true + } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 5ddc2d633..208ff2c4c 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -82,7 +82,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT initializeLoop() function initializeLoop() { - if (bot.RESUME === true && tradingEngine.last.candle !== undefined) { + if (bot.FIRST_EXECUTION === false && tradingEngine.last.candle !== undefined) { /* Estimate the Initial Candle based on the last candle saved at tradingEngine */ let firstBegin = candles[0].begin let lastBegin = tradingEngine.last.candle.begin @@ -294,7 +294,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT if (dataDependencyNode.referenceParent.config.codeName !== 'Multi-Period-Daily') { continue } let singularVariableName = dataDependencyNode.referenceParent.parentNode.config.singularVariableName let pluralVariableName = dataDependencyNode.referenceParent.parentNode.config.pluralVariableName - let currentElement = getElement(thisChart[pluralVariableName], candle, 'Daily' + '-' + mapKey + '-' + pluralVariableName) + let currentElement = getElement(thisChart[pluralVariableName], 'Daily' + '-' + mapKey + '-' + pluralVariableName) thisChart[singularVariableName] = currentElement } } diff --git a/Session.js b/Session.js index 8b362bd64..beedc4463 100644 --- a/Session.js +++ b/Session.js @@ -61,6 +61,7 @@ bot.SESSION = JSON.parse(message.event.session) bot.DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter) bot.RESUME = message.event.resume + bot.FIRST_EXECUTION = true bot.SESSION.stop = stopSession // stop function setUpAppSchema() From 124f6a71d4325ccd764650c9c20e36d159eaad41 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 6 Jul 2020 13:58:10 +0200 Subject: [PATCH 079/113] Ready First Half of Beta 6 --- LowFrequencyTrading/TradingEngine.js | 1 + LowFrequencyTrading/TradingPosition.js | 7 +++++++ LowFrequencyTrading/TradingStrategy.js | 7 +++++++ LowFrequencyTrading/TradingSystem.js | 5 +---- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 62f0889b5..7a549481d 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -5,6 +5,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { */ const MODULE_NAME = 'Trading Engine' let thisObject = { + cloneValues: cloneValues, setCurrentCandle: setCurrentCandle, updateEpisodeCountersAndStatistics: updateEpisodeCountersAndStatistics, updateDistanceToEventsCounters: updateDistanceToEventsCounters, diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index 51389abc8..ad7bc42fc 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -57,6 +57,13 @@ exports.newTradingPosition = function newTradingPosition(bot, logger) { tradingEngine.current.position.status.value = 'Closed' tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value tradingEngine.current.position.exitType.value = exitType + /* + Now that the position is closed, it is the right time to move this position from current to last at the Trading Engine data structure. + */ + const TRADING_ENGINE_MODULE = require('./TradingEngine.js') + let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) + + tradingEngineModule.cloneValues(tradingEngine.current.position, tradingEngine.last.position) } function resetPosition() { diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index 0cd221649..a07627cd1 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -45,6 +45,13 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger) { tradingEngine.current.strategy.exitType.value = exitType tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value + /* + Now that the strategy is closed, it is the right time to move this strategy from current to last at the Trading Engine data structure. + */ + const TRADING_ENGINE_MODULE = require('./TradingEngine.js') + let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) + + tradingEngineModule.cloneValues(tradingEngine.current.strategy, tradingEngine.last.strategy) } function resetStrategy() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index c027c2612..0aaa58865 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -832,9 +832,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.last.position.positionStatistics.profitLoss.value = 0 - tradingEngine.last.position.positionStatistics.ROI.value = 0 - let feePaid = 0 if (bot.sessionAndMarketBaseAssetsAreEqual) { @@ -874,7 +871,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.updateStatistics() /* Recalculating Episode Counters */ - if (tradingEngine.last.position.positionStatistics.profitLoss.value > 0) { + if (tradingEngine.current.position.positionStatistics.profitLoss.value > 0) { tradingEngine.episode.episodeCounters.hits.value++ } else { tradingEngine.episode.episodeCounters.fails.value++ From 19fcaa820f6f5b80d8e9e57302a2aecbeacdc9c8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 7 Jul 2020 19:16:01 +0200 Subject: [PATCH 080/113] 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 + } } From 0c7f2f3865698c51877f808f7f3eec51ce03f333 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 8 Jul 2020 10:41:04 +0200 Subject: [PATCH 081/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 4436142c8..63821bc28 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -432,26 +432,26 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { 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) + checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) } } - function checkOrders(orders) { + function checkOrders(orders, executionAlgorithm, executionNode) { for (let i = 0; i < orders.length; i++) { let order = orders[i] switch (order.status) { case 'Open': { - let mustCancelOrder = checkOrderEvent(order.cancelOrderEvent) + let mustCancelOrder = checkOrderEvent(order.cancelOrderEvent, order, executionAlgorithm, executionNode) if (mustCancelOrder === true) { // Cancel Order order.status = 'Closed' order.exitType = 'Cancelled' } - let mustMoveOrder = checkOrderEvent(order.moveOrderEvent) + let mustMoveOrder = checkOrderEvent(order.moveOrderEvent, order, executionAlgorithm, executionNode) if (mustMoveOrder === true && order.status === 'Open') { // Move Order order.status = 'Open' @@ -464,7 +464,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { break default: { order.status = 'Not Open' - let mustCreateOrder = checkOrderEvent(order.createOrderEvent) + let mustCreateOrder = checkOrderEvent(order.createOrderEvent, order, executionAlgorithm, executionNode) if (mustCreateOrder === true) { // Create Order order.status = 'Open' @@ -474,7 +474,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } - function checkOrderEvent(event) { + function checkOrderEvent(event, order, executionAlgorithm, executionNode) { if (event !== undefined) { for (let k = 0; k < event.situations.length; k++) { let situation = event.situations[k] @@ -489,9 +489,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { 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 + tradingSystem.highlights.push(order.id) + tradingSystem.highlights.push(executionAlgorithm.id) + tradingSystem.highlights.push(executionNode.id) announcementsModule.makeAnnoucements(event) return true // only one event can pass at the time From 549ee50cd9244db779f7b5bfa3b3ca7792f82440 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 12 Jul 2020 13:13:24 +0200 Subject: [PATCH 082/113] Big Reorganization into Stages --- LowFrequencyTrading/TradingSimulation.js | 80 +- LowFrequencyTrading/TradingSystem.js | 893 +++++++++++++---------- server.js | 7 + 3 files changed, 523 insertions(+), 457 deletions(-) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 87c5e53f2..ed061680a 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -41,12 +41,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT const MIN_TAKE_PROFIT_VALUE = 0.0000000001 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER - /* Variables to know when we need to open or close a position. */ - let triggeringOn - let triggeringOff - let takingPosition - let closingPosition - /* These are the Modules we will need to run the Simulation */ const EXCHANGE_API = require('../ExchangeAPI'); exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); @@ -56,10 +50,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() - const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') - let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger) - tradingExecutionModule.initialize() - const TRADING_RECORDS_MODULE = require('./TradingRecords.js') let tradingRecordsModule = TRADING_RECORDS_MODULE.newTradingRecords(bot, logger) tradingRecordsModule.initialize(outputDatasetsMap) @@ -153,68 +143,21 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.mantainPositions() tradingEngineModule.updateEpisodeCountersAndStatistics() - /* - The next step is about evaluationg Conditions and Formulas of the Trading System. - */ - tradingSystemModule.evalConditions() - tradingSystemModule.evalFormulas() - /* - Next we check if we will be triggering on, off or taking position. - */ - triggeringOn = tradingSystemModule.checkTriggerOn() - triggeringOff = tradingSystemModule.checkTriggerOff() - takingPosition = tradingSystemModule.checkTakePosition() - - /* Time to check the execution of orders */ - tradingSystemModule.checkExecution() - - /* Stop Loss Management */ - if (takingPosition !== true) { - tradingSystemModule.checkStopPhasesEvents() - tradingSystemModule.calculateStopLoss() - } - - /* Take Profit Management */ - if (takingPosition !== true) { - tradingSystemModule.checkTakeProfitPhaseEvents() - tradingSystemModule.calculateTakeProfit() - } - tradingEngineModule.updateDistanceToEventsCounters() - /* Checking if Stop or Take Profit were hit */ - if (takingPosition !== true) { - closingPosition = tradingSystemModule.checkStopLossOrTakeProfitWasHit() - } + /* Run the Trigger Stage */ + tradingSystemModule.triggerStage() - /* Taking a Position */ - if (takingPosition === true) { - tradingSystemModule.getReadyToTakePosition() - /* - Whem we calculated all the formulas at the begining of the loop, we didn't know - we were going to take position in this same loop cycle. Some might formulas depends on - calculations that are triggered only when we are taking position. For that reason - we will calculate all formulas again, and after that the Stop Loss and Take Profit. - */ - tradingSystemModule.evalFormulas() - tradingSystemModule.calculateTakeProfit() - tradingSystemModule.calculateStopLoss() + /* Run the Open Stage */ + tradingSystemModule.openStage() - tradingExecutionModule.takePosition() - tradingSystemModule.takePosition() - takingPosition = false - } + /* Run the Manage Stage */ + tradingSystemModule.manageStage() - /* Closing a Position */ - if (closingPosition === true) { - tradingSystemModule.getReadyToClosePosition() - tradingExecutionModule.closePosition() - tradingSystemModule.closePosition() - closingPosition = false - } + /* Run the Close Stage */ + tradingSystemModule.closeStage() - /* After a position was closed, we need to close the strategy. */ - tradingSystemModule.exitStrategyAfterPosition() + /* Add new records to the process output */ tradingRecordsModule.appendRecords() controlLoop() @@ -246,10 +189,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function afterLoop() { tradingEngineModule.finalize() - tradingExecutionModule.finalize() tradingSystemModule.finalize() tradingRecordsModule.finalize() + tradingEngineModule = undefined + tradingSystemModule = undefined + tradingRecordsModule = undefined + callback() } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 63821bc28..3806416ab 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -8,24 +8,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { reset: reset, mantainStrategies: mantainStrategies, mantainPositions: mantainPositions, - evalConditions: evalConditions, - evalFormulas: evalFormulas, - checkTriggerOn: checkTriggerOn, - checkTriggerOff: checkTriggerOff, - checkTakePosition: checkTakePosition, - checkExecution: checkExecution, - checkStopPhasesEvents: checkStopPhasesEvents, - calculateStopLoss: calculateStopLoss, - checkTakeProfitPhaseEvents: checkTakeProfitPhaseEvents, - calculateTakeProfit: calculateTakeProfit, - checkStopLossOrTakeProfitWasHit: checkStopLossOrTakeProfitWasHit, - getReadyToTakePosition: getReadyToTakePosition, - takePosition: takePosition, - getReadyToClosePosition: getReadyToClosePosition, - closePosition: closePosition, - exitStrategyAfterPosition: exitStrategyAfterPosition, - getPositionSize: getPositionSize, - getPositionRate: getPositionRate, + triggerStage: triggerStage, + openStage: openStage, + manageStage: manageStage, + closeStage: closeStage, initialize: initialize, finalize: finalize } @@ -51,6 +37,15 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const SNAPSHOTS_MODULE = require('./Snapshots.js') let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) + const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') + let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger) + + /* Variables to know when we need to open or close a position. */ + let triggeringOn + let triggeringOff + let takingPosition + let closingPosition + return thisObject function initialize() { @@ -62,6 +57,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.initialize() announcementsModule.initialize() snapshotsModule.initialize() + tradingExecutionModule.initialize() } function finalize() { @@ -77,6 +73,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { snapshotsModule.finalize() snapshotsModule = undefined + tradingExecutionModule.finalize() + tradingExecutionModule = undefined + chart = undefined conditions = undefined @@ -120,28 +119,39 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.updateEnds() } - function evalConditions() { - evalNode(bot.simulationState.tradingSystem, 'Conditions') + function evalConditions(startingNode, descendentOfNodeType) { + evalNode(startingNode, 'Conditions', descendentOfNodeType) } - function evalFormulas() { - evalNode(bot.simulationState.tradingSystem, 'Formulas') + function evalFormulas(startingNode, descendentOfNodeType) { + evalNode(startingNode, 'Formulas', descendentOfNodeType) } - function evalNode(node, evaluating) { + function evalNode(node, evaluating, descendentOfNodeType, isDescendent) { if (node === undefined) { return } + /* Verify if this node is decendent of the specified node type */ + if (isDescendent !== true) { + if (node.type === descendentOfNodeType) { + isDescendent = true + } + } + /* Here we check if there is a codition to be evaluated */ if (node.type === 'Condition' && evaluating === 'Conditions') { /* We will eval this condition */ - evalCondition(node) + if (isDescendent === true) { + evalCondition(node) + } } /* Here we check if there is a formula to be evaluated */ if (node.type === 'Formula' && evaluating === 'Formulas') { if (node.code !== undefined) { /* We will eval this formula */ - evalFormula(node) + if (isDescendent === true) { + evalFormula(node) + } } } @@ -158,7 +168,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { case 'node': { if (property.name !== previousPropertyName) { if (node[property.name] !== undefined) { - evalNode(node[property.name], evaluating) + evalNode(node[property.name], evaluating, descendentOfNodeType, isDescendent) } previousPropertyName = property.name } @@ -168,7 +178,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (node[property.name] !== undefined) { let nodePropertyArray = node[property.name] for (let m = 0; m < nodePropertyArray.length; m++) { - evalNode(nodePropertyArray[m], evaluating) + evalNode(nodePropertyArray[m], evaluating, descendentOfNodeType, isDescendent) } } break @@ -268,29 +278,89 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { logger.write(MODULE_NAME, '[INFO] evalFormula -> error = ' + error) } - function checkTriggerOn() { - if ( - tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue - ) { - /* - To pick a new strategy we will check that any of the situations of the trigger on is true. - Once we enter into one strategy, we will ignore market conditions for others. However there is also - a strategy trigger off which can be hit before taking a position. If hit, we would - be outside a strategy again and looking for the conditions to enter all over again. - */ + function triggerStage() { + /* + We check if we will be triggering on, off or taking position. + */ + triggeringOn = checkTriggerOn() + triggeringOff = checkTriggerOff() + takingPosition = checkTakePosition() - for (let j = 0; j < tradingSystem.tradingStrategies.length; j++) { - if ( // If a strategy was already picked during the loop, we exit the loop - tradingEngine.current.strategy.index.value !== tradingEngine.current.strategy.index.config.initialValue - ) { continue } + function checkTriggerOn() { + if ( + tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue + ) { + /* + To pick a new strategy we will check that any of the situations of the trigger on is true. + Once we enter into one strategy, we will ignore market conditions for others. However there is also + a strategy trigger off which can be hit before taking a position. If hit, we would + be outside a strategy again and looking for the conditions to enter all over again. + */ + evalConditions(tradingSystem, 'Trigger On Event') - let strategy = tradingSystem.tradingStrategies[j] + for (let j = 0; j < tradingSystem.tradingStrategies.length; j++) { + if ( // If a strategy was already picked during the loop, we exit the loop + tradingEngine.current.strategy.index.value !== tradingEngine.current.strategy.index.config.initialValue + ) { continue } + + let strategy = tradingSystem.tradingStrategies[j] + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + if (triggerStage.triggerOn !== undefined) { + for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { + let situation = triggerStage.triggerOn.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(triggerStage.triggerOn.id) + tradingSystem.highlights.push(triggerStage.id) + + tradingStrategyModule.openStrategy('Trigger Stage', j, situation.name, strategy.name) + + tradingEngine.current.distanceToEvent.triggerOn.value = 1 + + announcementsModule.makeAnnoucements(triggerStage.triggerOn) + announcementsModule.makeAnnoucements(triggerStage) + + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyEntry() + } + } + } + + logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) + return true // This Means that we have just met the conditions to trigger on. + } + } + } + } + } + } + return false // This Means that we have not met the conditions to trigger on. + } + + function checkTriggerOff() { + if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage + evalConditions(strategy, 'Trigger Off Event') + if (triggerStage !== undefined) { - if (triggerStage.triggerOn !== undefined) { - for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { - let situation = triggerStage.triggerOn.situations[k] + if (triggerStage.triggerOff !== undefined) { + for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { + let situation = triggerStage.triggerOff.situations[k] let passed if (situation.conditions.length > 0) { passed = true @@ -301,208 +371,201 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingSystem.values.push([situation.id, passed]) if (passed) { tradingSystem.highlights.push(situation.id) - tradingSystem.highlights.push(triggerStage.triggerOn.id) + tradingSystem.highlights.push(triggerStage.triggerOff.id) tradingSystem.highlights.push(triggerStage.id) - tradingStrategyModule.openStrategy('Trigger Stage', j, situation.name, strategy.name) + tradingStrategyModule.closeStrategy('Trigger Off') - tradingEngine.current.distanceToEvent.triggerOn.value = 1 + tradingEngine.current.distanceToEvent.triggerOff.value = 1 - announcementsModule.makeAnnoucements(triggerStage.triggerOn) - announcementsModule.makeAnnoucements(triggerStage) + announcementsModule.makeAnnoucements(triggerStage.triggerOff) + + logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) + return true // This Means that we have just met the conditions to trigger off. + } + } + } + } + } + return false // This Means that we have not met the conditions to trigger off. + } + + function checkTakePosition() { + /* Take Position Condition */ + if ( + tradingEngine.current.strategy.stageType.value === 'Trigger Stage' && + tradingEngine.current.strategy.status.value === 'Open' + ) { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let triggerStage = strategy.triggerStage + + evalConditions(strategy, 'Take Position Event') + evalFormulas(strategy, 'Take Position Event') + + if (triggerStage !== undefined) { + if (triggerStage.takePosition !== undefined) { + for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { + let situation = triggerStage.takePosition.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(triggerStage.takePosition.id) + tradingSystem.highlights.push(triggerStage.id) + + tradingStrategyModule.updateStageType('Open Stage') + tradingPositionModule.openPosition(situation.name) + + announcementsModule.makeAnnoucements(triggerStage.takePosition) + announcementsModule.makeAnnoucements(strategy.openStage) if (bot.SESSION.type === 'Backtesting Session') { if (sessionParameters.snapshots !== undefined) { - if (sessionParameters.snapshots.config.strategy === true) { - snapshotsModule.strategyEntry() + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionEntry() } } } - logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) - return true // This Means that we have just met the conditions to trigger on. + logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') + return true // This Means that we have just met the conditions to take position. } } } } } + return false // This Means that we have not met the conditions to take position. } - return false // This Means that we have not met the conditions to trigger on. } - function checkTriggerOff() { - if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let triggerStage = strategy.triggerStage + function openStage() { - if (triggerStage !== undefined) { - if (triggerStage.triggerOff !== undefined) { - for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { - let situation = triggerStage.triggerOff.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(triggerStage.triggerOff.id) - tradingSystem.highlights.push(triggerStage.id) - - tradingStrategyModule.closeStrategy('Trigger Off') - - tradingEngine.current.distanceToEvent.triggerOff.value = 1 - - announcementsModule.makeAnnoucements(triggerStage.triggerOff) - - logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) - return true // This Means that we have just met the conditions to trigger off. - } - } - } - } - } - return false // This Means that we have not met the conditions to trigger off. - } - - function checkTakePosition() { - /* Take Position Condition */ - if ( - tradingEngine.current.strategy.stageType.value === 'Trigger Stage' && - tradingEngine.current.strategy.status.value === 'Open' - ) { - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let triggerStage = strategy.triggerStage - - if (triggerStage !== undefined) { - if (triggerStage.takePosition !== undefined) { - for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { - let situation = triggerStage.takePosition.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(triggerStage.takePosition.id) - tradingSystem.highlights.push(triggerStage.id) - - tradingStrategyModule.updateStageType('Open Stage') - tradingPositionModule.openPosition(situation.name) - - announcementsModule.makeAnnoucements(triggerStage.takePosition) - announcementsModule.makeAnnoucements(strategy.openStage) - - if (bot.SESSION.type === 'Backtesting Session') { - if (sessionParameters.snapshots !== undefined) { - if (sessionParameters.snapshots.config.position === true) { - snapshotsModule.positionEntry() - } - } - } - - logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') - return true // This Means that we have just met the conditions to take position. - } - } - } - } - } - 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) + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = stageNode.openExecution + + evalConditions(stageNode, 'Initial Definition') + evalFormulas(stageNode, 'Initial Definition') + + /* Taking a Position */ + if (takingPosition === true) { + getReadyToTakePosition() + + calculateTakeProfit() + calculateStopLoss() + + tradingExecutionModule.takePosition() + takePosition() + } + + /* Every Loop Cycle Activity */ + evalConditions(stageNode, 'Open Execution') + evalFormulas(stageNode, 'Open Execution') + + checkExecution(executionNode) + } - if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { - let executionNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage.closeExecution - checkExecutionAlgorithms(executionNode) - } + function getReadyToTakePosition() { + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.positions.value++ - function checkExecutionAlgorithms(executionNode) { - for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { - let executionAlgorithm = executionNode.executionAlgorithms[i] - checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.takePosition.value = 1 + + /* Position size and rate */ + tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) + + /* We take what was calculated at the formula of position rate and apply the slippage. */ + tradingPositionModule.applySlippageToRate() + + function getPositionSize() { + let balance + if (bot.sessionAndMarketBaseAssetsAreEqual) { + balance = tradingEngine.current.balance.baseAsset.value + } else { + balance = tradingEngine.current.balance.quotedAsset.value + } + const DEFAULT_VALUE = balance + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + + if (strategy.openStage === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionSize === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return DEFAULT_VALUE + + let value = formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) + if (value === undefined) return DEFAULT_VALUE + return value + } + + function getPositionRate() { + const DEFAULT_VALUE = tradingEngine.current.candle.close.value + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + + if (strategy.openStage === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionRate === undefined) return DEFAULT_VALUE + if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return DEFAULT_VALUE + + let value = formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) + if (value === undefined) return DEFAULT_VALUE + return value } } - function checkOrders(orders, executionAlgorithm, executionNode) { - for (let i = 0; i < orders.length; i++) { - let order = orders[i] - switch (order.status) { - case 'Open': { - let mustCancelOrder = checkOrderEvent(order.cancelOrderEvent, order, executionAlgorithm, executionNode) - if (mustCancelOrder === true) { - // Cancel Order - order.status = 'Closed' - order.exitType = 'Cancelled' - } + function takePosition() { + tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - let mustMoveOrder = checkOrderEvent(order.moveOrderEvent, order, executionAlgorithm, executionNode) - 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, order, executionAlgorithm, executionNode) - if (mustCreateOrder === true) { - // Create Order - order.status = 'Open' - } - } - } - } - } + let feePaid = 0 - function checkOrderEvent(event, order, executionAlgorithm, executionNode) { - 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 - } + if (bot.sessionAndMarketBaseAssetsAreEqual) { + feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - passed = checkConditions(situation, passed) + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value + } else { + feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingSystem.values.push([situation.id, passed]) - if (passed) { - tradingSystem.highlights.push(situation.id) - tradingSystem.highlights.push(event.id) - tradingSystem.highlights.push(order.id) - tradingSystem.highlights.push(executionAlgorithm.id) - tradingSystem.highlights.push(executionNode.id) - - announcementsModule.makeAnnoucements(event) - return true // only one event can pass at the time - } - } + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value } } } - function checkStopPhasesEvents() { + function manageStage() { + if (takingPosition === true) { + /* We jump the management at the loop cycle where the position is being taken. */ + takingPosition = false + return + } + if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let manageStage = strategy.manageStage + evalConditions(manageStage, 'Manage Stage') + evalFormulas(manageStage, 'Manage Stage') + + /* Stop Loss Management */ + checkStopPhasesEvents() + calculateStopLoss() + + /* Take Profit Management */ + checkTakeProfitPhaseEvents() + calculateTakeProfit() + + /* Checking if Stop or Take Profit were hit */ + closingPosition = checkStopLossOrTakeProfitWasHit() + } + + function checkStopPhasesEvents() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage @@ -616,44 +679,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } } - } - function calculateStopLoss() { - if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.initialStopLoss !== undefined) { - phase = openStage.initialDefinition.initialStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] - } - } - } - - if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.managedStopLoss !== undefined) { - phase = manageStage.managedStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] - } - } - - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.stopLoss.value - tradingPositionModule.applyStopLossFormula(formulas, phase.formula.id) - - if (tradingEngine.current.position.stopLoss.value !== previousValue) { - announcementsModule.makeAnnoucements(phase) - } - } - } - } - } - - function checkTakeProfitPhaseEvents() { - if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + function checkTakeProfitPhaseEvents() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let openStage = strategy.openStage let manageStage = strategy.manageStage @@ -767,45 +794,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } } } - } - function calculateTakeProfit() { - if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - let openStage = strategy.openStage - let manageStage = strategy.manageStage - let phase - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { - if (openStage.initialDefinition !== undefined) { - if (openStage.initialDefinition.initialTakeProfit !== undefined) { - phase = openStage.initialDefinition.initialTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] - } - } - } - - if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { - if (manageStage.managedTakeProfit !== undefined) { - phase = manageStage.managedTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] - } - } - - if (phase !== undefined) { - if (phase.formula !== undefined) { - let previousValue = tradingEngine.current.position.takeProfit.value - tradingPositionModule.applyTakeProfitFormula(formulas, phase.formula.id) - - if (tradingEngine.current.position.takeProfit.value !== previousValue) { - announcementsModule.makeAnnoucements(phase) - } - } - } - } - } - - function checkStopLossOrTakeProfitWasHit() { - { - if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + function checkStopLossOrTakeProfitWasHit() { + { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] /* Checking what happened since the last execution. We need to know if the Stop Loss @@ -841,143 +832,264 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { announcementsModule.makeAnnoucements(strategy.closeStage) return true // This means that the Take Profit was hit. } + return false // This means that the neither the STOP nor the Take Profit were hit. } - return false // This means that the neither the STOP nor the Take Profit were hit. } } - function getReadyToTakePosition() { - /* Updating Episode Counters */ - tradingEngine.episode.episodeCounters.positions.value++ + function closeStage() { - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.takePosition.value = 1 + if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + let executionNode = stageNode.closeExecution - /* Position size and rate */ - tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) + evalConditions(stageNode, 'Close Execution') + evalFormulas(stageNode, 'Close Execution') - /* We take what was calculated at the formula of position rate and apply the slippage. */ - tradingPositionModule.applySlippageToRate() - } + /* Closing a Position */ + if (closingPosition === true) { + getReadyToClosePosition() + tradingExecutionModule.closePosition() + closePosition() + closingPosition = false + } - function takePosition() { - tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - - let feePaid = 0 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value - } else { - feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value - } - } - - function getReadyToClosePosition() { - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.closePosition.value = 1 - - /* Position size and rate */ - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] - } - - function closePosition() { - /* We calculate the new balances after the position is closed. */ - let feePaid = 0 - if (bot.sessionAndMarketBaseAssetsAreEqual) { - feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = 0 - } else { - feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.baseAsset.value = 0 + checkExecution(executionNode) } - tradingPositionModule.updateStatistics() + /* After a position was closed, we need to close the strategy. */ + exitStrategyAfterPosition() - /* Recalculating Episode Counters */ - if (tradingEngine.current.position.positionStatistics.profitLoss.value > 0) { - tradingEngine.episode.episodeCounters.hits.value++ - } else { - tradingEngine.episode.episodeCounters.fails.value++ + function getReadyToClosePosition() { + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.closePosition.value = 1 + + /* Position size and rate */ + let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] } - /* Recalculating Episode Statistics */ - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance - tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 - } else { - tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance - tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 - tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value - tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + function closePosition() { + /* We calculate the new balances after the position is closed. */ + let feePaid = 0 + if (bot.sessionAndMarketBaseAssetsAreEqual) { + feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid + tradingEngine.current.balance.quotedAsset.value = 0 + } else { + feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid + tradingEngine.current.balance.baseAsset.value = 0 + } + + tradingPositionModule.updateStatistics() + + /* Recalculating Episode Counters */ + if (tradingEngine.current.position.positionStatistics.profitLoss.value > 0) { + tradingEngine.episode.episodeCounters.hits.value++ + } else { + tradingEngine.episode.episodeCounters.fails.value++ + } + + /* Recalculating Episode Statistics */ + if (bot.sessionAndMarketBaseAssetsAreEqual) { + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.baseAsset.value - sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionBaseAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionBaseAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + } else { + tradingEngine.episode.episodeStatistics.profitLoss.value = tradingEngine.current.balance.quotedAsset.value - sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.ROI.value = (sessionParameters.sessionQuotedAsset.config.initialBalance + tradingEngine.episode.episodeStatistics.profitLoss.value) / sessionParameters.sessionQuotedAsset.config.initialBalance - 1 + tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value + tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 + } } - } - function exitStrategyAfterPosition() { - if (tradingEngine.current.strategy.stageType.value !== 'Close Stage') { return } + function exitStrategyAfterPosition() { + if (tradingEngine.current.strategy.stageType.value !== 'Close Stage') { return } - tradingStrategyModule.closeStrategy('Position Closed') - tradingEngine.current.distanceToEvent.triggerOff.value = 1 + tradingStrategyModule.closeStrategy('Position Closed') + tradingEngine.current.distanceToEvent.triggerOff.value = 1 - if (bot.SESSION.type === 'Backtesting Session') { - if (sessionParameters.snapshots !== undefined) { - if (sessionParameters.snapshots.config.strategy === true) { - snapshotsModule.strategyExit() + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.strategy === true) { + snapshotsModule.strategyExit() + } } } - } - if (bot.SESSION.type === 'Backtesting Session') { - if (sessionParameters.snapshots !== undefined) { - if (sessionParameters.snapshots.config.position === true) { - snapshotsModule.positionExit() + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionExit() + } } } } } - function getPositionSize() { - let balance - if (bot.sessionAndMarketBaseAssetsAreEqual) { - balance = tradingEngine.current.balance.baseAsset.value - } else { - balance = tradingEngine.current.balance.quotedAsset.value - } - const DEFAULT_VALUE = balance + function calculateStopLoss() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase - if (strategy.openStage === undefined) return DEFAULT_VALUE - if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE - if (strategy.openStage.initialDefinition.positionSize === undefined) return DEFAULT_VALUE - if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return DEFAULT_VALUE + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.initialStopLoss !== undefined) { + phase = openStage.initialDefinition.initialStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value] + } + } + } - let value = formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) - if (value === undefined) return DEFAULT_VALUE - return value + if (tradingEngine.current.position.stopLoss.stopLossStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.managedStopLoss !== undefined) { + phase = manageStage.managedStopLoss.phases[tradingEngine.current.position.stopLoss.stopLossPhase.value - 1] + } + } + + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.stopLoss.value + tradingPositionModule.applyStopLossFormula(formulas, phase.formula.id) + + if (tradingEngine.current.position.stopLoss.value !== previousValue) { + announcementsModule.makeAnnoucements(phase) + } + } + } } - function getPositionRate() { - const DEFAULT_VALUE = tradingEngine.current.candle.close.value + function calculateTakeProfit() { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase - if (strategy.openStage === undefined) return DEFAULT_VALUE - if (strategy.openStage.initialDefinition === undefined) return DEFAULT_VALUE - if (strategy.openStage.initialDefinition.positionRate === undefined) return DEFAULT_VALUE - if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return DEFAULT_VALUE + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.initialTakeProfit !== undefined) { + phase = openStage.initialDefinition.initialTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value] + } + } + } - let value = formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) - if (value === undefined) return DEFAULT_VALUE - return value + if (tradingEngine.current.position.takeProfit.takeProfitStage.value === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.managedTakeProfit !== undefined) { + phase = manageStage.managedTakeProfit.phases[tradingEngine.current.position.takeProfit.takeProfitPhase.value - 1] + } + } + + if (phase !== undefined) { + if (phase.formula !== undefined) { + let previousValue = tradingEngine.current.position.takeProfit.value + tradingPositionModule.applyTakeProfitFormula(formulas, phase.formula.id) + + if (tradingEngine.current.position.takeProfit.value !== previousValue) { + announcementsModule.makeAnnoucements(phase) + } + } + } + } + + function checkExecution(executionNode) { + + checkExecutionAlgorithms(executionNode) + + function checkExecutionAlgorithms(executionNode) { + for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { + let executionAlgorithm = executionNode.executionAlgorithms[i] + checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + } + } + + function checkOrders(orders, executionAlgorithm, executionNode) { + for (let i = 0; i < orders.length; i++) { + let order = orders[i] + + /* Basic Validations */ + if (order.referenceParent === undefined) { continue } + if (order.referenceParent.identifier === undefined) { continue } + if (order.referenceParent.begin === undefined) { continue } + if (order.referenceParent.end === undefined) { continue } + if (order.referenceParent.rate === undefined) { continue } + if (order.referenceParent.size === undefined) { continue } + if (order.referenceParent.status === undefined) { continue } + if (order.referenceParent.algorithmName === undefined) { continue } + if (order.referenceParent.orderCounters === undefined) { continue } + if (order.referenceParent.orderCounters.periods === undefined) { continue } + if (order.positionSize === undefined) { continue } + if (order.positionSize.formula === undefined) { continue } + + switch (order.referenceParent.status.value) { + case 'Open': { + order.referenceParent.end.value = tradingEngine.current.candle.end.value + + let mustCancelOrder = checkOrderEvent(order.cancelOrderEvent, order, executionAlgorithm, executionNode) + if (mustCancelOrder === true) { + // Cancel Order + order.referenceParent.status.value = 'Closed' + order.referenceParent.exitType.value = 'Cancelled' + } + + let mustMoveOrder = checkOrderEvent(order.moveOrderEvent, order, executionAlgorithm, executionNode) + if (mustMoveOrder === true && order.referenceParent.status.value === 'Open') { + // Move Order + order.referenceParent.status.value = 'Open' + } + } + break + case 'Closed': { + // Nothing to do here + } + break + default: { + order.referenceParent.status.value = 'Not Open' + let mustCreateOrder = checkOrderEvent(order.createOrderEvent, order, executionAlgorithm, executionNode) + if (mustCreateOrder === true) { + // Create Order + order.referenceParent.identifier.value = global.UNIQUE_ID() + order.referenceParent.begin.value = tradingEngine.current.candle.begin.value + order.referenceParent.end.value = tradingEngine.current.candle.end.value + order.referenceParent.rate.value = tradingEngine.current.candle.end.value + order.referenceParent.size.value = formulas.get(order.positionSize.formula.id) + order.referenceParent.status.value = 'Open' + order.referenceParent.algorithmName.value = executionAlgorithm.name + order.referenceParent.orderCounters.periods++ + } + } + } + } + } + + function checkOrderEvent(event, order, executionAlgorithm, executionNode) { + 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(order.id) + tradingSystem.highlights.push(executionAlgorithm.id) + tradingSystem.highlights.push(executionNode.id) + + announcementsModule.makeAnnoucements(event) + return true // only one event can pass at the time + } + } + } + } } function isItInside(elementWithTimestamp, elementWithBeginEnd) { @@ -993,6 +1105,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } function checkConditions(situation, passed) { + /* Given a Situation, we check if all children conditions are true or not */ for (let m = 0; m < situation.conditions.length; m++) { let condition = situation.conditions[m] diff --git a/server.js b/server.js index 56f923cae..b0a6a8c99 100644 --- a/server.js +++ b/server.js @@ -33,6 +33,13 @@ global.ONE_MIN_IN_MILISECONDS = 60 * 1000 global.LOGGER_MAP = new Map() global.SESSION_MAP = new Map() +global.UNIQUE_ID = function () { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { + var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8) + return v.toString(16) + }) +} + process.on('uncaughtException', function (err) { console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack) From 55c1c67e267287cee4397c7052dc5c2701d306e7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 13 Jul 2020 14:45:15 +0200 Subject: [PATCH 083/113] Market Orders Being Saved --- LowFrequencyTrading/TradingEngine.js | 9 + LowFrequencyTrading/TradingPosition.js | 5 +- LowFrequencyTrading/TradingRecords.js | 250 +++++++++++++---------- LowFrequencyTrading/TradingSimulation.js | 2 +- LowFrequencyTrading/TradingStrategy.js | 4 +- LowFrequencyTrading/TradingSystem.js | 72 ++++--- 6 files changed, 196 insertions(+), 146 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index 7a549481d..af33f924a 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -5,6 +5,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { */ const MODULE_NAME = 'Trading Engine' let thisObject = { + getNodeById: getNodeById, cloneValues: cloneValues, setCurrentCandle: setCurrentCandle, updateEpisodeCountersAndStatistics: updateEpisodeCountersAndStatistics, @@ -15,6 +16,7 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { let tradingEngine let sessionParameters + let nodesMap = new Map() return thisObject @@ -34,6 +36,11 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { function finalize() { tradingEngine = undefined sessionParameters = undefined + nodesMap = undefined + } + + function getNodeById(NodeId) { + return nodesMap.get(NodeId) } function setCurrentCandle(candle) { @@ -90,6 +97,8 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { function initializeNode(node) { if (node === undefined) { return } + /* Add to Node Map */ + nodesMap.set(node.id, node) /* Here we initialize the node value */ if (node.config !== undefined) { diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index ad7bc42fc..1712cd46e 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -1,4 +1,4 @@ -exports.newTradingPosition = function newTradingPosition(bot, logger) { +exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEngineModule) { /* This module packages all functions related to Positions. */ @@ -60,9 +60,6 @@ exports.newTradingPosition = function newTradingPosition(bot, logger) { /* Now that the position is closed, it is the right time to move this position from current to last at the Trading Engine data structure. */ - const TRADING_ENGINE_MODULE = require('./TradingEngine.js') - let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) - tradingEngineModule.cloneValues(tradingEngine.current.position, tradingEngine.last.position) } diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index f15526649..ca81843e4 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -45,85 +45,107 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { let outputDatasetArray = outputDatasetsMap.get(product.config.codeName) if (bot.processingDailyFiles === true && dataset.config.type === 'Daily Files') { - persistRecord() + persistRecords() } if (bot.processingDailyFiles === false && dataset.config.type === 'Market Files') { - persistRecord() + persistRecords() } - function persistRecord() { - pruneOutputFile(product, outputDatasetArray) - let record = scanRecordDefinition(product) + function persistRecords() { - if (product.config.saveAsObjects === true) { - /* For saving objects we need to take care of a different set of rules. */ - 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. + /* Clean the file from information of previous executions */ + pruneOutputFile(product, outputDatasetArray) + + /* + The product root can be a node or a node property of type array. + */ + let productRoot = eval(product.config.nodePath) + + if (product.config.nodePathType === 'array') { + /* + This means that the configured nodePath is not pointing to a node, but to a node property that is an array. + For that reason we will assume that each element of the array is a record to be outputed + */ + for (let index = 0; index < productRoot.length; index++) { + /* + The Product Root Node is the root of the node hiriarchy from where we are going to extract the record values. + */ + let productRootNode = productRoot[index] + let record = scanRecordDefinition(product, productRootNode, index) + persistIndividualRecord(record, product, outputDatasetArray) + } + } else { + /* + This means that the configured nodePath points to a single node, which is the one whose children constitutes + the record to be saved at the output file. + */ + /* + The Product Root Node is the root of the node hiriarchy from where we are going to extract the record values. + */ + let productRootNode = productRoot + let record = scanRecordDefinition(product, productRootNode) + persistIndividualRecord(record, product, outputDatasetArray) + } + } + } + + function persistIndividualRecord(record, product, outputDatasetArray) { + + if (product.config.saveAsObjects === true) { + /* For saving objects we need to take care of a different set of rules. */ + 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] + + /* Remove Open Records */ + spliceOpenRecords(j, product, outputDatasetArray) + + if (bot.processingDailyFiles) { + /* + When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, + since the object will be duplicated on the next day. How do we know we are positioned at the last candle + of the day? Easy: the end of the candle must be 1 millisecod before the next day. That happens at any + time frame. */ - let lastRecord = outputDatasetArray[outputDatasetArray.length - 1] - if (lastRecord !== undefined) { - let lastRecordValue = lastRecord[j] - if (lastRecordValue !== product.config.propertyValueThatClosesObject) { - outputDatasetArray.splice(outputDatasetArray.length - 1, 1) - } - } - if (bot.processingDailyFiles) { + let currentDay = new Date(tradingEngine.current.candle.end.value) + let nextDay = new Date(tradingEngine.current.candle.end.value + 1) + if (currentDay.getUTCDate() !== nextDay.getUTCDate()) { /* - When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, - since the object will be duplicated on the next day. How do we know we are positioned at the last candle - of the day? Easy: the end of the candle must be 1 millisecod before the next day. That happens at any - time frame. + We will save the object only if it is closed, becasuse we are at the last candle of the day. */ - let currentDay = new Date(tradingEngine.current.candle.end.value) - let nextDay = new Date(tradingEngine.current.candle.end.value + 1) - if (currentDay.getUTCDate() !== nextDay.getUTCDate()) { - /* - We will save the object only if it is closed, becasuse we are at the last candle of the day. - */ - if (propertyValue === product.config.propertyValueThatClosesObject) { - outputDatasetArray.push(record) - } - } else { - /* - When we are not at the end of the day, we will save the object normally, like in market files. - */ - if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { - outputDatasetArray.push(record) - } + if (propertyValue === product.config.propertyValueThatClosesObject) { + outputDatasetArray.push(record) } - } - else { + } else { /* - For Market Files we will add a record everytime that proeprty value does not match this + When we are not at the end of the day, we will save the object normally, like in market files. */ if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { outputDatasetArray.push(record) } } - break } + else { + /* + For Market Files we will add a record everytime that proeprty value does not match this + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + } + break } - } else { - /* When we are not dealing with objects, we add every record to the existing file. */ - outputDatasetArray.push(record) } + } else { + /* When we are not dealing with objects, we add every record to the existing file. */ + outputDatasetArray.push(record) } } - function scanRecordDefinition(product) { - /* - The Product Root Node is the root of the node hiriarchy branch that we need to look at - in order to fnd the node where we are going to extract the value. - */ - let productRootNode = eval(product.config.nodePath) + function scanRecordDefinition(product, productRootNode, index) { + let record = [] for (let j = 0; j < product.record.properties.length; j++) { let recordProperty = product.record.properties[j] @@ -201,64 +223,84 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } return record } - } - function safeNumericValue(value) { - /* - The purpose of this function is to check that value variable does not have a value that - will later break the JSON format of files where this is going to be stored at. - */ - if (value === Infinity) { - value = Number.MAX_SAFE_INTEGER + function safeNumericValue(value) { + /* + The purpose of this function is to check that value variable does not have a value that + will later break the JSON format of files where this is going to be stored at. + */ + if (value === Infinity) { + value = Number.MAX_SAFE_INTEGER + } + if (value === undefined) { + value = 0 + } + if (value === null) { + value = 0 + } + if (isNaN(value)) { + value = 0 + } + return value } - if (value === undefined) { - value = 0 - } - if (value === null) { - value = 0 - } - if (isNaN(value)) { - value = 0 - } - return value - } - function safeStringValue(value) { - /* - The purpose of this function is to check that value variable does not have a value that - will later break the JSON format of files where this is going to be stored at. - */ - if (value === undefined) { - value = '' + function safeStringValue(value) { + /* + The purpose of this function is to check that value variable does not have a value that + will later break the JSON format of files where this is going to be stored at. + */ + if (value === undefined) { + value = '' + } + if (value === null) { + value = '' + } + return value } - if (value === null) { - value = '' + + function pruneOutputFile(product, outputFile) { + if (outputFile.isPrunned === true) { return } + /* + When a session is resumed, we will be potentially reading output files belonging to a previous session execution. + For that reason we need to prune all the records that are beyond the current candle. We do not delete everything + because we might be resuming a stopped session, which is fine. + */ + for (let i = 0; i < outputFile.length; i++) { + let record = outputFile[i] + + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === 'end') { + let end = record[j] + if (end >= tradingEngine.current.candle.end.value) { + outputFile.splice(i, 1) + pruneOutputFile(product, outputFile) + return + } + } + } + } + outputFile.isPrunned = true } - return value - } - function pruneOutputFile(product, outputFile) { - if (outputFile.isPrunned === true) { return } - /* - When a session is resumed, we will be potentially reading output files belonging to a previous session execution. - For that reason we need to prune all the records that are beyond the current candle. We do not delete everything - because we might be resuming a stopped session, which is fine. - */ - for (let i = 0; i < outputFile.length; i++) { - let record = outputFile[i] + function spliceOpenRecords(j, product, outputDatasetArray) { + /* + Before adding records to the output file, we need to remove all open records, + because we will be adding the same records later with potentially + new or updated information. + */ - for (let j = 0; j < product.record.properties.length; j++) { - let recordProperty = product.record.properties[j] - if (recordProperty.config.codeName === 'end') { - let end = record[j] - if (end >= tradingEngine.current.candle.end.value) { - outputFile.splice(i, 1) - pruneOutputFile(product, outputFile) + for (let i = 0; i < outputDatasetArray.length; i++) { + let dataRecord = outputDatasetArray[i] + if (dataRecord !== undefined) { + let dataRecordValue = dataRecord[j] + if (dataRecordValue !== product.config.propertyValueThatClosesObject) { + outputDatasetArray.splice(i, 1) + spliceOpenRecords(j, product, outputDatasetArray) return } } } } - outputFile.isPrunned = true } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index ed061680a..d8b18737b 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -55,7 +55,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingRecordsModule.initialize(outputDatasetsMap) const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') - let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) + let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger, tradingEngineModule) tradingSystemModule.initialize() /* Setting up the candles array: The whole simulation is based on the array of candles at the time-frame defined at the session parameters. */ diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index a07627cd1..9b6fa41cb 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -1,4 +1,4 @@ -exports.newTradingStrategy = function newTradingStrategy(bot, logger) { +exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEngineModule) { /* This module packages all functions related to Strategies. */ @@ -48,8 +48,6 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger) { /* Now that the strategy is closed, it is the right time to move this strategy from current to last at the Trading Engine data structure. */ - const TRADING_ENGINE_MODULE = require('./TradingEngine.js') - let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.cloneValues(tradingEngine.current.strategy, tradingEngine.last.strategy) } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 3806416ab..0ea5f1562 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -1,4 +1,4 @@ -exports.newTradingSystem = function newTradingSystem(bot, logger) { +exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineModule) { /* The Trading System is the user defined set of rules compliant with the Trading Protocol that defines the trading logic to be applied during each cycle of the Simulation. @@ -26,10 +26,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let formulas = new Map() const TRADING_STRATEGY_MODULE = require('./TradingStrategy.js') - let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger) + let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger, tradingEngineModule) const TRADING_POSITION_MODULE = require('./TradingPosition.js') - let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger) + let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger, tradingEngineModule) const ANNOUNCEMENTS_MODULE = require('./Announcements.js') let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) @@ -1008,37 +1008,41 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { function checkOrders(orders, executionAlgorithm, executionNode) { for (let i = 0; i < orders.length; i++) { - let order = orders[i] + let tradingSystemOrder = orders[i] /* Basic Validations */ - if (order.referenceParent === undefined) { continue } - if (order.referenceParent.identifier === undefined) { continue } - if (order.referenceParent.begin === undefined) { continue } - if (order.referenceParent.end === undefined) { continue } - if (order.referenceParent.rate === undefined) { continue } - if (order.referenceParent.size === undefined) { continue } - if (order.referenceParent.status === undefined) { continue } - if (order.referenceParent.algorithmName === undefined) { continue } - if (order.referenceParent.orderCounters === undefined) { continue } - if (order.referenceParent.orderCounters.periods === undefined) { continue } - if (order.positionSize === undefined) { continue } - if (order.positionSize.formula === undefined) { continue } + if (tradingSystemOrder.positionSize === undefined) { continue } + if (tradingSystemOrder.positionSize.formula === undefined) { continue } + if (tradingSystemOrder.referenceParent === undefined) { continue } - switch (order.referenceParent.status.value) { + let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) + + if (tradingEngineOrder.identifier === undefined) { continue } + if (tradingEngineOrder.begin === undefined) { continue } + if (tradingEngineOrder.end === undefined) { continue } + if (tradingEngineOrder.rate === undefined) { continue } + if (tradingEngineOrder.size === undefined) { continue } + if (tradingEngineOrder.status === undefined) { continue } + if (tradingEngineOrder.algorithmName === undefined) { continue } + if (tradingEngineOrder.orderCounters === undefined) { continue } + if (tradingEngineOrder.orderCounters.periods === undefined) { continue } + + + switch (tradingEngineOrder.status.value) { case 'Open': { - order.referenceParent.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value - let mustCancelOrder = checkOrderEvent(order.cancelOrderEvent, order, executionAlgorithm, executionNode) + let mustCancelOrder = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (mustCancelOrder === true) { // Cancel Order - order.referenceParent.status.value = 'Closed' - order.referenceParent.exitType.value = 'Cancelled' + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Cancelled' } - let mustMoveOrder = checkOrderEvent(order.moveOrderEvent, order, executionAlgorithm, executionNode) - if (mustMoveOrder === true && order.referenceParent.status.value === 'Open') { + let mustMoveOrder = checkOrderEvent(tradingSystemOrder.moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (mustMoveOrder === true && tradingEngineOrder.status.value === 'Open') { // Move Order - order.referenceParent.status.value = 'Open' + tradingEngineOrder.status.value = 'Open' } } break @@ -1047,18 +1051,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { } break default: { - order.referenceParent.status.value = 'Not Open' - let mustCreateOrder = checkOrderEvent(order.createOrderEvent, order, executionAlgorithm, executionNode) + tradingEngineOrder.status.value = 'Not Open' + let mustCreateOrder = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (mustCreateOrder === true) { // Create Order - order.referenceParent.identifier.value = global.UNIQUE_ID() - order.referenceParent.begin.value = tradingEngine.current.candle.begin.value - order.referenceParent.end.value = tradingEngine.current.candle.end.value - order.referenceParent.rate.value = tradingEngine.current.candle.end.value - order.referenceParent.size.value = formulas.get(order.positionSize.formula.id) - order.referenceParent.status.value = 'Open' - order.referenceParent.algorithmName.value = executionAlgorithm.name - order.referenceParent.orderCounters.periods++ + tradingEngineOrder.identifier.value = global.UNIQUE_ID() + tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value + tradingEngineOrder.size.value = formulas.get(tradingSystemOrder.positionSize.formula.id) + tradingEngineOrder.status.value = 'Open' + tradingEngineOrder.algorithmName.value = executionAlgorithm.name + tradingEngineOrder.orderCounters.periods.value++ } } } From 97b03499a429d9883cf799a5cd7b21bd4aca7020 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 13 Jul 2020 15:52:50 +0200 Subject: [PATCH 084/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 0ea5f1562..168fca33f 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -1011,9 +1011,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM let tradingSystemOrder = orders[i] /* Basic Validations */ - if (tradingSystemOrder.positionSize === undefined) { continue } - if (tradingSystemOrder.positionSize.formula === undefined) { continue } - if (tradingSystemOrder.referenceParent === undefined) { continue } + if (tradingSystemOrder.config.positionSizePercentage === undefined) { continue } let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) @@ -1059,7 +1057,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value tradingEngineOrder.end.value = tradingEngine.current.candle.end.value tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value - tradingEngineOrder.size.value = formulas.get(tradingSystemOrder.positionSize.formula.id) + tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 tradingEngineOrder.status.value = 'Open' tradingEngineOrder.algorithmName.value = executionAlgorithm.name tradingEngineOrder.orderCounters.periods.value++ From c7a2c383b6c888e7546b5dc6e74ba9ec2f994ca0 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 14 Jul 2020 09:25:56 +0200 Subject: [PATCH 085/113] Update TradingRecords.js --- LowFrequencyTrading/TradingRecords.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index ca81843e4..cb7ff4b0d 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -57,6 +57,17 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { /* Clean the file from information of previous executions */ pruneOutputFile(product, outputDatasetArray) + /* Clean Open Records */ + if (product.config.saveAsObjects === true) { + for (let j = 0; j < product.record.properties.length; j++) { + let recordProperty = product.record.properties[j] + if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { + /* Remove Open Records */ + spliceOpenRecords(j, product, outputDatasetArray) + } + } + } + /* The product root can be a node or a node property of type array. */ @@ -99,9 +110,6 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { if (recordProperty.config.codeName === product.config.propertyNameThatDefinesObject) { let propertyValue = record[j] - /* Remove Open Records */ - spliceOpenRecords(j, product, outputDatasetArray) - if (bot.processingDailyFiles) { /* When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, From ccd492efb5a3b768a6ec00266dabef6ad649ce92 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 14 Jul 2020 10:02:03 +0200 Subject: [PATCH 086/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 168fca33f..6403acf5d 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -1029,6 +1029,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM switch (tradingEngineOrder.status.value) { case 'Open': { tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.orderCounters.periods.value++ + tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS let mustCancelOrder = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (mustCancelOrder === true) { @@ -1050,8 +1052,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM break default: { tradingEngineOrder.status.value = 'Not Open' - let mustCreateOrder = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (mustCreateOrder === true) { + let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { // Create Order tradingEngineOrder.identifier.value = global.UNIQUE_ID() tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value @@ -1059,8 +1061,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 tradingEngineOrder.status.value = 'Open' + tradingEngineOrder.orderName.value = tradingSystemOrder.name tradingEngineOrder.algorithmName.value = executionAlgorithm.name - tradingEngineOrder.orderCounters.periods.value++ + tradingEngineOrder.situationName.value = situationName } } } @@ -1087,7 +1090,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(executionNode.id) announcementsModule.makeAnnoucements(event) - return true // only one event can pass at the time + return situation.name // if the event is triggered, we return the name of the situation that passed } } } From 52378738eacca4da84393995f5f461d1a57184c2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 14 Jul 2020 15:18:43 +0200 Subject: [PATCH 087/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 61 +++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 6403acf5d..c4f44e10f 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -323,7 +323,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.triggerOn.id) tradingSystem.highlights.push(triggerStage.id) - tradingStrategyModule.openStrategy('Trigger Stage', j, situation.name, strategy.name) + tradingStrategyModule.openStrategy(j, situation.name, strategy.name) + tradingStrategyModule.updateStageType('Trigger Stage') tradingEngine.current.distanceToEvent.triggerOn.value = 1 @@ -1024,7 +1025,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (tradingEngineOrder.algorithmName === undefined) { continue } if (tradingEngineOrder.orderCounters === undefined) { continue } if (tradingEngineOrder.orderCounters.periods === undefined) { continue } - + if (tradingEngineOrder.orderStatistics.days === undefined) { continue } + if (tradingEngineOrder.orderStatistics.percentageFilled === undefined) { continue } + if (tradingEngineOrder.orderStatistics.actualRate === undefined) { continue } + if (tradingEngineOrder.orderStatistics.feesPaid === undefined) { continue } switch (tradingEngineOrder.status.value) { case 'Open': { @@ -1037,13 +1041,17 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM // Cancel Order tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Cancelled' + break } let mustMoveOrder = checkOrderEvent(tradingSystemOrder.moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (mustMoveOrder === true && tradingEngineOrder.status.value === 'Open') { // Move Order tradingEngineOrder.status.value = 'Open' + break } + + simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) } break case 'Closed': { @@ -1070,6 +1078,55 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } + function simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + switch (bot.SESSION.type) { + case 'Backtesting Session': { + break + } + case 'Live Trading Session': { + return + } + case 'Fordward Testing Session': { + return + } + case 'Paper Trading Session': { + break + } + } + + if (tradingSystemOrder.simulatedExchangeEvents === undefined) { return } + + /* Partial Fill Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { + tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 + if (tradingEngineOrder.orderStatistics.percentageFilled.value >= 100) { + tradingEngineOrder.orderStatistics.percentageFilled.value = 100 + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + } + } + } + + /* Actual Rate Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { + if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { + tradingEngineOrder.orderStatistics.actualRate.value = formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) + } + } + } + + /* Fees Paid Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { + if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 + } + } + } + } + function checkOrderEvent(event, order, executionAlgorithm, executionNode) { if (event !== undefined) { for (let k = 0; k < event.situations.length; k++) { From a45e3a26ce374e2ac796c572818d3d7c068996b4 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 16 Jul 2020 10:22:34 +0200 Subject: [PATCH 088/113] A --- LowFrequencyTrading/TradingStrategy.js | 3 +- LowFrequencyTrading/TradingSystem.js | 112 +++++++++++++++++-------- 2 files changed, 77 insertions(+), 38 deletions(-) diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index 9b6fa41cb..97d68a7cb 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -27,14 +27,13 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEng tradingEngine = undefined } - function openStrategy(stageType, index, situationName, strategyName) { + function openStrategy(index, situationName, strategyName) { 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 tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value tradingEngine.current.strategy.endRate.value = tradingEngine.current.candle.min.value - tradingEngine.current.strategy.stageType.value = stageType tradingEngine.current.strategy.index.value = index tradingEngine.current.strategy.situationName.value = situationName tradingEngine.current.strategy.strategyName.value = strategyName diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index c4f44e10f..4fbf90aca 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -447,7 +447,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM function openStage() { + /* Most of the stuff happens when we are at the Open Stage */ if (tradingEngine.current.strategy.stageType.value === 'Open Stage') { + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution @@ -470,7 +472,27 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalFormulas(stageNode, 'Open Execution') checkExecution(executionNode) + } + /* + We might have jumped to the Close Stage before we leave the Open Stage. + In such cases there are orders that need to be checked what happened to them, + and others that need to be cancelled. + */ + if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { + + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = stageNode.openExecution + + /* Check if there are unfilled orders */ + if (tradingEngine.current.position.ordersSize.value !== tradingEngine.current.position.filledSize.value) { + /* There are unfilled orders, we will check if they were executed, and cancel the ones that were not. */ + + evalConditions(stageNode, 'Open Execution') + evalFormulas(stageNode, 'Open Execution') + + checkExecution(executionNode, true) + } } function getReadyToTakePosition() { @@ -624,10 +646,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.updateStopLoss(tradingEngine.current.position.stopLoss.stopLossPhase.value + 1, 'Manage Stage') - if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingStrategyModule.updateStageType('Manage Stage') - announcementsModule.makeAnnoucements(manageStage) - } announcementsModule.makeAnnoucements(nextPhaseEvent) return // only one event can pass at the time } @@ -668,10 +686,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM continue } - if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { - tradingStrategyModule.updateStageType('Manage Stage') - announcementsModule.makeAnnoucements(manageStage) - } announcementsModule.makeAnnoucements(moveToPhaseEvent) return // only one event can pass at the time } @@ -739,10 +753,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.updateTakeProfit(tradingEngine.current.position.takeProfit.takeProfitPhase.value + 1, 'Manage Stage') - if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingStrategyModule.updateStageType('Manage Stage') - announcementsModule.makeAnnoucements(manageStage) - } announcementsModule.makeAnnoucements(nextPhaseEvent) return // only one event can pass at the time } @@ -783,10 +793,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM continue } - if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { - tradingStrategyModule.updateStageType('Manage Stage') - announcementsModule.makeAnnoucements(manageStage) - } announcementsModule.makeAnnoucements(moveToPhaseEvent) return // only one event can pass at the time } @@ -864,9 +870,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM function getReadyToClosePosition() { /* Inicializing this counter */ tradingEngine.current.distanceToEvent.closePosition.value = 1 - - /* Position size and rate */ - let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] } function closePosition() { @@ -993,7 +996,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - function checkExecution(executionNode) { + function checkExecution(executionNode, exitStageNow) { checkExecutionAlgorithms(executionNode) @@ -1036,19 +1039,21 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.orderCounters.periods.value++ tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS - let mustCancelOrder = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (mustCancelOrder === true) { - // Cancel Order - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Cancelled' - break - } + if (exitStageNow !== true) { + let mustCancelOrder = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (mustCancelOrder === true) { + // Cancel Order + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Cancelled' + break + } - let mustMoveOrder = checkOrderEvent(tradingSystemOrder.moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (mustMoveOrder === true && tradingEngineOrder.status.value === 'Open') { - // Move Order - tradingEngineOrder.status.value = 'Open' - break + let mustMoveOrder = checkOrderEvent(tradingSystemOrder.moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (mustMoveOrder === true && tradingEngineOrder.status.value === 'Open') { + // Move Order + + break + } } simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) @@ -1059,6 +1064,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } break default: { + if (exitStageNow === true) { return } + tradingEngineOrder.status.value = 'Not Open' let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (situationName !== undefined) { @@ -1067,11 +1074,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value tradingEngineOrder.end.value = tradingEngine.current.candle.end.value tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value - tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 tradingEngineOrder.status.value = 'Open' tradingEngineOrder.orderName.value = tradingSystemOrder.name tradingEngineOrder.algorithmName.value = executionAlgorithm.name tradingEngineOrder.situationName.value = situationName + + /* The size depends on the Formula but also on how much of the total size has already been used. */ + tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 + if (tradingEngine.current.position.ordersSize.value + tradingEngineOrder.size.value > tradingEngine.current.position.size.value) { + /* We reduce the size to the remaining size of the position. */ + tradingEngineOrder.size.value = tradingEngineOrder.size.value - tradingEngine.current.position.ordersSize.value + } + tradingEngine.current.position.ordersSize.value = tradingEngine.current.position.ordersSize.value + tradingEngineOrder.size.value } } } @@ -1099,12 +1113,38 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Partial Fill Simulation */ if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { - tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 - if (tradingEngineOrder.orderStatistics.percentageFilled.value >= 100) { - tradingEngineOrder.orderStatistics.percentageFilled.value = 100 + + /* Percentage Filled Calculation */ + let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 + if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { + percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + } + tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Filled' } + + /* Filled Size Calculation */ + let filledSize = tradingEngineOrder.size.value * percentageFilled / 100 + tradingEngine.current.position.filledSize.value = tradingEngine.current.position.filledSize.value + filledSize + + if (exitStageNow !== true) { + /* Passing to Manage Stage Verification */ + if (tradingEngine.current.position.size.value === tradingEngine.current.position.filledSize.value) { + tradingStrategyModule.updateStageType('Manage Stage') + announcementsModule.makeAnnoucements(manageStage) + } + } else { + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 0) { + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Forced to Cancel' + } + if (tradingEngineOrder.orderStatistics.percentageFilled.value > 0 && tradingEngineOrder.orderStatistics.percentageFilled.value < 100) { + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Partially Filled' + } + } } } From 9cad9b598ae4ca41a0bbe895c9868f57e05a5827 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 16 Jul 2020 16:42:57 +0200 Subject: [PATCH 089/113] stage status --- LowFrequencyTrading/TradingSimulation.js | 3 +- LowFrequencyTrading/TradingStrategy.js | 23 +++- LowFrequencyTrading/TradingSystem.js | 162 +++++++++++++---------- 3 files changed, 115 insertions(+), 73 deletions(-) diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index d8b18737b..a6e1d4ae7 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -356,8 +356,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT function checkMinimunAndMaximunBalance() { /* Checks for Minimun and Maximun Balance. We do the check while not inside any strategy only. */ if ( - tradingEngine.current.strategy.stageType.value === 'No Stage' && - tradingEngine.current.strategy.index.value === -1 + tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue ) { /* First thing to do is to check if we are below the minimum balance, or above the maximun balance. diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index 97d68a7cb..c3ca67f0a 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -7,7 +7,7 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEng openStrategy: openStrategy, closeStrategy: closeStrategy, updateEnds: updateEnds, - updateStageType: updateStageType, + updateStageStatus: updateStageStatus, updateStatus: updateStatus, updateCounters: updateCounters, resetStrategy: resetStrategy, @@ -62,8 +62,25 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEng } } - function updateStageType(stageType) { - tradingEngine.current.strategy.stageType.value = stageType + function updateStageStatus(stage, status) { + switch (stage) { + case 'Trigger Stage': { + tradingEngine.current.strategy.triggerStageStatus.value = status + break + } + case 'Open Stage': { + tradingEngine.current.strategy.openStageStatus.value = status + break + } + case 'Manage Stage': { + tradingEngine.current.strategy.manageStageStatus.value = status + break + } + case 'Close Stage': { + tradingEngine.current.strategy.closeStageStatus.value = status + break + } + } } function updateStatus() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 4fbf90aca..750ad7bc6 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -40,12 +40,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger) - /* Variables to know when we need to open or close a position. */ - let triggeringOn - let triggeringOff - let takingPosition - let closingPosition - return thisObject function initialize() { @@ -282,9 +276,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* We check if we will be triggering on, off or taking position. */ - triggeringOn = checkTriggerOn() - triggeringOff = checkTriggerOff() - takingPosition = checkTakePosition() + checkTriggerOn() + checkTriggerOff() + checkTakePosition() function checkTriggerOn() { if ( @@ -324,7 +318,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.id) tradingStrategyModule.openStrategy(j, situation.name, strategy.name) - tradingStrategyModule.updateStageType('Trigger Stage') + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Open') tradingEngine.current.distanceToEvent.triggerOn.value = 1 @@ -340,18 +334,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) - return true // This Means that we have just met the conditions to trigger on. + return } } } } } } - return false // This Means that we have not met the conditions to trigger on. + return } function checkTriggerOff() { - if (tradingEngine.current.strategy.stageType.value === 'Trigger Stage') { + if (tradingEngine.current.strategy.triggerStageStatus.value === 'Open') { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage @@ -376,26 +370,26 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.id) tradingStrategyModule.closeStrategy('Trigger Off') + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') tradingEngine.current.distanceToEvent.triggerOff.value = 1 announcementsModule.makeAnnoucements(triggerStage.triggerOff) logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) - return true // This Means that we have just met the conditions to trigger off. + return } } } } } - return false // This Means that we have not met the conditions to trigger off. + return } function checkTakePosition() { /* Take Position Condition */ if ( - tradingEngine.current.strategy.stageType.value === 'Trigger Stage' && - tradingEngine.current.strategy.status.value === 'Open' + tradingEngine.current.strategy.triggerStageStatus.value === 'Open' ) { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let triggerStage = strategy.triggerStage @@ -420,7 +414,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.takePosition.id) tradingSystem.highlights.push(triggerStage.id) - tradingStrategyModule.updateStageType('Open Stage') + tradingStrategyModule.updateStageStatus('Open Stage', 'Open') + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') tradingPositionModule.openPosition(situation.name) announcementsModule.makeAnnoucements(triggerStage.takePosition) @@ -435,20 +430,32 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') - return true // This Means that we have just met the conditions to take position. + tradingStrategyModule.updateStageStatus('Manage Stage', 'Opening') } } } } } - return false // This Means that we have not met the conditions to take position. + return } } function openStage() { - /* Most of the stuff happens when we are at the Open Stage */ - if (tradingEngine.current.strategy.stageType.value === 'Open Stage') { + /* + if the Close stage is opened while the open stage is still open that means that + we need to stop placing orders, check what happened to the orders already placed, + and cancel all unfilled limit orders. We call this the Closing status of the Open Stage. + */ + if (tradingEngine.current.strategy.openStageStatus.value === 'Open' && tradingEngine.current.strategy.closeStageStatus.value === 'Open') { + tradingStrategyModule.updateStageStatus('Open Stage', 'Closing') + } + + /* + While the Open Stage is Open, we do our regular stuff: place orders and check + what happened to the orders already placed. + */ + if (tradingEngine.current.strategy.openStageStatus.value === 'Open') { let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution @@ -457,7 +464,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalFormulas(stageNode, 'Initial Definition') /* Taking a Position */ - if (takingPosition === true) { + if (tradingEngine.current.strategy.manageStageStatus.value === 'Opening') { getReadyToTakePosition() calculateTakeProfit() @@ -471,27 +478,45 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode) + checkExecution(executionNode, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + + /* + The Open is finished when the fillSize reaches the Position Size. + This can happens at any time when we update the filledSize value when we see + at the exchange that orders were filled. + */ + if (tradingEngine.current.position.openStageFilledSize.value === tradingEngine.current.position.size.value) { + tradingStrategyModule.updateStageStatus('Open Stage', 'Closed') + } } - /* - We might have jumped to the Close Stage before we leave the Open Stage. - In such cases there are orders that need to be checked what happened to them, - and others that need to be cancelled. + /* + During the closing status, we do not place new orders, just check + if the ones placed were filled, and cancel the ones not filled. */ - if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { + if (tradingEngine.current.strategy.openStageStatus.value === 'Closing') { let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution /* Check if there are unfilled orders */ - if (tradingEngine.current.position.ordersSize.value !== tradingEngine.current.position.filledSize.value) { + if (tradingEngine.current.position.openStageOrdersSize.value !== tradingEngine.current.position.openStageFilledSize.value) { /* There are unfilled orders, we will check if they were executed, and cancel the ones that were not. */ evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, true) + checkExecution(executionNode, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + } + + /* + The Closing is finished when the fillSize reaches the ordersSize. + This can happens either because we update the filledSize value when we see + at the exchange that orders were filled, or we reduce the ordersSize when + we cancel not yet filled orders. + */ + if (tradingEngine.current.position.openStageFilledSize.value === tradingEngine.current.position.openStageOrdersSize.value) { + tradingStrategyModule.updateStageStatus('Open Stage', 'Closed') } } @@ -564,13 +589,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function manageStage() { - if (takingPosition === true) { + if (tradingEngine.current.strategy.manageStageStatus.value === 'Opening') { /* We jump the management at the loop cycle where the position is being taken. */ - takingPosition = false + tradingStrategyModule.updateStageStatus('Manage Stage', 'Open') return } - if (tradingEngine.current.strategy.stageType.value === 'Open Stage' || tradingEngine.current.strategy.stageType.value === 'Manage Stage') { + if (tradingEngine.current.strategy.manageStageStatus.value === 'Open') { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let manageStage = strategy.manageStage evalConditions(manageStage, 'Manage Stage') @@ -585,7 +610,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM calculateTakeProfit() /* Checking if Stop or Take Profit were hit */ - closingPosition = checkStopLossOrTakeProfitWasHit() + checkStopLossOrTakeProfitWasHit() } function checkStopPhasesEvents() { @@ -820,9 +845,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.preventStopLossDistortion() tradingPositionModule.applySlippageToStopLoss() tradingPositionModule.closePosition('Stop Loss') - tradingStrategyModule.updateStageType('Close Stage') + tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') announcementsModule.makeAnnoucements(strategy.closeStage) - return true // This means that the STOP was hit. + return } /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ @@ -835,37 +860,46 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.preventTakeProfitDistortion() tradingPositionModule.applySlippageToTakeProfit() tradingPositionModule.closePosition('Take Profit') - tradingStrategyModule.updateStageType('Close Stage') + tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') announcementsModule.makeAnnoucements(strategy.closeStage) - return true // This means that the Take Profit was hit. + return } - return false // This means that the neither the STOP nor the Take Profit were hit. + return } } } function closeStage() { - if (tradingEngine.current.strategy.stageType.value === 'Close Stage') { + /* + This will happen only once, as soon as the Take Profit or Stop was hit. + */ + if (tradingEngine.current.strategy.closeStageStatus.value === 'Opening') { + getReadyToClosePosition() + tradingExecutionModule.closePosition() + closePosition() + tradingStrategyModule.updateStageStatus('Close Stage', 'Open') + } + + /* + This will happen as long as the Close Stage is Open. + */ + if (tradingEngine.current.strategy.closeStageStatus.value === 'Open') { let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage let executionNode = stageNode.closeExecution evalConditions(stageNode, 'Close Execution') evalFormulas(stageNode, 'Close Execution') - /* Closing a Position */ - if (closingPosition === true) { - getReadyToClosePosition() - tradingExecutionModule.closePosition() - closePosition() - closingPosition = false - } - - checkExecution(executionNode) + checkExecution(executionNode, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) } - /* After a position was closed, we need to close the strategy. */ - exitStrategyAfterPosition() + if (tradingEngine.current.strategy.closeStageStatus.value === 'Closing') { + + /* After a position was closed, we need to close the strategy. */ + exitStrategyAfterPosition() + tradingStrategyModule.updateStageStatus('Close Stage', 'Closed') + } function getReadyToClosePosition() { /* Inicializing this counter */ @@ -909,7 +943,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function exitStrategyAfterPosition() { - if (tradingEngine.current.strategy.stageType.value !== 'Close Stage') { return } tradingStrategyModule.closeStrategy('Position Closed') tradingEngine.current.distanceToEvent.triggerOff.value = 1 @@ -996,7 +1029,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - function checkExecution(executionNode, exitStageNow) { + function checkExecution(executionNode, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { checkExecutionAlgorithms(executionNode) @@ -1039,7 +1072,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.orderCounters.periods.value++ tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS - if (exitStageNow !== true) { + if (stageIsClosing !== true) { let mustCancelOrder = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (mustCancelOrder === true) { // Cancel Order @@ -1064,7 +1097,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } break default: { - if (exitStageNow === true) { return } + if (stageIsClosing === true) { return } tradingEngineOrder.status.value = 'Not Open' let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) @@ -1081,11 +1114,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* The size depends on the Formula but also on how much of the total size has already been used. */ tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 - if (tradingEngine.current.position.ordersSize.value + tradingEngineOrder.size.value > tradingEngine.current.position.size.value) { + if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit) { /* We reduce the size to the remaining size of the position. */ - tradingEngineOrder.size.value = tradingEngineOrder.size.value - tradingEngine.current.position.ordersSize.value + tradingEngineOrder.size.value = stageSizeLimit - stageOrdersSize.value } - tradingEngine.current.position.ordersSize.value = tradingEngine.current.position.ordersSize.value + tradingEngineOrder.size.value + stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value } } } @@ -1126,16 +1159,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } /* Filled Size Calculation */ - let filledSize = tradingEngineOrder.size.value * percentageFilled / 100 - tradingEngine.current.position.filledSize.value = tradingEngine.current.position.filledSize.value + filledSize + stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 - if (exitStageNow !== true) { - /* Passing to Manage Stage Verification */ - if (tradingEngine.current.position.size.value === tradingEngine.current.position.filledSize.value) { - tradingStrategyModule.updateStageType('Manage Stage') - announcementsModule.makeAnnoucements(manageStage) - } - } else { + if (stageIsClosing === true) { if (tradingEngineOrder.orderStatistics.percentageFilled.value === 0) { tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Forced to Cancel' From 8aa781a02799ae6cd025674e610145d4deab3efc Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 16 Jul 2020 21:36:42 +0200 Subject: [PATCH 090/113] many improvements --- LowFrequencyTrading/TradingPosition.js | 6 +- LowFrequencyTrading/TradingSystem.js | 124 ++++++++++++++++--------- 2 files changed, 83 insertions(+), 47 deletions(-) diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index 1712cd46e..d222d8647 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -5,7 +5,7 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng const MODULE_NAME = 'Trading Position' let thisObject = { openPosition: openPosition, - closePosition: closePosition, + closingPosition: closingPosition, applyStopLossFormula: applyStopLossFormula, applyTakeProfitFormula: applyTakeProfitFormula, preventStopLossDistortion: preventStopLossDistortion, @@ -53,8 +53,8 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng tradingEngine.current.position.takeProfit.takeProfitPhase.value = 0 } - function closePosition(exitType) { - tradingEngine.current.position.status.value = 'Closed' + function closingPosition(exitType) { + tradingEngine.current.position.status.value = 'Closing' tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value tradingEngine.current.position.exitType.value = exitType /* diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 750ad7bc6..fa4510700 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -318,7 +318,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.id) tradingStrategyModule.openStrategy(j, situation.name, strategy.name) - tradingStrategyModule.updateStageStatus('Trigger Stage', 'Open') tradingEngine.current.distanceToEvent.triggerOn.value = 1 @@ -334,14 +333,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) - return + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Open') } } } } } } - return } function checkTriggerOff() { @@ -370,20 +368,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.id) tradingStrategyModule.closeStrategy('Trigger Off') - tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') tradingEngine.current.distanceToEvent.triggerOff.value = 1 announcementsModule.makeAnnoucements(triggerStage.triggerOff) logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) - return + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') } } } } } - return } function checkTakePosition() { @@ -414,8 +410,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.takePosition.id) tradingSystem.highlights.push(triggerStage.id) - tradingStrategyModule.updateStageStatus('Open Stage', 'Open') - tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') tradingPositionModule.openPosition(situation.name) announcementsModule.makeAnnoucements(triggerStage.takePosition) @@ -430,18 +424,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') + tradingStrategyModule.updateStageStatus('Open Stage', 'Opening') + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') tradingStrategyModule.updateStageStatus('Manage Stage', 'Opening') } } } } } - return } } function openStage() { - /* if the Close stage is opened while the open stage is still open that means that we need to stop placing orders, check what happened to the orders already placed, @@ -451,6 +445,31 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingStrategyModule.updateStageStatus('Open Stage', 'Closing') } + if (tradingEngine.current.strategy.openStageStatus.value === 'Opening') { + + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + let executionNode = stageNode.openExecution + + evalConditions(stageNode, 'Initial Definition') + evalFormulas(stageNode, 'Initial Definition') + + getReadyToTakePosition() + + calculateTakeProfit() + calculateStopLoss() + + takePosition() + + /* Check Execution in opening stage mode */ + evalConditions(stageNode, 'Open Execution') + evalFormulas(stageNode, 'Open Execution') + + checkExecution(executionNode, true, false, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + + tradingStrategyModule.updateStageStatus('Open Stage', 'Open') + return + } + /* While the Open Stage is Open, we do our regular stuff: place orders and check what happened to the orders already placed. @@ -460,25 +479,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution - evalConditions(stageNode, 'Initial Definition') - evalFormulas(stageNode, 'Initial Definition') - - /* Taking a Position */ - if (tradingEngine.current.strategy.manageStageStatus.value === 'Opening') { - getReadyToTakePosition() - - calculateTakeProfit() - calculateStopLoss() - - tradingExecutionModule.takePosition() - takePosition() - } - /* Every Loop Cycle Activity */ evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + checkExecution(executionNode, false, false, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* The Open is finished when the fillSize reaches the Position Size. @@ -506,7 +511,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + checkExecution(executionNode, true, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) } /* @@ -844,8 +849,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.preventStopLossDistortion() tradingPositionModule.applySlippageToStopLoss() - tradingPositionModule.closePosition('Stop Loss') + tradingPositionModule.closingPosition('Stop Loss') tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Close') announcementsModule.makeAnnoucements(strategy.closeStage) return } @@ -859,8 +865,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.preventTakeProfitDistortion() tradingPositionModule.applySlippageToTakeProfit() - tradingPositionModule.closePosition('Take Profit') + tradingPositionModule.closingPosition('Take Profit') tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Close') announcementsModule.makeAnnoucements(strategy.closeStage) return } @@ -876,9 +883,17 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM */ if (tradingEngine.current.strategy.closeStageStatus.value === 'Opening') { getReadyToClosePosition() - tradingExecutionModule.closePosition() - closePosition() + + let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + let executionNode = stageNode.closeExecution + + evalConditions(stageNode, 'Close Execution') + evalFormulas(stageNode, 'Close Execution') + + checkExecution(executionNode, true, false, tradingEngine.current.position.openStageFilledSize.value, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + tradingStrategyModule.updateStageStatus('Close Stage', 'Open') + return } /* @@ -891,14 +906,26 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Close Execution') evalFormulas(stageNode, 'Close Execution') - checkExecution(executionNode, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize.value, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + + /* + The Close Stage is closed when the fillSize reaches the filledSize of the Open Stage. + */ + if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.closeStageFilledSize.value) { + tradingStrategyModule.updateStageStatus('Close Stage', 'Closed') + } } - if (tradingEngine.current.strategy.closeStageStatus.value === 'Closing') { + if ( + tradingEngine.current.strategy.triggerStageStatus.value === 'Closed' && + tradingEngine.current.strategy.openStageStatus.value === 'Closed' && + tradingEngine.current.strategy.manageStageStatus.value === 'Closed' && + tradingEngine.current.strategy.closeStageStatus.value === 'Closed' + ) { /* After a position was closed, we need to close the strategy. */ - exitStrategyAfterPosition() - tradingStrategyModule.updateStageStatus('Close Stage', 'Closed') + closePosition() + exitStrategyAndPosition() } function getReadyToClosePosition() { @@ -940,9 +967,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngine.episode.episodeStatistics.hitRatio.value = tradingEngine.episode.episodeCounters.hits.value / tradingEngine.episode.episodeCounters.positions.value tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } + + tradingEngine.current.position.status.value = 'Closed' } - function exitStrategyAfterPosition() { + function exitStrategyAndPosition() { tradingStrategyModule.closeStrategy('Position Closed') tradingEngine.current.distanceToEvent.triggerOff.value = 1 @@ -1029,7 +1058,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - function checkExecution(executionNode, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { + function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { checkExecutionAlgorithms(executionNode) @@ -1049,6 +1078,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Basic Validations */ if (tradingSystemOrder.config.positionSizePercentage === undefined) { continue } + if (tradingSystemOrder.referenceParent === undefined) { continue } let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) @@ -1089,7 +1119,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) + if (stageIsOpening === false) { + simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) + } } break case 'Closed': { @@ -1097,11 +1129,21 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } break default: { - if (stageIsClosing === true) { return } + if (stageIsClosing === true) { continue } tradingEngineOrder.status.value = 'Not Open' let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (situationName !== undefined) { + + /* Order Size Calculation */ + tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 + if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit) { + /* We reduce the size to the remaining size of the position. */ + tradingEngineOrder.size.value = stageSizeLimit - stageOrdersSize.value + } + + if (tradingEngineOrder.size.value <= 0) { continue } + // Create Order tradingEngineOrder.identifier.value = global.UNIQUE_ID() tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value @@ -1112,12 +1154,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.algorithmName.value = executionAlgorithm.name tradingEngineOrder.situationName.value = situationName - /* The size depends on the Formula but also on how much of the total size has already been used. */ - tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 - if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit) { - /* We reduce the size to the remaining size of the position. */ - tradingEngineOrder.size.value = stageSizeLimit - stageOrdersSize.value - } stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value } } From 8e8339d0223d659c95111eb7bad6fa056509447c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 18 Jul 2020 13:08:20 +0200 Subject: [PATCH 091/113] More fixes --- LowFrequencyTrading/TradingPosition.js | 7 +- LowFrequencyTrading/TradingSimulation.js | 2 +- LowFrequencyTrading/TradingStrategy.js | 1 - LowFrequencyTrading/TradingSystem.js | 160 ++++++++++++----------- server.js | 4 + 5 files changed, 95 insertions(+), 79 deletions(-) diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index d222d8647..d2104312a 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -6,6 +6,7 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng let thisObject = { openPosition: openPosition, closingPosition: closingPosition, + closePosition: closePosition, applyStopLossFormula: applyStopLossFormula, applyTakeProfitFormula: applyTakeProfitFormula, preventStopLossDistortion: preventStopLossDistortion, @@ -55,8 +56,12 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng function closingPosition(exitType) { tradingEngine.current.position.status.value = 'Closing' - tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value tradingEngine.current.position.exitType.value = exitType + } + + function closePosition() { + tradingEngine.current.position.status.value = 'Closed' + tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value /* Now that the position is closed, it is the right time to move this position from current to last at the Trading Engine data structure. */ diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index a6e1d4ae7..356fdb22f 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -139,8 +139,8 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT heartBeat() positionChartAtCurrentCandle() tradingSystemModule.reset(chart) - tradingSystemModule.mantainStrategies() tradingSystemModule.mantainPositions() + tradingSystemModule.mantainStrategies() tradingEngineModule.updateEpisodeCountersAndStatistics() tradingEngineModule.updateDistanceToEventsCounters() diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index c3ca67f0a..4eeb26a55 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -47,7 +47,6 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEng /* Now that the strategy is closed, it is the right time to move this strategy from current to last at the Trading Engine data structure. */ - tradingEngineModule.cloneValues(tradingEngine.current.strategy, tradingEngine.last.strategy) } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index fa4510700..87478571b 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -332,7 +332,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - logger.write(MODULE_NAME, '[INFO] checkTriggerOn -> Entering into Strategy: ' + strategy.name) tradingStrategyModule.updateStageStatus('Trigger Stage', 'Open') } } @@ -367,14 +366,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.triggerOff.id) tradingSystem.highlights.push(triggerStage.id) - tradingStrategyModule.closeStrategy('Trigger Off') - tradingEngine.current.distanceToEvent.triggerOff.value = 1 - announcementsModule.makeAnnoucements(triggerStage.triggerOff) - - logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) - tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Closed') + tradingStrategyModule.closeStrategy('Trigger Off') } } } @@ -383,7 +378,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function checkTakePosition() { - /* Take Position Condition */ if ( tradingEngine.current.strategy.triggerStageStatus.value === 'Open' ) { @@ -423,9 +417,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - logger.write(MODULE_NAME, '[INFO] checkTakePosition -> Conditions at the Take Position Event were met.') + tradingStrategyModule.updateStageStatus('Trigger Stage', 'Closed') tradingStrategyModule.updateStageStatus('Open Stage', 'Opening') - tradingStrategyModule.updateStageStatus('Trigger Stage', 'Close') tradingStrategyModule.updateStageStatus('Manage Stage', 'Opening') } } @@ -436,17 +429,19 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function openStage() { - /* - if the Close stage is opened while the open stage is still open that means that - we need to stop placing orders, check what happened to the orders already placed, - and cancel all unfilled limit orders. We call this the Closing status of the Open Stage. - */ + /* Abort Open Stage Check */ if (tradingEngine.current.strategy.openStageStatus.value === 'Open' && tradingEngine.current.strategy.closeStageStatus.value === 'Open') { + /* + if the Close stage is opened while the open stage is still open that means that + we need to stop placing orders, check what happened to the orders already placed, + and cancel all unfilled limit orders. We call this the Closing status of the Open Stage. + */ tradingStrategyModule.updateStageStatus('Open Stage', 'Closing') } + /* Opening Status Procedure */ if (tradingEngine.current.strategy.openStageStatus.value === 'Opening') { - + /* This procedure is intended to run only once */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution @@ -466,16 +461,17 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM checkExecution(executionNode, true, false, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + /* From here on, the state is officially Open */ tradingStrategyModule.updateStageStatus('Open Stage', 'Open') return } - /* - While the Open Stage is Open, we do our regular stuff: place orders and check - what happened to the orders already placed. - */ + /* Open Status Procedure */ if (tradingEngine.current.strategy.openStageStatus.value === 'Open') { - + /* + While the Open Stage is Open, we do our regular stuff: place orders and check + what happened to the orders already placed. + */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution @@ -495,12 +491,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - /* - During the closing status, we do not place new orders, just check - if the ones placed were filled, and cancel the ones not filled. - */ + /* Closing Status Procedure */ if (tradingEngine.current.strategy.openStageStatus.value === 'Closing') { - + /* + During the closing status, we do not place new orders, just check + if the ones placed were filled, and cancel the ones not filled. + */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution @@ -511,7 +507,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, true, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + checkExecution(executionNode, false, true, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) } /* @@ -594,12 +590,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function manageStage() { + /* Opening Status Procedure */ if (tradingEngine.current.strategy.manageStageStatus.value === 'Opening') { /* We jump the management at the loop cycle where the position is being taken. */ tradingStrategyModule.updateStageStatus('Manage Stage', 'Open') return } + /* Open Status Procedure */ if (tradingEngine.current.strategy.manageStageStatus.value === 'Open') { let strategy = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value] let manageStage = strategy.manageStage @@ -851,7 +849,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.applySlippageToStopLoss() tradingPositionModule.closingPosition('Stop Loss') tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') - tradingStrategyModule.updateStageStatus('Manage Stage', 'Close') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Closed') announcementsModule.makeAnnoucements(strategy.closeStage) return } @@ -867,7 +865,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.applySlippageToTakeProfit() tradingPositionModule.closingPosition('Take Profit') tradingStrategyModule.updateStageStatus('Close Stage', 'Opening') - tradingStrategyModule.updateStageStatus('Manage Stage', 'Close') + tradingStrategyModule.updateStageStatus('Manage Stage', 'Closed') announcementsModule.makeAnnoucements(strategy.closeStage) return } @@ -877,29 +875,23 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function closeStage() { - - /* - This will happen only once, as soon as the Take Profit or Stop was hit. - */ + /* Opening Status Procedure */ if (tradingEngine.current.strategy.closeStageStatus.value === 'Opening') { - getReadyToClosePosition() - - let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage - let executionNode = stageNode.closeExecution - - evalConditions(stageNode, 'Close Execution') - evalFormulas(stageNode, 'Close Execution') - - checkExecution(executionNode, true, false, tradingEngine.current.position.openStageFilledSize.value, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) - + /* + This will happen only once, as soon as the Take Profit or Stop was hit. + We wil not be placing orders at this time because we do not know + the total filled size of the Open Stage. By skiping execution now + we allow the open stage to get a final value for filledSize that we can use. + */ tradingStrategyModule.updateStageStatus('Close Stage', 'Open') return } - /* - This will happen as long as the Close Stage is Open. - */ + /* Open Status Procedure */ if (tradingEngine.current.strategy.closeStageStatus.value === 'Open') { + /* + This will happen as long as the Close Stage is Open. + */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage let executionNode = stageNode.closeExecution @@ -908,14 +900,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize.value, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) - /* - The Close Stage is closed when the fillSize reaches the filledSize of the Open Stage. - */ - if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.closeStageFilledSize.value) { + /* Close the Stage Validation */ + if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.openStageFilledSize.value) { + /* + The Close Stage is closed when the fillSize reaches the filledSize of the Open Stage. + */ tradingStrategyModule.updateStageStatus('Close Stage', 'Closed') } } + /* Exit Position Validation */ if ( tradingEngine.current.strategy.triggerStageStatus.value === 'Closed' && tradingEngine.current.strategy.openStageStatus.value === 'Closed' && @@ -923,18 +917,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngine.current.strategy.closeStageStatus.value === 'Closed' ) { - /* After a position was closed, we need to close the strategy. */ - closePosition() - exitStrategyAndPosition() + /* Exiting Everything now. */ + exitPositionAndStrategy() } - function getReadyToClosePosition() { - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.closePosition.value = 1 - } + function exitPositionAndStrategy() { - function closePosition() { - /* We calculate the new balances after the position is closed. */ + /* New Balances Calculation */ let feePaid = 0 if (bot.sessionAndMarketBaseAssetsAreEqual) { feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 @@ -946,6 +935,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngine.current.balance.baseAsset.value = 0 } + /* Needed for Episode Statistics */ tradingPositionModule.updateStatistics() /* Recalculating Episode Counters */ @@ -968,14 +958,16 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngine.episode.episodeStatistics.anualizedRateOfReturn.value = tradingEngine.episode.episodeStatistics.ROI.value / tradingEngine.episode.episodeStatistics.days.value * 365 } - tradingEngine.current.position.status.value = 'Closed' - } - - function exitStrategyAndPosition() { - - tradingStrategyModule.closeStrategy('Position Closed') - tradingEngine.current.distanceToEvent.triggerOff.value = 1 + /* Taking Position Snapshot */ + if (bot.SESSION.type === 'Backtesting Session') { + if (sessionParameters.snapshots !== undefined) { + if (sessionParameters.snapshots.config.position === true) { + snapshotsModule.positionExit() + } + } + } + /* Taking Strategy Snapshot */ if (bot.SESSION.type === 'Backtesting Session') { if (sessionParameters.snapshots !== undefined) { if (sessionParameters.snapshots.config.strategy === true) { @@ -984,13 +976,15 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - if (bot.SESSION.type === 'Backtesting Session') { - if (sessionParameters.snapshots !== undefined) { - if (sessionParameters.snapshots.config.position === true) { - snapshotsModule.positionExit() - } - } - } + /* Close the Position */ + tradingPositionModule.closePosition() + + /* Close the Strategy */ + tradingStrategyModule.closeStrategy('Position Closed') + + /* Distance to Events Updates */ + tradingEngine.current.distanceToEvent.closePosition.value = 1 + tradingEngine.current.distanceToEvent.triggerOff.value = 1 } } @@ -1144,17 +1138,25 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (tradingEngineOrder.size.value <= 0) { continue } - // Create Order + /* Create Order Procedure */ tradingEngineOrder.identifier.value = global.UNIQUE_ID() tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value tradingEngineOrder.end.value = tradingEngine.current.candle.end.value - tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value tradingEngineOrder.status.value = 'Open' tradingEngineOrder.orderName.value = tradingSystemOrder.name tradingEngineOrder.algorithmName.value = executionAlgorithm.name tradingEngineOrder.situationName.value = situationName + /* Order Rate Calculation */ + tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. + if (tradingSystemOrder.positionRate !== undefined) { + if (tradingSystemOrder.positionRate.formula !== undefined) { + tradingEngineOrder.rate.value = formulas.get(tradingSystemOrder.positionRate.formula.id) + } + } + stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) } } } @@ -1196,8 +1198,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Filled Size Calculation */ stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) - if (stageIsClosing === true) { + if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { if (tradingEngineOrder.orderStatistics.percentageFilled.value === 0) { tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Forced to Cancel' @@ -1206,6 +1209,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Partially Filled' } + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ + let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 + stageOrdersSize.value = stageOrdersSize.value - unfilledSize + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) } } } diff --git a/server.js b/server.js index b0a6a8c99..f2910b112 100644 --- a/server.js +++ b/server.js @@ -40,6 +40,10 @@ global.UNIQUE_ID = function () { }) } +global.PRECISE = function (floatNumber, precision) { + return parseFloat(Number.parseFloat(floatNumber).toPrecision(precision)) +} + process.on('uncaughtException', function (err) { console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack) From 58d5c570805677b140764584a3c99f1270ff4309 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 18 Jul 2020 13:21:21 +0200 Subject: [PATCH 092/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 87478571b..a908738e2 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -919,6 +919,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Exiting Everything now. */ exitPositionAndStrategy() + + /* Reset Exchange Orders */ + tradingEngine.exchangeOrders.initialize(tradingEngine.exchangeOrders) } function exitPositionAndStrategy() { From 139d39ab56693c3a036043626c43bd3ee5d1e896 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 18 Jul 2020 15:23:18 +0200 Subject: [PATCH 093/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 36 ++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index a908738e2..b720ea95a 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -1100,19 +1100,34 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS if (stageIsClosing !== true) { - let mustCancelOrder = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (mustCancelOrder === true) { - // Cancel Order + let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ + let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 + stageOrdersSize.value = stageOrdersSize.value - unfilledSize + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + /* Cancel the Order */ tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Cancelled' + tradingEngineOrder.exitType.value = 'Cancelled by Event' break } - let mustMoveOrder = checkOrderEvent(tradingSystemOrder.moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (mustMoveOrder === true && tradingEngineOrder.status.value === 'Open') { - // Move Order - - break + if (tradingSystemOrder.moveOrderEvents !== undefined) { + for (let j = 0; j < tradingSystemOrder.moveOrderEvents.length; j++) { + let moveOrderEvent = tradingSystemOrder.moveOrderEvents[j] + let situationName = checkOrderEvent(moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + if (moveOrderEvent.positionRate !== undefined) { + if (moveOrderEvent.positionRate.formula !== undefined) { + /* Move Order to a different Rate */ + tradingEngineOrder.rate.value = formulas.get(moveOrderEvent.positionRate.formula.id) + tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) + break + } + } + } + } } } @@ -1155,6 +1170,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (tradingSystemOrder.positionRate !== undefined) { if (tradingSystemOrder.positionRate.formula !== undefined) { tradingEngineOrder.rate.value = formulas.get(tradingSystemOrder.positionRate.formula.id) + tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) } } @@ -1206,7 +1222,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { if (tradingEngineOrder.orderStatistics.percentageFilled.value === 0) { tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Forced to Cancel' + tradingEngineOrder.exitType.value = 'Cancelled by Closing Stage' } if (tradingEngineOrder.orderStatistics.percentageFilled.value > 0 && tradingEngineOrder.orderStatistics.percentageFilled.value < 100) { tradingEngineOrder.status.value = 'Closed' From 8a65d9e379dff82afddd3b0a909a8284525bd646 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 19 Jul 2020 13:31:19 +0200 Subject: [PATCH 094/113] Files being written fine --- LowFrequencyTrading/TradingEngine.js | 46 +++++- LowFrequencyTrading/TradingRecords.js | 102 ++++++------- LowFrequencyTrading/TradingSimulation.js | 1 + LowFrequencyTrading/TradingSystem.js | 176 +++++++++++++++++------ 4 files changed, 223 insertions(+), 102 deletions(-) diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index af33f924a..c8e7e4fdd 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -147,30 +147,66 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { } function updateDistanceToEventsCounters() { - /* Keeping Distance Counters Up-to-date */ + /* Keeping Distance Counters Up-to-date while avoinding counting before the first event happens. */ if ( - tradingEngine.current.distanceToEvent.triggerOn.value > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.triggerOn.value > 0 ) { tradingEngine.current.distanceToEvent.triggerOn.value++ } if ( - tradingEngine.current.distanceToEvent.triggerOff.value > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.triggerOff.value > 0 ) { tradingEngine.current.distanceToEvent.triggerOff.value++ } if ( - tradingEngine.current.distanceToEvent.takePosition.value > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.takePosition.value > 0 ) { tradingEngine.current.distanceToEvent.takePosition.value++ } if ( - tradingEngine.current.distanceToEvent.closePosition.value > 0 // with this we avoind counting before the first event happens. + tradingEngine.current.distanceToEvent.closePosition.value > 0 ) { tradingEngine.current.distanceToEvent.closePosition.value++ } + + if ( + tradingEngine.current.distanceToEvent.nextPhase.value > 0 + ) { + tradingEngine.current.distanceToEvent.nextPhase.value++ + } + + if ( + tradingEngine.current.distanceToEvent.moveToPhase.value > 0 + ) { + tradingEngine.current.distanceToEvent.moveToPhase.value++ + } + + if ( + tradingEngine.current.distanceToEvent.createOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.createOrder.value++ + } + + if ( + tradingEngine.current.distanceToEvent.moveOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.moveOrder.value++ + } + + if ( + tradingEngine.current.distanceToEvent.cancelOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.cancelOrder.value++ + } + + if ( + tradingEngine.current.distanceToEvent.closeOrder.value > 0 + ) { + tradingEngine.current.distanceToEvent.closeOrder.value++ + } } } diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index cb7ff4b0d..d79347f2d 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -101,57 +101,6 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { } } - function persistIndividualRecord(record, product, outputDatasetArray) { - - if (product.config.saveAsObjects === true) { - /* For saving objects we need to take care of a different set of rules. */ - 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 (bot.processingDailyFiles) { - /* - When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, - since the object will be duplicated on the next day. How do we know we are positioned at the last candle - of the day? Easy: the end of the candle must be 1 millisecod before the next day. That happens at any - time frame. - */ - let currentDay = new Date(tradingEngine.current.candle.end.value) - let nextDay = new Date(tradingEngine.current.candle.end.value + 1) - if (currentDay.getUTCDate() !== nextDay.getUTCDate()) { - /* - We will save the object only if it is closed, becasuse we are at the last candle of the day. - */ - if (propertyValue === product.config.propertyValueThatClosesObject) { - outputDatasetArray.push(record) - } - } else { - /* - When we are not at the end of the day, we will save the object normally, like in market files. - */ - if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { - outputDatasetArray.push(record) - } - } - } - else { - /* - For Market Files we will add a record everytime that proeprty value does not match this - */ - if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { - outputDatasetArray.push(record) - } - } - break - } - } - } else { - /* When we are not dealing with objects, we add every record to the existing file. */ - outputDatasetArray.push(record) - } - } - function scanRecordDefinition(product, productRootNode, index) { let record = [] @@ -232,6 +181,57 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { return record } + function persistIndividualRecord(record, product, outputDatasetArray) { + + if (product.config.saveAsObjects === true) { + /* For saving objects we need to take care of a different set of rules. */ + 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 (bot.processingDailyFiles) { + /* + When dealing with Daily Files, we need to avoid to write an open object at the last 'candle' of the day, + since the object will be duplicated on the next day. How do we know we are positioned at the last candle + of the day? Easy: the end of the candle must be 1 millisecod before the next day. That happens at any + time frame. + */ + let currentDay = new Date(tradingEngine.current.candle.end.value) + let nextDay = new Date(tradingEngine.current.candle.end.value + 1) + if (currentDay.getUTCDate() !== nextDay.getUTCDate()) { + /* + We will save the object only if it is closed, becasuse we are at the last candle of the day. + */ + if (propertyValue === product.config.propertyValueThatClosesObject) { + outputDatasetArray.push(record) + } + } else { + /* + When we are not at the end of the day, we will save the object normally, like in market files. + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + } + } + else { + /* + For Market Files we will add a record everytime that proeprty value does not match this + */ + if (propertyValue !== product.config.propertyValueThatPreventsSavingObject) { + outputDatasetArray.push(record) + } + } + break + } + } + } else { + /* When we are not dealing with objects, we add every record to the existing file. */ + outputDatasetArray.push(record) + } + } + function safeNumericValue(value) { /* The purpose of this function is to check that value variable does not have a value that diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 356fdb22f..98014e6fc 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -141,6 +141,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.reset(chart) tradingSystemModule.mantainPositions() tradingSystemModule.mantainStrategies() + tradingSystemModule.mantainOrders() tradingEngineModule.updateEpisodeCountersAndStatistics() tradingEngineModule.updateDistanceToEventsCounters() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index b720ea95a..8a943b666 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -8,6 +8,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM reset: reset, mantainStrategies: mantainStrategies, mantainPositions: mantainPositions, + mantainOrders: mantainOrders, triggerStage: triggerStage, openStage: openStage, manageStage: manageStage, @@ -113,6 +114,50 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingPositionModule.updateEnds() } + function mantainOrders() { + /* Reset All Exchange Orders that are on Closed status, to their initial value */ + if (tradingEngine.current.strategy.index.value === tradingEngine.current.strategy.index.config.initialValue) { return } + + let stageNode + let executionNode + + stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage + executionNode = stageNode.openExecution + resetExecution(executionNode) + + stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].closeStage + executionNode = stageNode.closeExecution + resetExecution(executionNode) + + function resetExecution(executionNode) { + + resetExecutionAlgorithms(executionNode) + + function resetExecutionAlgorithms(executionNode) { + for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { + let executionAlgorithm = executionNode.executionAlgorithms[i] + resetOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + resetOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + resetOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + resetOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + } + } + + function resetOrders(orders, executionAlgorithm, executionNode) { + for (let i = 0; i < orders.length; i++) { + let tradingSystemOrder = orders[i] + if (tradingSystemOrder.referenceParent === undefined) { continue } + let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) + if (tradingEngineOrder.status === undefined) { continue } + if (tradingEngineOrder.status.value === 'Closed') { + /* We reset the order data structure inside the Trading Engine to its initial value */ + tradingEngineOrder.initialize(tradingEngineOrder) + } + } + } + } + } + function evalConditions(startingNode, descendentOfNodeType) { evalNode(startingNode, 'Conditions', descendentOfNodeType) } @@ -319,6 +364,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingStrategyModule.openStrategy(j, situation.name, strategy.name) + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.strategies.value++ + /* Initialize this */ tradingEngine.current.distanceToEvent.triggerOn.value = 1 announcementsModule.makeAnnoucements(triggerStage.triggerOn) @@ -441,6 +489,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Opening Status Procedure */ if (tradingEngine.current.strategy.openStageStatus.value === 'Opening') { + /* Reset the Exchange Orders data structure to its initial value */ + tradingEngine.exchangeOrders.initialize(tradingEngine.exchangeOrders) + /* This procedure is intended to run only once */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution @@ -919,9 +970,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Exiting Everything now. */ exitPositionAndStrategy() - - /* Reset Exchange Orders */ - tradingEngine.exchangeOrders.initialize(tradingEngine.exchangeOrders) } function exitPositionAndStrategy() { @@ -1094,25 +1142,80 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (tradingEngineOrder.orderStatistics.feesPaid === undefined) { continue } switch (tradingEngineOrder.status.value) { + case 'Not Open': { + { + /* Check if we need to Create this Order */ + if (stageIsClosing === true) { continue } + let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + + /* Order Size Calculation */ + tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 + if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit) { + /* We reduce the size to the remaining size of the position. */ + tradingEngineOrder.size.value = stageSizeLimit - stageOrdersSize.value + } + + if (tradingEngineOrder.size.value <= 0) { continue } + + /* Create Order Procedure */ + tradingEngineOrder.identifier.value = global.UNIQUE_ID() + tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.status.value = 'Open' + tradingEngineOrder.orderName.value = tradingSystemOrder.name + tradingEngineOrder.algorithmName.value = executionAlgorithm.name + tradingEngineOrder.situationName.value = situationName + + /* Order Rate Calculation */ + tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. + if (tradingSystemOrder.positionRate !== undefined) { + if (tradingSystemOrder.positionRate.formula !== undefined) { + tradingEngineOrder.rate.value = formulas.get(tradingSystemOrder.positionRate.formula.id) + tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) + } + } + + /* Update State Orders Size */ + stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.orders.value++ + /* Initialize this */ + tradingEngine.current.distanceToEvent.createOrder.value = 1 + } + } + break + } case 'Open': { tradingEngineOrder.end.value = tradingEngine.current.candle.end.value tradingEngineOrder.orderCounters.periods.value++ tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + /* If the stage is closing we wont be moving or cancelling orders based on defined events */ if (stageIsClosing !== true) { + + /* Check if we need to Cancel this Order */ let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (situationName !== undefined) { + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 stageOrdersSize.value = stageOrdersSize.value - unfilledSize stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + /* Cancel the Order */ tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Cancelled by Event' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.cancelOrder.value = 1 break } + /* Check if we need to Move this Order */ if (tradingSystemOrder.moveOrderEvents !== undefined) { for (let j = 0; j < tradingSystemOrder.moveOrderEvents.length; j++) { let moveOrderEvent = tradingSystemOrder.moveOrderEvents[j] @@ -1120,9 +1223,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (situationName !== undefined) { if (moveOrderEvent.positionRate !== undefined) { if (moveOrderEvent.positionRate.formula !== undefined) { + /* Move Order to a different Rate */ tradingEngineOrder.rate.value = formulas.get(moveOrderEvent.positionRate.formula.id) tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) + + /* Initialize this */ + tradingEngine.current.distanceToEvent.moveOrder.value = 1 break } } @@ -1131,58 +1238,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } + /* Simulate Events that happens at the Exchange, if needed. */ if (stageIsOpening === false) { simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) } } - break - case 'Closed': { - // Nothing to do here - } - break - default: { - if (stageIsClosing === true) { continue } - - tradingEngineOrder.status.value = 'Not Open' - let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (situationName !== undefined) { - - /* Order Size Calculation */ - tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 - if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit) { - /* We reduce the size to the remaining size of the position. */ - tradingEngineOrder.size.value = stageSizeLimit - stageOrdersSize.value - } - - if (tradingEngineOrder.size.value <= 0) { continue } - - /* Create Order Procedure */ - tradingEngineOrder.identifier.value = global.UNIQUE_ID() - tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value - tradingEngineOrder.end.value = tradingEngine.current.candle.end.value - tradingEngineOrder.status.value = 'Open' - tradingEngineOrder.orderName.value = tradingSystemOrder.name - tradingEngineOrder.algorithmName.value = executionAlgorithm.name - tradingEngineOrder.situationName.value = situationName - - /* Order Rate Calculation */ - tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. - if (tradingSystemOrder.positionRate !== undefined) { - if (tradingSystemOrder.positionRate.formula !== undefined) { - tradingEngineOrder.rate.value = formulas.get(tradingSystemOrder.positionRate.formula.id) - tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) - } - } - - stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value - stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - } - } } } } function simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ switch (bot.SESSION.type) { case 'Backtesting Session': { break @@ -1198,6 +1265,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } + /* Filter by what is defined at the Strategy */ if (tradingSystemOrder.simulatedExchangeEvents === undefined) { return } /* Partial Fill Simulation */ @@ -1211,8 +1279,13 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { + + /* Close this Order */ tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 } /* Filled Size Calculation */ @@ -1221,13 +1294,24 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { if (tradingEngineOrder.orderStatistics.percentageFilled.value === 0) { + + /* Close this Order */ tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Cancelled by Closing Stage' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 } if (tradingEngineOrder.orderStatistics.percentageFilled.value > 0 && tradingEngineOrder.orderStatistics.percentageFilled.value < 100) { + + /* Close this Order */ tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Partially Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 } + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 From 07e099ae679d3a290bb9cac8cfbedc3271b15fdd Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 19 Jul 2020 14:44:03 +0200 Subject: [PATCH 095/113] Serial Number --- LowFrequencyTrading/TradingPosition.js | 1 + LowFrequencyTrading/TradingStrategy.js | 1 + LowFrequencyTrading/TradingSystem.js | 26 +++++++++++++++----------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index d2104312a..c1ef42052 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -43,6 +43,7 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng function openPosition(situationName) { tradingEngine.current.position.status.value = 'Open' + tradingEngine.current.position.serialNumber.value = tradingEngine.episode.episodeCounters.positions.value tradingEngine.current.position.begin.value = tradingEngine.current.candle.begin.value tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index 4eeb26a55..422a7a82c 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -29,6 +29,7 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEng function openStrategy(index, situationName, strategyName) { tradingEngine.current.strategy.status.value = 'Open' + tradingEngine.current.strategy.serialNumber.value = tradingEngine.episode.episodeCounters.strategies.value tradingEngine.current.strategy.begin.value = tradingEngine.current.candle.begin.value tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 8a943b666..b4a4273c2 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -366,6 +366,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Updating Episode Counters */ tradingEngine.episode.episodeCounters.strategies.value++ + /* Initialize this */ tradingEngine.current.distanceToEvent.triggerOn.value = 1 @@ -452,6 +453,12 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingSystem.highlights.push(triggerStage.takePosition.id) tradingSystem.highlights.push(triggerStage.id) + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.positions.value++ + + /* Inicializing this counter */ + tradingEngine.current.distanceToEvent.takePosition.value = 1 + tradingPositionModule.openPosition(situation.name) announcementsModule.makeAnnoucements(triggerStage.takePosition) @@ -573,11 +580,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function getReadyToTakePosition() { - /* Updating Episode Counters */ - tradingEngine.episode.episodeCounters.positions.value++ - - /* Inicializing this counter */ - tradingEngine.current.distanceToEvent.takePosition.value = 1 /* Position size and rate */ tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) @@ -1158,11 +1160,18 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (tradingEngineOrder.size.value <= 0) { continue } + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.orders.value++ + + /* Initialize this */ + tradingEngine.current.distanceToEvent.createOrder.value = 1 + /* Create Order Procedure */ + tradingEngineOrder.status.value = 'Open' tradingEngineOrder.identifier.value = global.UNIQUE_ID() tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value tradingEngineOrder.end.value = tradingEngine.current.candle.end.value - tradingEngineOrder.status.value = 'Open' + tradingEngineOrder.serialNumber.value = tradingEngine.episode.episodeCounters.orders.value tradingEngineOrder.orderName.value = tradingSystemOrder.name tradingEngineOrder.algorithmName.value = executionAlgorithm.name tradingEngineOrder.situationName.value = situationName @@ -1179,11 +1188,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Update State Orders Size */ stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - - /* Updating Episode Counters */ - tradingEngine.episode.episodeCounters.orders.value++ - /* Initialize this */ - tradingEngine.current.distanceToEvent.createOrder.value = 1 } } break From da78ec2bba0612be54277681e882e6c1dffe4836 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 19 Jul 2020 17:00:40 +0200 Subject: [PATCH 096/113] Bug Fixes --- LowFrequencyTrading/TradingPosition.js | 4 ++-- LowFrequencyTrading/TradingSystem.js | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index c1ef42052..33e8917f3 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -92,8 +92,8 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng } function updateSizeAndRate(size, rate) { - tradingEngine.current.position.size.value = size - tradingEngine.current.position.rate.value = rate + tradingEngine.current.position.size.value = global.PRECISE(size, 10) + tradingEngine.current.position.rate.value = global.PRECISE(rate, 10) } function applySlippageToRate() { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index b4a4273c2..f85cc686e 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -517,7 +517,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, true, false, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + checkExecution(executionNode, true, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* From here on, the state is officially Open */ tradingStrategyModule.updateStageStatus('Open Stage', 'Open') @@ -537,7 +537,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, false, false, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + checkExecution(executionNode, false, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* The Open is finished when the fillSize reaches the Position Size. @@ -565,7 +565,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, false, true, tradingEngine.current.position.size.value, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + checkExecution(executionNode, false, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) } /* @@ -951,7 +951,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Close Execution') evalFormulas(stageNode, 'Close Execution') - checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize.value, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) /* Close the Stage Validation */ if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.openStageFilledSize.value) { @@ -1107,6 +1107,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { + /* Enforcing Precision Limit */ + stageSizeLimit.value = global.PRECISE(stageSizeLimit.value, 10) + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + checkExecutionAlgorithms(executionNode) function checkExecutionAlgorithms(executionNode) { @@ -1153,9 +1158,10 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Order Size Calculation */ tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 - if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit) { + if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit.value) { /* We reduce the size to the remaining size of the position. */ - tradingEngineOrder.size.value = stageSizeLimit - stageOrdersSize.value + tradingEngineOrder.size.value = stageSizeLimit.value - stageOrdersSize.value + tradingEngineOrder.size.value = global.PRECISE(tradingEngineOrder.size.value, 10) } if (tradingEngineOrder.size.value <= 0) { continue } From 1838628258a68e90ea9241e8d1f68a1228cd624b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 20 Jul 2020 11:39:49 +0200 Subject: [PATCH 097/113] Placing Orders At the Exchange Implemented --- .../ExchangeAPI.js | 103 +++++++++++------- LowFrequencyTrading/Snapshots.js | 2 +- LowFrequencyTrading/TradingSystem.js | 42 ++++++- Root.js | 8 +- Session.js | 4 +- 5 files changed, 109 insertions(+), 50 deletions(-) rename ExchangeAPI.js => LowFrequencyTrading/ExchangeAPI.js (57%) diff --git a/ExchangeAPI.js b/LowFrequencyTrading/ExchangeAPI.js similarity index 57% rename from ExchangeAPI.js rename to LowFrequencyTrading/ExchangeAPI.js index 617b5a5a8..437b52665 100644 --- a/ExchangeAPI.js +++ b/LowFrequencyTrading/ExchangeAPI.js @@ -2,18 +2,19 @@ let MODULE_NAME = "Exchange API"; let LOG_INFO = true; - let apiClient; let bot = BOT let thisObject = { - initialize: initialize, getTicker: getTicker, getOpenOrders: getOpenOrders, getOrder: getOrder, createOrder: createOrder, - getMaxDecimalPositions: getMaxDecimalPositions + initialize: initialize, + finalize: finalize }; + let tradingSystem + let exchangeId let options = {} let exchange @@ -23,10 +24,12 @@ return thisObject; function initialize() { + tradingSystem = bot.simulationState.tradingSystem + exchangeId = bot.exchange.toLowerCase() - let key = process.env.KEY - let secret = process.env.SECRET + let key = bot.KEY + let secret = bot.SECRET if (key === "undefined") { key = undefined } if (secret === "undefined") { secret = undefined } @@ -43,11 +46,14 @@ exchange = new exchangeClass(exchangeConstructorParams) } - function getMaxDecimalPositions() { - return 8 + function finalize() { + tradingSystem = undefined + exchangeId = undefined + options = undefined + exchange = undefined } - async function getTicker(market, callBackFunction) { + async function getTicker(callBackFunction) { try { logInfo("getTicker -> Entering function."); const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset @@ -108,37 +114,60 @@ } } - async function createOrder(market, side, price, cost, amount, callBackFunction) { + async function createOrder(tradingSystemOrder, tradingEngineOrder) { + + let price = tradingEngineOrder.rate // CCXT: how much quote currency you are willing to pay for a trade lot of base currency (for limit orders only) + let amount = tradingEngineOrder.size // CCXT: how much of currency you want to trade + let type // CCXT: a string literal type of order, ccxt currently unifies market and limit orders only + let side // CCXT: a string literal for the direction of your order, buy or sell + let symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset // CCXT: a string literal symbol of the market you wish to trade on, like BTC/USD, ZEC/ETH, DOGE/DASH, etc + + switch (tradingSystemOrder.type) { + case 'Market Buy Order': { + type = 'market' + side = 'buy' + break + } + case 'Market Sell Order': { + type = 'market' + side = 'sell' + break + } + case 'Limit Buy Order': { + type = 'limit' + side = 'buy' + break + } + case 'Limit Sell Order': { + type = 'limit' + side = 'sell' + break + } + } + + /* Basic Logging */ + logInfo("createOrder -> symbol = " + symbol); + logInfo("createOrder -> side = " + side); + logInfo("createOrder -> cost = " + cost); + logInfo("createOrder -> amount = " + amount); + logInfo("createOrder -> price = " + price); + + /* Basic Validations */ + if (side !== "buy" && side !== "sell") { + logError("createOrder -> side must be either 'buy' or 'sell'."); + return + } + if (exchange.has['createOrder'] === false) { + logError("createOrder -> Exchange does not support createOrder command."); + return + } + try { - logInfo("createOrder -> Entering function."); - - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - - logInfo("createOrder -> market = " + market.assetA + "_" + market.assetB); - logInfo("createOrder -> side = " + side); - logInfo("createOrder -> cost = " + cost); - logInfo("createOrder -> amount = " + amount); - logInfo("createOrder -> price = " + price); - - if (side !== "buy" && side !== "sell") { - logError("createOrder -> side must be either 'buy' or 'sell'."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - let type = 'market' - let order - - if (exchange.has['createOrder']) { - order = await (exchange.createOrder(symbol, type, side, amount)) - callBackFunction(global.DEFAULT_OK_RESPONSE, order) - } else { - logError("createOrder -> Exchange does not support createOrder command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - + let order = await (exchange.createOrder(symbol, type, side, amount)) + return order.id } catch (err) { - logError("createOrder -> err = " + JSON.stringify(err.stack) || err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + tradingSystem.errors.push([tradingSystemOrder.id, err.message]) + logError("getOrder -> Error = " + err.message); } } diff --git a/LowFrequencyTrading/Snapshots.js b/LowFrequencyTrading/Snapshots.js index d36073854..89c10767b 100644 --- a/LowFrequencyTrading/Snapshots.js +++ b/LowFrequencyTrading/Snapshots.js @@ -244,7 +244,7 @@ exports.newSnapshots = function newSnapshots(bot, logger) { } } } catch (err) { - tradingSystem.errors.push(nodeWithCode.id, err.message) + tradingSystem.errors.push([nodeWithCode.id, err.message]) logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> nodeWithCode.code = ' + nodeWithCode.code) logger.write(MODULE_NAME, '[ERROR] runSimulation -> addCodeToSnapshot -> err = ' + err.stack) } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index f85cc686e..b990c0afb 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -26,6 +26,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM let conditions = new Map() let formulas = new Map() + const EXCHANGE_API_MODULE = require('./ExchangeAPI.js') + let exchangeAPIModule = EXCHANGE_API_MODULE.newExchangeAPI(bot, logger) + const TRADING_STRATEGY_MODULE = require('./TradingStrategy.js') let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger, tradingEngineModule) @@ -48,6 +51,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngine = bot.simulationState.tradingEngine sessionParameters = bot.SESSION.parameters + exchangeAPIModule.initialize() tradingStrategyModule.initialize() tradingPositionModule.initialize() announcementsModule.initialize() @@ -56,6 +60,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function finalize() { + exchangeAPIModule.finalize() + exchangeAPIModule = undefined + tradingStrategyModule.finalize() tradingStrategyModule = undefined @@ -1166,6 +1173,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (tradingEngineOrder.size.value <= 0) { continue } + /* Place Order at the Exchange */ + if (placeOrderAtExchange(tradingSystemOrder, tradingEngineOrder) !== true) { continue } + /* Updating Episode Counters */ tradingEngine.episode.episodeCounters.orders.value++ @@ -1191,7 +1201,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - /* Update State Orders Size */ + /* Update Stage Orders Size */ stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) } @@ -1249,14 +1259,38 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } /* Simulate Events that happens at the Exchange, if needed. */ - if (stageIsOpening === false) { - simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) - } + simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) } } } } + function placeOrderAtExchange(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return true + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { + return true + } + } + + let orderId = exchangeAPIModule.createOrder(tradingSystemOrder, tradingEngineOrder) + + if (orderId !== undefined) { + tradingEngineOrder.exchangeId.value = orderId + return true + } + } + function simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) { /* Filter by Session Type */ diff --git a/Root.js b/Root.js index cecc40636..be3cc5da9 100644 --- a/Root.js +++ b/Root.js @@ -225,18 +225,14 @@ /* Bot Type */ botConfig.type = global.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.type - /* Setting the Key to use */ - process.env.KEY = undefined - process.env.SECRET = undefined - if (botConfig.processNode) { if (botConfig.processNode.marketReference) { if (botConfig.processNode.marketReference.keyInstance !== undefined) { if (botConfig.processNode.marketReference.keyInstance.referenceParent !== undefined) { let key = botConfig.processNode.marketReference.keyInstance.referenceParent - process.env.KEY = key.config.codeName - process.env.SECRET = key.config.secret + botConfig.KEY = key.config.codeName + botConfig.SECRET = key.config.secret } } } diff --git a/Session.js b/Session.js index beedc4463..806468fea 100644 --- a/Session.js +++ b/Session.js @@ -300,7 +300,7 @@ } function startLiveTrading(message) { - if (process.env.KEY === undefined || process.env.SECRET === undefined) { // TODO Remove this from Process .env + if (bot.KEY === undefined || bot.SECRET === undefined) { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> Key 'codeName' or 'secret' not provided. Plese check that and try again."); } return false } @@ -311,7 +311,7 @@ } function startFordwardTesting(message) { - if (process.env.KEY === undefined || process.env.SECRET === undefined) { + if (bot.KEY === undefined || bot.SECRET === undefined) { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startFordwardTesting -> Key name or Secret not provided, not possible to run the process in Forward Testing mode."); } console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.") return false From b98650af8dd866cec20065ef34a6954d3d49057d Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 20 Jul 2020 16:18:43 +0200 Subject: [PATCH 098/113] Trading Execution Implemented --- LowFrequencyTrading/ExchangeAPI.js | 92 ++-- LowFrequencyTrading/TradingExecution.js | 656 ++++++++++++++---------- LowFrequencyTrading/TradingSystem.js | 331 +----------- 3 files changed, 450 insertions(+), 629 deletions(-) diff --git a/LowFrequencyTrading/ExchangeAPI.js b/LowFrequencyTrading/ExchangeAPI.js index 437b52665..7bcfd280a 100644 --- a/LowFrequencyTrading/ExchangeAPI.js +++ b/LowFrequencyTrading/ExchangeAPI.js @@ -5,10 +5,9 @@ let bot = BOT let thisObject = { - getTicker: getTicker, - getOpenOrders: getOpenOrders, getOrder: getOrder, createOrder: createOrder, + cancelOrder: cancelOrder, initialize: initialize, finalize: finalize }; @@ -53,64 +52,27 @@ exchange = undefined } - async function getTicker(callBackFunction) { - try { - logInfo("getTicker -> Entering function."); - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - let ticker + async function getOrder(tradingSystemOrder, tradingEngineOrder) { - if (exchange.has['fetchTicker']) { - ticker = await (exchange.fetchTicker(symbol)) - callBackFunction(global.DEFAULT_OK_RESPONSE, ticker) - } else { - logError("getTicker -> Exchange does not support fetchTicker command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + let orderId = tradingEngineOrder.exchangeId.value - } catch (err) { - logError("getTicker -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + /* Basic Logging */ + logInfo("getOrder -> Entering function. orderId = " + orderId); + + const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset + + /* Basic Validations */ + if (exchange.has['fetchOrder'] === false) { + logError("getOrder -> Exchange does not support fetchOrder command."); + return } - } - async function getOpenOrders(market, callBackFunction) { try { - logInfo("getOpenOrders -> Entering function. market = " + JSON.stringify(market)); - - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - let openOrders - - if (exchange.has['fetchOpenOrders']) { - openOrders = await (exchange.fetchOpenOrders(symbol)) - callBackFunction(global.DEFAULT_OK_RESPONSE, openOrders) - } else { - logError("getOpenOrders -> Exchange does not support fetchOpenOrders command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - } catch (err) { - logError("getOpenOrders -> Error = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - async function getOrder(orderId, market, callBackFunction) { - try { - logInfo("getOrder -> Entering function. orderId = " + orderId); - - const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset - let order - - if (exchange.has['fetchOrder']) { - order = await (exchange.fetchOrder(orderId, symbol)) - callBackFunction(global.DEFAULT_OK_RESPONSE, order) - } else { - logError("getOrder -> Exchange does not support fetchOrder command."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + let order = await (exchange.fetchOrder(orderId, symbol)) + return order } catch (err) { + tradingSystem.errors.push([tradingSystemOrder.id, err.message]) logError("getOrder -> Error = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -171,6 +133,30 @@ } } + async function cancelOrder(tradingSystemOrder, tradingEngineOrder) { + + let orderId = tradingEngineOrder.exchangeId.value + + /* Basic Logging */ + logInfo("getOrder -> Entering function. orderId = " + orderId); + + const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset + + /* Basic Validations */ + if (exchange.has['fetchOrder'] === false) { + logError("getOrder -> Exchange does not support fetchOrder command."); + return + } + + try { + let order = await (exchange.cancelOrder(orderId, symbol)) + return true + } catch (err) { + tradingSystem.errors.push([tradingSystemOrder.id, err.message]) + logError("getOrder -> Error = " + err.message); + } + } + function logInfo(message) { if (LOG_INFO === true) { logger.write(MODULE_NAME, '[INFO] ' + message) } } diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index abdb8351b..7d3813664 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -5,313 +5,447 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { const MODULE_NAME = 'Trading Execution' let thisObject = { - takePosition: takePosition, - closePosition: closePosition, + checkExecution: checkExecution, initialize: initialize, finalize: finalize } let tradingEngine + let tradingSystem return thisObject function initialize() { - + tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine - } function finalize() { + tradingSystem = undefined tradingEngine = undefined } - function takePosition() { - /* 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) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> slippageAmount = ' + slippageAmount) - logger.write(MODULE_NAME, '[PERSIST] runSimulation -> loop -> takePositionNow -> tradingEngine.current.position.rate.value = ' + tradingEngine.current.position.rate.value) + function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // We need to put the order because It was not put yet. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return + /* Enforcing Precision Limit */ + stageSizeLimit.value = global.PRECISE(stageSizeLimit.value, 10) + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + + checkExecutionAlgorithms(executionNode) + + function checkExecutionAlgorithms(executionNode) { + for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { + let executionAlgorithm = executionNode.executionAlgorithms[i] + checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + } + } + + function checkOrders(orders, executionAlgorithm, executionNode) { + for (let i = 0; i < orders.length; i++) { + + let tradingSystemOrder = orders[i] + + /* Trading System Validations */ + if (tradingSystemOrder.config.positionSizePercentage === undefined) { continue } + if (tradingSystemOrder.referenceParent === undefined) { continue } + + let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) + + /* Trading Engine Validations */ + if (tradingEngineOrder.serialNumber === undefined) { continue } + if (tradingEngineOrder.identifier === undefined) { continue } + if (tradingEngineOrder.exchangeId === undefined) { continue } + if (tradingEngineOrder.begin === undefined) { continue } + if (tradingEngineOrder.end === undefined) { continue } + if (tradingEngineOrder.rate === undefined) { continue } + if (tradingEngineOrder.size === undefined) { continue } + if (tradingEngineOrder.status === undefined) { continue } + if (tradingEngineOrder.algorithmName === undefined) { continue } + if (tradingEngineOrder.orderCounters === undefined) { continue } + if (tradingEngineOrder.orderCounters.periods === undefined) { continue } + if (tradingEngineOrder.orderStatistics.days === undefined) { continue } + if (tradingEngineOrder.orderStatistics.percentageFilled === undefined) { continue } + if (tradingEngineOrder.orderStatistics.actualRate === undefined) { continue } + if (tradingEngineOrder.orderStatistics.feesPaid === undefined) { continue } + + switch (tradingEngineOrder.status.value) { + case 'Not Open': { + { + /* Check if we need to Create this Order */ + if (stageIsClosing === true) { continue } + let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + + /* Open a new order */ + openOrder(tradingSystemOrder, tradingEngineOrder, situationName) } } break } - case 'Position Closed': { // Waiting for a confirmation that the position was closed. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return + case 'Open': { + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.orderCounters.periods.value++ + tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS + + /* Simulate Events that happens at the Exchange, if needed. */ + simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) + + /* Check Events that happens at the Exchange, if needed. */ + checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) + + /* If the stage is closing or the order is not still Open, we wont be cancelling orders based on defined events */ + if (stageIsClosing !== true && tradingEngineOrder.status.value === 'Open') { + + /* Check if we need to Cancel this Order */ + let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) + if (situationName !== undefined) { + simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') + exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') } } - break - } - case 'Taking Position': { // Waiting for a confirmation that the position was taken. - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is Taking Position.') - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Exiting code block because status is In a Position.') - break - } - } - } else { // The context does not exist so it means we are not in a position. - if (strategy.openStage !== undefined) { - if (strategy.openStage.openExecution !== undefined) { - putOpeningOrder() - return } } } - } else { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> takePositionNow -> Not trading live.') } - } + function openOrder(tradingSystemOrder, tradingEngineOrder, situationName) { + /* Order Size Calculation */ + tradingEngineOrder.size.value = tradingSystem.formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 + if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit.value) { + /* We reduce the size to the remaining size of the position. */ + tradingEngineOrder.size.value = stageSizeLimit.value - stageOrdersSize.value + tradingEngineOrder.size.value = global.PRECISE(tradingEngineOrder.size.value, 10) + } - function putOpeningOrder() { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Entering function.') + /* Check Order Size */ + if (tradingEngineOrder.size.value <= 0) { return } - /* We wont take a position unless we are withing the sessionParameters.timeRange.config.initialDatetime and the sessionParameters.timeRange.config.finalDatetime range */ - if (sessionParameters.timeRange.config.initialDatetime !== undefined) { - if (candle.end < sessionParameters.timeRange.config.initialDatetime) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because current candle ends before the start date. -> sessionParameters.timeRange.config.initialDatetime = ' + sessionParameters.timeRange.config.initialDatetime) - takePositionAtSimulation() - return + /* Place Order at the Exchange */ + if (createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) !== true) { return } + + /* Updating Episode Counters */ + tradingEngine.episode.episodeCounters.orders.value++ + + /* Initialize this */ + tradingEngine.current.distanceToEvent.createOrder.value = 1 + + /* Create Order Procedure */ + tradingEngineOrder.status.value = 'Open' + tradingEngineOrder.identifier.value = global.UNIQUE_ID() + tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value + tradingEngineOrder.end.value = tradingEngine.current.candle.end.value + tradingEngineOrder.serialNumber.value = tradingEngine.episode.episodeCounters.orders.value + tradingEngineOrder.orderName.value = tradingSystemOrder.name + tradingEngineOrder.algorithmName.value = executionAlgorithm.name + tradingEngineOrder.situationName.value = situationName + + /* Order Rate Calculation */ + tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. + if (tradingSystemOrder.positionRate !== undefined) { + if (tradingSystemOrder.positionRate.formula !== undefined) { + tradingEngineOrder.rate.value = tradingSystem.formulas.get(tradingSystemOrder.positionRate.formula.id) + tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) + } + } + + /* Update Stage Orders Size */ + stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + } + + function createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return true + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { + return true + } + } + + let orderId = exchangeAPIModule.createOrder(tradingSystemOrder, tradingEngineOrder) + + if (orderId !== undefined) { + tradingEngineOrder.exchangeId.value = orderId + return true } } - /* We wont take a position if we are past the final datetime */ - if (sessionParameters.timeRange.config.finalDatetime !== undefined) { - if (candle.begin > sessionParameters.timeRange.config.finalDatetime) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> putOpeningOrder -> Not placing the trade at the exchange because current candle begins after the end date. -> sessionParameters.timeRange.config.finalDatetime = ' + sessionParameters.timeRange.config.finalDatetime) - takePositionAtSimulation() - return + function simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + break + } + case 'Live Trading Session': { + return + } + case 'Fordward Testing Session': { + return + } + case 'Paper Trading Session': { + break + } + } + + /* Filter by what is defined at the Strategy */ + if (tradingSystemOrder.simulatedExchangeEvents === undefined) { return } + + /* Partial Fill Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { + + /* Percentage Filled Calculation */ + let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 + if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { + percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + } + tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + + /* Filled Size Calculation */ + stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + + if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { + simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') + } + } + } + + /* Actual Rate Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { + if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { + tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) + } + } + } + + /* Fees Paid Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { + if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 + } + } } } - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (variable.executionContext !== undefined) { - if (variable.executionContext.periods !== undefined) { - if (tradingEngine.episode.episodeCounters.periods.value <= variable.executionContext.periods) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because it was already placed at a previous execution.') - takePositionAtSimulation() + function checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return true + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { + return true + } + } + + let order = exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) + + if (order === undefined) { return } + + const AT_EXCHANGE_STATUS = { + OPEN: 'open', + CLOSED: 'closed', + CANCELLED: 'canceled' + } + + /* Status Checks */ + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100 && order.status === AT_EXCHANGE_STATUS.CLOSED) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + if (tradingEngineOrder.orderStatistics.percentageFilled.value < 100 && order.status === AT_EXCHANGE_STATUS.CLOSED) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Closed at the Exchange' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + if (order.status === AT_EXCHANGE_STATUS.CANCELLED) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Cancelled at the Exchange' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + + syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) + + /* Forced Cancellation Check */ + if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { + exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') + } + } + + function syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) { + /* Percentage Filled */ + let currentPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value + let percentageFilled = order.filled * 100 / tradingEngineOrder.size + percentageFilled = global.PRECISE(percentageFilled.value, 10) + tradingEngineOrder.orderStatistics.percentageFilled.value = percentageFilled + + /* Filled Size Calculation */ + stageFilledSize.value = stageFilledSize.value - tradingEngineOrder.size.value * currentPercentageFilled / 100 // First remove the current filled size + stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 // Second add the new filled size + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + + /* Actual Rate Simulation */ + tradingEngineOrder.orderStatistics.actualRate.value = order.cost / order.filled + tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) + + /* Fees Paid Simulation */ + tradingEngineOrder.orderStatistics.feesPaid.value = order.fee.rate + tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) + } + + function simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + break + } + case 'Live Trading Session': { + return + } + case 'Fordward Testing Session': { + return + } + case 'Paper Trading Session': { + break + } + } + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = exitType + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ + let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 + stageOrdersSize.value = stageOrdersSize.value - unfilledSize + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + } + + function exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { + + /* Filter by Session Type */ + switch (bot.SESSION.type) { + case 'Backtesting Session': { + return + } + case 'Live Trading Session': { + break + } + case 'Fordward Testing Session': { + break + } + case 'Paper Trading Session': { return } } - } - /* We are not going to place orders based on outdated information. The next filter prevents firing orders when backtesting. */ - if (currentDay) { - let today = new Date(Math.trunc((new Date().valueOf()) / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) - let processDay = new Date(Math.trunc(currentDay.valueOf() / global.ONE_DAY_IN_MILISECONDS) * global.ONE_DAY_IN_MILISECONDS) - if (today.valueOf() !== processDay.valueOf()) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Not placing the trade at the exchange because the current candle belongs to the previous day and that is considered simulation and not live trading.') - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> today = ' + today) - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> processDay = ' + processDay) - takePositionAtSimulation() - return + /* Check if we can cancel the order at the Exchange. */ + if (exchangeAPIModule.exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder) === true) { + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = exitType + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + + /* Since the order was cancelled, we remove the unfilled amount of the order from here */ + let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 + stageOrdersSize.value = stageOrdersSize.value - unfilledSize + stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) + + /* + Perhaps the order was filled a bit more between the last time we checked and when it was cancelled. + To sync our accounting, we need to check the order one last time and if it changed, fix it. + */ + + let order = exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) + + if (order === undefined) { return } + + const AT_EXCHANGE_STATUS = { + OPEN: 'open', + CLOSED: 'closed', + CANCELLED: 'canceled' + } + + syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) } } - let orderPrice - let amountA - let amountB - let orderSide - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - orderSide = 'sell' - - orderPrice = tradingEngine.current.position.rate.value - - amountA = tradingEngine.current.position.size.value * orderPrice - amountB = tradingEngine.current.position.size.value - } else { - orderSide = 'buy' - - orderPrice = tradingEngine.current.position.rate.value - - amountA = tradingEngine.current.position.size.value - amountB = tradingEngine.current.position.size.value / orderPrice - } - - variable.executionContext = { - status: 'Taking Position', - periods: tradingEngine.episode.episodeCounters.periods.value - } - - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> Ready to create order.') - exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) - - function onOrderCreated(err, order) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Entering function.') - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') - variable.executionContext = { - status: 'In a Position', - periods: tradingEngine.episode.episodeCounters.periods.value, - amountA: amountA, - amountB: amountB, - orderId: order.id - } - takePositionAtSimulation() - return + function checkOrderEvent(event, order, executionAlgorithm, executionNode) { + 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 } - case global.DEFAULT_FAIL_RESPONSE.result: { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) - strategy.openStage.openExecution.error = err.message - afterLoop() - return - } - case global.DEFAULT_RETRY_RESPONSE.result: { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Message = ' + err.message) - strategy.openStage.openExecution.error = err.message - afterLoop() - return + + 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(order.id) + tradingSystem.highlights.push(executionAlgorithm.id) + tradingSystem.highlights.push(executionNode.id) + + announcementsModule.makeAnnoucements(event) + return situation.name // if the event is triggered, we return the name of the situation that passed } } - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } - } - } - - function closePosition() { - /* Check that we are in LIVE MODE */ - if (bot.SESSION.type === 'Live Trading Session' || bot.SESSION.type === 'Fordward Testion Session') { - /* We see if we need to put the actual order at the exchange. */ - if (variable.executionContext !== undefined) { - switch (variable.executionContext.status) { - case 'Without a Position': { // No way to close anything at the exchange. - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Without a Position.') - break - } - case 'In a Position': { // This should mean that we already put the order at the exchange. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } - } - break - } - case 'Closing Position': { // Waiting for a confirmation that the position was taken. - if (strategy.closeStage !== undefined) { - if (strategy.closeStage.closeExecution !== undefined) { - putClosingOrder() - return - } - } - break - } - - case 'Position Closed': { // - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Closing a Position -> Exiting code block because status is Position Closed.') - break - } - } - } - } - } - - function putClosingOrder() { - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (variable.executionContext !== undefined) { - if (variable.executionContext.periods !== undefined) { - if (tradingEngine.episode.episodeCounters.periods.value <= variable.executionContext.periods) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> Exiting function because this closing was already submited at a previous execution.') - closePositionAtSimulation() - return - } - } - } - - let orderPrice - let amountA - let amountB - let orderSide - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - orderSide = 'buy' - - orderPrice = ticker.last + 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - - amountA = tradingEngine.current.balance.quotedAsset.value - amountB = tradingEngine.current.balance.quotedAsset.value / orderPrice - } else { - orderSide = 'sell' - - orderPrice = ticker.last - 100 // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. - - amountA = tradingEngine.current.balance.baseAsset.value * orderPrice - amountB = tradingEngine.current.balance.baseAsset.value - } - - variable.executionContext = { - status: 'Closing Position', - periods: tradingEngine.episode.episodeCounters.periods.value - } - - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> About to close position at the exchange.') - exchangeAPI.createOrder(bot.market, orderSide, orderPrice, amountA, amountB, onOrderCreated) - - function onOrderCreated(err, order) { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Entering function.') - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_OK_RESPONSE ') - variable.executionContext = { - status: 'Position Closed', - periods: tradingEngine.episode.episodeCounters.periods.value, - amountA: amountA, - amountB: amountB, - orderId: order.id - } - closePositionAtSimulation() - return - } - case global.DEFAULT_FAIL_RESPONSE.result: { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> DEFAULT_FAIL_RESPONSE ') - /* We will assume that the problem is temporary, and expect that it will work at the next execution. */ - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return - } - case global.DEFAULT_RETRY_RESPONSE.result: { - logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> putOpeningOrder -> onOrderCreated -> DEFAULT_RETRY_RESPONSE ') - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return - } - } - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> Unexpected Response -> Message = ' + err.message) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return - } catch (err) { - logger.write(MODULE_NAME, '[ERROR] runSimulation -> loop -> putClosingOrder -> onOrderCreated -> err = ' + err.stack) - callBackFunction(global.DEFAULT_FAIL_RESPONSE) - return } } } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index b990c0afb..27c4ae05c 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -23,8 +23,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM let tradingEngine let sessionParameters - let conditions = new Map() - let formulas = new Map() + tradingSystem.conditions = new Map() + tradingSystem.formulas = new Map() const EXCHANGE_API_MODULE = require('./ExchangeAPI.js') let exchangeAPIModule = EXCHANGE_API_MODULE.newExchangeAPI(bot, logger) @@ -80,8 +80,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM chart = undefined - conditions = undefined - formulas = undefined + tradingSystem.conditions = undefined + tradingSystem.formulas = undefined tradingSystem.highlights = undefined tradingSystem.errors = undefined @@ -270,7 +270,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - conditions.set(node.id, value) + tradingSystem.conditions.set(node.id, value) if (value === true) { tradingSystem.highlights.push(node.id) @@ -311,7 +311,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - formulas.set(node.id, value) + tradingSystem.formulas.set(node.id, value) if (error !== undefined) { tradingSystem.errors.push([node.id, error]) @@ -524,7 +524,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, true, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + tradingExecutionModule.checkExecution(executionNode, true, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* From here on, the state is officially Open */ tradingStrategyModule.updateStageStatus('Open Stage', 'Open') @@ -544,7 +544,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, false, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* The Open is finished when the fillSize reaches the Position Size. @@ -572,7 +572,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - checkExecution(executionNode, false, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + tradingExecutionModule.checkExecution(executionNode, false, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) } /* @@ -609,7 +609,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (strategy.openStage.initialDefinition.positionSize === undefined) return DEFAULT_VALUE if (strategy.openStage.initialDefinition.positionSize.formula === undefined) return DEFAULT_VALUE - let value = formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) + let value = tradingSystem.formulas.get(strategy.openStage.initialDefinition.positionSize.formula.id) if (value === undefined) return DEFAULT_VALUE return value } @@ -623,7 +623,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (strategy.openStage.initialDefinition.positionRate === undefined) return DEFAULT_VALUE if (strategy.openStage.initialDefinition.positionRate.formula === undefined) return DEFAULT_VALUE - let value = formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) + let value = tradingSystem.formulas.get(strategy.openStage.initialDefinition.positionRate.formula.id) if (value === undefined) return DEFAULT_VALUE return value } @@ -958,7 +958,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Close Execution') evalFormulas(stageNode, 'Close Execution') - checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) /* Close the Stage Validation */ if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.openStageFilledSize.value) { @@ -1071,7 +1071,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (phase !== undefined) { if (phase.formula !== undefined) { let previousValue = tradingEngine.current.position.stopLoss.value - tradingPositionModule.applyStopLossFormula(formulas, phase.formula.id) + tradingPositionModule.applyStopLossFormula(tradingSystem.formulas, phase.formula.id) if (tradingEngine.current.position.stopLoss.value !== previousValue) { announcementsModule.makeAnnoucements(phase) @@ -1103,7 +1103,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM if (phase !== undefined) { if (phase.formula !== undefined) { let previousValue = tradingEngine.current.position.takeProfit.value - tradingPositionModule.applyTakeProfitFormula(formulas, phase.formula.id) + tradingPositionModule.applyTakeProfitFormula(tradingSystem.formulas, phase.formula.id) if (tradingEngine.current.position.takeProfit.value !== previousValue) { announcementsModule.makeAnnoucements(phase) @@ -1112,305 +1112,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { - - /* Enforcing Precision Limit */ - stageSizeLimit.value = global.PRECISE(stageSizeLimit.value, 10) - stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) - - checkExecutionAlgorithms(executionNode) - - function checkExecutionAlgorithms(executionNode) { - for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { - let executionAlgorithm = executionNode.executionAlgorithms[i] - checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) - } - } - - function checkOrders(orders, executionAlgorithm, executionNode) { - for (let i = 0; i < orders.length; i++) { - let tradingSystemOrder = orders[i] - - /* Basic Validations */ - if (tradingSystemOrder.config.positionSizePercentage === undefined) { continue } - if (tradingSystemOrder.referenceParent === undefined) { continue } - - let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id) - - if (tradingEngineOrder.identifier === undefined) { continue } - if (tradingEngineOrder.begin === undefined) { continue } - if (tradingEngineOrder.end === undefined) { continue } - if (tradingEngineOrder.rate === undefined) { continue } - if (tradingEngineOrder.size === undefined) { continue } - if (tradingEngineOrder.status === undefined) { continue } - if (tradingEngineOrder.algorithmName === undefined) { continue } - if (tradingEngineOrder.orderCounters === undefined) { continue } - if (tradingEngineOrder.orderCounters.periods === undefined) { continue } - if (tradingEngineOrder.orderStatistics.days === undefined) { continue } - if (tradingEngineOrder.orderStatistics.percentageFilled === undefined) { continue } - if (tradingEngineOrder.orderStatistics.actualRate === undefined) { continue } - if (tradingEngineOrder.orderStatistics.feesPaid === undefined) { continue } - - switch (tradingEngineOrder.status.value) { - case 'Not Open': { - { - /* Check if we need to Create this Order */ - if (stageIsClosing === true) { continue } - let situationName = checkOrderEvent(tradingSystemOrder.createOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (situationName !== undefined) { - - /* Order Size Calculation */ - tradingEngineOrder.size.value = formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 - if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit.value) { - /* We reduce the size to the remaining size of the position. */ - tradingEngineOrder.size.value = stageSizeLimit.value - stageOrdersSize.value - tradingEngineOrder.size.value = global.PRECISE(tradingEngineOrder.size.value, 10) - } - - if (tradingEngineOrder.size.value <= 0) { continue } - - /* Place Order at the Exchange */ - if (placeOrderAtExchange(tradingSystemOrder, tradingEngineOrder) !== true) { continue } - - /* Updating Episode Counters */ - tradingEngine.episode.episodeCounters.orders.value++ - - /* Initialize this */ - tradingEngine.current.distanceToEvent.createOrder.value = 1 - - /* Create Order Procedure */ - tradingEngineOrder.status.value = 'Open' - tradingEngineOrder.identifier.value = global.UNIQUE_ID() - tradingEngineOrder.begin.value = tradingEngine.current.candle.begin.value - tradingEngineOrder.end.value = tradingEngine.current.candle.end.value - tradingEngineOrder.serialNumber.value = tradingEngine.episode.episodeCounters.orders.value - tradingEngineOrder.orderName.value = tradingSystemOrder.name - tradingEngineOrder.algorithmName.value = executionAlgorithm.name - tradingEngineOrder.situationName.value = situationName - - /* Order Rate Calculation */ - tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. - if (tradingSystemOrder.positionRate !== undefined) { - if (tradingSystemOrder.positionRate.formula !== undefined) { - tradingEngineOrder.rate.value = formulas.get(tradingSystemOrder.positionRate.formula.id) - tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) - } - } - - /* Update Stage Orders Size */ - stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value - stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - } - } - break - } - case 'Open': { - tradingEngineOrder.end.value = tradingEngine.current.candle.end.value - tradingEngineOrder.orderCounters.periods.value++ - tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / global.ONE_DAY_IN_MILISECONDS - - /* If the stage is closing we wont be moving or cancelling orders based on defined events */ - if (stageIsClosing !== true) { - - /* Check if we need to Cancel this Order */ - let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (situationName !== undefined) { - - /* Since the order was cancelled, we remove the unfilled amount of the order from here */ - let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value - let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 - stageOrdersSize.value = stageOrdersSize.value - unfilledSize - stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - - /* Cancel the Order */ - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Cancelled by Event' - - /* Initialize this */ - tradingEngine.current.distanceToEvent.cancelOrder.value = 1 - break - } - - /* Check if we need to Move this Order */ - if (tradingSystemOrder.moveOrderEvents !== undefined) { - for (let j = 0; j < tradingSystemOrder.moveOrderEvents.length; j++) { - let moveOrderEvent = tradingSystemOrder.moveOrderEvents[j] - let situationName = checkOrderEvent(moveOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) - if (situationName !== undefined) { - if (moveOrderEvent.positionRate !== undefined) { - if (moveOrderEvent.positionRate.formula !== undefined) { - - /* Move Order to a different Rate */ - tradingEngineOrder.rate.value = formulas.get(moveOrderEvent.positionRate.formula.id) - tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) - - /* Initialize this */ - tradingEngine.current.distanceToEvent.moveOrder.value = 1 - break - } - } - } - } - } - } - - /* Simulate Events that happens at the Exchange, if needed. */ - simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) - } - } - } - } - - function placeOrderAtExchange(tradingSystemOrder, tradingEngineOrder) { - - /* Filter by Session Type */ - switch (bot.SESSION.type) { - case 'Backtesting Session': { - return true - } - case 'Live Trading Session': { - break - } - case 'Fordward Testing Session': { - break - } - case 'Paper Trading Session': { - return true - } - } - - let orderId = exchangeAPIModule.createOrder(tradingSystemOrder, tradingEngineOrder) - - if (orderId !== undefined) { - tradingEngineOrder.exchangeId.value = orderId - return true - } - } - - function simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) { - - /* Filter by Session Type */ - switch (bot.SESSION.type) { - case 'Backtesting Session': { - break - } - case 'Live Trading Session': { - return - } - case 'Fordward Testing Session': { - return - } - case 'Paper Trading Session': { - break - } - } - - /* Filter by what is defined at the Strategy */ - if (tradingSystemOrder.simulatedExchangeEvents === undefined) { return } - - /* Partial Fill Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { - - /* Percentage Filled Calculation */ - let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 - if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { - percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value - } - tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled - if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { - - /* Close this Order */ - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Filled' - - /* Initialize this */ - tradingEngine.current.distanceToEvent.closeOrder.value = 1 - } - - /* Filled Size Calculation */ - stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 - stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) - - if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { - if (tradingEngineOrder.orderStatistics.percentageFilled.value === 0) { - - /* Close this Order */ - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Cancelled by Closing Stage' - - /* Initialize this */ - tradingEngine.current.distanceToEvent.closeOrder.value = 1 - } - if (tradingEngineOrder.orderStatistics.percentageFilled.value > 0 && tradingEngineOrder.orderStatistics.percentageFilled.value < 100) { - - /* Close this Order */ - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Partially Filled' - - /* Initialize this */ - tradingEngine.current.distanceToEvent.closeOrder.value = 1 - } - - /* Since the order was cancelled, we remove the unfilled amount of the order from here */ - let unfilledPercentage = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value - let unfilledSize = tradingEngineOrder.size.value * unfilledPercentage / 100 - stageOrdersSize.value = stageOrdersSize.value - unfilledSize - stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - } - } - } - - /* Actual Rate Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { - if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { - tradingEngineOrder.orderStatistics.actualRate.value = formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) - } - } - } - - /* Fees Paid Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { - if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { - tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 - } - } - } - } - - function checkOrderEvent(event, order, executionAlgorithm, executionNode) { - 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(order.id) - tradingSystem.highlights.push(executionAlgorithm.id) - tradingSystem.highlights.push(executionNode.id) - - announcementsModule.makeAnnoucements(event) - return situation.name // if the event is triggered, we return the name of the situation that passed - } - } - } - } - } - function isItInside(elementWithTimestamp, elementWithBeginEnd) { /* The function is to allow in Conditions and Formulas to easily know when an element with timestamp (like the ones of single files) are inside the time range @@ -1429,8 +1130,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM let condition = situation.conditions[m] let value = false - if (conditions.get(condition.id) !== undefined) { - value = conditions.get(condition.id) + if (tradingSystem.conditions.get(condition.id) !== undefined) { + value = tradingSystem.conditions.get(condition.id) } if (value === true) { tradingSystem.highlights.push(condition.id) From 29cb678de379d9256cc8dfc5ef014b5ae5836618 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 21 Jul 2020 10:15:13 +0200 Subject: [PATCH 099/113] Debugging now --- LowFrequencyTrading/ExchangeAPI.js | 6 ++---- LowFrequencyTrading/TradingEngine.js | 6 ------ LowFrequencyTrading/TradingExecution.js | 10 +++++++++- LowFrequencyTrading/TradingSimulation.js | 4 ---- LowFrequencyTrading/TradingSystem.js | 19 ++++++------------- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/LowFrequencyTrading/ExchangeAPI.js b/LowFrequencyTrading/ExchangeAPI.js index 7bcfd280a..8455fce3b 100644 --- a/LowFrequencyTrading/ExchangeAPI.js +++ b/LowFrequencyTrading/ExchangeAPI.js @@ -1,8 +1,6 @@ -exports.newExchangeAPI = function newExchangeAPI(logger, BOT) { +exports.newExchangeAPI = function newExchangeAPI(bot, logger) { let MODULE_NAME = "Exchange API"; - let LOG_INFO = true; - let bot = BOT let thisObject = { getOrder: getOrder, @@ -158,7 +156,7 @@ } function logInfo(message) { - if (LOG_INFO === true) { logger.write(MODULE_NAME, '[INFO] ' + message) } + logger.write(MODULE_NAME, '[INFO] ' + message) } function logError(message) { diff --git a/LowFrequencyTrading/TradingEngine.js b/LowFrequencyTrading/TradingEngine.js index c8e7e4fdd..e10bfe927 100644 --- a/LowFrequencyTrading/TradingEngine.js +++ b/LowFrequencyTrading/TradingEngine.js @@ -190,12 +190,6 @@ exports.newTradingEngine = function newTradingEngine(bot, logger) { tradingEngine.current.distanceToEvent.createOrder.value++ } - if ( - tradingEngine.current.distanceToEvent.moveOrder.value > 0 - ) { - tradingEngine.current.distanceToEvent.moveOrder.value++ - } - if ( tradingEngine.current.distanceToEvent.cancelOrder.value > 0 ) { diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 7d3813664..590613d32 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -1,4 +1,4 @@ -exports.newTradingExecution = function newTradingExecution(bot, logger) { +exports.newTradingExecution = function newTradingExecution(bot, logger, tradingEngineModule) { /* The Trading Execution modules manages the execution of orders against the exchanges. */ @@ -13,16 +13,24 @@ exports.newTradingExecution = function newTradingExecution(bot, logger) { let tradingEngine let tradingSystem + const EXCHANGE_API_MODULE = require('./ExchangeAPI.js') + let exchangeAPIModule = EXCHANGE_API_MODULE.newExchangeAPI(bot, logger) + return thisObject function initialize() { tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine + + exchangeAPIModule.initialize() } function finalize() { tradingSystem = undefined tradingEngine = undefined + + exchangeAPIModule.finalize() + exchangeAPIModule = undefined } function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 98014e6fc..045f353d7 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -42,10 +42,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER /* These are the Modules we will need to run the Simulation */ - const EXCHANGE_API = require('../ExchangeAPI'); - exchangeAPI = EXCHANGE_API.newExchangeAPI(logger, bot); - exchangeAPI.initialize(); - const TRADING_ENGINE_MODULE = require('./TradingEngine.js') let tradingEngineModule = TRADING_ENGINE_MODULE.newTradingEngine(bot, logger) tradingEngineModule.initialize() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 27c4ae05c..5ffdc50dd 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -23,27 +23,21 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM let tradingEngine let sessionParameters - tradingSystem.conditions = new Map() - tradingSystem.formulas = new Map() - - const EXCHANGE_API_MODULE = require('./ExchangeAPI.js') - let exchangeAPIModule = EXCHANGE_API_MODULE.newExchangeAPI(bot, logger) - const TRADING_STRATEGY_MODULE = require('./TradingStrategy.js') let tradingStrategyModule = TRADING_STRATEGY_MODULE.newTradingStrategy(bot, logger, tradingEngineModule) const TRADING_POSITION_MODULE = require('./TradingPosition.js') let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger, tradingEngineModule) + const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') + let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger, tradingEngineModule) + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) const SNAPSHOTS_MODULE = require('./Snapshots.js') let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) - const TRADING_ENGINE_EXECUTION = require('./TradingExecution.js') - let tradingExecutionModule = TRADING_ENGINE_EXECUTION.newTradingExecution(bot, logger) - return thisObject function initialize() { @@ -51,7 +45,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM tradingEngine = bot.simulationState.tradingEngine sessionParameters = bot.SESSION.parameters - exchangeAPIModule.initialize() + tradingSystem.conditions = new Map() + tradingSystem.formulas = new Map() + tradingStrategyModule.initialize() tradingPositionModule.initialize() announcementsModule.initialize() @@ -60,9 +56,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } function finalize() { - exchangeAPIModule.finalize() - exchangeAPIModule = undefined - tradingStrategyModule.finalize() tradingStrategyModule = undefined From 83bd8b4a11222a3a494cdbd2b34f12dab9c71cd2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 21 Jul 2020 10:51:59 +0200 Subject: [PATCH 100/113] Backtest Running again --- LowFrequencyTrading/TradingExecution.js | 16 +++++-- LowFrequencyTrading/TradingSystem.js | 56 +++++++++++++------------ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 590613d32..8ca04ab8b 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -12,25 +12,35 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE let tradingEngine let tradingSystem + let sessionParameters const EXCHANGE_API_MODULE = require('./ExchangeAPI.js') let exchangeAPIModule = EXCHANGE_API_MODULE.newExchangeAPI(bot, logger) + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') + let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + return thisObject function initialize() { tradingSystem = bot.simulationState.tradingSystem tradingEngine = bot.simulationState.tradingEngine + sessionParameters = bot.SESSION.parameters exchangeAPIModule.initialize() + announcementsModule.initialize() } function finalize() { tradingSystem = undefined tradingEngine = undefined + sessionParameters = undefined exchangeAPIModule.finalize() exchangeAPIModule = undefined + + announcementsModule.finalize() + announcementsModule = undefined } function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { @@ -89,7 +99,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE if (situationName !== undefined) { /* Open a new order */ - openOrder(tradingSystemOrder, tradingEngineOrder, situationName) + openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) } } break @@ -120,7 +130,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } } - function openOrder(tradingSystemOrder, tradingEngineOrder, situationName) { + function openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) { /* Order Size Calculation */ tradingEngineOrder.size.value = tradingSystem.formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit.value) { @@ -440,7 +450,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 5ffdc50dd..14b9d34d5 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -53,6 +53,28 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM announcementsModule.initialize() snapshotsModule.initialize() tradingExecutionModule.initialize() + + /* Adding Functions to Trading System definition */ + + tradingSystem.checkConditions = function (situation, passed) { + /* Given a Situation, we check if all children conditions are true or not */ + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m] + let value = false + if (tradingSystem.conditions.get(condition.id) !== undefined) { + value = tradingSystem.conditions.get(condition.id) + } + if (value === true) { + tradingSystem.highlights.push(condition.id) + } + else { + passed = false + } + tradingSystem.values.push([condition.id, value]) + } + return passed + } } function finalize() { @@ -354,7 +376,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -407,7 +429,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -445,7 +467,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -714,7 +736,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -744,7 +766,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -821,7 +843,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -851,7 +873,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM passed = true } - passed = checkConditions(situation, passed) + passed = tradingSystem.checkConditions(situation, passed) tradingSystem.values.push([situation.id, passed]) if (passed) { @@ -1116,25 +1138,5 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM return false } } - - function checkConditions(situation, passed) { - /* Given a Situation, we check if all children conditions are true or not */ - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m] - let value = false - if (tradingSystem.conditions.get(condition.id) !== undefined) { - value = tradingSystem.conditions.get(condition.id) - } - if (value === true) { - tradingSystem.highlights.push(condition.id) - } - else { - passed = false - } - tradingSystem.values.push([condition.id, value]) - } - return passed - } } From fe43a621e4027998e82b157be0cc47a047348cee Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 21 Jul 2020 11:12:42 +0200 Subject: [PATCH 101/113] Key Reference --- LowFrequencyTrading/TradingSystem.js | 3 +-- Root.js | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 14b9d34d5..6ca9b3852 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -54,8 +54,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM snapshotsModule.initialize() tradingExecutionModule.initialize() - /* Adding Functions to Trading System definition */ - + /* Adding Functions used elsewhere to Trading System definition */ tradingSystem.checkConditions = function (situation, passed) { /* Given a Situation, we check if all children conditions are true or not */ for (let m = 0; m < situation.conditions.length; m++) { diff --git a/Root.js b/Root.js index be3cc5da9..5349465ce 100644 --- a/Root.js +++ b/Root.js @@ -227,9 +227,9 @@ if (botConfig.processNode) { if (botConfig.processNode.marketReference) { - if (botConfig.processNode.marketReference.keyInstance !== undefined) { - if (botConfig.processNode.marketReference.keyInstance.referenceParent !== undefined) { - let key = botConfig.processNode.marketReference.keyInstance.referenceParent + if (botConfig.processNode.marketReference.keyReference !== undefined) { + if (botConfig.processNode.marketReference.keyReference.referenceParent !== undefined) { + let key = botConfig.processNode.marketReference.keyReference.referenceParent botConfig.KEY = key.config.codeName botConfig.SECRET = key.config.secret From 943ce538ca306d85c3a4c7d6e5147b0fdc4e0ad3 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 21 Jul 2020 12:58:43 +0200 Subject: [PATCH 102/113] Await Waiting --- LowFrequencyTrading/ExchangeAPI.js | 11 ++++------- LowFrequencyTrading/TradingExecution.js | 22 +++++++++++----------- Session.js | 6 +++--- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/LowFrequencyTrading/ExchangeAPI.js b/LowFrequencyTrading/ExchangeAPI.js index 8455fce3b..8d6adc9a1 100644 --- a/LowFrequencyTrading/ExchangeAPI.js +++ b/LowFrequencyTrading/ExchangeAPI.js @@ -28,9 +28,6 @@ let key = bot.KEY let secret = bot.SECRET - if (key === "undefined") { key = undefined } - if (secret === "undefined") { secret = undefined } - const exchangeClass = ccxt[exchangeId] const exchangeConstructorParams = { 'apiKey': key, @@ -76,8 +73,8 @@ async function createOrder(tradingSystemOrder, tradingEngineOrder) { - let price = tradingEngineOrder.rate // CCXT: how much quote currency you are willing to pay for a trade lot of base currency (for limit orders only) - let amount = tradingEngineOrder.size // CCXT: how much of currency you want to trade + let price = tradingEngineOrder.rate.value // CCXT: how much quote currency you are willing to pay for a trade lot of base currency (for limit orders only) + let amount = tradingEngineOrder.size.value // CCXT: how much of currency you want to trade let type // CCXT: a string literal type of order, ccxt currently unifies market and limit orders only let side // CCXT: a string literal for the direction of your order, buy or sell let symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset // CCXT: a string literal symbol of the market you wish to trade on, like BTC/USD, ZEC/ETH, DOGE/DASH, etc @@ -108,7 +105,7 @@ /* Basic Logging */ logInfo("createOrder -> symbol = " + symbol); logInfo("createOrder -> side = " + side); - logInfo("createOrder -> cost = " + cost); + logInfo("createOrder -> type = " + type); logInfo("createOrder -> amount = " + amount); logInfo("createOrder -> price = " + price); @@ -123,7 +120,7 @@ } try { - let order = await (exchange.createOrder(symbol, type, side, amount)) + let order = await (exchange.createOrder(symbol, type, side, amount, price)) return order.id } catch (err) { tradingSystem.errors.push([tradingSystemOrder.id, err.message]) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 8ca04ab8b..2b6d89777 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -142,6 +142,15 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE /* Check Order Size */ if (tradingEngineOrder.size.value <= 0) { return } + /* Order Rate Calculation */ + tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. + if (tradingSystemOrder.positionRate !== undefined) { + if (tradingSystemOrder.positionRate.formula !== undefined) { + tradingEngineOrder.rate.value = tradingSystem.formulas.get(tradingSystemOrder.positionRate.formula.id) + tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) + } + } + /* Place Order at the Exchange */ if (createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) !== true) { return } @@ -161,21 +170,12 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE tradingEngineOrder.algorithmName.value = executionAlgorithm.name tradingEngineOrder.situationName.value = situationName - /* Order Rate Calculation */ - tradingEngineOrder.rate.value = tradingEngine.current.position.rate.value // By default this is the order rate. - if (tradingSystemOrder.positionRate !== undefined) { - if (tradingSystemOrder.positionRate.formula !== undefined) { - tradingEngineOrder.rate.value = tradingSystem.formulas.get(tradingSystemOrder.positionRate.formula.id) - tradingEngineOrder.rate.value = global.PRECISE(tradingEngineOrder.rate.value, 10) - } - } - /* Update Stage Orders Size */ stageOrdersSize.value = stageOrdersSize.value + tradingEngineOrder.size.value stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) } - function createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) { + async function createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) { /* Filter by Session Type */ switch (bot.SESSION.type) { @@ -193,7 +193,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } } - let orderId = exchangeAPIModule.createOrder(tradingSystemOrder, tradingEngineOrder) + let orderId = await exchangeAPIModule.createOrder(tradingSystemOrder, tradingEngineOrder) if (orderId !== undefined) { tradingEngineOrder.exchangeId.value = orderId diff --git a/Session.js b/Session.js index 806468fea..f7524e5ce 100644 --- a/Session.js +++ b/Session.js @@ -306,7 +306,7 @@ } bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) - pProcessConfig.liveWaitTime = sessionParameters.timeFrame.config.value + pProcessConfig.liveWaitTime = bot.SESSION.parameters.timeFrame.config.value return true } @@ -335,14 +335,14 @@ bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100 bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100 - pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value + pProcessConfig.normalWaitTime = bot.SESSION.parameters.timeFrame.config.value return true } function startPaperTrading(message) { bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) - pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value + pProcessConfig.normalWaitTime = bot.SESSION.parameters.timeFrame.config.value return true } From 95e16c7ab78d06ab995253b2bdc39698c8fafd2d Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 21 Jul 2020 16:49:25 +0200 Subject: [PATCH 103/113] await problem solved --- LowFrequencyTrading/ExchangeAPI.js | 4 +- LowFrequencyTrading/TradingExecution.js | 60 +++++++++++------------- LowFrequencyTrading/TradingSimulation.js | 6 +-- LowFrequencyTrading/TradingSystem.js | 12 ++--- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/LowFrequencyTrading/ExchangeAPI.js b/LowFrequencyTrading/ExchangeAPI.js index 8d6adc9a1..633f9a59c 100644 --- a/LowFrequencyTrading/ExchangeAPI.js +++ b/LowFrequencyTrading/ExchangeAPI.js @@ -63,7 +63,7 @@ } try { - let order = await (exchange.fetchOrder(orderId, symbol)) + let order = await exchange.fetchOrder(orderId, symbol) return order } catch (err) { tradingSystem.errors.push([tradingSystemOrder.id, err.message]) @@ -144,7 +144,7 @@ } try { - let order = await (exchange.cancelOrder(orderId, symbol)) + let order = await exchange.cancelOrder(orderId, symbol) return true } catch (err) { tradingSystem.errors.push([tradingSystemOrder.id, err.message]) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 2b6d89777..3aed57b8c 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -43,26 +43,26 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE announcementsModule = undefined } - function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { + async function checkExecution(executionNode, stageIsOpening, stageIsClosing, stageSizeLimit, stageOrdersSize, stageFilledSize) { /* Enforcing Precision Limit */ stageSizeLimit.value = global.PRECISE(stageSizeLimit.value, 10) stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) - checkExecutionAlgorithms(executionNode) + await checkExecutionAlgorithms(executionNode) - function checkExecutionAlgorithms(executionNode) { + async function checkExecutionAlgorithms(executionNode) { for (let i = 0; i < executionNode.executionAlgorithms.length; i++) { let executionAlgorithm = executionNode.executionAlgorithms[i] - checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) - checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.marketBuyOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.marketSellOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.limitBuyOrders, executionAlgorithm, executionNode) + await checkOrders(executionAlgorithm.limitSellOrders, executionAlgorithm, executionNode) } } - function checkOrders(orders, executionAlgorithm, executionNode) { + async function checkOrders(orders, executionAlgorithm, executionNode) { for (let i = 0; i < orders.length; i++) { let tradingSystemOrder = orders[i] @@ -99,7 +99,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE if (situationName !== undefined) { /* Open a new order */ - openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) + await openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) } } break @@ -113,7 +113,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE simulateExchangeEvents(tradingSystemOrder, tradingEngineOrder) /* Check Events that happens at the Exchange, if needed. */ - checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) + await checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) /* If the stage is closing or the order is not still Open, we wont be cancelling orders based on defined events */ if (stageIsClosing !== true && tradingEngineOrder.status.value === 'Open') { @@ -122,7 +122,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode) if (situationName !== undefined) { simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') - exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') + await exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Cancel Event') } } } @@ -130,7 +130,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } } - function openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) { + async function openOrder(executionAlgorithm, tradingSystemOrder, tradingEngineOrder, situationName) { /* Order Size Calculation */ tradingEngineOrder.size.value = tradingSystem.formulas.get(executionAlgorithm.positionSize.formula.id) * tradingSystemOrder.config.positionSizePercentage / 100 if (stageOrdersSize.value + tradingEngineOrder.size.value > stageSizeLimit.value) { @@ -152,7 +152,8 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } /* Place Order at the Exchange */ - if (createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) !== true) { return } + let result = await createOrderAtExchange(tradingSystemOrder, tradingEngineOrder) + if (result !== true) { return } /* Updating Episode Counters */ tradingEngine.episode.episodeCounters.orders.value++ @@ -271,7 +272,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } } - function checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) { + async function checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) { /* Filter by Session Type */ switch (bot.SESSION.type) { @@ -289,7 +290,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } } - let order = exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) + let order = await exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) if (order === undefined) { return } @@ -300,7 +301,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } /* Status Checks */ - if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100 && order.status === AT_EXCHANGE_STATUS.CLOSED) { + if (order.remaining === 0 && order.status === AT_EXCHANGE_STATUS.CLOSED) { /* Close this Order */ tradingEngineOrder.status.value = 'Closed' @@ -309,7 +310,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE /* Initialize this */ tradingEngine.current.distanceToEvent.closeOrder.value = 1 } - if (tradingEngineOrder.orderStatistics.percentageFilled.value < 100 && order.status === AT_EXCHANGE_STATUS.CLOSED) { + if (order.remaining > 0 && order.status === AT_EXCHANGE_STATUS.CLOSED) { /* Close this Order */ tradingEngineOrder.status.value = 'Closed' @@ -332,14 +333,14 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE /* Forced Cancellation Check */ if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { - exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') + await exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') } } function syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) { /* Percentage Filled */ let currentPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value - let percentageFilled = order.filled * 100 / tradingEngineOrder.size + let percentageFilled = order.filled * 100 / (order.filled + order.remaining) percentageFilled = global.PRECISE(percentageFilled.value, 10) tradingEngineOrder.orderStatistics.percentageFilled.value = percentageFilled @@ -348,12 +349,12 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 // Second add the new filled size stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) - /* Actual Rate Simulation */ - tradingEngineOrder.orderStatistics.actualRate.value = order.cost / order.filled + /* Actual Rate Calculation */ + tradingEngineOrder.orderStatistics.actualRate.value = order.price tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) - /* Fees Paid Simulation */ - tradingEngineOrder.orderStatistics.feesPaid.value = order.fee.rate + /* Fees Paid Calculation */ + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value - order.amount tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) } @@ -389,7 +390,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) } - function exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { + async function exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { /* Filter by Session Type */ switch (bot.SESSION.type) { @@ -408,7 +409,8 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } /* Check if we can cancel the order at the Exchange. */ - if (exchangeAPIModule.exchangeCancelOrder(tradingSystemOrder, tradingEngineOrder) === true) { + let result = await exchangeAPIModule.cancelOrder(tradingSystemOrder, tradingEngineOrder) + if (result === true) { /* Close this Order */ tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = exitType @@ -427,16 +429,10 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE To sync our accounting, we need to check the order one last time and if it changed, fix it. */ - let order = exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) + let order = await exchangeAPIModule.getOrder(tradingSystemOrder, tradingEngineOrder) if (order === undefined) { return } - const AT_EXCHANGE_STATUS = { - OPEN: 'open', - CLOSED: 'closed', - CANCELLED: 'canceled' - } - syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) } } diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index 045f353d7..58470f756 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -108,7 +108,7 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT loop() } - function loop() { + async function loop() { if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Processing candle # ' + tradingEngine.current.candle.index.value) } let candle = candles[tradingEngine.current.candle.index.value] // This is the current candle the Simulation is working at. @@ -146,13 +146,13 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.triggerStage() /* Run the Open Stage */ - tradingSystemModule.openStage() + await tradingSystemModule.openStage() /* Run the Manage Stage */ tradingSystemModule.manageStage() /* Run the Close Stage */ - tradingSystemModule.closeStage() + await tradingSystemModule.closeStage() /* Add new records to the process output */ tradingRecordsModule.appendRecords() diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 6ca9b3852..7cb87893c 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -504,7 +504,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - function openStage() { + async function openStage() { /* Abort Open Stage Check */ if (tradingEngine.current.strategy.openStageStatus.value === 'Open' && tradingEngine.current.strategy.closeStageStatus.value === 'Open') { /* @@ -538,7 +538,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - tradingExecutionModule.checkExecution(executionNode, true, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + await tradingExecutionModule.checkExecution(executionNode, true, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* From here on, the state is officially Open */ tradingStrategyModule.updateStageStatus('Open Stage', 'Open') @@ -558,7 +558,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + await tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) /* The Open is finished when the fillSize reaches the Position Size. @@ -586,7 +586,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') - tradingExecutionModule.checkExecution(executionNode, false, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) + await tradingExecutionModule.checkExecution(executionNode, false, true, tradingEngine.current.position.size, tradingEngine.current.position.openStageOrdersSize, tradingEngine.current.position.openStageFilledSize) } /* @@ -948,7 +948,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } } - function closeStage() { + async function closeStage() { /* Opening Status Procedure */ if (tradingEngine.current.strategy.closeStageStatus.value === 'Opening') { /* @@ -972,7 +972,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM evalConditions(stageNode, 'Close Execution') evalFormulas(stageNode, 'Close Execution') - tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) + await tradingExecutionModule.checkExecution(executionNode, false, false, tradingEngine.current.position.openStageFilledSize, tradingEngine.current.position.closeStageOrdersSize, tradingEngine.current.position.closeStageFilledSize) /* Close the Stage Validation */ if (tradingEngine.current.position.closeStageFilledSize.value === tradingEngine.current.position.openStageFilledSize.value) { From c09957b529fe3ebfb7049c9513b250e78ad86d66 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 21 Jul 2020 17:02:10 +0200 Subject: [PATCH 104/113] Update TradingExecution.js --- LowFrequencyTrading/TradingExecution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 3aed57b8c..2c6b6afe3 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -341,7 +341,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE /* Percentage Filled */ let currentPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value let percentageFilled = order.filled * 100 / (order.filled + order.remaining) - percentageFilled = global.PRECISE(percentageFilled.value, 10) + percentageFilled = global.PRECISE(percentageFilled, 10) tradingEngineOrder.orderStatistics.percentageFilled.value = percentageFilled /* Filled Size Calculation */ From 062ef778e9487044ac6e3eedaf8dc57d634fac68 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 22 Jul 2020 10:23:13 +0200 Subject: [PATCH 105/113] Balance Problem Fixed --- LowFrequencyTrading/TradingExecution.js | 161 +++++++++++++++++------- LowFrequencyTrading/TradingSystem.js | 44 ++----- 2 files changed, 127 insertions(+), 78 deletions(-) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 2c6b6afe3..4cd6a6912 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -48,7 +48,6 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE /* Enforcing Precision Limit */ stageSizeLimit.value = global.PRECISE(stageSizeLimit.value, 10) stageOrdersSize.value = global.PRECISE(stageOrdersSize.value, 10) - stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) await checkExecutionAlgorithms(executionNode) @@ -223,41 +222,18 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE /* Filter by what is defined at the Strategy */ if (tradingSystemOrder.simulatedExchangeEvents === undefined) { return } - /* Partial Fill Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { - - /* Percentage Filled Calculation */ - let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 - if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { - percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value - } - tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled - if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { - - /* Close this Order */ - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Filled' - - /* Initialize this */ - tradingEngine.current.distanceToEvent.closeOrder.value = 1 - } - - /* Filled Size Calculation */ - stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 - stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) - - if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { - simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') - } - } - } + let previousSizeFilled = tradingEngineOrder.orderStatistics.sizeFilled.value + let previousPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value + let previousAmountReceived = tradingEngineOrder.orderStatistics.amountReceived.value + let previousFeesPaid = tradingEngineOrder.orderStatistics.feesPaid.value /* Actual Rate Simulation */ if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { + /* Calculate this only once for this order */ if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) + tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) } } } @@ -267,8 +243,59 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 + tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) } } + } else { + /* Fees are simulated based on the Session Paremeters */ + switch (tradingEngineOrder.type) { + case 'Market Order': { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + break + } + case 'Limit Order': { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.maker / 100 + break + } + } + } + + /* Partial Fill Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { + + /* Percentage Filled */ + let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 + if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { + percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + } + tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled + + /* Check if we need to close */ + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + } + } + + /* Size Filled */ + tradingEngineOrder.orderStatistics.sizeFilled.value = tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 + tradingEngineOrder.orderStatistics.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.sizeFilled.value, 10) + + /* Amount Received */ + tradingEngineOrder.orderStatistics.amountReceived.value = tradingEngineOrder.orderStatistics.sizeFilled.value * tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate + tradingEngineOrder.orderStatistics.amountReceived.value = global.PRECISE(tradingEngineOrder.orderStatistics.amountReceived.value, 10) + + doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + + if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { + simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') } } @@ -338,24 +365,74 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE } function syncWithExchange(tradingSystemOrder, tradingEngineOrder, order) { + + let previousSizeFilled = tradingEngineOrder.orderStatistics.sizeFilled.value + let previousPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value + let previousAmountReceived = tradingEngineOrder.orderStatistics.amountReceived.value + let previousFeesPaid = tradingEngineOrder.orderStatistics.feesPaid.value + + /* Fees Paid */ + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value - order.amount + tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) + /* Percentage Filled */ - let currentPercentageFilled = tradingEngineOrder.orderStatistics.percentageFilled.value - let percentageFilled = order.filled * 100 / (order.filled + order.remaining) - percentageFilled = global.PRECISE(percentageFilled, 10) - tradingEngineOrder.orderStatistics.percentageFilled.value = percentageFilled + tradingEngineOrder.orderStatistics.percentageFilled.value = order.filled * 100 / (order.filled + order.remaining) + tradingEngineOrder.orderStatistics.percentageFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.percentageFilled.value, 10) - /* Filled Size Calculation */ - stageFilledSize.value = stageFilledSize.value - tradingEngineOrder.size.value * currentPercentageFilled / 100 // First remove the current filled size - stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * percentageFilled / 100 // Second add the new filled size - stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + /* Size Filled */ + tradingEngineOrder.orderStatistics.sizeFilled.value = order.filled + tradingEngineOrder.orderStatistics.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.sizeFilled.value, 10) - /* Actual Rate Calculation */ + /* Amount Received */ + tradingEngineOrder.orderStatistics.amountReceived.value = order.cost + tradingEngineOrder.orderStatistics.amountReceived.value = global.PRECISE(tradingEngineOrder.orderStatistics.amountReceived.value, 10) + + /* Actual Rate */ tradingEngineOrder.orderStatistics.actualRate.value = order.price tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) - /* Fees Paid Calculation */ - tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value - order.amount - tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) + doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + } + + function doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) { + + /* Unaccount for the previous Stage Filled Size */ + stageFilledSize.value = stageFilledSize.value - tradingEngineOrder.size.value * previousPercentageFilled / 100 + /* Account for the current Stage Filled Size */ + stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 + stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + + /* Balances Update */ + switch (order.side) { + case 'buy': { + + /* Undo the previous accounting */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - previousFeesPaid - previousSizeFilled + /* Account the current filling and fees */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngineOrder.orderStatistics.sizeFilled.value + tradingEngineOrder.orderStatistics.feesPaid.value + + /* Undo the previous accounting */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value + previousAmountReceived + /* Account the current filling and fees */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngineOrder.orderStatistics.amountReceived.value + + break + } + case 'sell': { + + /* Undo the previous accounting */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + previousFeesPaid + previousSizeFilled + /* Account the current filling and fees */ + tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngineOrder.orderStatistics.sizeFilled.value - tradingEngineOrder.orderStatistics.feesPaid.value + + /* Undo the previous accounting */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value - previousAmountReceived + /* Account the current filling and fees */ + tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngineOrder.orderStatistics.amountReceived.value + + break + } + } } function simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, exitType) { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 7cb87893c..32f87c3b6 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -517,13 +517,14 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Opening Status Procedure */ if (tradingEngine.current.strategy.openStageStatus.value === 'Opening') { - /* Reset the Exchange Orders data structure to its initial value */ - tradingEngine.exchangeOrders.initialize(tradingEngine.exchangeOrders) /* This procedure is intended to run only once */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage let executionNode = stageNode.openExecution + /* Reset the Exchange Orders data structure to its initial value */ + tradingEngine.exchangeOrders.initialize(tradingEngine.exchangeOrders) + evalConditions(stageNode, 'Initial Definition') evalFormulas(stageNode, 'Initial Definition') @@ -532,9 +533,11 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM calculateTakeProfit() calculateStopLoss() - takePosition() + /* Remember the balance we had before taking the position to later calculate profit or loss */ + tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - /* Check Execution in opening stage mode */ + /* Check Execution in opening stage node */ evalConditions(stageNode, 'Open Execution') evalFormulas(stageNode, 'Open Execution') @@ -573,7 +576,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Closing Status Procedure */ if (tradingEngine.current.strategy.openStageStatus.value === 'Closing') { /* - During the closing status, we do not place new orders, just check + During the closing stage status, we do not place new orders, just check if the ones placed were filled, and cancel the ones not filled. */ let stageNode = tradingSystem.tradingStrategies[tradingEngine.current.strategy.index.value].openStage @@ -642,25 +645,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM return value } } - - function takePosition() { - tradingEngine.previous.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.previous.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - - let feePaid = 0 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - feePaid = tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.position.size.value * tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - tradingEngine.current.position.size.value - } else { - feePaid = tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.position.size.value / tradingEngine.current.position.rate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value - tradingEngine.current.position.size.value - } - } } function manageStage() { @@ -997,18 +981,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM function exitPositionAndStrategy() { - /* New Balances Calculation */ - let feePaid = 0 - if (bot.sessionAndMarketBaseAssetsAreEqual) { - feePaid = tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + tradingEngine.current.balance.quotedAsset.value / tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.quotedAsset.value = 0 - } else { - feePaid = tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - tradingEngine.current.balance.quotedAsset.value = tradingEngine.current.balance.quotedAsset.value + tradingEngine.current.balance.baseAsset.value * tradingEngine.current.position.endRate.value - feePaid - tradingEngine.current.balance.baseAsset.value = 0 - } - /* Needed for Episode Statistics */ tradingPositionModule.updateStatistics() From 006d9c95515a0ba84a844033815c6b293334dc94 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 22 Jul 2020 14:49:58 +0200 Subject: [PATCH 106/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 32f87c3b6..b3f706e47 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -608,7 +608,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Position size and rate */ tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) - /* We take what was calculated at the formula of position rate and apply the slippage. */ + /* We take what was calculated at the formula of position rate and apply the slippage, if needed. */ tradingPositionModule.applySlippageToRate() function getPositionSize() { From 6a195a4677d0ea3e956f0416bd0c6ae755b4966b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 22 Jul 2020 15:26:04 +0200 Subject: [PATCH 107/113] Code Polishing --- LowFrequencyTrading/TradingExecution.js | 169 +++++++++++++++--------- LowFrequencyTrading/TradingSystem.js | 3 - 2 files changed, 105 insertions(+), 67 deletions(-) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 4cd6a6912..7a1288dde 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -227,76 +227,117 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE let previousAmountReceived = tradingEngineOrder.orderStatistics.amountReceived.value let previousFeesPaid = tradingEngineOrder.orderStatistics.feesPaid.value - /* Actual Rate Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { - /* Calculate this only once for this order */ - if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { - tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) - tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) - } - } - } - - /* Fees Paid Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { - if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { - tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 - tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) - } - } - } else { - /* Fees are simulated based on the Session Paremeters */ - switch (tradingEngineOrder.type) { - case 'Market Order': { - tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.taker / 100 - break - } - case 'Limit Order': { - tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.maker / 100 - break - } - } - } - - /* Partial Fill Simulation */ - if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { - if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { - - /* Percentage Filled */ - let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 - if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { - percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value - } - tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled - - /* Check if we need to close */ - if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { - - /* Close this Order */ - tradingEngineOrder.status.value = 'Closed' - tradingEngineOrder.exitType.value = 'Filled' - - /* Initialize this */ - tradingEngine.current.distanceToEvent.closeOrder.value = 1 - } - } - } - - /* Size Filled */ - tradingEngineOrder.orderStatistics.sizeFilled.value = tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 - tradingEngineOrder.orderStatistics.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.sizeFilled.value, 10) - - /* Amount Received */ - tradingEngineOrder.orderStatistics.amountReceived.value = tradingEngineOrder.orderStatistics.sizeFilled.value * tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate - tradingEngineOrder.orderStatistics.amountReceived.value = global.PRECISE(tradingEngineOrder.orderStatistics.amountReceived.value, 10) + actualRateSimulation() + feesPaidSimulation() + orderFillingSimulation() + sizeFilledSimulation() + amountReceivedSimulation() doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + /* If the Stage is Closing and this order is still open, we need to cancel it now */ if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { simulateCancelOrder(tradingSystemOrder, tradingEngineOrder, 'Closing Stage') } + + function actualRateSimulation() { + /* Actual Rate Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) { + /* Calculate this only once for this order */ + if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) { + tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id) + } + } + } else { + switch (tradingEngineOrder.type) { + case 'Market Order': { + /* Actual Rate is simulated based on the Session Paremeters */ + let slippageAmount = tradingEngineOrder.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 + switch (tradingSystemOrder.type) { + case 'Market Sell Order': { + tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value - slippageAmount + break + } + case 'Market Buy Order': { + tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value + slippageAmount + break + } + } + break + } + case 'Limit Order': { + /* In Limit Orders the actual rate is the rate of the order, there is no slippage */ + tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value + break + } + } + } + tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) + } + + function feesPaidSimulation() { + /* Fees Paid Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage !== undefined) { + if (tradingEngineOrder.orderStatistics.feesPaid.value === tradingEngineOrder.orderStatistics.feesPaid.config.initialValue) { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * tradingSystemOrder.simulatedExchangeEvents.simulatedFeesPaid.config.percentage / 100 + } + } + } else { + /* Fees are simulated based on the Session Paremeters */ + switch (tradingEngineOrder.type) { + case 'Market Order': { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.taker / 100 + break + } + case 'Limit Order': { + tradingEngineOrder.orderStatistics.feesPaid.value = tradingEngineOrder.size.value * bot.SESSION.parameters.feeStructure.config.maker / 100 + break + } + } + } + tradingEngineOrder.orderStatistics.feesPaid.value = global.PRECISE(tradingEngineOrder.orderStatistics.feesPaid.value, 10) + } + + function orderFillingSimulation() { + /* Order Filling Simulation */ + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill !== undefined) { + if (tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability !== undefined) { + + /* Percentage Filled */ + let percentageFilled = tradingSystemOrder.simulatedExchangeEvents.simulatedPartialFill.config.fillProbability * 100 + if (tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled > 100) { + percentageFilled = 100 - tradingEngineOrder.orderStatistics.percentageFilled.value + } + tradingEngineOrder.orderStatistics.percentageFilled.value = tradingEngineOrder.orderStatistics.percentageFilled.value + percentageFilled + tradingEngineOrder.orderStatistics.percentageFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.percentageFilled.value, 10) + + /* Check if we need to close */ + if (tradingEngineOrder.orderStatistics.percentageFilled.value === 100) { + + /* Close this Order */ + tradingEngineOrder.status.value = 'Closed' + tradingEngineOrder.exitType.value = 'Filled' + + /* Initialize this */ + tradingEngine.current.distanceToEvent.closeOrder.value = 1 + } + } + } + } + + function sizeFilledSimulation() { + /* Size Filled */ + tradingEngineOrder.orderStatistics.sizeFilled.value = tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 + tradingEngineOrder.orderStatistics.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderStatistics.sizeFilled.value, 10) + } + + function amountReceivedSimulation() { + /* Amount Received */ + tradingEngineOrder.orderStatistics.amountReceived.value = tradingEngineOrder.orderStatistics.sizeFilled.value * tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate + tradingEngineOrder.orderStatistics.amountReceived.value = global.PRECISE(tradingEngineOrder.orderStatistics.amountReceived.value, 10) + } } async function checkExchangeEvents(tradingSystemOrder, tradingEngineOrder) { diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index b3f706e47..488748757 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -608,9 +608,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM /* Position size and rate */ tradingPositionModule.updateSizeAndRate(getPositionSize(), getPositionRate()) - /* We take what was calculated at the formula of position rate and apply the slippage, if needed. */ - tradingPositionModule.applySlippageToRate() - function getPositionSize() { let balance if (bot.sessionAndMarketBaseAssetsAreEqual) { From 60dec8e082d0b0723ffbf749541eba83a17ea878 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 22 Jul 2020 15:29:21 +0200 Subject: [PATCH 108/113] Update TradingSystem.js --- LowFrequencyTrading/TradingSystem.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 488748757..7aafa99d0 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -18,7 +18,6 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM } let chart - let tradingSystem let tradingEngine let sessionParameters @@ -54,7 +53,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger, tradingEngineM snapshotsModule.initialize() tradingExecutionModule.initialize() - /* Adding Functions used elsewhere to Trading System definition */ + /* Adding Functions used elsewhere to Trading System Definition */ tradingSystem.checkConditions = function (situation, passed) { /* Given a Situation, we check if all children conditions are true or not */ for (let m = 0; m < situation.conditions.length; m++) { From 744d5939a3937bac07885fcb203d0db2f2a065c2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 22 Jul 2020 19:47:40 +0200 Subject: [PATCH 109/113] code improvement --- Session.js | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/Session.js b/Session.js index f7524e5ce..056d01a56 100644 --- a/Session.js +++ b/Session.js @@ -161,41 +161,46 @@ } } } - if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) { // if the initialDatetime is missing we create a default one. - bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date()).valueOf() - } else { - bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf() - } - if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { // if the finalDatetime is missing we create a default one. - bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date()).valueOf() - } else { - bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf() - } + /* Session Type Forced Values */ + let today = new Date((new Date()).valueOf()) + let aYearAgo = today - global.ONE_YEAR_IN_MILISECONDS + let aYearFromNow = today + global.ONE_YEAR_IN_MILISECONDS switch (bot.SESSION.type) { - case 'Live Trading Session': { - if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() > (new Date()).valueOf()) { - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date() - } + case 'Backtesting Session': { + useDefaultDatetimes(aYearAgo, today) break } case 'Live Trading Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS) + useDefaultDatetimes(today, aYearFromNow) break } case 'Fordward Testing Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS) + useDefaultDatetimes(today, aYearFromNow) break } case 'Paper Trading Session': { - bot.SESSION.parameters.timeRange.config.initialDatetime = new Date() - bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS) + useDefaultDatetimes(today, aYearFromNow) break } } + function useDefaultDatetimes(initialDefault, finalDefault) { + /* Initial Datetime */ + if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) { + bot.SESSION.parameters.timeRange.config.initialDatetime = initialDefault + } else { + bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf() + } + + /* Final Datetime */ + if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { + bot.SESSION.parameters.timeRange.config.finalDatetime = finalDefault + } else { + bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf() + } + } + /* Time Frame */ if (bot.SESSION.parameters.timeFrame === undefined) { let errorMessage = "Session Parameters Node with no Time Frame." From ac6493dff135925605be4df0b11efba221ddcda8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 22 Jul 2020 20:33:17 +0200 Subject: [PATCH 110/113] Bugs Fixed --- LowFrequencyTrading/TradingExecution.js | 13 +++++++------ LowFrequencyTrading/TradingRecords.js | 9 ++++++++- server.js | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/LowFrequencyTrading/TradingExecution.js b/LowFrequencyTrading/TradingExecution.js index 7a1288dde..b588689b7 100644 --- a/LowFrequencyTrading/TradingExecution.js +++ b/LowFrequencyTrading/TradingExecution.js @@ -233,7 +233,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE sizeFilledSimulation() amountReceivedSimulation() - doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + doTheAccounting(tradingSystemOrder, tradingEngineOrder, previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) /* If the Stage is Closing and this order is still open, we need to cancel it now */ if (stageIsClosing === true && tradingEngineOrder.status.value !== 'Closed') { @@ -432,10 +432,10 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE tradingEngineOrder.orderStatistics.actualRate.value = order.price tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10) - doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) + doTheAccounting(tradingSystemOrder, tradingEngineOrder, previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) } - function doTheAccounting(previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) { + function doTheAccounting(tradingSystemOrder, tradingEngineOrder, previousSizeFilled, previousPercentageFilled, previousAmountReceived, previousFeesPaid) { /* Unaccount for the previous Stage Filled Size */ stageFilledSize.value = stageFilledSize.value - tradingEngineOrder.size.value * previousPercentageFilled / 100 @@ -443,9 +443,10 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE stageFilledSize.value = stageFilledSize.value + tradingEngineOrder.size.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 stageFilledSize.value = global.PRECISE(stageFilledSize.value, 10) + /* Balances Update */ - switch (order.side) { - case 'buy': { + switch (true) { + case tradingSystemOrder.type === 'Market Buy Order' || tradingSystemOrder.type === 'Limit Buy Order': { /* Undo the previous accounting */ tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value - previousFeesPaid - previousSizeFilled @@ -459,7 +460,7 @@ exports.newTradingExecution = function newTradingExecution(bot, logger, tradingE break } - case 'sell': { + case tradingSystemOrder.type === 'Market Sell Order' || tradingSystemOrder.type === 'Limit Sell Order': { /* Undo the previous accounting */ tradingEngine.current.balance.baseAsset.value = tradingEngine.current.balance.baseAsset.value + previousFeesPaid + previousSizeFilled diff --git a/LowFrequencyTrading/TradingRecords.js b/LowFrequencyTrading/TradingRecords.js index d79347f2d..789d67feb 100644 --- a/LowFrequencyTrading/TradingRecords.js +++ b/LowFrequencyTrading/TradingRecords.js @@ -119,7 +119,14 @@ exports.newTradingRecords = function newTradingRecords(bot, logger) { the Root Node specifically for this property only. */ if (recordProperty.config.nodePath !== undefined) { - propertyRootNode = eval(recordProperty.config.nodePath) + try { + propertyRootNode = eval(recordProperty.config.nodePath) + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] appendRecords -> scanRecordDefinition -> Error at Record Property Definition -> recordProperty.name = " + recordProperty.name); + logger.write(MODULE_NAME, "[ERROR] appendRecords -> scanRecordDefinition -> Error at Record Property Definition -> recordProperty.config.nodePath = " + recordProperty.config.nodePath); + logger.write(MODULE_NAME, "[ERROR] appendRecords -> scanRecordDefinition -> Error at Record Property Definition -> err.stack = " + err.stack); + throw 'Can not continue with a Definition Error like this.' + } } /* The Target Node is the node from where we are going to exctract the value. diff --git a/server.js b/server.js index f2910b112..b9e30080f 100644 --- a/server.js +++ b/server.js @@ -47,12 +47,14 @@ global.PRECISE = function (floatNumber, precision) { process.on('uncaughtException', function (err) { console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack) + console.log(err.stack) global.EXIT_NODE_PROCESS() }) process.on('unhandledRejection', (reason, p) => { console.log('[ERROR] Task Server -> server -> unhandledRejection -> reason = ' + JSON.stringify(reason)) console.log('[ERROR] Task Server -> server -> unhandledRejection -> p = ' + JSON.stringify(p)) + console.log(reason.stack) global.EXIT_NODE_PROCESS() }) From cc17a4d65bf0085da650df7249c6e743fe508c55 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 23 Jul 2020 09:43:46 +0200 Subject: [PATCH 111/113] More fixes --- LowFrequencyTrading/TradingPosition.js | 12 ++---------- LowFrequencyTrading/TradingStrategy.js | 1 + 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/LowFrequencyTrading/TradingPosition.js b/LowFrequencyTrading/TradingPosition.js index 33e8917f3..8e38d100d 100644 --- a/LowFrequencyTrading/TradingPosition.js +++ b/LowFrequencyTrading/TradingPosition.js @@ -16,7 +16,6 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng updateStopLoss: updateStopLoss, updateTakeProfit: updateTakeProfit, updateSizeAndRate: updateSizeAndRate, - applySlippageToRate: applySlippageToRate, updateEnds: updateEnds, updateStatus: updateStatus, updateCounters: updateCounters, @@ -44,6 +43,7 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng function openPosition(situationName) { tradingEngine.current.position.status.value = 'Open' tradingEngine.current.position.serialNumber.value = tradingEngine.episode.episodeCounters.positions.value + tradingEngine.current.position.identifier.value = global.UNIQUE_ID() tradingEngine.current.position.begin.value = tradingEngine.current.candle.begin.value tradingEngine.current.position.end.value = tradingEngine.current.candle.end.value @@ -94,16 +94,8 @@ exports.newTradingPosition = function newTradingPosition(bot, logger, tradingEng function updateSizeAndRate(size, rate) { tradingEngine.current.position.size.value = global.PRECISE(size, 10) tradingEngine.current.position.rate.value = global.PRECISE(rate, 10) - } - function applySlippageToRate() { - let slippageAmount = tradingEngine.current.position.rate.value * bot.SESSION.parameters.slippage.config.positionRate / 100 - - if (bot.sessionAndMarketBaseAssetsAreEqual) { - tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value - slippageAmount - } else { - tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value + slippageAmount - } + tradingEngine.current.position.beginRate.value = tradingEngine.current.position.rate.value } function preventStopLossDistortion() { diff --git a/LowFrequencyTrading/TradingStrategy.js b/LowFrequencyTrading/TradingStrategy.js index 422a7a82c..adcf0ca71 100644 --- a/LowFrequencyTrading/TradingStrategy.js +++ b/LowFrequencyTrading/TradingStrategy.js @@ -30,6 +30,7 @@ exports.newTradingStrategy = function newTradingStrategy(bot, logger, tradingEng function openStrategy(index, situationName, strategyName) { tradingEngine.current.strategy.status.value = 'Open' tradingEngine.current.strategy.serialNumber.value = tradingEngine.episode.episodeCounters.strategies.value + tradingEngine.current.strategy.identifier.value = global.UNIQUE_ID() tradingEngine.current.strategy.begin.value = tradingEngine.current.candle.begin.value tradingEngine.current.strategy.end.value = tradingEngine.current.candle.end.value tradingEngine.current.strategy.beginRate.value = tradingEngine.current.candle.min.value From 09c114e2ef182306d0aa7845d8c0d028e5ae463b Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 24 Jul 2020 07:51:46 +0200 Subject: [PATCH 112/113] Code Mantainance --- MultiPeriodDaily.js | 937 +++++++++++++++---------------------------- MultiPeriodMarket.js | 388 +++++------------- 2 files changed, 441 insertions(+), 884 deletions(-) diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index 66e2f586d..2d5e9741d 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -1,7 +1,5 @@ exports.newMultiPeriodDaily = function newMultiPeriodDaily(bot, logger, UTILITIES, FILE_STORAGE) { - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; const MODULE_NAME = "Multi Period Daily"; const GMT_SECONDS = ':00.000 GMT+0000'; @@ -11,16 +9,14 @@ start: start }; - let utilities = UTILITIES.newCloudUtilities(logger); + let utilities = UTILITIES.newCloudUtilities(logger) + let fileStorage = FILE_STORAGE.newFileStorage(logger) let statusDependencies; let dataDependenciesModule; let dataFiles = new Map; - let indicatorOutputModule; - let fileStorage = FILE_STORAGE.newFileStorage(logger); - let bootstrappingTheProcess = false let processConfig; @@ -29,27 +25,17 @@ return thisObject; function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { + logger.fileName = MODULE_NAME; + logger.initialize() - try { + statusDependencies = pStatusDependencies; + dataDependenciesModule = pDataDependencies; + processConfig = pProcessConfig; - logger.fileName = MODULE_NAME; - logger.initialize(); + let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - statusDependencies = pStatusDependencies; - dataDependenciesModule = pDataDependencies; - processConfig = pProcessConfig; - - let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - - indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE); - indicatorOutputModule.initialize(callBackFunction); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) + indicatorOutputModule.initialize(callBackFunction) } function finalize() { @@ -63,15 +49,10 @@ } function start(callBackFunction) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } - let market = bot.market; /* Context Variables */ - let contextVariables = { lastFile: undefined, // Datetime of the last file files sucessfully produced by this process. dateBeginOfMarket: undefined, // Datetime of the first trade file in the whole market history. @@ -79,724 +60,468 @@ }; let previousDay; // Holds the date of the previous day relative to the processing date. - let interExecutionMemoryArray; - getContextVariables(); + getContextVariables() function getContextVariables() { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Entering function."); } - let thisReport; let reportKey; let statusReport; - /* - We look first for the bot who knows the begining of the market in order to get when the market starts. + We look first for the bot who knows the begining of the market in order to get when the market starts. */ - statusReport = statusDependencies.reportsByMainUtility.get("Market Starting Point") if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Starting Point -> Status Report does not exist or Market Starting Point not defined. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Starting Point -> Status Report does not exist or Market Starting Point not defined. Retrying Later. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } thisReport = statusReport.file; if (thisReport.beginingOfMarket === undefined) { - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey); - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> It is too early too run this process since the trade history of the market is not there yet."); + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey) + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> It is too early too run this process since the trade history of the market is not there yet.") let customOK = { result: global.CUSTOM_OK_RESPONSE.result, message: "Dependency does not exist." } - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message); - callBackFunction(customOK); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message) + callBackFunction(customOK) + return } - contextVariables.dateBeginOfMarket = new Date(thisReport.beginingOfMarket.year + "-" + thisReport.beginingOfMarket.month + "-" + thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + GMT_SECONDS); - + contextVariables.dateBeginOfMarket = new Date(thisReport.beginingOfMarket.year + "-" + thisReport.beginingOfMarket.month + "-" + thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + GMT_SECONDS) /* - Here we get the status report from the bot who knows which is the end of the market. + Here we get the status report from the bot who knows which is the end of the market. */ - statusReport = statusDependencies.reportsByMainUtility.get("Market Ending Point") if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Ending Point -> Status Report does not exist or Market Ending Point not defined. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Market Ending Point -> Status Report does not exist or Market Ending Point not defined. Retrying Later. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } thisReport = statusReport.file; if (thisReport.lastFile === undefined) { - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey); + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey) let customOK = { result: global.CUSTOM_OK_RESPONSE.result, message: "Dependency not ready." } - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message); - callBackFunction(customOK); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> customOK = " + customOK.message) + callBackFunction(customOK) + return } - contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()); + contextVariables.dateEndOfMarket = new Date(thisReport.lastFile.valueOf()) /* Finally we get our own Status Report. */ - statusReport = statusDependencies.reportsByMainUtility.get("Self Reference") if (statusReport === undefined) { // This means the status report does not exist, that could happen for instance at the begining of a month. - logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Self Reference -> Status Report does not exist or Self Reference not defined. Retrying Later. "); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; + logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Self Reference -> Status Report does not exist or Self Reference not defined. Retrying Later. ") + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return } if (statusReport.status === "Status Report is corrupt.") { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because self dependecy Status Report is corrupt. Aborting Process."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because self dependecy Status Report is corrupt. Aborting Process.") + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return } thisReport = statusReport.file; if (thisReport.lastFile !== undefined) { - beginingOfMarket = new Date(thisReport.beginingOfMarket); + beginingOfMarket = new Date(thisReport.beginingOfMarket) if (beginingOfMarket.valueOf() !== contextVariables.dateBeginOfMarket.valueOf()) { // Reset Mechanism for Begining of the Market - logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Reset Mechanism for Begining of the Market Activated. -> reportKey = " + reportKey); + logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Reset Mechanism for Begining of the Market Activated. -> reportKey = " + reportKey) - beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()); - startFromBegining(); - return; + beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()) + startFromBegining() + return } if (bot.hasTheBotJustStarted === true && processConfig.framework.startDate !== undefined && processConfig.framework.startDate.resumeExecution === false) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining because bot has just started and resume execution was true."); } - startFromBegining(); - return; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining because bot has just started and resume execution was true.") } + startFromBegining() + return } - contextVariables.lastFile = new Date(thisReport.lastFile); + contextVariables.lastFile = new Date(thisReport.lastFile) interExecutionMemoryArray = thisReport.interExecutionMemoryArray; - processTimeFrames(); - return; - + processTimeFrames() } else { /* In the case when there is no status report, we assume like the last processed file is the one on the date of Begining of Market. */ - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining of the market because own status report not found or lastFile was undefined."); } - beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()); - startFromBegining(); - return; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> Starting from the begining of the market because own status report not found or lastFile was undefined.") } + beginingOfMarket = new Date(contextVariables.dateBeginOfMarket.valueOf()) + startFromBegining() } function startFromBegining() { + contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS) - contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + GMT_SECONDS); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile); } - + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile) } /* The first time the process is running is the right time to create the data structure that is going to be shared across different executions. This data structure has one object per each timeFrame. */ - - interExecutionMemoryArray = []; + interExecutionMemoryArray = [] /* Also, we will remember that we are bootstrapping the process, this will allow us, to advanceTime if necesary. It can happen that a process depends on data from another process that does not produce a file during the first day, or even more days, in that situation we want the process not to just wait for that data that will never arrive, but to advance time until it finds valid data. */ - bootstrappingTheProcess = true for (let i = 0; i < global.dailyFilePeriods.length; i++) { let emptyObject = {}; - interExecutionMemoryArray.push(emptyObject); + interExecutionMemoryArray.push(emptyObject) } - processTimeFrames(); - + processTimeFrames() } } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack) if (err.message === "Cannot read property 'file' of undefined") { - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot configuration to see if all of its statusDependencies declarations are correct. "); - logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)); + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot configuration to see if all of its statusDependencies declarations are correct. ") + logger.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)) } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } function processTimeFrames() { + let n; + let botNeverRan = true; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> Entering function."); } + bot.multiPeriodDailyProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS) // Go back one day to start well when we advance time at the begining of the loop. + let fromDate = new Date(bot.multiPeriodDailyProcessDatetime.valueOf()) + let lastDate = new Date() - try { + advanceTime() - let n; - let botNeverRan = true; + function advanceTime() { + bot.multiPeriodDailyProcessDatetime = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS) + previousDay = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS) - bot.multiPeriodDailyProcessDatetime = new Date(contextVariables.lastFile.valueOf() - ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. - let fromDate = new Date(bot.multiPeriodDailyProcessDatetime.valueOf()) - let lastDate = new Date() + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> bot.multiPeriodDailyProcessDatetime = " + bot.multiPeriodDailyProcessDatetime) } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> previousDay = " + previousDay) } - advanceTime(); + /* Validation that we are not going past the head of the market. */ + if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf()) { - function advanceTime() { + const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> " + logText) } - try { + callBackFunction(global.DEFAULT_OK_RESPONSE) + return - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> Entering function."); } - - bot.multiPeriodDailyProcessDatetime = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); - previousDay = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> bot.multiPeriodDailyProcessDatetime = " + bot.multiPeriodDailyProcessDatetime); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> previousDay = " + previousDay); } - - /* Validation that we are not going past the head of the market. */ - - if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateEndOfMarket.valueOf()) { - - const logText = "Head of the market found @ " + previousDay.getUTCFullYear() + "/" + (previousDay.getUTCMonth() + 1) + "/" + previousDay.getUTCDate() + "."; - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> " + logText); } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - /* Telling the world we are alive and doing well */ - let currentDateString = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2); - let currentDate = new Date(bot.multiPeriodDailyProcessDatetime) - let percentage = global.getPercentage(fromDate, currentDate, lastDate) - bot.processHeartBeat(currentDateString, percentage) - - if (global.areEqualDates(currentDate, new Date()) === false) { - logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage); - } - - checkStopTaskGracefully(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> advanceTime -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } } - function checkStopTaskGracefully() { + /* Telling the world we are alive and doing well */ + let currentDateString = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2) + let currentDate = new Date(bot.multiPeriodDailyProcessDatetime) + let percentage = global.getPercentage(fromDate, currentDate, lastDate) + bot.processHeartBeat(currentDateString, percentage) - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFramesDailyFiles -> checkStopTaskGracefully -> Entering function."); } - - /* Validation that we dont need to stop. */ - - if (global.STOP_TASK_GRACEFULLY === true) { - - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - - } - - periodsLoop(); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFramesDailyFiles -> checkStopTaskGracefully -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + if (global.areEqualDates(currentDate, new Date()) === false) { + logger.newInternalLoop(bot.codeName, bot.process, currentDate, percentage) } - - function periodsLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoop -> Entering function."); } - - /* - - We will iterate through all posible periods. - - */ - - n = 0 // loop Variable representing each possible period as defined at the periods array. - - periodsLoopBody(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> Entering function."); } - - const timeFrame = global.dailyFilePeriods[n][0]; - const timeFrameLabel = global.dailyFilePeriods[n][1]; - - if (processConfig.framework.validPeriods !== undefined) { - let validPeriod = false; - for (let i = 0; i < processConfig.framework.validPeriods.length; i++) { - let period = processConfig.framework.validPeriods[i]; - if (period === timeFrameLabel) { validPeriod = true } - } - if (validPeriod === false) { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> Discarding period for not being listed as a valid period. -> timeFrameLabel = " + timeFrameLabel); } - periodsControlLoop(); - return; - } - } - - let dependencyIndex = 0; - dataFiles = new Map(); - - dependencyLoopBody(); - - function dependencyLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - - if (dependency === undefined) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> You need to add at least one Data Dependency to the process Multi Period Daily. Aborting process."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - let previousFile; - let currentFile; - - if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { - getPreviousFile(); - } else { - previousFile = []; - getCurrentFile() - } - - function getPreviousFile() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> Entering function."); } - - let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> text = " + text); } - - if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { - - /* - Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. - When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find - all the dependencies and the first run of the bot is successful. - - After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. - */ - logger.write(MODULE_NAME, "[WARN] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found."); - - advanceTime(); - return; - } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - previousFile = JSON.parse(text); - - getCurrentFile(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function getCurrentFile() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Entering function."); } - - let dateForPath = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2); - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - let fileName = "Data.json"; - - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> text = " + text); } - - if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { - - if (bootstrappingTheProcess === true) { - /* - In the special situation where we are running this process for the first time (no status report found), - we will consider the possibility that a depanant process does not produce a file during the first day of the market, - or even more than one day. That is why we are going to advance time here. - */ - advanceTime() - return - } - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.code); - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> filePath = " + filePath); - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> fileName = " + fileName); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Not OK -> onFileReceived -> err = " + err.code); - callBackFunction(err); - return; - } - - currentFile = JSON.parse(text); - - let dataFile = previousFile.concat(currentFile); - - dataFiles.set(dependency.id, dataFile); - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> Catch Error -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getCurrentFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> dependencyControlLoop -> Entering function."); } - - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - - dependencyLoopBody(); - - } else { - - generateOutput(); - - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function generateOutput() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> Entering function."); } - - const timeFrame = global.dailyFilePeriods[n][0]; - const timeFrameLabel = global.dailyFilePeriods[n][1]; - - indicatorOutputModule.start( - dataFiles, - timeFrame, - timeFrameLabel, - bot.multiPeriodDailyProcessDatetime, - interExecutionMemoryArray[n], - onOutputGenerated); - - function onOutputGenerated(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - callBackFunction(err); - return; - } - - botNeverRan = false; - periodsControlLoop(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsControlLoop -> Entering function."); } - - n++; - - if (n < global.dailyFilePeriods.length) { - - periodsLoopBody(); - - } else { - - n = 0; - - writeDataRanges(onWritten); - - function onWritten(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> controlLoop -> onWritten -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> controlLoop -> onWritten -> err = " + err.stack); - callBackFunction(err); - return; - } - - writeStatusReport(bot.multiPeriodDailyProcessDatetime, advanceTime); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> controlLoop -> onWritten -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - + checkStopTaskGracefully() } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + function checkStopTaskGracefully() { + /* Validation that we dont need to stop. */ + if (global.STOP_TASK_GRACEFULLY === true) { + callBackFunction(global.DEFAULT_OK_RESPONSE) + return + } + timeFramesLoop() + } + + function timeFramesLoop() { + /* + We will iterate through all posible timeFrames. + */ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody() + } + + function timeFramesLoopBody() { + const timeFrame = global.dailyFilePeriods[n][0] + const timeFrameLabel = global.dailyFilePeriods[n][1] + + if (processConfig.framework.validPeriods !== undefined) { + let validPeriod = false; + for (let i = 0; i < processConfig.framework.validPeriods.length; i++) { + let period = processConfig.framework.validPeriods[i] + if (period === timeFrameLabel) { validPeriod = true } + } + if (validPeriod === false) { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> timeFramesLoopBody -> Discarding period for not being listed as a valid period. -> timeFrameLabel = " + timeFrameLabel) } + timeFramesControlLoop() + return + } + } + let dependencyIndex = 0 + dataFiles = new Map() + + dependencyLoopBody() + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex] + if (dependency === undefined) { + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> You need to add at least one Data Dependency to the process Multi Period Daily. Aborting process.") + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return + } + + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex] + let previousFile + let currentFile + + if (bot.multiPeriodDailyProcessDatetime.valueOf() > contextVariables.dateBeginOfMarket.valueOf()) { + getPreviousFile() + } else { + previousFile = [] + getCurrentFile() + } + + function getPreviousFile() { + let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2) + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + let fileName = "Data.json" + + datasetModule.getTextFile(filePath, fileName, onFileReceived) + + function onFileReceived(err, text) { + if ((err.message === "File does not exist." && botNeverRan === true) || err.code === 'The specified key does not exist.') { + /* + Sometimes one of the dependencies of an indicator for some reasons are not calculated from the begining of the market. + When that happens we can not find those files. What we do in this situation is to move the time fordward until we can find + all the dependencies and the first run of the bot is successful. + + After that, we will not accept more missing files on any of the dependencies, and if any is missing we will abort the processing. + */ + logger.write(MODULE_NAME, "[WARN] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> Skipping day because file " + filePath + "/" + fileName + " was not found.") + + advanceTime() + return + } + + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack) + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getPreviousFile -> onFileReceived -> err = " + err.stack) + callBackFunction(err) + return + } + + previousFile = JSON.parse(text) + getCurrentFile() + } + + } + + function getCurrentFile() { + let dateForPath = bot.multiPeriodDailyProcessDatetime.getUTCFullYear() + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.multiPeriodDailyProcessDatetime.getUTCDate(), 2) + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Daily") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel + "/" + dateForPath; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + let fileName = "Data.json"; + + datasetModule.getTextFile(filePath, fileName, onFileReceived) + + function onFileReceived(err, text) { + if ((err.result === "Fail Because" && err.message === "File does not exist.") || err.code === 'The specified key does not exist.') { + if (bootstrappingTheProcess === true) { + /* + In the special situation where we are running this process for the first time (no status report found), + we will consider the possibility that a depanant process does not produce a file during the first day of the market, + or even more than one day. That is why we are going to advance time here. + */ + advanceTime() + return + } + + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> err = " + err.code) + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> filePath = " + filePath) + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> onFileReceived -> fileName = " + fileName) + callBackFunction(global.DEFAULT_RETRY_RESPONSE) + return + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getCurrentFile -> Not OK -> onFileReceived -> err = " + err.code) + callBackFunction(err) + return + } + + currentFile = JSON.parse(text) + let dataFile = previousFile.concat(currentFile) + dataFiles.set(dependency.id, dataFile) + dependencyControlLoop() + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody() + } else { + generateOutput() + } + + function generateOutput() { + const timeFrame = global.dailyFilePeriods[n][0] + const timeFrameLabel = global.dailyFilePeriods[n][1] + + indicatorOutputModule.start( + dataFiles, + timeFrame, + timeFrameLabel, + bot.multiPeriodDailyProcessDatetime, + interExecutionMemoryArray[n], + onOutputGenerated) + + function onOutputGenerated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err) + return + } + botNeverRan = false; + timeFramesControlLoop() + } + } + } + } + + function timeFramesControlLoop() { + n++ + if (n < global.dailyFilePeriods.length) { + timeFramesLoopBody() + } else { + n = 0; + writeDataRanges(onWritten) + + function onWritten(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> controlLoop -> onWritten -> err = " + err.stack) + callBackFunction(err) + return + } + writeStatusReport(bot.multiPeriodDailyProcessDatetime, advanceTime) + } + } } } function writeDataRanges(callBack) { + let outputDatasetIndex = -1; + controlLoop() - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRanges -> Entering function."); } - - let outputDatasetIndex = -1; - controlLoop(); - - function productLoopBody() { - let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; - writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodDailyProcessDatetime, productCodeName, controlLoop); - } - - function controlLoop() { - - outputDatasetIndex++; - - if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { - productLoopBody(); - } else { - callBack(global.DEFAULT_OK_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRanges -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); + function productLoopBody() { + let productCodeName = bot.processNode.referenceParent.processOutput.outputDatasets[outputDatasetIndex].referenceParent.parentNode.config.codeName; + writeDataRange(contextVariables.dateBeginOfMarket, bot.multiPeriodDailyProcessDatetime, productCodeName, controlLoop) } + function controlLoop() { + outputDatasetIndex++ + if (outputDatasetIndex < bot.processNode.referenceParent.processOutput.outputDatasets.length) { + productLoopBody() + } else { + callBack(global.DEFAULT_OK_RESPONSE) + } + } } function writeDataRange(pBegin, pEnd, productCodeName, callBack) { + let dataRange = { + begin: pBegin.valueOf(), + end: pEnd.valueOf() + }; + let fileContent = JSON.stringify(dataRange) + let fileName = '/Data.Range.json'; + let filePath = bot.filePathRoot + "/Output/" + productCodeName + "/" + bot.process + fileName; - try { + fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated) - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> Entering function."); } - - let dataRange = { - begin: pBegin.valueOf(), - end: pEnd.valueOf() - }; - - let fileContent = JSON.stringify(dataRange); - - let fileName = '/Data.Range.json'; - let filePath = bot.filePathRoot + "/Output/" + productCodeName + "/" + bot.process + fileName; - - fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated); - - function onFileCreated(err) { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack); - callBack(err); - return; - } - - if (LOG_FILE_CONTENT === true) { - logger.write(MODULE_NAME, "[INFO] start -> writeDataRange -> onFileCreated -> Content written = " + fileContent); - } - - let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset - let event = { - dateRange: dataRange - } - - global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) - - callBack(global.DEFAULT_OK_RESPONSE); + function onFileCreated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> onFileCreated -> err = " + err.stack) + callBack(err) + return } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeDataRange -> err = " + err.stack); - callBack(global.DEFAULT_FAIL_RESPONSE); + let key = bot.dataMine + "-" + bot.codeName + "-" + productCodeName + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset + let event = { + dateRange: dataRange + } + + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Data Range Updated', event) + callBack(global.DEFAULT_OK_RESPONSE) } } function writeStatusReport(lastFileDate, callBack) { + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Daily" + let thisReport = statusDependencies.statusReports.get(reportKey) - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> Entering function."); } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> lastFileDate = " + lastFileDate); } + thisReport.file.lastExecution = bot.currentDaytime; + thisReport.file.lastFile = lastFileDate; + thisReport.file.interExecutionMemoryArray = interExecutionMemoryArray; + thisReport.file.beginingOfMarket = beginingOfMarket.toUTCString() + thisReport.save(callBack) - try { - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Daily" - let thisReport = statusDependencies.statusReports.get(reportKey); - - thisReport.file.lastExecution = bot.currentDaytime; - thisReport.file.lastFile = lastFileDate; - thisReport.file.interExecutionMemoryArray = interExecutionMemoryArray; - thisReport.file.beginingOfMarket = beginingOfMarket.toUTCString() - thisReport.save(callBack); - - bot.hasTheBotJustStarted = false; - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + bot.hasTheBotJustStarted = false; } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } - - function pad(str, max) { - str = str.toString(); - return str.length < max ? pad(" " + str, max) : str; - } }; diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js index a0b4d41ed..85002e5eb 100644 --- a/MultiPeriodMarket.js +++ b/MultiPeriodMarket.js @@ -1,49 +1,33 @@ exports.newMultiPeriodMarket = function newMultiPeriodMarket(bot, logger, UTILITIES, FILE_STORAGE) { - - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; const MODULE_NAME = "Multi Period Market"; - thisObject = { initialize: initialize, finalize: finalize, start: start }; - let utilities = UTILITIES.newCloudUtilities(logger); + let utilities = UTILITIES.newCloudUtilities(logger) let statusDependencies; let dataDependenciesModule; - let dataFiles = new Map(); - + let dataFiles = new Map() let indicatorOutputModule; - let processConfig; return thisObject; function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) { + logger.fileName = MODULE_NAME; + logger.initialize() - try { + statusDependencies = pStatusDependencies; + dataDependenciesModule = pDataDependencies; + processConfig = pProcessConfig; - logger.fileName = MODULE_NAME; - logger.initialize(); + let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - - statusDependencies = pStatusDependencies; - dataDependenciesModule = pDataDependencies; - processConfig = pProcessConfig; - - let INDICATOR_OUTPUT_MODULE = require("./IndicatorOutput") - - indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE); - indicatorOutputModule.initialize(callBackFunction); - - } catch (err) { - logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + indicatorOutputModule = INDICATOR_OUTPUT_MODULE.newIndicatorOutput(bot, logger, UTILITIES, FILE_STORAGE) + indicatorOutputModule.initialize(callBackFunction) } function finalize() { @@ -56,272 +40,120 @@ } function start(callBackFunction) { - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } - - let market = bot.market; - - /* Context Variables */ - - let contextVariables = { - dateBeginOfMarket: undefined, // Datetime of the first trade file in the whole market history. - dateEndOfMarket: undefined // Datetime of the last file available to be used as an input of this process. - }; - - processTimeFrames(); + processTimeFrames() function processTimeFrames() { + let n; + timeFramesLoop() - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> Entering function."); } - - try { - - let n; - - periodsLoop(); - - function periodsLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoop -> Entering function."); } - - /* - - We will iterate through all posible periods. - - */ - - n = 0 // loop Variable representing each possible period as defined at the periods array. - - periodsLoopBody(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> Entering function."); } - - const timeFrame = global.marketFilesPeriods[n][0]; - const timeFrameLabel = global.marketFilesPeriods[n][1]; - - let dependencyIndex = 0; - dataFiles = new Map; - - dependencyLoopBody(); - - function dependencyLoopBody() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> Entering function."); } - - let dependency = dataDependenciesModule.nodeArray[dependencyIndex]; - let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex]; - - getFile(); - - function getFile() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> Entering function."); } - - let dateForPath = bot.processDatetime.getUTCFullYear() + '/' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.processDatetime.getUTCDate(), 2); - let fileName = "Data.json"; - - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Market") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } - datasetModule.getTextFile(filePath, fileName, onFileReceived); - - function onFileReceived(err, text) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> Entering function."); } - if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> text = " + text); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(err); - return; - } - - let dataFile = JSON.parse(text); - dataFiles.set(dependency.id, dataFile); - - dependencyControlLoop(); - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> dependencyLoopBody -> getFile -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoop -> dependencyLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function dependencyControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> dependencyControlLoop -> Entering function."); } - - dependencyIndex++; - - if (dependencyIndex < dataDependenciesModule.nodeArray.length) { - - dependencyLoopBody(); - - } else { - - generateOutput(); - - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> dependencyControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function generateOutput() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> Entering function."); } - - const timeFrame = global.marketFilesPeriods[n][0]; - const timeFrameLabel = global.marketFilesPeriods[n][1]; - - indicatorOutputModule.start( - dataFiles, - timeFrame, - timeFrameLabel, - undefined, - undefined, - onOutputGenerated); - - function onOutputGenerated(err) { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - callBackFunction(err); - return; - } - - periodsControlLoop(); - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> onOutputGenerated -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> generateOutput -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsLoopBody -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function periodsControlLoop() { - - try { - - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> periodsControlLoop -> Entering function."); } - - n++; - - if (n < global.marketFilesPeriods.length) { - - periodsLoopBody(); - - } else { - - writeStatusReport(callBackFunction); - - } - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> periodsControlLoop -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - + function timeFramesLoop() { + /* We will iterate through all posible timeFrames.*/ + n = 0 // loop Variable representing each possible period as defined at the timeFrames array. + timeFramesLoopBody() } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + function timeFramesLoopBody() { + const timeFrame = global.marketFilesPeriods[n][0] + const timeFrameLabel = global.marketFilesPeriods[n][1] + + let dependencyIndex = 0; + dataFiles = new Map; + + dependencyLoopBody() + + function dependencyLoopBody() { + let dependency = dataDependenciesModule.nodeArray[dependencyIndex] + let datasetModule = dataDependenciesModule.dataSetsModulesArray[dependencyIndex] + + getFile() + + function getFile() { + let dateForPath = bot.processDatetime.getUTCFullYear() + '/' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.processDatetime.getUTCDate(), 2) + let fileName = "Data.json"; + + let filePath + if (dependency.referenceParent.config.codeName === "Multi-Period-Market") { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; + } else { + filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; + } + datasetModule.getTextFile(filePath, fileName, onFileReceived) + + function onFileReceived(err, text) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write(MODULE_NAME, "[ERROR] start -> processTimeFrames -> timeFramesLoopBody -> dependencyLoopBody -> getFile -> onFileReceived -> err = " + err.stack) + callBackFunction(err) + return; + } + + let dataFile = JSON.parse(text) + dataFiles.set(dependency.id, dataFile) + + dependencyControlLoop() + } + } + } + + function dependencyControlLoop() { + dependencyIndex++; + if (dependencyIndex < dataDependenciesModule.nodeArray.length) { + dependencyLoopBody() + } else { + generateOutput() + } + + function generateOutput() { + indicatorOutputModule.start( + dataFiles, + timeFrame, + timeFrameLabel, + undefined, + undefined, + onOutputGenerated) + + function onOutputGenerated(err) { + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + callBackFunction(err) + return; + } + timeFramesControlLoop() + } + } + } + } + + function timeFramesControlLoop() { + n++; + if (n < global.marketFilesPeriods.length) { + timeFramesLoopBody() + } else { + writeStatusReport(callBackFunction) + } } } function writeStatusReport(callBack) { + let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Market" + let thisReport = statusDependencies.statusReports.get(reportKey) - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStatusReport -> Entering function."); } + thisReport.file.lastExecution = bot.processDatetime; + thisReport.save(callBack) - try { - - let reportKey = bot.dataMine + "-" + bot.codeName + "-" + "Multi-Period-Market" - let thisReport = statusDependencies.statusReports.get(reportKey); - - thisReport.file.lastExecution = bot.processDatetime; - thisReport.save(callBack); - - if (global.areEqualDates(bot.processDatetime, new Date()) === false) { - logger.newInternalLoop(bot.codeName, bot.process, bot.processDatetime); - } - - /* Telling the world we are alive and doing well */ - let currentDateString = bot.processDatetime.getUTCFullYear() + '-' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.processDatetime.getUTCDate(), 2); - let currentDate = new Date(bot.processDatetime) - let lastDate = new Date() - bot.processHeartBeat(currentDateString, global.getPercentage(currentDate, currentDate, lastDate)) - } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + if (global.areEqualDates(bot.processDatetime, new Date()) === false) { + logger.newInternalLoop(bot.codeName, bot.process, bot.processDatetime) } + + /* Telling the world we are alive and doing well */ + let currentDateString = bot.processDatetime.getUTCFullYear() + '-' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '-' + utilities.pad(bot.processDatetime.getUTCDate(), 2) + let currentDate = new Date(bot.processDatetime) + let lastDate = new Date() + bot.processHeartBeat(currentDateString, global.getPercentage(currentDate, currentDate, lastDate)) } } - catch (err) { - logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } -}; +} From 3d87922c685ea4b5f989f84a60cfc6cdb6ad8d1a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 24 Jul 2020 11:02:00 +0200 Subject: [PATCH 113/113] Time Frame Filter at Multi Period Market --- MultiPeriodMarket.js | 21 +++++++++++++-------- ProcessOutput.js | 5 +++-- Root.js | 6 ++++++ StatusReport.js | 2 +- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js index 85002e5eb..3e29f55f7 100644 --- a/MultiPeriodMarket.js +++ b/MultiPeriodMarket.js @@ -57,6 +57,15 @@ const timeFrame = global.marketFilesPeriods[n][0] const timeFrameLabel = global.marketFilesPeriods[n][1] + /* Check Time Frames Filter */ + if (bot.marketTimeFrames !== undefined) { + if (bot.marketTimeFrames.includes(timeFrameLabel) === false) { + /* We are not going to process this Time Frame */ + timeFramesControlLoop() + return + } + } + let dependencyIndex = 0; dataFiles = new Map; @@ -69,15 +78,8 @@ getFile() function getFile() { - let dateForPath = bot.processDatetime.getUTCFullYear() + '/' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.processDatetime.getUTCDate(), 2) let fileName = "Data.json"; - - let filePath - if (dependency.referenceParent.config.codeName === "Multi-Period-Market") { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; - } else { - filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + dateForPath; - } + let filePath = dependency.referenceParent.parentNode.config.codeName + '/' + dependency.referenceParent.config.codeName + "/" + timeFrameLabel; datasetModule.getTextFile(filePath, fileName, onFileReceived) function onFileReceived(err, text) { @@ -138,6 +140,9 @@ let thisReport = statusDependencies.statusReports.get(reportKey) thisReport.file.lastExecution = bot.processDatetime; + if (bot.marketTimeFrames !== undefined) { + thisReport.file.timeFrames = bot.marketTimeFrames + } thisReport.save(callBack) if (global.areEqualDates(bot.processDatetime, new Date()) === false) { diff --git a/ProcessOutput.js b/ProcessOutput.js index 2130a7cf6..f9cb330d1 100644 --- a/ProcessOutput.js +++ b/ProcessOutput.js @@ -14,7 +14,7 @@ return thisObject; - function raiseEvents(lastFile, callBackFunction) { + function raiseEvents(lastFile, timeFrames, callBackFunction) { try { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] raiseEvents -> Entering function."); } @@ -83,7 +83,8 @@ key = dataMine + "-" + botName + "-" + product + "-" + dataset + "-" + bot.exchange + "-" + bot.market.baseAsset + '/' + bot.market.quotedAsset let event = { - lastFile: lastFile + lastFile: lastFile, + timeFrames: timeFrames } global.EVENT_SERVER_CLIENT.createEventHandler(key, 'Dataset Updated') global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Dataset Updated', event) diff --git a/Root.js b/Root.js index 5349465ce..06b1fe77b 100644 --- a/Root.js +++ b/Root.js @@ -225,6 +225,12 @@ /* Bot Type */ botConfig.type = global.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.type + /* Time Frame Filter */ + if (global.TASK_NODE.bot.timeFramesFilter !== undefined) { + botConfig.dailyTimeFrames = global.TASK_NODE.bot.timeFramesFilter.config.dailyTimeFrames + botConfig.marketTimeFrames = global.TASK_NODE.bot.timeFramesFilter.config.marketTimeFrames + } + if (botConfig.processNode) { if (botConfig.processNode.marketReference) { if (botConfig.processNode.marketReference.keyReference !== undefined) { diff --git a/StatusReport.js b/StatusReport.js index ff40b4e74..4da076767 100644 --- a/StatusReport.js +++ b/StatusReport.js @@ -334,7 +334,7 @@ let processOutput = PROCESS_OUTPUT.newProcessOutput(bot, logger) - processOutput.raiseEvents(thisObject.file.lastFile, callBackFunction); + processOutput.raiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames, callBackFunction); return; }