From 2ad8ccb812a9e783674bcb9cb62136845387ad2f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 27 Apr 2018 10:07:04 +0200 Subject: [PATCH] Live Trade Working --- .vs/AACloud/v15/.suo | Bin 97280 -> 102912 bytes Assistant.js | 377 ++++++++++++++++++------------- Context.js | 6 +- Poloniex API Client.js | 7 + Run.js | 14 +- Trading Bot Process Main Loop.js | 13 +- 6 files changed, 253 insertions(+), 164 deletions(-) diff --git a/.vs/AACloud/v15/.suo b/.vs/AACloud/v15/.suo index b688e9640951fec1bb5f6b00e0dcac05aee0f312..b3c983cd6b964ff01058aee6a512eb45c1d166a2 100644 GIT binary patch delta 3665 zcmd6p3s6+&7035nF3Tq;{VokUcw#F3Kn2^{>MVn5?Nt%?58FixVG=n3?8KM6JEJ~twI&Eh9 zF+a|^-*@hJzk6PH-M50>hmE^3IrCNXc#FYcG#CuQpnqUs0K!1viPuUdDrWyJ)z+&(WR|e0@ zlUW(hVP+NhF?bWK25UeK5ZBg%-+`Zip*sc!VxXJ()8OsF2bV@Hy2H;Iw**`qj-{`< z*m_;!f)TjCgw3Y%qeSQ}pqA8&peGw*;d%w{yNY2^3AA8#*wkN9w5^kBNlZT#8%8M@7q zpqNSAqk!QI453q8AK9SPBR?ZIDRl;3yP(Kqg@NQ_z(}bz=r^PO6s?BcGpXN5E)E%d zgd|YFl;!ed4^D%(z!~s1cn5TWcR_!1m+updZheg%0Dyvm!q z9(m$7(0&V!f#X0dT#z=PgEAmNd>wQEA9%01WOn>nD1QX79~tLxeSUZjQ9b27ueQIx zj+D^J5>d~^>)}U(3yK5e@)e#uWRw8AqSx72d;Mn43dF`!`lHj7tVB5CF zXi?fQDgFs8=x=Uw5j)RLU`Mx&D>{wr`0zMg<$r!yM>V|Jk*`guq*Olj(E>g7uVq1+ zdn~y__oLFIu!+gQ-|Fk+%&Qj8y>>uh<& zPuXU#TDd}Vn<L>5@!L}foH5c5iHnas^*5}sUpF1eTm7SfpaYGC4&hBth8_{n4*cXRrbPRNN zZ!YQn8wCm%4w%^5_jj*HY2SN>_{^P|dVimTbjO!HiqED{4{>96BJb*+q)`~z^@{!j z)M_N9MJ2ydRqOMYkyBBokxw;K4rz{1@@pkw6lax;3k)gKM}6sIsEFvcuVEQ&A+Bq% z`l2Ezjbh~+KU6U&n89%y|Lwj-W9f+65ktkH6fLc1pXI+Rt%uZ(^b4sSkqiG#$$quY zhDz3nE*g)!MNWNSAwR4YyfSos$5YV zQPf&Ysupji#nPujjHUI}=pLG1C2d^pNE@*GONVurv^g!tMAu^$Bg{j??w4P&^6Sub zAD?K)%*4FSC6VR#yfc4(yfoxZ7KDE~hG)9roll5JPO(va#u iI;@ZKnPuSnEpXC^yWD}V{Ws^Kjw)JW@hz#L!}Kq@k4FFi delta 2934 zcmdVb2~bpZ90&0I{r6xO7M4qN6k=Jmytq68uSHPt0KxLc0Gk{&H4ij2mGvT0B-x+w z%q!e6CXZD1C0mBFYU5}*rRkV8lS0{yV>+4FWK+=hk1#rpIW;!T^kzQuKi|Ip`~QCb zw~ieK$FKVJaZy6kdam9G!WuBO4pV8bGoiw^lNDMgB~yr(qI52fFHDn z0gYzD~ZWuuhgSi-AK zBTTYq1zIs>S*`t-qLK@;GpZ~rQVsXjiB2V0@ETYPa&y+9EQPmVJ(R&A;573dzP+m- zU$B_VX0GQ`{ym*$>(kus4onezOdC&c@`B((@i`JVc$xT&cwtDoscDt|SZ#9x`-qdI z-VOOGR6TJpiFidjo7$m^)nE+56q!~n(>TB8OB2agl)KQrZpzxL$FTkjb zf=qgu)DGawmVT-=qfYM(PT`atRSz%ez0tadS7SbjEs=*vUaqkL&XM`zFjhWtnL(bq zOpE%P*PwMx)3Kw2jdz5Fs{^JUCB+#`ZIwBrDV6P6W_8Bwa#KgS)RnkJmMJJ#!$f!k zieWn(D$B~@RWq~8=7dHZM!Ocga0HIRG58c3%JP18Ef(&Ad@@Wx+ZDu%7ZoXGc@(S0 zE<2-lEfLn`=$F4OpDE)lpC`|ltnWg}*7L=odr{c~A2bhy<89TN^Qq)2);TZF36DVg z8HfZcM1c*u0he6vP#BF$48%el#6x%J0X-oRdP5&b0QvMJlzpKe^oId35Kk{Yu$cQt^1+=oB_xwE;YrE326w#!*bG+K3ftgakY7I&FF*d=Fqw`r6J!?b1DTeoPz5Ty0NddJ zRD%aTf{)=Kd;&GlP?cvPKD{u2536RbaG&?wc$NaS?o$OM?x!eUIopPF-A0eK3)TG# zY3lsqn*S`VMq|d)Wf{#;wPz?>`K2%Q;z0-6vc1-d=NZ_3VIQ658yE7O<0tXg-;D8# zYx>i|ZFt4VZ}YUinHG&E1T7z=Q2hqw;xw{(>{H00Rr1D@{r`Iy`1JQNTwb?}&(&{Y zTT5lPvq zy|GTH*5B($*^@vWcztcSvfP)f%IWD8r$mgRA9#5Ec*QY~dbJ$Dp$No-V}t`(DM*wX z$xEG;x%t!{FJ(|J&7)b$1QYG7G?3t`i(!giK3x-O8jrz`l1L=RD0VY>dHIzH|H<-L zH(l}G7d<%(X$V=awsgsoG@il$IwD%nKtrYzvZ?6`_UiFj3H6K%qMf?R86>L1DASkB zo-8YE4*YMj?`dM_zPYC Candles more than two minutes old. Retrying later."); - callBack(global.DEFAULT_RETRY_RESPONSE); + logger.write("[INFO] initialize -> getMarketRate -> onFileReceived -> Market Rate " + delay + " minutes old." ); + + if (delay > 30) { + + logger.write("[ERROR] initialize -> getMarketRate -> onFileReceived -> Market Rate more than 10 minutes old. Retrying later."); + + callBackFunction(global.DEFAULT_RETRY_RESPONSE); return; } @@ -188,6 +193,8 @@ function onDone(err) { try { + if (FULL_LOG === true) { logger.write("[INFO] initialize -> validateExchangeSyncronicity -> onDone -> Entering function."); } + switch (err.result) { case global.DEFAULT_OK_RESPONSE.result: { logger.write("[INFO] initialize -> validateExchangeSyncronicity -> onDone -> Execution finished well. :-)"); @@ -336,194 +343,218 @@ try { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> Entering function."); } + let position = context.executionContext.positions[i]; + let exchangePosition; for (let j = 0; j < exchangePositions.length; j++) { if (position.id === exchangePositions[j].id) { - /* + exchangePosition = exchangePositions[j]; + positionFound(); + return; + } + } - We need to know if the order was partially executed. To know that, we compare the amounts on file with the ones - received from the exchange. If they are the same, then the order is intact. Otherwise, to confirm a partial execution, - we will request the associated trades from the exchange. + positionNotFound(); + return; - */ + function positionFound() { - if (position.amountB === parseFloat(exchangePositions[j].amountB)) { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionFound -> Entering function."); } - /* Position is still there, untouched. Nothing to do here. */ + /* + + We need to know if the order was partially executed. To know that, we compare the amounts on file with the ones + received from the exchange. If they are the same, then the order is intact. Otherwise, to confirm a partial execution, + we will request the associated trades from the exchange. + + */ - next(); - return; + if (position.amountB === parseFloat(exchangePosition.amountB)) { - } else { + /* Position is still there, untouched. Nothing to do here. */ - getPositionTradesAtExchange(position.id, confirmOrderWasPartiallyExecuted); - return; + next(); + return; - function confirmOrderWasPartiallyExecuted(pTrades) { + } else { - try { - /* + getPositionTradesAtExchange(position.id, confirmOrderWasPartiallyExecuted); + return; - To confirm everything is ok, we will add all the amounts on trades plus the remaining amounts - at the order at the exchange and they must be equal to the one on file. Otherwise something very strange could - have happened, in which case we will halt the bot execution. + function confirmOrderWasPartiallyExecuted(pTrades) { - */ + try { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionFound -> confirmOrderWasPartiallyExecuted -> Entering function."); } - let sumAssetA = 0; - let sumAssetB = 0; + /* + + To confirm everything is ok, we will add all the amounts on trades plus the remaining amounts + at the order at the exchange and they must be equal to the one on file. Otherwise something very strange could + have happened, in which case we will halt the bot execution. + + */ - for (let k = 0; k < pTrades.length; k++) { + let sumAssetA = 0; + let sumAssetB = 0; - sumAssetA = sumAssetA + pTrades[k].amountA; - sumAssetB = sumAssetB + pTrades[k].amountB; + for (let k = 0; k < pTrades.length; k++) { - } + sumAssetA = sumAssetA + pTrades[k].amountA; + sumAssetB = sumAssetB + pTrades[k].amountB; - /* To this we add the current position amounts. */ + } - sumAssetA = sumAssetA + exchangePositions[j].amountA; - sumAssetB = sumAssetB + exchangePositions[j].amountB; + /* To this we add the current position amounts. */ - /* And finally we add the fees */ + sumAssetA = sumAssetA + exchangePosition.amountA; + sumAssetB = sumAssetB + exchangePosition.amountB; - sumAssetA = sumAssetA + exchangePositions[j].fee; + /* And finally we add the fees */ - if ( - position.amountA !== sumAssetA || - position.amountB !== sumAssetB - ) { - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> Cannot be confirmed that a partially execution was done well."); - callBack(global.DEFAULT_FAIL_RESPONSE); - return; - } + sumAssetA = sumAssetA + exchangePosition.fee; - /* - - Confirmed that order was partially executed. Next thing to do is to remember the trades and the new position. - - */ - - position.amountA = exchangePositions[j].amountA; - position.amountB = exchangePositions[j].amountB; - position.date = (new Date(exchangePositions[j].date)).valueOf(); - - applyTradesToContext(pTrades); - - let newTransaction = { - type: position.type + " partially executed", - position: position - }; - - context.executionContext.transactions.push(newTransaction); - - /* All done. */ - - next(); - - } catch (err) { - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> err = " + err.message); + if ( + position.amountA !== sumAssetA || + position.amountB !== sumAssetB + ) { + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionFound > confirmOrderWasPartiallyExecuted -> Cannot be confirmed that a partially execution was done well."); callBack(global.DEFAULT_FAIL_RESPONSE); return; } + + /* + + Confirmed that order was partially executed. Next thing to do is to remember the trades and the new position. + + */ + + position.amountA = exchangePosition.amountA; + position.amountB = exchangePosition.amountB; + position.date = (new Date(exchangePosition.date)).valueOf(); + + applyTradesToContext(pTrades); + + let newTransaction = { + type: position.type + " partially executed", + position: position + }; + + context.executionContext.transactions.push(newTransaction); + + /* All done. */ + + next(); + + } catch (err) { + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionFound > confirmOrderWasPartiallyExecuted -> err = " + err.message); + callBack(global.DEFAULT_FAIL_RESPONSE); + return; } } } } - /* Position not found: we need to know if the order was executed. */ + function positionNotFound() { - getPositionTradesAtExchange(position.id, confirmOrderWasExecuted); + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> Entering function."); } - function getPositionTradesAtExchange(pPositionId, innerCallBack) { + /* Position not found: we need to know if the order was executed. */ - try { + getPositionTradesAtExchange(position.id, confirmOrderWasExecuted); - if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> Entering function."); } - if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> pPositionId = " + pPositionId); } + function getPositionTradesAtExchange(pPositionId, innerCallBack) { - /* - - Given one position, we request all the associated trades to it. - - */ + try { - if (bot.backTestingMode === true) { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> Entering function."); } + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> pPositionId = " + pPositionId); } - if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> Backtest Mode Detected."); } + /* + + Given one position, we request all the associated trades to it. + + */ - let trades = []; + if (bot.backTestingMode === true) { - /* We look for the position at the executionContext */ + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> Backtest Mode Detected."); } - for (let i = 0; i < context.executionContext.positions.length; i++) { + let trades = []; - let thisPosition = context.executionContext.positions[i]; + /* We look for the position at the executionContext */ - if (thisPosition.id === pPositionId) { + for (let i = 0; i < context.executionContext.positions.length; i++) { - 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 thisPosition = context.executionContext.positions[i]; - trades.push(trade); + if (thisPosition.id === pPositionId) { - onResponse(global.DEFAULT_OK_RESPONSE, trades); + 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() + } - return; - } - } + trades.push(trade); - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> Position not found at Executioin Context."); - callBack(global.DEFAULT_FAIL_RESPONSE); - return; - - } else { + onResponse(global.DEFAULT_OK_RESPONSE, trades); - if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> Live Mode Detected."); } - exchangeAPI.getExecutedTrades(pPositionId, onResponse); - - } - - function onResponse(err, pTrades) { - - try { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { // Everything went well, we have the information requested. - logger.write("[INFO] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> onResponse -> Execution finished well. :-)"); - innerCallBack(pTrades); - } - break; - case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> onResponse -> Retry Later. Requesting Execution Retry."); - callBack(global.DEFAULT_RETRY_RESPONSE); return; } - break; - case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> onResponse -> Operation Failed. Aborting the process."); - callBack(global.DEFAULT_FAIL_RESPONSE); - return; - } - break; } - } catch (err) { - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> onResponse -> err = " + err.message); + + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> Position not found at Executioin Context."); callBack(global.DEFAULT_FAIL_RESPONSE); + return; + + } else { + + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> Live Mode Detected."); } + exchangeAPI.getExecutedTrades(pPositionId, onResponse); + } + + function onResponse(err, pTrades) { + + try { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> onResponse -> Entering function."); } + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { // Everything went well, we have the information requested. + logger.write("[INFO] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> onResponse -> Execution finished well. :-)"); + innerCallBack(pTrades); + } + break; + case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> onResponse -> Retry Later. Requesting Execution Retry."); + callBack(global.DEFAULT_RETRY_RESPONSE); + return; + } + break; + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> onResponse -> Operation Failed. Aborting the process."); + callBack(global.DEFAULT_FAIL_RESPONSE); + return; + } + break; + } + } catch (err) { + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> onResponse -> err = " + err.message); + callBack(global.DEFAULT_FAIL_RESPONSE); + } + } + } catch (err) { + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> positionNotFound -> getPositionTradesAtExchange -> err = " + err.message); + callBack(global.DEFAULT_FAIL_RESPONSE); } - } catch (err) { - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> err = " + err.message); - callBack(global.DEFAULT_FAIL_RESPONSE); } } @@ -547,21 +578,19 @@ for (let k = 0; k < pTrades.length; k++) { - sumAssetA = sumAssetA + pTrades[k].amountA; - sumAssetB = sumAssetB + pTrades[k].amountB; + sumAssetA = sumAssetA + Number(pTrades[k].amountA); + sumAssetB = sumAssetB + Number(pTrades[k].amountB); + + /* We add the fees */ + + sumAssetA = sumAssetA - Number(pTrades[k].fee); } - /* We add the fees */ - - //sumAssetA = sumAssetA + exchangePositions[j].fee; - if ( - position.amountA !== sumAssetA || position.amountB !== sumAssetB ) { logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Cannot be confirmed that the order was executed. It must be manually cancelled by the user or cancelled by the exchange itself."); - logger.write("[HINT] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Verify also that you were not running under a Exchange Simulation and you turned it off without deleting the Status Report file."); logger.write("[HINT] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> If the process was abruptally cancelled and then restarted, it is possible that now is not sincronized with the exchange."); logger.write("[HINT] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> In any case, to continue, you must delete the Status Report file so as to start over. Also, you must manually delete the orders at the exchange."); callBack(global.DEFAULT_FAIL_RESPONSE); @@ -616,18 +645,18 @@ if (trade.type === 'buy') { - context.executionContext.balance.assetA = context.executionContext.balance.assetA - trade.amountA; - context.executionContext.balance.assetB = context.executionContext.balance.assetB + trade.amountB; + 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 + trade.amountB; + context.executionContext.availableBalance.assetB = context.executionContext.availableBalance.assetB + Number(trade.amountB); } if (trade.type === 'sell') { - context.executionContext.balance.assetA = context.executionContext.balance.assetA + trade.amountA; - context.executionContext.balance.assetB = context.executionContext.balance.assetB - trade.amountB; + 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 + trade.amountA; + context.executionContext.availableBalance.assetA = context.executionContext.availableBalance.assetA + Number(trade.amountA); } } @@ -681,6 +710,8 @@ function next() { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> next -> Entering function."); } + i++; controlLoop(); @@ -688,6 +719,8 @@ function controlLoop() { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> controlLoop -> Entering function."); } + if (i < context.executionContext.positions.length) { loopBody(); @@ -701,6 +734,8 @@ function final() { + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> final -> Entering function."); } + callBack(global.DEFAULT_OK_RESPONSE); } @@ -720,6 +755,12 @@ if (FULL_LOG === true) { logger.write("[INFO] putPosition -> pAmountA = " + pAmountA); } if (FULL_LOG === true) { logger.write("[INFO] putPosition -> pAmountB = " + pAmountB); } + /* Removing extra decimals. */ + + pRate = Number(pRate.toFixed(8)); + pAmountA = Number(pAmountA.toFixed(8)); + pAmountB = Number(pAmountB.toFixed(8)); + /* Validations that the limits are not surpassed. */ if (pType === 'buy') { @@ -738,6 +779,24 @@ callBackFunction(err); return; } + + let aRate = Number((pAmountA / pRate).toFixed(8)); + + if (aRate !== pAmountB) { + + logger.write("[ERROR] putPosition -> Input Validations -> pAmountA / pRate !== pAmountB."); + logger.write("[ERROR] putPosition -> Input Validations -> pAmountA = " + pAmountA); + logger.write("[ERROR] putPosition -> Input Validations -> pAmountB = " + pAmountB); + logger.write("[ERROR] putPosition -> Input Validations -> pRate = " + pRate); + + let err = { + result: global.DEFAULT_FAIL_RESPONSE.result, + message: 'pAmountB * pRate !== pAmountA' + }; + + callBackFunction(err); + return; + } } if (pType === 'sell') { @@ -756,22 +815,24 @@ callBackFunction(err); return; } - } - if (pAmountB * pRate !== pAmountA) { + let bRate = Number((pAmountB * pRate).toFixed(8)); - logger.write("[ERROR] putPosition -> Input Validations -> pAmountB * pRate !== pAmountA."); - logger.write("[ERROR] putPosition -> Input Validations -> pAmountA = " + pAmountA); - logger.write("[ERROR] putPosition -> Input Validations -> pAmountB = " + pAmountB); - logger.write("[ERROR] putPosition -> Input Validations -> pRate = " + pRate); + if (bRate !== pAmountA) { - let err = { - result: global.DEFAULT_FAIL_RESPONSE.result, - message: 'pAmountB * pRate !== pAmountA' - }; + logger.write("[ERROR] putPosition -> Input Validations -> pAmountB * pRate !== pAmountA."); + logger.write("[ERROR] putPosition -> Input Validations -> pAmountA = " + pAmountA); + logger.write("[ERROR] putPosition -> Input Validations -> pAmountB = " + pAmountB); + logger.write("[ERROR] putPosition -> Input Validations -> pRate = " + pRate); - callBackFunction(err); - return; + let err = { + result: global.DEFAULT_FAIL_RESPONSE.result, + message: 'pAmountB * pRate !== pAmountA' + }; + + callBackFunction(err); + return; + } } /* All validations passed, we proceed. */ diff --git a/Context.js b/Context.js index b0f2ff6c4..c462c30f9 100644 --- a/Context.js +++ b/Context.js @@ -208,8 +208,8 @@ /* Move some values to the new record, in case there are no transactions that re-calculate them. */ - thisObject.newHistoryRecord.buyAvgRate = thisObject.executionHistory[1]; - thisObject.newHistoryRecord.sellAvgRate = thisObject.executionHistory[2]; + thisObject.newHistoryRecord.buyAvgRate = thisObject.executionHistory[thisObject.executionHistory.length - 1][1]; + thisObject.newHistoryRecord.sellAvgRate = thisObject.executionHistory[thisObject.executionHistory.length - 1][2]; getExecutionContext(callBack); @@ -386,7 +386,7 @@ once we advance into multiple competitions scheme. */ - const INITIAL_INVESTMENT = 0.0001; // This is just for this release of the platform. + const INITIAL_INVESTMENT = 0.001; // This is just for this release of the platform. thisObject.executionContext = { investment: { // This is used to calculate profits. diff --git a/Poloniex API Client.js b/Poloniex API Client.js index e6bf2641e..16504a1dd 100644 --- a/Poloniex API Client.js +++ b/Poloniex API Client.js @@ -400,6 +400,13 @@ logger.write("[INFO] analizeResponse -> exchangeErr = " + JSON.stringify(exchangeErr)); logger.write("[INFO] analizeResponse -> exchangeResponse = " + JSON.stringify(exchangeResponse)); + if (JSON.stringify(exchangeResponse).indexOf("error") > 0) { + + logger.write("[ERROR] analizeResponse -> Unexpected response from the Exchange."); + notOkCallBack(global.DEFAULT_FAIL_RESPONSE); + return; + } + if ( JSON.stringify(exchangeResponse).indexOf("Connection timed out") > 0 || JSON.stringify(exchangeResponse).indexOf("Connection Error") > 0 || diff --git a/Run.js b/Run.js index a9d385a0c..a837572d7 100644 --- a/Run.js +++ b/Run.js @@ -283,6 +283,12 @@ for (let p = 0; p < global.PLATFORM_CONFIG.executionList.length; p++) { let month = pad((new Date()).getUTCMonth() + 1, 2); let year = (new Date()).getUTCFullYear(); + if (processConfig.startMode.noTime.resumeExecution === "true") { + botConfig.hasTheBotJustStarted = false; + } else { + botConfig.hasTheBotJustStarted = true; + } + switch (botConfig.type) { case 'Extraction': { runExtractionBot(botConfig, processConfig, month, year); @@ -331,6 +337,12 @@ for (let p = 0; p < global.PLATFORM_CONFIG.executionList.length; p++) { processConfig.normalWaitTime = processConfig.startMode.timePeriod.waitTime; processConfig.retryWaitTime = processConfig.startMode.timePeriod.waitTime; + if (processConfig.startMode.noTime.resumeExecution === "true") { + botConfig.hasTheBotJustStarted = false; + } else { + botConfig.hasTheBotJustStarted = true; + } + switch (botConfig.type) { case 'Trading': { runTradingBot(botConfig, processConfig); @@ -460,8 +472,6 @@ for (let p = 0; p < global.PLATFORM_CONFIG.executionList.length; p++) { try { if (FULL_LOG === true) { logger.write("[INFO] runTradingBot -> Entering function."); } - pBotConfig.hasTheBotJustStarted = true; - let tradingBotMainLoop = TRADING_BOT_MAIN_LOOP_MODULE.newTradingBotMainLoop(pBotConfig); tradingBotMainLoop.initialize(listItem.botPath, pProcessConfig, onInitializeReady); diff --git a/Trading Bot Process Main Loop.js b/Trading Bot Process Main Loop.js index 43e74dd8f..4ed118198 100644 --- a/Trading Bot Process Main Loop.js +++ b/Trading Bot Process Main Loop.js @@ -106,7 +106,18 @@ if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> Live Mode detected."); } - bot.processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. + // This will be considered the process date and time, so as to have it consistenly all over the execution. + + let localDate = new Date(); + + bot.processDatetime = new Date(Date.UTC( + localDate.getUTCFullYear(), + localDate.getUTCMonth(), + localDate.getUTCDate(), + localDate.getUTCHours(), + localDate.getUTCMinutes(), + localDate.getUTCSeconds(), + localDate.getUTCMilliseconds())); }