From be2b46c13abb32ad602ca718d775f234d09b19d5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 27 Feb 2018 11:20:39 +0100 Subject: [PATCH] Several bug fixed during debugging --- .vs/AAMariam/v15/.suo | Bin 19968 -> 23552 bytes Trading-Process/Interval.js | 251 ++++++++++++++++++++++++++++++++---- 2 files changed, 224 insertions(+), 27 deletions(-) diff --git a/.vs/AAMariam/v15/.suo b/.vs/AAMariam/v15/.suo index 73e1ef83ca080682d9008b08a03c32b6de1c3669..5d8bc44e136544045379c38163e8f450b3971e55 100644 GIT binary patch delta 2096 zcmc&#|4&<06uJ1g9dybjTb*i+Gw8}kzm zl--CHgctD$f^6*-PV#P)SI4^AyO9SF^%YxRg*PByA4_vx$klha!~Yc6S$>ZYtVRg1 zS?4=WTc+(&2=rMZ3WdlFVSBW-MIo5BT9h9&gdSW&=`cfRLfHtx{<}Pg@_xiqh&W;h zaR8A(sEEUeVZ?O_D`b~h-F#-3+TGmgk2CJ;vv&mtxfG?VZG%E!Rz=w3aZ)7vzj zhK06qn;92Xm8am2h$n&aQG+0xp-;ksbKKTdrLvMo2AfSO(O*%UvmqX|z^ktB?s5&9 z|1Ev5DpCb0rRL@|(`*LiAnNlh#}=4|x;&dhO&a+W+7rkZP@@?h_QU7GKI?NR+hEhx&1LY{j9Z__ z@fBF@OX(g9eCd81*80?nsJqmA*I^f~Gb6$}jKYQ>TMwf08f^N$axyE5=Ix`X;dXsS z+<{6ehq^NBThyaucOKew;QuISG7WG8g()YYUq-fPDJV8lXmb+P;BOWs0p`lHL)j`!ZZQ2OmAO`uiPL~H!sW?lnl+`YPFvq`?#%gV3eRvd@; zIT-Rz!O50D*Ddjz1BIO>Y2wYB*DSVo=q~)t7X&@Sup*s?6?eNHNx(*Q5Q^JekE+ID zq{c*_H&UWSF>ST7qpHgP&0gv^{$)WxYk0dX?EXR!se86nZ?@QPfj>_*QAaReC~joX68ZJxyTi;7H~Z+4-sI z*}0db%zXaDOrdaWWKNrzU!2MXPZgGzUzZhCj>aOgG<+~DOG-phrI;+sQbHLTmSr`n z#$rQnfIaysH@qEs2;Kyy8on|Q!Iz^dniM3Hq4xVYH75A(GXXwJ_x47XF?n}UE8}?N z4`bp@qGxR%W93&;CpnhjI{cW9xas!Lqf8GBHD4t7I=B;0bQ=xfY|CKX!q0TYO;QEmO1iU{rYet({M0E~Z%PCgCxyEIIxJTJO?R4T1YkdQ{Cr+KZY-x#b O;UWVr7cC>$f&KzS5U~dU delta 1012 zcmb_bF>ljA6!u*%jg#D^rlL(!r$9;yEC^asAe93Sfs#_ufEqx`LPiEaAh{A`fJ&?# zh~LWO0kNUrg@FM?#6Ts41S10ye}JKt$O62VCY3uO5KsF2&hLG9{@!PM^(9+DiLbYxjYXe-VtX`Avv)(2H(3QXhaNEr_d^%pEuU64wk(Dh3<*q_t%jcc`D-gE zC$N!1j3elN7v?D373S;&u85~F5BJw5=Dmo0h%90fu^*8`Od$-vEvl^5$hiDw;!bO+ z5edp$fO2M_B5FY!_5fnxX~-gNK*i*8*&C3(`V+hAw&zB5x}dOE;V z^gWPXijZHb7Z}W4d)K!!y~9pq(75xM&!e^GwzW>r$YuQ>O$^hi8g$d||F4Szh5nt3 z>q?8`CbFHP_lBI|HMV68eViCzUwLi#AH=s~*B`t&Y!+_hzs#nfG 0) { - const logText = "[WARN] onExchangeCallReturned - Timeout reached while trying to access the Exchange API. Requesting new execution later. : ERROR = " + err.message; + const logText = "[WARN] getOrderTradesAtExchange - onExchangeCallReturned - Timeout reached while trying to access the Exchange API. Requesting new execution later. : ERROR = " + err.message; logger.write(logText); /* We abort the process and request a new execution at the configured amount of time. */ @@ -705,7 +711,7 @@ if (err.message.indexOf("ECONNRESET") > 0) { - const logText = "[WARN] onExchangeCallReturned - The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message; + const logText = "[WARN] getOrderTradesAtExchange - onExchangeCallReturned - The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message; logger.write(logText); /* We abort the process and request a new execution at the configured amount of time. */ @@ -716,14 +722,14 @@ } else { - const logText = "[ERROR] onExchangeCallReturned - Unexpected error trying to contact the Exchange. This will halt this bot process. : ERROR = " + err.message; + const logText = "[ERROR] getOrderTradesAtExchange - onExchangeCallReturned - Unexpected error trying to contact the Exchange. This will halt this bot process. : ERROR = " + err.message; logger.write(logText); return; } } } catch (err) { - const logText = "[ERROR] onExchangeCallReturned : ERROR : exchangeResponse.error = " + exchangeResponse.error; + const logText = "[ERROR] getOrderTradesAtExchange -onExchangeCallReturned : ERROR : exchangeResponse.error = " + exchangeResponse.error; logger.write(logText); return; } @@ -774,7 +780,7 @@ } } catch (err) { - const logText = "[ERROR] 'onExchangeCallReturned' - ERROR : " + err.message; + const logText = "[ERROR] 'getOrderTradesAtExchange - onExchangeCallReturned' - ERROR : " + err.message; logger.write(logText); } @@ -1079,11 +1085,11 @@ if (executionContext.positions[0].type === "buy") { - validateBuyPosition(); + validateBuyPosition(executionContext.positions[0]); } else { - validateSellPosition(); + validateSellPosition(executionContext.positions[0]); } @@ -1150,16 +1156,61 @@ } - function validateBuyPosition() { + function validateBuyPosition(pPosition) { + /* For simplicity of this example bot, we will do the same than when we are selling. */ + + validateSellPosition(pPosition); } - function validateSellPosition() { + function validateSellPosition(pPosition) { + let candleArray; + let candle; + let weight; + + /* + + Keeping in mind this is an example of traing bot, we are going to put some logic here that in the end will move the current position + up or down. It will move it down if the bot feels it is time to sell, and up if it feels that selling is not a good idea. + + To achieve a final rate to move the current position at the exchange, we are going to go through the available candles and patterns + and each one is going to make a micro-move, and at the end we will have a final rate to send a move command to the exchange. + + We will use a weight to give more or less importance to different Time Periods. + + */ + + let diff; + let variationPercentage; + let timePeriodName; + + let targetRate = pPosition.rate; + + let weightArray = [1 / (24 * 60), 1 / (12 * 60), 1 / (8 * 60), 1 / (6 * 60), 1 / (4 * 60), 1 / (3 * 60), 1 / (2 * 60), 1 / (1 * 60)]; + + for (i = 0; i < marketFilesPeriods.length; i++) { + + weight = weightArray[i]; + + timePeriodName = marketFilesPeriods[i][1]; + + candleArray = candlesMap.get(timePeriodName); + candle = candleArray[candleArray.length - 1]; // The last candle of the 10 candles array. + + diff = candle.close - candle.open; + variationPercentage = diff * 100 / candle.open; // This is the % of how much the rate increased or decreced from open to close. + + targetRate = targetRate + targetRate * variationPercentage / 100 * weight; + + } + + /* Finally we move the order position to where we have just estimated is a better place. */ + + movePositionAtExchange(pPosition, targetRate, writeStatusAndContext); } - } function putPositionAtExchange(pType, pRate, pAmountA, pAmountB, callBackFunction) { @@ -1183,7 +1234,7 @@ if (err.message.indexOf("ETIMEDOUT") > 0) { - const logText = "[WARN] onExchangeCallReturned - Timeout reached while trying to access the Exchange API. Requesting new execution later. : ERROR = " + err.message; + const logText = "[WARN] putPositionAtExchange - onExchangeCallReturned - Timeout reached while trying to access the Exchange API. Requesting new execution later. : ERROR = " + err.message; logger.write(logText); /* We abort the process and request a new execution at the configured amount of time. */ @@ -1195,7 +1246,7 @@ if (err.message.indexOf("ECONNRESET") > 0) { - const logText = "[WARN] onExchangeCallReturned - The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message; + const logText = "[WARN] putPositionAtExchange - onExchangeCallReturned - The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message; logger.write(logText); /* We abort the process and request a new execution at the configured amount of time. */ @@ -1206,14 +1257,14 @@ } else { - const logText = "[ERROR] onExchangeCallReturned - Unexpected error trying to contact the Exchange. This will halt this bot process. : ERROR = " + err.message; + const logText = "[ERROR] putPositionAtExchange -onExchangeCallReturned - Unexpected error trying to contact the Exchange. This will halt this bot process. : ERROR = " + err.message; logger.write(logText); return; } } } catch (err) { - const logText = "[ERROR] onExchangeCallReturned : ERROR : exchangeResponse.error = " + exchangeResponse.error; + const logText = "[ERROR] putPositionAtExchange - onExchangeCallReturned : ERROR : exchangeResponse.error = " + exchangeResponse.error; logger.write(logText); return; } @@ -1254,16 +1305,153 @@ rate: pRate, amountA: pAmountA, amountB: pAmountB, - date: (processDatetime.valueOf()) + date: (processDatetime.valueOf()), + status: "open", + trades: [] }; executionContext.positions.push(position); + newHistoryRecord.newPositions++; + callBackFunction(); } } catch (err) { - const logText = "[ERROR] 'onExchangeCallReturned' - ERROR : " + err.message; + const logText = "[ERROR] 'putPositionAtExchange - onExchangeCallReturned' - ERROR : " + err.message; + logger.write(logText); + + } + } + + } + + function movePositionAtExchange(pPosition, pNewRate, callBackFunction) { + + poloniexApiClient.moveOrder(pPosition.id, pNewRate, pPosition.AmountB, onExchangeCallReturned); + + function onExchangeCallReturned(err, exchangeResponse) { + + try { + + if (err || exchangeResponse.error !== undefined) { + try { + + if (err.message.indexOf("ETIMEDOUT") > 0) { + + const logText = "[WARN] movePositionAtExchange - onExchangeCallReturned - Timeout reached while trying to access the Exchange API. Requesting new execution later. : ERROR = " + err.message; + logger.write(logText); + + /* We abort the process and request a new execution at the configured amount of time. */ + + callBackFunction(true, nextIntervalLapse); + return; + + } else { + + if (err.message.indexOf("ECONNRESET") > 0) { + + const logText = "[WARN] movePositionAtExchange - onExchangeCallReturned - The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message; + logger.write(logText); + + /* We abort the process and request a new execution at the configured amount of time. */ + + callBackFunction(true, nextIntervalLapse); + return; + + } else { + + + const logText = "[ERROR] movePositionAtExchange -onExchangeCallReturned - Unexpected error trying to contact the Exchange. This will halt this bot process. : ERROR = " + err.message; + logger.write(logText); + return; + } + } + + } catch (err) { + const logText = "[ERROR] movePositionAtExchange - onExchangeCallReturned : ERROR : exchangeResponse.error = " + exchangeResponse.error; + logger.write(logText); + return; + } + + return; + + } else { + + /* + + This is what we can receive from the exchange. We will ignore the trades list for now, and analize + what happened at the next bot execution. + + { + "orderNumber":31226040, + "resultingTrades": + [{ + "amount":"338.8732", + "date":"2014-10-18 23:03:21", + "rate":"0.00000173", + "total":"0.00058625", + "tradeID":"16164", + "type":"buy" + }] + } + + */ + + /* + + We will add this new position to the executionContext. + + */ + + if (exchangeResponse.success !== 1) { + + const logText = "[ERROR] movePositionAtExchange -onExchangeCallReturned - Exchange said operation was not successful. Since we do not know what this means we will halt this bot process. : ERROR = " + err.message; + logger.write(logText); + return; + + } + + let newPosition = { + id: exchangeResponse.orderNumber, + type: pType, + rate: pRate, + amountA: pAmountB * pRate, + amountB: pAmountB, + date: (processDatetime.valueOf()), + status: "open", + trades: [] + }; + + let oldPosition = JSON.parse(JSON.stringify(pPosition)); + + /* We need to update the position we have on file. */ + + for (let i = 0; i < executionContext.positions.length; i++) { + + if (executionContext.positions[i].id === pOrderId) { + + executionContext.positions[i] = newPosition; + + break; + } + } + + let newTransaction = { + type: "movePosition", + oldPosition: oldPosition, + newPosition: newPosition + }; + + executionContext.transactions.push(newTransaction); + + newHistoryRecord.movedPositions++; + + callBackFunction(); + } + } + catch (err) { + const logText = "[ERROR] 'movePositionAtExchange -onExchangeCallReturned' - ERROR : " + err.message; logger.write(logText); } @@ -1336,7 +1524,15 @@ try { - executionHistory.push(newHistoryRecord); + let newRecord = [ + newHistoryRecord.date.valueOf(), + newHistoryRecord.rate, + newHistoryRecord.newPositions, + newHistoryRecord.newTrades, + newHistoryRecord.movedPositions + ]; + + executionHistory.push(newRecord); let fileContent = JSON.stringify(executionHistory); @@ -1412,6 +1608,7 @@ } } + catch (err) { const logText = "[ERROR] 'Start' - ERROR : " + err.message; logger.write(logText);