Merge branch 'master' of https://github.com/AdvancedAlgos/AACloudPlatform
This commit is contained in:
commit
6c1c709ab1
4 files changed
+73
-67
No files matched your search
Binary file not shown.
+58
-54
@@ -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) {
|
||||
|
||||
+8
-7
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user