From 1b58955a177ffe6d6ca10c896bdcd703e855b518 Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 24 Jun 2019 20:52:38 -0300 Subject: [PATCH 1/6] Adding template fixes --- src/schema/queries/FileContent.js | 3 + src/storage/templates/Simulator/Commons.js | 111 +++++++++--------- .../templates/Simulator/User.Bot.Daily.js | 1 + .../templates/Simulator/User.Bot.Market.js | 1 + .../templates/Simulator/this.bot.config.json | 12 +- 5 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 7c6e5e532..348714f25 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -22,6 +22,9 @@ const resolve = async (parent, { file }, context) => { return fileContent } catch (err) { + if(err.message === 'The specified key does not exist.') + logger.warn('getFileContent -> Error: %s', err.message) + else logger.error('getFileContent -> Error: %s', err.message) throw err } diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index d0f98ae4b..fd0fc4237 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -73,7 +73,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Initial Default Values */ - let initialDate = startDate; + let initialDate = startDate; const DEFAULT_BASE_ASSET_BALANCE = 1 const DEFAULT_BASE_ASSET_MINIMUN_BALANCE = 0.5 @@ -92,7 +92,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (tradingSystem.parameters !== undefined) { if (tradingSystem.parameters.baseAsset !== undefined) { if (tradingSystem.parameters.baseAsset.formula !== undefined) { - let receivedParameters + let receivedParameters try { receivedParameters = JSON.parse(tradingSystem.parameters.baseAsset.formula.code); @@ -159,7 +159,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Strategy and Phases */ let currentStrategyIndex = -1; - let strategyStage = 'No Stage'; + let strategyStage = 'No Stage'; /* Stop Loss Management */ @@ -167,7 +167,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER let stopLoss = 0; let stopLossPhase = 0; - let stopLossStage = 'No Stage'; + let stopLossStage = 'No Stage'; /* Take Profit Management */ @@ -176,7 +176,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let previousTakeProfit = 0; let takeProfit = 0; let takeProfitPhase = 0; - let takeProfitStage = 'No Stage'; + let takeProfitStage = 'No Stage'; /* Simulation Records */ @@ -309,8 +309,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; - } - + } + lastProfit = interExecutionMemory.lastProfit; profit = interExecutionMemory.profit; lastProfitPercent = interExecutionMemory.lastProfitPercent; @@ -330,9 +330,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.hitRatio = hitRatio; yesterday.ROI = ROI; yesterday.anualizedRateOfReturn = anualizedRateOfReturn; - + } - + /* Main Simulation Loop: We go thourgh all the candles at this time period. */ for (let i = 0; i < candles.length; i++) { @@ -490,7 +490,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (phase.formula !== undefined) { try { - formulaValue = eval(phase.formula.code); + formulaValue = eval(phase.formula.code); if (formulaValue === Infinity) { formulaError= "Formula evaluates to Infinity." formulaValue = MAX_STOP_LOSS_VALUE @@ -502,12 +502,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (formulaValue < MIN_STOP_LOSS_VALUE) { formulaValue = MIN_STOP_LOSS_VALUE } - } - formulasErrors.push('"' + formulaError + '"') - formulasValues.push( formulaValue) - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p; - formulas.set(key, formulaValue) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -555,12 +555,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (formulaValue < MIN_TAKE_PROFIT_VALUE) { formulaValue = MIN_TAKE_PROFIT_VALUE } - } - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p; - formulas.set(key, formulaValue) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -614,12 +614,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (formulaValue < MIN_STOP_LOSS_VALUE) { formulaValue = MIN_STOP_LOSS_VALUE } - } - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p; - formulas.set(key, formulaValue) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -667,12 +667,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (formulaValue < MIN_TAKE_PROFIT_VALUE) { formulaValue = MIN_TAKE_PROFIT_VALUE } - } - formulasErrors.push('"' + formulaError + '"') - formulasValues.push(formulaValue) - let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p; - formulas.set(key, formulaValue) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -702,7 +702,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition; let error = '' - let value + let value try { value = eval(node.code); @@ -734,11 +734,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if ( strategyStage === 'No Stage' && - currentStrategyIndex === -1 + currentStrategyIndex === -1 ) { let minimunBalance let maximunBalance - let balance + let balance if (baseAsset === 'BTC') { balance = balanceAssetA @@ -756,12 +756,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* 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++) { @@ -869,7 +869,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { balanceAssetB = balanceAssetB + balanceAssetA * stopLoss; balanceAssetA = 0; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -904,7 +904,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { balanceAssetB = balanceAssetB + balanceAssetA * takeProfit; balanceAssetA = 0; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -920,7 +920,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { stopLossStage = 'No Stage'; takeProfitStage = 'No Stage'; currentTrade.end = candle.end; - currentTrade.status = 1; + currentTrade.status = 1; currentTrade.exitType = 2; currentTrade.endRate = takeProfit; @@ -995,7 +995,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let openStage = strategy.openStage let manageStage = strategy.manageStage - let parentNode + let parentNode let j = currentStrategyIndex let stageKey let initialDefinitionKey = '' @@ -1008,7 +1008,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { initialDefinitionKey = '-' + 'initialDefinition' stageKey = 'openStage' p = stopLossPhase - 1 - } + } } } @@ -1017,7 +1017,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { parentNode = manageStage stageKey = 'manageStage' p = stopLossPhase - 2 - } + } } let phase = parentNode.stopLoss.phases[p]; @@ -1056,7 +1056,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let strategy = tradingSystem.strategies[currentStrategyIndex]; let openStage = strategy.openStage let manageStage = strategy.manageStage - let phase + let phase let key if (stopLossStage === 'Open Stage' && openStage !== undefined) { @@ -1192,11 +1192,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* positionSize default is the whole balance if no size was defined by the user */ if (baseAsset === 'BTC') { - positionSize = balanceAssetA; + positionSize = balanceAssetA; } else { - positionSize = balanceAssetB; - } - + positionSize = balanceAssetB; + } + /* Check if the user defined a position size */ let strategy = tradingSystem.strategies[currentStrategyIndex]; let triggerStage = strategy.triggerStage @@ -1205,7 +1205,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (triggerStage.positionSize !== undefined) { if (triggerStage.positionSize.formula !== undefined) { try { - positionSize = eval(triggerStage.positionSize.formula.code); + positionSize = eval(triggerStage.positionSize.formula.code); } catch (err) { triggerStage.positionSize.formula.error = err.message } @@ -1214,7 +1214,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { positionSize = balanceAssetA; } else { positionSize = balanceAssetB; - } + } } } } @@ -1238,7 +1238,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { balanceAssetA = balanceAssetA + positionSize / marketRate; balanceAssetB = balanceAssetB - positionSize; - } + } positionInstant = candle.end; @@ -1265,7 +1265,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { yesterday.Roundtrips++; - } + } } if (baseAsset === 'BTC') { @@ -1278,7 +1278,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { lastProfitPercent = lastProfit / previousBalanceAssetB * 100; if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } profit = balanceAssetB - initialBalanceB; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -1290,7 +1290,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { currentTrade.lastProfitPercent = lastProfitPercent; currentTrade.stopRate = stopLoss; - + if (lastProfit > 0) { hits++; @@ -1318,7 +1318,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { ROI = (initialBalanceB + profit) / initialBalanceB - 1; hitRatio = hits / roundtrips; anualizedRateOfReturn = ROI / days * 365; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -1421,7 +1421,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { "") } - let strategyStageNumber + let strategyStageNumber switch (strategyStage) { case 'No Stage': { strategyStageNumber = 0 @@ -1837,6 +1837,3 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } }; - - - diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 7f2baca56..9bd5892a0 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -401,6 +401,7 @@ at the current date. */ if (record.end < currentDay.valueOf()) { continue; } + if (record.stopRate === undefined) { record.stopRate = 0} fileContent = fileContent + separator + '[' + record.begin + "," + diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index 52658e30e..8c976caed 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -374,6 +374,7 @@ for (let i = 0; i < tradesArray.length; i++) { let record = tradesArray[i]; + if (record.stopRate === undefined) { record.stopRate = 0 } fileContent = fileContent + separator + '[' + record.begin + "," + diff --git a/src/storage/templates/Simulator/this.bot.config.json b/src/storage/templates/Simulator/this.bot.config.json index b6e0d1b8f..d0d8572de 100644 --- a/src/storage/templates/Simulator/this.bot.config.json +++ b/src/storage/templates/Simulator/this.bot.config.json @@ -264,8 +264,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "TradingSimulation" - }, - "relatedToTradingEngine": true + } }, { "codeName": "Simulation-Conditions", @@ -304,8 +303,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "Conditions" - }, - "relatedToTradingEngine": true + } }, { "codeName": "Simulation-Strategies", @@ -344,8 +342,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "Strategies" - }, - "relatedToTradingEngine": true + } }, { "codeName": "Simulation-Trades", @@ -384,8 +381,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "Trades" - }, - "relatedToTradingEngine": true + } } ] } From dbf137315b538079c69fed2bc55dee58c043752d Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 29 Jun 2019 15:01:17 -0300 Subject: [PATCH 2/6] Adding container to log --- src/schema/queries/FileContent.js | 2 +- src/schema/queries/PlotterCode.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 348714f25..ec30b555f 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -7,7 +7,7 @@ import { getFileContentRemote } from '../../storage/providers/AzureStorage' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('getFileContent -> Entering Function: ' + file.filePath) + logger.debug('getFileContent -> Entering Function: ' + file.container + '/' + file.filePath) try { diff --git a/src/schema/queries/PlotterCode.js b/src/schema/queries/PlotterCode.js index b16afd5c6..6008bd919 100644 --- a/src/schema/queries/PlotterCode.js +++ b/src/schema/queries/PlotterCode.js @@ -7,7 +7,7 @@ import { getFileContentRemote } from '../../storage/providers/AzureStorage' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('getPlotterCode -> Entering Function: ' + file.filePath) + logger.debug('getPlotterCode -> Entering Function: ' + file.container + '/' + file.filePath) try { From a1acacbe1c8aaa37020a5209d98608bf2db06dd1 Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 29 Jun 2019 15:01:28 -0300 Subject: [PATCH 3/6] bot updates --- src/storage/templates/Simulator/Commons.js | 290 +++++++++++---------- 1 file changed, 157 insertions(+), 133 deletions(-) diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index fd0fc4237..f408aac7f 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -398,6 +398,14 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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 set some variables that would be nice if they have a value before the formulas are calculated. */ + + if (strategyStage === 'Trigger Stage' || strategyStage === 'No Stage') { + + positionRate = candle.close; + + } + /* We define and evaluate all conditions to be used later during the simulation loop. */ conditionsArrayRecord.push(candle.begin); @@ -405,7 +413,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { evaluateConditionsAndFormulas(tradingSystem, conditions); - function evaluateConditionsAndFormulas(tradingSystem, conditions) { + function evaluateConditionsAndFormulas(tradingSystem, conditions) { for (let j = 0; j < tradingSystem.strategies.length; j++) { @@ -731,7 +739,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } - if ( strategyStage === 'No Stage' && currentStrategyIndex === -1 @@ -849,88 +856,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - /* Checking if Stop or Take Profit were hit */ - - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' - ) { - - /* 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 ((baseAsset === 'BTC' && candle.max >= stopLoss) || (baseAsset !== 'BTC' && candle.min <= stopLoss)) { - - if (baseAsset === 'BTC') { - balanceAssetA = balanceAssetA + balanceAssetB / stopLoss; - balanceAssetB = 0; - } else { - balanceAssetB = balanceAssetB + balanceAssetA * stopLoss; - balanceAssetA = 0; - } - - if (currentDay !== undefined) { - if (positionInstant < currentDay.valueOf()) { - yesterday.balanceAssetA = balanceAssetA; - yesterday.balanceAssetB = balanceAssetB; - } - } - - marketRate = stopLoss; - type = '"Buy@StopLoss"'; - strategyStage = 'Close Stage'; - stopLossStage = 'No Stage'; - takeProfitStage = 'No Stage'; - currentTrade.end = candle.end; - currentTrade.status = 1; - currentTrade.exitType = 1; - currentTrade.endRate = stopLoss; - - currentStrategy.number = currentStrategyIndex - currentStrategy.end = candle.end; - currentStrategy.endRate = candle.min; - currentStrategy.status = 1; - } - - /* Take Profit condition: Here we verify if the Take Profit was filled or not. */ - - if ((baseAsset === 'BTC' && candle.min <= takeProfit) || (baseAsset !== 'BTC' && candle.max >= takeProfit)) { - - if (baseAsset === 'BTC') { - balanceAssetA = balanceAssetA + balanceAssetB / takeProfit; - balanceAssetB = 0; - } else { - balanceAssetB = balanceAssetB + balanceAssetA * takeProfit; - balanceAssetA = 0; - } - - if (currentDay !== undefined) { - if (positionInstant < currentDay.valueOf()) { - yesterday.balanceAssetA = balanceAssetA; - yesterday.balanceAssetB = balanceAssetB; - - } - } - - marketRate = takeProfit; - type = '"Buy@TakeProfit"'; - strategyStage = 'Close Stage'; - stopLossStage = 'No Stage'; - takeProfitStage = 'No Stage'; - currentTrade.end = candle.end; - currentTrade.status = 1; - currentTrade.exitType = 2; - currentTrade.endRate = takeProfit; - - currentStrategy.number = currentStrategyIndex - currentStrategy.end = candle.end; - currentStrategy.endRate = candle.min; - currentStrategy.status = 1; - } - } - /* Take Position Condition */ if (strategyStage === 'Trigger Stage') { @@ -1075,8 +1000,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - if (phase.formula !== undefined) { - stopLoss = formulas.get(key) + if (phase !== undefined) { + if (phase.formula !== undefined) { + stopLoss = formulas.get(key) + } } } @@ -1178,8 +1105,92 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - if (phase.formula !== undefined) { - takeProfit = formulas.get(key) + if (phase !== undefined) { + if (phase.formula !== undefined) { + takeProfit = formulas.get(key) + } + } + } + + /* Checking if Stop or Take Profit were hit */ + + if ( + strategyStage === 'Open Stage' || + strategyStage === 'Manage Stage' + ) { + + /* 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 ((baseAsset === 'BTC' && candle.max >= stopLoss) || (baseAsset !== 'BTC' && candle.min <= stopLoss)) { + + if (baseAsset === 'BTC') { + balanceAssetA = balanceAssetA + balanceAssetB / stopLoss; + balanceAssetB = 0; + } else { + balanceAssetB = balanceAssetB + balanceAssetA * stopLoss; + balanceAssetA = 0; + } + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + } + } + + marketRate = stopLoss; + type = '"Buy@StopLoss"'; + strategyStage = 'Close Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; + currentTrade.end = candle.end; + currentTrade.status = 1; + currentTrade.exitType = 1; + currentTrade.endRate = stopLoss; + + currentStrategy.number = currentStrategyIndex + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + } + + /* Take Profit condition: Here we verify if the Take Profit was filled or not. */ + + if ((baseAsset === 'BTC' && candle.min <= takeProfit) || (baseAsset !== 'BTC' && candle.max >= takeProfit)) { + + if (baseAsset === 'BTC') { + balanceAssetA = balanceAssetA + balanceAssetB / takeProfit; + balanceAssetB = 0; + } else { + balanceAssetB = balanceAssetB + balanceAssetA * takeProfit; + balanceAssetA = 0; + } + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + } + } + + marketRate = takeProfit; + type = '"Buy@TakeProfit"'; + strategyStage = 'Close Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; + currentTrade.end = candle.end; + currentTrade.status = 1; + currentTrade.exitType = 2; + currentTrade.endRate = takeProfit; + + currentStrategy.number = currentStrategyIndex + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; } } @@ -1215,13 +1226,18 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { positionSize = balanceAssetB; } + } else { + if (baseAsset === 'BTC') { + if (positionSize > balanceAssetA) { positionSize = balanceAssetA} + } else { + if (positionSize > balanceAssetB) { positionSize = balanceAssetB} + } } } } } marketRate = candle.close; - positionRate = marketRate; calculateStopLoss(); calculateTakeProfit(); @@ -1233,10 +1249,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { lastProfitPercent = 0; if (baseAsset === 'BTC') { - balanceAssetB = balanceAssetB + positionSize * marketRate; + balanceAssetB = balanceAssetB + positionSize * positionRate; balanceAssetA = balanceAssetA - positionSize; } else { - balanceAssetA = balanceAssetA + positionSize / marketRate; + balanceAssetA = balanceAssetA + positionSize / positionRate; balanceAssetB = balanceAssetB - positionSize; } @@ -1372,53 +1388,53 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } orderRecord = createMessage( - messageId, - MESSAGE_ENTITY.SimulationEngine, - MESSAGE_ENTITY.SimulationExecutor, - messageType, - (new Date()).valueOf(), - orderId.toString(), - ORDER_CREATOR.SimulationEngine, - (new Date()).valueOf(), - ORDER_OWNER.User, - global.EXCHANGE_NAME, - "BTC_USDT", - 0, - ORDER_TYPE.Limit, - marketRate, - stopLoss, - takeProfit, - ORDER_DIRECTION.Sell, - -1, - ORDER_STATUS.Signaled, - 0, - "") + messageId, + MESSAGE_ENTITY.SimulationEngine, + MESSAGE_ENTITY.SimulationExecutor, + messageType, + (new Date()).valueOf(), + orderId.toString(), + ORDER_CREATOR.SimulationEngine, + (new Date()).valueOf(), + ORDER_OWNER.User, + global.EXCHANGE_NAME, + "BTC_USDT", + 0, + ORDER_TYPE.Limit, + marketRate, + stopLoss, + takeProfit, + ORDER_DIRECTION.Sell, + -1, + ORDER_STATUS.Signaled, + 0, + "") } else { orderRecord = createMessage( - messageId, - MESSAGE_ENTITY.SimulationEngine, - MESSAGE_ENTITY.SimulationExecutor, - MESSAGE_TYPE.HeartBeat, - (new Date()).valueOf(), - "", - "", - 0, - "", - "", - "", - 0, - "", - 0, - 0, - 0, - "", - 0, - "", - 0, - "") + messageId, + MESSAGE_ENTITY.SimulationEngine, + MESSAGE_ENTITY.SimulationExecutor, + MESSAGE_TYPE.HeartBeat, + (new Date()).valueOf(), + "", + "", + 0, + "", + "", + "", + 0, + "", + 0, + 0, + 0, + "", + 0, + "", + 0, + "") } let strategyStageNumber @@ -1445,6 +1461,14 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } + if (balanceAssetA === Infinity) { + balanceAssetA = Number.MAX_SAFE_INTEGER + } + + if (balanceAssetB === Infinity) { + balanceAssetB = Number.MAX_SAFE_INTEGER + } + simulationRecord = { begin: candle.begin, end: candle.end, From 8de0ab20a857c159f3dd76ba78bd74d93c683e41 Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 29 Jun 2019 15:04:19 -0300 Subject: [PATCH 4/6] Simulator template update --- src/storage/templates/Simulator/Commons.js | 211 +++++++++--------- .../templates/Simulator/User.Bot.Daily.js | 41 ++-- .../templates/Simulator/User.Bot.Market.js | 42 ++-- 3 files changed, 155 insertions(+), 139 deletions(-) diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index f408aac7f..570286d26 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -53,38 +53,41 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } async function runSimulation( - recordsArray, - conditionsArray, - strategiesArray, - tradesArray, - lastObjectsArray, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - callback) { + callback, + callBackFunction) { try { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Entering function."); } + let recordsArray = []; + let conditionsArray = []; + let strategiesArray = []; + let tradesArray = []; + let lastObjectsArray = []; + let tradingSystem = await strategy.getStrategy(); /* Initial Default Values */ let initialDate = startDate; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> initialDate = " + initialDate); } const DEFAULT_BASE_ASSET_BALANCE = 1 const DEFAULT_BASE_ASSET_MINIMUN_BALANCE = 0.5 const DEFAULT_BASE_ASSET_MAXIMUN_BALANCE = 2 let initialBalanceA = DEFAULT_BASE_ASSET_BALANCE - let minimunBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE - let maximunBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE + let minimumBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE + let maximumBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE let initialBalanceB = 0 - let minimunBalanceB = 0 - let maximunBalanceB = 0 + let minimumBalanceB = 0 + let maximumBalanceB = 0 let baseAsset = 'BTC' /* Parameters Processing */ @@ -112,19 +115,19 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { initialBalanceA = DEFAULT_BASE_ASSET_BALANCE; initialBalanceB = 0 } - if (receivedParameters.minimunBalance !== undefined) { - minimunBalanceA = receivedParameters.minimunBalance; - minimunBalanceB = 0 + if (receivedParameters.minimumBalance !== undefined) { + minimumBalanceA = receivedParameters.minimumBalance; + minimumBalanceB = 0 } else { - minimunBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; - minimunBalanceB = 0 + minimumBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; + minimumBalanceB = 0 } - if (receivedParameters.maximunBalance !== undefined) { - maximunBalanceA = receivedParameters.maximunBalance; - maximunBalanceB = 0 + if (receivedParameters.maximumBalance !== undefined) { + maximumBalanceA = receivedParameters.maximumBalance; + maximumBalanceB = 0 } else { - maximunBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; - maximunBalanceB = 0 + maximumBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; + maximumBalanceB = 0 } } else { if (receivedParameters.initialBalance !== undefined) { @@ -134,19 +137,19 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { initialBalanceB = DEFAULT_BASE_ASSET_BALANCE; initialBalanceA = 0 } - if (receivedParameters.minimunBalance !== undefined) { - minimunBalanceB = receivedParameters.minimunBalance; - minimunBalanceA = 0 + if (receivedParameters.minimumBalance !== undefined) { + minimumBalanceB = receivedParameters.minimumBalance; + minimumBalanceA = 0 } else { - minimunBalanceB = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; - minimunBalanceA = 0 + minimumBalanceB = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; + minimumBalanceA = 0 } - if (receivedParameters.maximunBalance !== undefined) { - maximunBalanceB = receivedParameters.maximunBalance; - maximunBalanceA = 0 + if (receivedParameters.maximumBalance !== undefined) { + maximumBalanceB = receivedParameters.maximumBalance; + maximumBalanceA = 0 } else { - maximunBalanceB = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; - maximunBalanceA = 0 + maximumBalanceB = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; + maximumBalanceA = 0 } } } catch (err) { @@ -239,9 +242,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let balanceAssetA = initialBalanceA; let balanceAssetB = initialBalanceB; - let lastProfit = 0; + let lastTradeProfitLoss = 0; let profit = 0; - let lastProfitPercent = 0; + let lastTradeROI = 0; let roundtrips = 0; let fails = 0; @@ -258,9 +261,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; - yesterday.lastProfit = 0; + yesterday.lastTradeProfitLoss = 0; yesterday.profit = 0; - yesterday.lastProfitPercent = 0; + yesterday.lastTradeROI = 0; yesterday.Roundtrips = 0; yesterday.fails = 0; @@ -281,9 +284,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { interExecutionMemory.balanceAssetA = balanceAssetA; interExecutionMemory.balanceAssetB = balanceAssetB; - interExecutionMemory.lastProfit = lastProfit; + interExecutionMemory.lastTradeProfitLoss = lastTradeProfitLoss; interExecutionMemory.profit = profit; - interExecutionMemory.lastProfitPercent = lastProfitPercent; + interExecutionMemory.lastTradeROI = lastTradeROI; interExecutionMemory.roundtrips = 0; interExecutionMemory.fails = 0; @@ -311,9 +314,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } - lastProfit = interExecutionMemory.lastProfit; + lastTradeProfitLoss = interExecutionMemory.lastTradeProfitLoss; profit = interExecutionMemory.profit; - lastProfitPercent = interExecutionMemory.lastProfitPercent; + lastTradeROI = interExecutionMemory.lastTradeROI; roundtrips = interExecutionMemory.roundtrips; fails = interExecutionMemory.fails; @@ -334,6 +337,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Main Simulation Loop: We go thourgh all the candles at this time period. */ + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Main Simulation Loop -> candles.length = " + candles.length); } for (let i = 0; i < candles.length; i++) { @@ -398,14 +402,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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 set some variables that would be nice if they have a value before the formulas are calculated. */ - - if (strategyStage === 'Trigger Stage' || strategyStage === 'No Stage') { - - positionRate = candle.close; - - } - /* We define and evaluate all conditions to be used later during the simulation loop. */ conditionsArrayRecord.push(candle.begin); @@ -739,26 +735,26 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } + /* Trigger On Conditions */ if ( strategyStage === 'No Stage' && currentStrategyIndex === -1 ) { - let minimunBalance - let maximunBalance + let minimumBalance + let maximumBalance let balance if (baseAsset === 'BTC') { balance = balanceAssetA - minimunBalance = minimunBalanceA - maximunBalance = maximunBalanceA + minimumBalance = minimumBalanceA + maximumBalance = maximumBalanceA } else { balance = balanceAssetB - minimunBalance = minimunBalanceB - maximunBalance = maximunBalanceB + minimumBalance = minimumBalanceB + maximumBalance = maximumBalanceB } - if (balance > minimunBalance && balance < maximunBalance) { - /* Trigger On Conditions */ + if (balance > minimumBalance && balance < maximumBalance) { /* Here we need to pick a strategy, or if there is not suitable strategy for the current @@ -791,7 +787,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -810,12 +809,16 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } } else { - tradingSystem.error = "Balance below the minimun. No more strategies will be executed." + if (balance < minimumBalance) { + tradingSystem.error = "Balance below the minimum." + } + if (balance > maximumBalance) { + tradingSystem.error = "Balance above the maximum." + } } } /* Trigger Off Condition */ - if (strategyStage === 'Trigger Stage') { let strategy = tradingSystem.strategies[currentStrategyIndex]; @@ -836,7 +839,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -857,7 +863,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Take Position Condition */ - if (strategyStage === 'Trigger Stage') { let strategy = tradingSystem.strategies[currentStrategyIndex]; @@ -878,7 +883,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -903,10 +911,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Stop Loss Management */ - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' + (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') && + takePositionNow !== true ) { checkStopPhases() @@ -960,7 +967,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -1008,10 +1018,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Take Profit Management */ - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' + (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') && + takePositionNow !== true ) { checkTakeProfitPhases(); @@ -1065,7 +1074,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -1113,10 +1125,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Checking if Stop or Take Profit were hit */ - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' + (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') && + takePositionNow !== true ) { /* Checking what happened since the last execution. We need to know if the Stop Loss @@ -1195,7 +1206,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Taking a Position */ - if ( takePositionNow === true ) { @@ -1238,15 +1248,13 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } marketRate = candle.close; - - calculateStopLoss(); - calculateTakeProfit(); + positionRate = candle.close; previousBalanceAssetA = balanceAssetA; previousBalanceAssetB = balanceAssetB; - lastProfit = 0; - lastProfitPercent = 0; + lastTradeProfitLoss = 0; + lastTradeROI = 0; if (baseAsset === 'BTC') { balanceAssetB = balanceAssetB + positionSize * positionRate; @@ -1263,8 +1271,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; - yesterday.lastProfit = lastProfit; - yesterday.lastProfitPercent = lastProfitPercent; + yesterday.lastTradeProfitLoss = lastTradeProfitLoss; + yesterday.lastTradeROI = lastTradeROI; } } @@ -1273,7 +1281,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Closing a Position */ - if (strategyStage === 'Close Stage') { roundtrips++; @@ -1285,29 +1292,29 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } if (baseAsset === 'BTC') { - lastProfit = balanceAssetA - previousBalanceAssetA; - lastProfitPercent = lastProfit / previousBalanceAssetA * 100; - if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + lastTradeProfitLoss = balanceAssetA - previousBalanceAssetA; + lastTradeROI = lastTradeProfitLoss * 100 / positionSize; + if (isNaN(lastTradeROI)) { lastTradeROI = 0; } profit = balanceAssetA - initialBalanceA; } else { - lastProfit = balanceAssetB - previousBalanceAssetB; - lastProfitPercent = lastProfit / previousBalanceAssetB * 100; - if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + lastTradeProfitLoss = balanceAssetB - previousBalanceAssetB; + lastTradeROI = lastTradeProfitLoss * 100 / positionSize; + if (isNaN(lastTradeROI)) { lastTradeROI = 0; } profit = balanceAssetB - initialBalanceB; } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { - yesterday.lastProfit = lastProfit; + yesterday.lastTradeProfitLoss = lastTradeProfitLoss; yesterday.profit = profit; - yesterday.lastProfitPercent = lastProfitPercent; + yesterday.lastTradeROI = lastTradeROI; } } - currentTrade.lastProfitPercent = lastProfitPercent; + currentTrade.lastTradeROI = lastTradeROI; currentTrade.stopRate = stopLoss; - if (lastProfit > 0) { + if (lastTradeProfitLoss > 0) { hits++; if (currentDay !== undefined) { @@ -1478,7 +1485,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { balanceA: balanceAssetA, balanceB: balanceAssetB, profit: profit, - lastProfit: lastProfit, + lastTradeProfitLoss: lastTradeProfitLoss, stopLoss: stopLoss, roundtrips: roundtrips, hits: hits, @@ -1489,7 +1496,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { days: days, anualizedRateOfReturn: anualizedRateOfReturn, positionRate: positionRate, - lastProfitPercent: lastProfitPercent, + lastTradeROI: lastTradeROI, strategy: currentStrategyIndex, strategyStageNumber: strategyStageNumber, takeProfit: takeProfit, @@ -1498,11 +1505,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { orderRecord: orderRecord, positionSize: positionSize, initialBalanceA: initialBalanceA, - minimunBalanceA: minimunBalanceA, - maximunBalanceA: maximunBalanceA, + minimumBalanceA: minimumBalanceA, + maximumBalanceA: maximumBalanceA, initialBalanceB: initialBalanceB, - minimunBalanceB: minimunBalanceB, - maximunBalanceB: maximunBalanceB + minimumBalanceB: minimumBalanceB, + maximumBalanceB: maximumBalanceB } recordsArray.push(simulationRecord); @@ -1547,7 +1554,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { (currentTrade.begin !== 0 && i === candles.length - 1 && lastCandle.end !== lastInstantOfTheDay) ) { - currentTrade.profit = lastProfit; + currentTrade.profit = lastTradeProfitLoss; tradesArray.push(currentTrade); @@ -1555,7 +1562,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { begin: 0, end: 0, status: 0, - lastProfitPercent: 0, + lastTradeROI: 0, exitType: 0, beginRate: 0, endRate: 0 @@ -1575,9 +1582,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { interExecutionMemory.balanceAssetA = yesterday.balanceAssetA; interExecutionMemory.balanceAssetB = yesterday.balanceAssetB; - interExecutionMemory.lastProfit = yesterday.lastProfit; + interExecutionMemory.lastTradeProfitLoss = yesterday.lastTradeProfitLoss; interExecutionMemory.profit = yesterday.profit; - interExecutionMemory.lastProfitPercent = yesterday.lastProfitPercent; + interExecutionMemory.lastTradeROI = yesterday.lastTradeROI; interExecutionMemory.roundtrips = interExecutionMemory.roundtrips + yesterday.Roundtrips; interExecutionMemory.fails = interExecutionMemory.fails + yesterday.fails; @@ -1593,7 +1600,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - callback(tradingSystem); + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> callback -> recordsArray.length = " + recordsArray.length); } + + callback(tradingSystem, recordsArray, conditionsArray, strategiesArray, tradesArray, lastObjectsArray); function getElement(pArray, begin, end) { diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 9bd5892a0..7cc323c75 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -51,13 +51,13 @@ let market = global.MARKET; let dataFile; - let recordsArray = []; - let conditionsArray = []; - let strategiesArray = []; - let tradesArray = []; + let recordsArray + let conditionsArray + let strategiesArray + let tradesArray + let lastObjectsArray let tradingSystem = {}; - let lastObjectsArray = []; commons.initializeData(); @@ -96,20 +96,23 @@ } commons.runSimulation( - recordsArray, - conditionsArray, - strategiesArray, - tradesArray, - lastObjectsArray, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - writeFiles) + writeFiles, + callBackFunction) + + function writeFiles(pTradingSystem, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { - function writeFiles(pTradingSystem) { tradingSystem = pTradingSystem + recordsArray = pRecordsArray + conditionsArray = pConditionsArray + strategiesArray = pStrategiesArray + tradesArray = pTradesArray + lastObjectsArray = pLastObjectsArray + writeRecordsFile() } @@ -141,7 +144,7 @@ record.balanceA + "," + record.balanceB + "," + record.profit + "," + - record.lastProfit + "," + + record.lastTradeProfitLoss + "," + record.stopLoss + "," + record.roundtrips + "," + record.hits + "," + @@ -152,7 +155,7 @@ record.days + "," + record.anualizedRateOfReturn + "," + record.positionRate + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.strategy + "," + record.strategyStageNumber + "," + record.takeProfit + "," + @@ -161,11 +164,11 @@ JSON.stringify(record.orderRecord) + "," + record.positionSize + "," + record.initialBalanceA + "," + - record.minimunBalanceA + "," + - record.maximunBalanceA + "," + + record.minimumBalanceA + "," + + record.maximumBalanceA + "," + record.initialBalanceB + "," + - record.minimunBalanceB + "," + - record.maximunBalanceB + "]"; + record.minimumBalanceB + "," + + record.maximumBalanceB + "]"; if (separator === "") { separator = ","; } @@ -407,7 +410,7 @@ record.begin + "," + record.end + "," + record.status + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.beginRate + "," + record.endRate + "," + record.exitType + "," + diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index 8c976caed..dcdcc835b 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -49,11 +49,11 @@ let market = global.MARKET; let dataFile; - let recordsArray = []; - let conditionsArray = []; - let strategiesArray = []; - let tradesArray = []; - let lastObjectsArray = []; + let recordsArray + let conditionsArray + let strategiesArray + let tradesArray + let lastObjectsArray let tradingSystem = {}; let interExecutionMemory = {}; @@ -96,20 +96,23 @@ } commons.runSimulation( - recordsArray, - conditionsArray, - strategiesArray, - tradesArray, - lastObjectsArray, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - writeFiles) + writeFiles, + callBackFunction) + + function writeFiles(pTradingSystem, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { - function writeFiles(pTradingSystem) { tradingSystem = pTradingSystem + recordsArray = pRecordsArray + conditionsArray = pConditionsArray + strategiesArray = pStrategiesArray + tradesArray = pTradesArray + lastObjectsArray = pLastObjectsArray + writeRecordsFile() } @@ -137,7 +140,7 @@ record.balanceA + "," + record.balanceB + "," + record.profit + "," + - record.lastProfit + "," + + record.lastTradeProfitLoss + "," + record.stopLoss + "," + record.roundtrips + "," + record.hits + "," + @@ -148,7 +151,7 @@ record.days + "," + record.anualizedRateOfReturn + "," + record.positionRate + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.strategy + "," + record.strategyStageNumber + "," + record.takeProfit + "," + @@ -157,11 +160,11 @@ JSON.stringify(record.orderRecord) + "," + record.positionSize + "," + record.initialBalanceA + "," + - record.minimunBalanceA + "," + - record.maximunBalanceA + "," + + record.minimumBalanceA + "," + + record.maximumBalanceA + "," + record.initialBalanceB + "," + - record.minimunBalanceB + "," + - record.maximunBalanceB + "]"; + record.minimumBalanceB + "," + + record.maximumBalanceB + "]"; if (separator === "") { separator = ","; } @@ -380,7 +383,7 @@ record.begin + "," + record.end + "," + record.status + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.beginRate + "," + record.endRate + "," + record.exitType + "," + @@ -441,3 +444,4 @@ } } }; + From 23fde238880fc80de61bc2f40ee9107242272ddd Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 1 Jul 2019 16:34:11 -0300 Subject: [PATCH 5/6] Adding Clone Id --- src/models/ecosystem.js | 4 ++++ src/schema/types/Bot.js | 1 + src/schema/types/input/Bot.js | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index 1e6a2b9d8..bcaf36a46 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -58,6 +58,10 @@ const ecosystemSchema = new Schema({ type: String, required: false }, + cloneId: { + type: String, + required: false + }, products: [{ codeName: { type: String, diff --git a/src/schema/types/Bot.js b/src/schema/types/Bot.js index 9ce0405ab..347587f02 100644 --- a/src/schema/types/Bot.js +++ b/src/schema/types/Bot.js @@ -15,6 +15,7 @@ const BotType = new GraphQLObjectType({ profilePicture: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString }, + cloneId: { type: GraphQLString }, products: { type: new GraphQLList(ProductType), resolve(parent) { diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js index dfbadf5fb..5b1ed1096 100644 --- a/src/schema/types/input/Bot.js +++ b/src/schema/types/input/Bot.js @@ -11,7 +11,8 @@ const Type = new GraphQLInputObjectType({ codeName: { type: new GraphQLNonNull(GraphQLString) }, displayName: { type: new GraphQLNonNull(GraphQLString) }, repo: { type: GraphQLString }, - configFile: { type: GraphQLString } + configFile: { type: GraphQLString }, + cloneId: { type: GraphQLString } }), }); From 5a6f38fa7b558b87f72e740739fcd7be91b5563c Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 1 Jul 2019 16:34:24 -0300 Subject: [PATCH 6/6] Fixing env variable names --- src/storage/templates/TradingBot/User.Bot.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index 963c6d3c9..7cb73e57d 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -573,7 +573,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function getSignalsByCloneId(orderStatus) { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -594,7 +594,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) @@ -610,7 +610,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function updateSignal(signalId, message) { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -631,7 +631,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) @@ -646,7 +646,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function createSignal(message) { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -667,7 +667,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) @@ -682,7 +682,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function getAutopilot() { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -699,7 +699,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } })