diff --git a/Assistant.js b/Assistant.js index fcc4ae73a..9a98b1cbd 100644 --- a/Assistant.js +++ b/Assistant.js @@ -469,7 +469,10 @@ trades.push(trade); - onResponse(global.DEFAULT_OK_RESPONSE, trades); + let order = { + trades: trades + } + onResponse(global.DEFAULT_OK_RESPONSE, order); return; } @@ -488,7 +491,7 @@ } } - function onResponse(err, pTrades) { + function onResponse(err, order) { try { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> onResponse -> Entering function."); } @@ -496,7 +499,7 @@ switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { // Everything went well, we have the information requested. if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> onResponse -> Execution finished well."); } - innerCallBack(pTrades); + innerCallBack(order); } break; case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. @@ -523,13 +526,14 @@ } } - function confirmOrderWasExecuted(pTrades) { + function confirmOrderWasExecuted(order) { try { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Entering function."); } - if (global.LOG_CONTROL[MODULE_NAME].logContent === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> pTrades = " + JSON.stringify(pTrades)); } + if (global.LOG_CONTROL[MODULE_NAME].logContent === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> trades = " + JSON.stringify(trades)); } + let trades = order.trades /* To confirm everything is ok, we will add all the amounts on trades asociated to the order and @@ -542,8 +546,8 @@ let sumQuotedAsset = 0; let sumRate = 0; - for (let k = 0; k < pTrades.length; k++) { - let trade = pTrades[k]; + for (let k = 0; k < trades.length; k++) { + let trade = trades[k]; sumBaseAsset = sumBaseAsset + thisObject.truncDecimals(trade.amountA); sumQuotedAsset = sumQuotedAsset + thisObject.truncDecimals(trade.amountB); sumRate = sumRate + thisObject.truncDecimals(trade.rate); @@ -572,12 +576,12 @@ position.status = "executed"; if (position.type === "sell") { - context.newHistoryRecord.sellExecRate = sumRate / pTrades.length; + context.newHistoryRecord.sellExecRate = sumRate / trades.length; } else { - context.newHistoryRecord.buyExecRate = sumRate / pTrades.length; + context.newHistoryRecord.buyExecRate = sumRate / trades.length; } - applyTradesToContext(pTrades); + applyTradesToContext(trades); let newTransaction = { type: position.type + " executed", @@ -597,11 +601,13 @@ } } - function confirmOrderWasPartiallyExecuted(pTrades) { + function confirmOrderWasPartiallyExecuted(trades) { try { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> Entering function."); } + let trades = order.trades + /* To confirm everything is ok, we will add all the amounts on trades plus the remaining amounts @@ -613,8 +619,8 @@ let sumBaseAsset = 0; let sumQuotedAsset = 0; - for (let k = 0; k < pTrades.length; k++) { - let trade = pTrades[k]; + for (let k = 0; k < trades.length; k++) { + let trade = trades[k]; sumBaseAsset = sumBaseAsset + thisObject.truncDecimals(trade.amountA); sumQuotedAsset = sumQuotedAsset + thisObject.truncDecimals(trade.amountB); } @@ -663,7 +669,7 @@ position.amountB = exchangePosition.amountB; position.date = (new Date(exchangePosition.date)).valueOf(); - applyTradesToContext(pTrades); + applyTradesToContext(trades); let newTransaction = { type: position.type + " partially executed", @@ -683,7 +689,7 @@ } } - function applyTradesToContext(pTrades) { + function applyTradesToContext(trades) { try { @@ -691,9 +697,9 @@ /* Here we apply the trades that already happened at the exchange to the balance and available balance of the bot. We also calculate its profits. */ - for (k = 0; k < pTrades.length; k++) { + for (k = 0; k < trades.length; k++) { - let trade = pTrades[k]; + let trade = trades[k]; position.trades.push(trade); diff --git a/Commons.js b/Commons.js index aaf3d3dcf..5ad2fb509 100644 --- a/Commons.js +++ b/Commons.js @@ -71,7 +71,7 @@ return } if (outputDatasetNode.referenceParent.parentNode.dataBuilding === undefined) { - logger.write(MODULE_NAME, "[WARN] start -> Product Definition without a Data Building Procedure. Product Definition = " + JSON.stringify(outputDatasetNode.referenceParent.parentNode)); + logger.write(MODULE_NAME, "[WARN] start -> Product Definition " + outputDatasetNode.referenceParent.parentNode.name + " without a Data Building Procedure. Product Definition = " + JSON.stringify(outputDatasetNode.referenceParent.parentNode)); } if (outputDatasetNode.referenceParent.code.codeName === undefined) { logger.write(MODULE_NAME, "[ERROR] start -> Dataset witn no codeName defined. Product Dataset = " + JSON.stringify(outputDatasetNode.referenceParent)); diff --git a/ExchangeAPI.js b/ExchangeAPI.js index 1944ec123..a48e18aac 100644 --- a/ExchangeAPI.js +++ b/ExchangeAPI.js @@ -96,7 +96,7 @@ } } - function getOrder(orderId, market, callBackFunction) { + async function getOrder(orderId, market, callBackFunction) { try { logInfo("getOrder -> Entering function. orderId = " + orderId); @@ -116,7 +116,7 @@ } } - function createOrder(market, side, pRate, amount, price, callBackFunction) { + async function createOrder(market, side, price, cost, amount, callBackFunction) { try { logInfo("createOrder -> Entering function."); @@ -124,7 +124,7 @@ logInfo("createOrder -> market = " + market.assetA + "_" + market.assetB); logInfo("createOrder -> side = " + side); - logInfo("createOrder -> pRate = " + rate); + logInfo("createOrder -> cost = " + cost); logInfo("createOrder -> amount = " + amount); logInfo("createOrder -> price = " + price); @@ -137,7 +137,7 @@ let order if (exchange.has['createOrder']) { - order = await(exchange.createOrder(symbol, type, side, amount, price)) + 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/Session.js b/Session.js index 0aaf44d55..359548ea9 100644 --- a/Session.js +++ b/Session.js @@ -2,6 +2,7 @@ const MODULE_NAME = "Session" const FULL_LOG = true; + const ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000 let thisObject = { initialize: initialize @@ -97,6 +98,17 @@ bot.STOP_SESSION = true } + function checkDatetimes() { + if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startLiveTrading -> 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.VALUES_TO_USE.timeRange.finalDatetime.valueOf() < (new Date()).valueOf()) { + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startLiveTrading -> 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 + } + } + function startBackTesting(message) { bot.startMode = "Backtest" if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() > (new Date()).valueOf()) { @@ -117,9 +129,7 @@ } bot.startMode = "Live" - if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - } + checkDatetimes() bot.resumeExecution = false; bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true @@ -135,9 +145,7 @@ } bot.startMode = "Live" - if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - } + checkDatetimes() bot.resumeExecution = false; bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) bot.hasTheBotJustStarted = true @@ -164,9 +172,7 @@ function startPaperTrading(message) { bot.startMode = "Backtest" - if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) { - bot.VALUES_TO_USE.timeRange.initialDatetime = new Date() - } + checkDatetimes() bot.resumeExecution = false; bot.hasTheBotJustStarted = true bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf()) @@ -204,7 +210,7 @@ } /* Session Type Dependant Default Values */ - const ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000 + switch (bot.SESSION.type) { case 'Backtesting Session': { bot.VALUES_TO_USE.timeRange.initialDatetime = new Date((new Date()).valueOf() - ONE_YEAR_IN_MILISECONDS) @@ -247,6 +253,8 @@ bot.VALUES_TO_USE.baseAsset = code.codeName; } + code = tradingSystem.parameters.baseAsset.code + if (bot.VALUES_TO_USE.baseAsset === 'BTC') { if (code.initialBalance !== undefined) { bot.VALUES_TO_USE.initialBalanceA = code.initialBalance; @@ -347,15 +355,17 @@ /* Base Asset and Initial Balances. */ { if (bot.SESSION.parameters.baseAsset !== undefined) { - if (tradingSystem.parameters.baseAsset.referenceParent !== undefined) { - if (tradingSystem.parameters.baseAsset.referenceParent.referenceParent !== undefined) { + if (bot.SESSION.parameters.baseAsset.referenceParent !== undefined) { + if (bot.SESSION.parameters.baseAsset.referenceParent.referenceParent !== undefined) { - let code = tradingSystem.parameters.baseAsset.referenceParent.referenceParent.code + let code = bot.SESSION.parameters.baseAsset.referenceParent.referenceParent.code if (code.codeName !== undefined) { bot.VALUES_TO_USE.baseAsset = code.codeName; } + code = bot.SESSION.parameters.baseAsset.code + if (bot.VALUES_TO_USE.baseAsset === 'BTC') { if (code.initialBalance !== undefined) { bot.VALUES_TO_USE.initialBalanceA = code.initialBalance;