From 1afc012a2f2e0d3a9ddc5667b92782866f705164 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 11 Aug 2019 14:18:09 +0200 Subject: [PATCH] Multiple Fixes to allow it to run without user keys, in BACKTEST mode. --- Assistant.js | 12 ++++---- MultiPeriodDaily.js | 14 ++++++++-- MultiPeriodMarket.js | 9 ++++-- Root.js | 4 +++ Run.js | 49 +++++++++++++++++++++++++++++++-- TradingEngineProcessMainLoop.js | 2 -- sequence.json | 5 +--- 7 files changed, 78 insertions(+), 17 deletions(-) diff --git a/Assistant.js b/Assistant.js index 1ef164205..810cf8f75 100644 --- a/Assistant.js +++ b/Assistant.js @@ -157,11 +157,13 @@ let candleArray; - if (err.result === global.CUSTOM_FAIL_RESPONSE.result) { // Just past midnight, this file will not exist for a couple of minutes. - if (err.message === "File does not exist." || err.message === "The specified key does not exist.") { - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - return; - } + if (err.result === global.CUSTOM_FAIL_RESPONSE.result || err.code === "The specified key does not exist." || err.message === "File does not exist.") { // Just past midnight, this file will not exist for a couple of minutes. + + logger.write(MODULE_NAME, "[WARN] initialize -> getMarketRateFromIndicator -> onFileReceived -> err = " + JSON.stringify(err)); + logger.write(MODULE_NAME, "[WARN] initialize -> getMarketRateFromIndicator -> This could happen when there are still holes on trades and the process needs to catch up. "); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } if (err.result === global.DEFAULT_OK_RESPONSE.result) { diff --git a/MultiPeriodDaily.js b/MultiPeriodDaily.js index 003f97ec8..caa3b6672 100644 --- a/MultiPeriodDaily.js +++ b/MultiPeriodDaily.js @@ -409,7 +409,12 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> periodsLoopBody -> dependencyLoopBody -> getPreviousFile -> Entering function."); } let dateForPath = previousDay.getUTCFullYear() + '/' + utilities.pad(previousDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(previousDay.getUTCDate(), 2); - let filePath = dependency.product + '/' + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + let filePath + if (dependency.dataSet === "Multi-Period-Daily") { + filePath = dependency.product + '/' + dependency.dataSet + "/" + outputPeriodLabel + "/" + dateForPath; + } else { + filePath = dependency.product + '/' + dependency.dataSet + "/" + dateForPath; + } let fileName = market.assetA + '_' + market.assetB + ".json"; storage.getTextFile(filePath, fileName, onFileReceived); @@ -474,7 +479,12 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 = dependency.product + '/' + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + let filePath + if (dependency.dataSet === "Multi-Period-Daily") { + filePath = dependency.product + '/' + dependency.dataSet + "/" + outputPeriodLabel + "/" + dateForPath; + } else { + filePath = dependency.product + '/' + dependency.dataSet + "/" + dateForPath; + } let fileName = market.assetA + '_' + market.assetB + ".json"; storage.getTextFile(filePath, fileName, onFileReceived); diff --git a/MultiPeriodMarket.js b/MultiPeriodMarket.js index ceab3ff19..b78128fcf 100644 --- a/MultiPeriodMarket.js +++ b/MultiPeriodMarket.js @@ -168,10 +168,15 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> processTimePeriods -> 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 = market.assetA + '_' + market.assetB + ".json"; - let filePath = dependency.product + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; - + let filePath + if (dependency.dataSet === "Multi-Period-Market") { + filePath = dependency.product + '/' + dependency.dataSet + "/" + outputPeriodLabel; + } else { + filePath = dependency.product + '/' + dependency.dataSet + "/" + dateForPath; + } storage.getTextFile(filePath, fileName, onFileReceived); function onFileReceived(err, text) { diff --git a/Root.js b/Root.js index fe4275081..d753fd8bc 100644 --- a/Root.js +++ b/Root.js @@ -443,6 +443,10 @@ runTradingBot(botConfig, processConfig); break; } + case 'Trading-Engine': { + runTradingEngine(botConfig, processConfig); + break; + } default: { console.log(logDisplace + "Root : [ERROR] start -> findProcess -> Unexpected bot type. -> botConfig.type = " + botConfig.type); } diff --git a/Run.js b/Run.js index a6eed7422..f4a3fe316 100644 --- a/Run.js +++ b/Run.js @@ -82,7 +82,7 @@ function sequenceExecution(currentStep) { process.env.KEY = key.name process.env.SECRET = key.code - + } } } @@ -90,7 +90,6 @@ function sequenceExecution(currentStep) { } } - let stepKey = execution.devTeam + '.' + execution.bot + '.' + execution.process; if (processedSteps.has(stepKey)) { processedSteps.set(stepKey, processedSteps.get(stepKey) + 1); @@ -142,6 +141,14 @@ async function readExecutionConfiguration(execution) { let finalDatetime = process.env.END_DATE_TIME if (execution.type === 'Trading-Engine') { + + /* We set the START MODE of the Trading Engine */ + if (process.env.KEY === undefined || process.env.SECRET === undefined) { + process.env.START_MODE = "backtest" // if we dont have keys, we swtich to backtest mode to avoid exchange errors. + } else { + process.env.START_MODE = "live" // If we have keys, then we are in live mode. + } + if (definition !== undefined) { if (definition.simulationParams !== undefined) { if (definition.simulationParams.beginDatetime !== undefined) { @@ -178,6 +185,44 @@ async function readExecutionConfiguration(execution) { } } } + /* Get the initial balance from the definition */ + let tradingSystem = definition.tradingSystem + + if (tradingSystem) { + if (tradingSystem.parameters !== undefined) { + if (tradingSystem.parameters.baseAsset !== undefined) { + if (tradingSystem.parameters.baseAsset.formula !== undefined) { + let receivedParameters + try { + receivedParameters = JSON.parse(tradingSystem.parameters.baseAsset.formula.code); + + if (receivedParameters.name !== undefined) { + baseAsset = receivedParameters.name; + if (baseAsset !== 'BTC' && baseAsset !== 'USDT') { + /* using BTC as default */ + baseAsset = 'BTC' + } + } + + if (baseAsset === 'BTC') { + if (receivedParameters.initialBalance !== undefined) { + process.env.INITIAL_BALANCE_ASSET_A = receivedParameters.initialBalance; + process.env.INITIAL_BALANCE_ASSET_B = 0 + } + } else { + if (receivedParameters.initialBalance !== undefined) { + process.env.INITIAL_BALANCE_ASSET_B = receivedParameters.initialBalance; + process.env.INITIAL_BALANCE_ASSET_A = 0 + } + } + } catch (err) { + process.env.INITIAL_BALANCE_ASSET_A = 0.001 // default + process.env.INITIAL_BALANCE_ASSET_B = 0 // default + } + } + } + } + } } } diff --git a/TradingEngineProcessMainLoop.js b/TradingEngineProcessMainLoop.js index 19db8df72..2da74a9c0 100644 --- a/TradingEngineProcessMainLoop.js +++ b/TradingEngineProcessMainLoop.js @@ -36,8 +36,6 @@ try { if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } - bot.startMode = 'Live' // The Trading Engine does not support different start modes. Only live for background compatibility. - UI_COMMANDS = pUI_COMMANDS; processConfig = pProcessConfig; diff --git a/sequence.json b/sequence.json index fa8694f9a..10ca0ec47 100644 --- a/sequence.json +++ b/sequence.json @@ -140,9 +140,6 @@ "exchangeName": "Poloniex", "process": "Multi-Period-Daily", "beginDatetime": "2019-08-01T08:00:00.000Z", - "timePeriod": "05-min", - "baseAsset": "assetB", - "balanceAssetA": "0", - "balanceAssetB": "0.001" + "timePeriod": "05-min" } ]