From 32afb3a1a64894a9ce40daf47a43e34ad555656d Mon Sep 17 00:00:00 2001 From: Matias Benitez Date: Thu, 5 Jul 2018 15:28:07 -0300 Subject: [PATCH] Adding fix for the move and put position. We need to ensure the format of the values received as parameters on movePosition and putPosition. Rounding issues are fixed at validating the exchange positions on only 6 positions. Parsing additional values is not necesary. --- Assistant.js | 53 ++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/Assistant.js b/Assistant.js index f725356e7..7c14a0f58 100644 --- a/Assistant.js +++ b/Assistant.js @@ -181,7 +181,7 @@ if (bot.processDatetime.valueOf() >= candle.begin && bot.processDatetime.valueOf() < candle.end) { marketRate = (candle.open + candle.close) / 2; - marketRate = Number(parseFloat(marketRate).toFixed(8)); + marketRate = Number(marketRate.toFixed(8)); context.newHistoryRecord.marketRate = marketRate; ticker = { @@ -543,9 +543,9 @@ id: Math.trunc(Math.random(1) * 1000000), type: thisPosition.type, rate: thisPosition.rate.toString(), - amountA: Number(parseFloat(thisPosition.amountA).toFixed(8)).toString(), - amountB: Number(parseFloat(thisPosition.amountB).toFixed(8)).toString(), - fee: Number(parseFloat(feeRate).toFixed(8)).toString(), + amountA: Number(thisPosition.amountA.toFixed(8)).toString(), + amountB: Number(thisPosition.amountB.toFixed(8)).toString(), + fee: Number(feeRate.toFixed(8)).toString(), date: (new Date()).valueOf() } @@ -636,8 +636,8 @@ sumAssetB = sumAssetB + Number(trade.amountB); } - sumAssetA = Number(parseFloat(sumAssetA).toFixed(8)); - sumAssetB = Number(parseFloat(sumAssetB).toFixed(8)); + sumAssetA = Number(sumAssetA.toFixed(8)); + sumAssetB = Number(sumAssetB.toFixed(8)); if (position.amountB !== sumAssetB) { logger.write(MODULE_NAME, "[ERROR] ordersExecutionCheck -> loopBody -> position.amountB = " + position.amountB); @@ -711,10 +711,10 @@ sumAssetA = sumAssetA + Number(exchangePosition.amountA); sumAssetB = sumAssetB + Number(exchangePosition.amountB); - sumAssetA = Number(parseFloat(sumAssetA).toFixed(6)); // Fixed to 6 positions to avoid rounding issues - sumAssetB = Number(parseFloat(sumAssetB).toFixed(8)); + sumAssetA = Number(sumAssetA.toFixed(6)); // Fixed to 6 positions to avoid rounding issues + sumAssetB = Number(sumAssetB.toFixed(8)); - if (parseFloat(position.amountA).toFixed(6) !== sumAssetA || position.amountB !== sumAssetB ) { + if (position.amountA.toFixed(6) !== sumAssetA || position.amountB !== sumAssetB ) { logger.write(MODULE_NAME, "[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> position.amountA = " + position.amountA); logger.write(MODULE_NAME, "[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> sumAssetA = " + sumAssetA); logger.write(MODULE_NAME, "[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> position.amountB = " + position.amountB); @@ -779,28 +779,28 @@ if (trade.type === 'buy') { let fee = Number(trade.fee) * trade.amountB; - let fixedFee = Number(parseFloat(fee).toFixed(8)); + let fixedFee = Number(fee.toFixed(8)); assetA = context.executionContext.balance.assetA - Number(trade.amountA); assetB = context.executionContext.balance.assetB + Number(trade.amountB) - fixedFee; let available = context.executionContext.availableBalance.assetB + Number(trade.amountB) - fixedFee; - context.executionContext.availableBalance.assetB = Number(parseFloat(available).toFixed(8)); + context.executionContext.availableBalance.assetB = Number(available.toFixed(8)); } if (trade.type === 'sell') { let fee = Number(trade.fee) * trade.amountA; - let fixedFee = Number(parseFloat(fee).toFixed(8)); + let fixedFee = Number(fee.toFixed(8)); assetA = context.executionContext.balance.assetA + Number(trade.amountA) - fixedFee; assetB = context.executionContext.balance.assetB - Number(trade.amountB); let available = context.executionContext.availableBalance.assetA + Number(trade.amountA) - fixedFee; - context.executionContext.availableBalance.assetA = Number(parseFloat(available).toFixed(8)); + context.executionContext.availableBalance.assetA = Number(available.toFixed(8)); } - context.executionContext.balance.assetA = Number(parseFloat(assetA).toFixed(8)); - context.executionContext.balance.assetB = Number(parseFloat(assetB).toFixed(8)); + context.executionContext.balance.assetA = Number(assetA.toFixed(8)); + context.executionContext.balance.assetB = Number(assetB.toFixed(8)); } } catch (err) { @@ -859,10 +859,6 @@ try { if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> Entering function."); } - if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pType = " + pType); } - if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pRate = " + pRate); } - if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pAmountA = " + pAmountA); } - if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pAmountB = " + pAmountB); } /* Removing extra decimals. */ @@ -870,6 +866,11 @@ pAmountA = Number(parseFloat(pAmountA).toFixed(8)); pAmountB = Number(parseFloat(pAmountB).toFixed(8)); + if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pType = " + pType); } + if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pRate = " + pRate); } + if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pAmountA = " + pAmountA); } + if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] putPosition -> pAmountB = " + pAmountB); } + /* Validations that the limits are not surpassed. */ if (pType === 'buy') { @@ -889,7 +890,7 @@ return; } - let aRate = Number(parseFloat(pAmountA / pRate).toFixed(8)); + let aRate = Number((pAmountA / pRate).toFixed(8)); if (aRate !== pAmountB) { @@ -925,7 +926,7 @@ return; } - let bRate = Number(parseFloat(pAmountB * pRate).toFixed(8)); + let bRate = Number((pAmountB * pRate).toFixed(8)); if (bRate !== pAmountA) { @@ -1022,14 +1023,14 @@ if (position.type === 'buy') { context.executionContext.availableBalance.assetA = context.executionContext.availableBalance.assetA - pAmountA; - context.executionContext.availableBalance.assetA = Number(parseFloat(context.executionContext.availableBalance.assetA).toFixed(8)); + context.executionContext.availableBalance.assetA = Number(context.executionContext.availableBalance.assetA.toFixed(8)); context.newHistoryRecord.lastBuyRate = pRate; } if (position.type === 'sell') { context.executionContext.availableBalance.assetB = context.executionContext.availableBalance.assetB - pAmountB; - context.executionContext.availableBalance.assetB = Number(parseFloat(context.executionContext.availableBalance.assetB).toFixed(8)); + context.executionContext.availableBalance.assetB = Number(context.executionContext.availableBalance.assetB.toFixed(8)); context.newHistoryRecord.lastSellRate = pRate; } @@ -1068,9 +1069,12 @@ if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> pPosition = " + JSON.stringify(pPosition)); } if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> pNewRate = " + pNewRate); } + /* Removing extra decimals. */ + pNewRate = Number(parseFloat(pNewRate).toFixed(8)); + let newAmountB; if (pPosition.type === "buy") { - newAmountB = parseFloat(pPosition.amountA / pNewRate).toFixed(7); // If it was fixed to 8 positions there could be some rounding up and there will be not enough AssetA for buying + newAmountB = Number((pPosition.amountA / pNewRate).toFixed(8)); } else { newAmountB = pPosition.amountB; } @@ -1293,4 +1297,5 @@ return context.executionContext.remember[pKey]; } + }; \ No newline at end of file