diff --git a/.vs/AACloud/v15/.suo b/.vs/AACloud/v15/.suo index 4c842122b..147d6c648 100644 Binary files a/.vs/AACloud/v15/.suo and b/.vs/AACloud/v15/.suo differ diff --git a/Assistant.js b/Assistant.js index 2bdd48fd3..a69982466 100644 --- a/Assistant.js +++ b/Assistant.js @@ -565,17 +565,22 @@ let thisPosition = context.executionContext.positions[i]; if (thisPosition.id === pPositionId) { - - let trade = { - id: Math.trunc(Math.random(1) * 1000000), - type: thisPosition.type, - rate: thisPosition.rate, - amountA: thisPosition.amountA, - amountB: thisPosition.amountB, - fee: 0, - date: (new Date()).valueOf() - } - + + let feeRate = 0.0025; // Default fee + + if (Math.random(1) < 0.5) + feeRate = 0.0015; // Some times we will pay less fee + + let trade = { + id: Math.trunc(Math.random(1) * 1000000), + type: thisPosition.type, + rate: thisPosition.rate.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() + } + trades.push(trade); onResponse(global.DEFAULT_OK_RESPONSE, trades); @@ -658,21 +663,15 @@ let sumAssetB = 0; for (let k = 0; k < pTrades.length; k++) { - - sumAssetA = sumAssetA + Number(pTrades[k].amountA); - sumAssetB = sumAssetB + Number(pTrades[k].amountB); - - /* We add the fees */ - - sumAssetA = sumAssetA - Number(pTrades[k].fee); - + let trade = pTrades[k]; + sumAssetA = sumAssetA + Number(trade.amountA); + sumAssetB = sumAssetB + Number(trade.amountB); } + + sumAssetA = Number(sumAssetA.toFixed(8)); + sumAssetB = Number(sumAssetB.toFixed(8)); - sumAssetB = Number(sumAssetB.toFixed(8)); - - if ( - position.amountB !== sumAssetB - ) { + if (position.amountB !== sumAssetB) { logger.write("[ERROR] ordersExecutionCheck -> loopBody -> position.amountB = " + position.amountB); logger.write("[ERROR] ordersExecutionCheck -> loopBody -> sumAssetB = " + sumAssetB); @@ -732,37 +731,31 @@ let sumAssetA = 0; let sumAssetB = 0; - - for (let k = 0; k < pTrades.length; k++) { - - sumAssetA = sumAssetA + Number(pTrades[k].amountA); - sumAssetB = sumAssetB + Number(pTrades[k].amountB); - - /* We add the fees */ - - sumAssetA = sumAssetA - Number(pTrades[k].fee); - + + for (let k = 0; k < pTrades.length; k++) { + let trade = pTrades[k]; + sumAssetA = sumAssetA + Number(trade.amountA); + sumAssetB = sumAssetB + Number(trade.amountB); } - + /* To this we add the current position amounts. */ - - sumAssetA = sumAssetA + Number(exchangePosition.amountA.toFixed(8)); - sumAssetB = sumAssetB + Number(exchangePosition.amountB.toFixed(8)); - + + sumAssetA = sumAssetA + Number(exchangePosition.amountA); + sumAssetB = sumAssetB + Number(exchangePosition.amountB); + sumAssetA = Number(sumAssetA.toFixed(8)); sumAssetB = Number(sumAssetB.toFixed(8)); - if ( - position.amountA !== sumAssetA || - position.amountB !== sumAssetB - ) { + if (position.amountA !== sumAssetA || position.amountB !== sumAssetB ) { logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> position.amountA = " + position.amountA); logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> sumAssetA = " + sumAssetA); logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> position.amountB = " + position.amountB); logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> sumAssetB = " + sumAssetB); logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> Cannot be confirmed that a partially execution was done well."); - callBack(global.DEFAULT_FAIL_RESPONSE); + + /* There are diferences on the responses between the getPosition and getTrades that causes some issues, let's retry. */ + callBack(global.DEFAULT_RETRY_RESPONSE); return; } @@ -813,22 +806,33 @@ context.newHistoryRecord.newTrades++; /* Calculate Balances */ - + let assetA = 0; + let assetB = 0; + if (trade.type === 'buy') { + let fee = Number(trade.fee) * trade.amountB; + let fixedFee = Number(fee.toFixed(8)); + + assetA = context.executionContext.balance.assetA - Number(trade.amountA); + assetB = context.executionContext.balance.assetB + Number(trade.amountB) - fixedFee; - context.executionContext.balance.assetA = context.executionContext.balance.assetA - Number(trade.amountA); - context.executionContext.balance.assetB = context.executionContext.balance.assetB + Number(trade.amountB); - - context.executionContext.availableBalance.assetB = context.executionContext.availableBalance.assetB + Number(trade.amountB); + let available = context.executionContext.availableBalance.assetB + Number(trade.amountB) - fixedFee; + context.executionContext.availableBalance.assetB = Number(available.toFixed(8)); } if (trade.type === 'sell') { - - context.executionContext.balance.assetA = context.executionContext.balance.assetA + Number(trade.amountA); - context.executionContext.balance.assetB = context.executionContext.balance.assetB - Number(trade.amountB); - - context.executionContext.availableBalance.assetA = context.executionContext.availableBalance.assetA + Number(trade.amountA); + let fee = Number(trade.fee) * trade.amountA; + 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(available.toFixed(8)); } + + context.executionContext.balance.assetA = Number(assetA.toFixed(8)); + context.executionContext.balance.assetB = Number(assetB.toFixed(8)); } } catch (err) { diff --git a/Context.js b/Context.js index 9a1598e9d..91e0a14ff 100644 --- a/Context.js +++ b/Context.js @@ -443,20 +443,21 @@ once we advance into multiple competitions scheme. */ - const INITIAL_INVESTMENT = 0.001; // This is just for this release of the platform. + const INITIAL_INVESTMENT_A = 0; // This is just for this release of the platform. + const INITIAL_INVESTMENT_B = .001; // This is just for this release of the platform. thisObject.executionContext = { investment: { // This is used to calculate profits. - assetA: 0, - assetB: INITIAL_INVESTMENT + assetA: INITIAL_INVESTMENT_A, + assetB: INITIAL_INVESTMENT_B }, balance: { // This is the total balance that includes positions at the order book + funds available to be traded. - assetA: 0, - assetB: INITIAL_INVESTMENT // It starts with the initial investment. + assetA: INITIAL_INVESTMENT_A, + assetB: INITIAL_INVESTMENT_B // It starts with the initial investment. }, availableBalance: { // This is the balance the bot has at any moment in time available to be traded (not in positions at the order book). - assetA: 0, - assetB: INITIAL_INVESTMENT // It starts with the initial investment. + assetA: INITIAL_INVESTMENT_A, + assetB: INITIAL_INVESTMENT_B // It starts with the initial investment. }, profits: { assetA: 0, diff --git a/Poloniex API Client.js b/Poloniex API Client.js index f0891d6d9..f8bfd88b9 100644 --- a/Poloniex API Client.js +++ b/Poloniex API Client.js @@ -403,15 +403,16 @@ logger.write("[INFO] analizeResponse -> exchangeErr = " + stringExchangeErr); logger.write("[INFO] analizeResponse -> exchangeResponse = " + stringExchangeResponse); - if ( - stringExchangeResponse.indexOf("Connection timed out") > 0 || - stringExchangeResponse.indexOf("Connection Error") > 0 || - stringExchangeResponse.indexOf("Bad gateway") > 0 || - stringExchangeErr.indexOf("ETIMEDOUT") > 0 || + if (stringExchangeErr.indexOf("ETIMEDOUT") > 0 || stringExchangeErr.indexOf("ENOTFOUND") > 0 || stringExchangeErr.indexOf("ECONNREFUSED") > 0 || stringExchangeErr.indexOf("ESOCKETTIMEDOUT") > 0 || - stringExchangeErr.indexOf("ECONNRESET") > 0) { + stringExchangeErr.indexOf("ECONNRESET") > 0 || + (stringExchangeResponse !== undefined && ( + stringExchangeResponse.indexOf("Connection timed out") > 0 || + stringExchangeResponse.indexOf("Connection Error") > 0 || + stringExchangeResponse.indexOf("Bad gateway") > 0 || + stringExchangeResponse.indexOf("Internal error. Please try again") > 0))) { logger.write("[WARN] analizeResponse -> Timeout reached or connection problem while trying to access the Exchange API. Requesting new execution later."); notOkCallBack(global.DEFAULT_RETRY_RESPONSE);