From 5294b2c8c71b7a83f8d83c4e8d184508267de6bf Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 5 Mar 2018 17:32:49 +0100 Subject: [PATCH] Two Functions Migrated to the Exchange API --- .vs/AAMariam-Trading-Bot/v15/.suo | Bin 26624 -> 26624 bytes Trading-Process/Interval.js | 239 +++++------------------------- 2 files changed, 40 insertions(+), 199 deletions(-) diff --git a/.vs/AAMariam-Trading-Bot/v15/.suo b/.vs/AAMariam-Trading-Bot/v15/.suo index 0a36433059a4bb4c444bd29197a4c6a39f04613f..a6f2113fb722e4b22f30270847aa6420cbdb3065 100644 GIT binary patch delta 2378 zcmeH|-%nd*7{|}sLOG-7FxUzlSXV3HWE;gED21|?w)OyS8rHH+$+8Qls~eLs1BDo5 zQYFS4E=Hd#z0hPAyPDZp7sbRMOIUQ?toY*((3@RpvI|JYmWbcyK$XsnM8ky_KFO!= z^SJ^<&%prvPH+J5Go}lE7(_r6 zJPTsL0NtPm@H}$b8`XYvj)0@!7|oVe(;0h%eg?fHcnzEeJbe)DS@1d-0_VUmFqfT8 z#4<`s4A7V|C0?Rs{JuzI>JgbYftOe5`|1W-Qh)vLX56O=zB$%OKH>E}aF=au4;8uW zf;%KKj+6+}nBxw`c7!NhX(+{blkYh{wU|#vqLtl;RP(<1PV>(w$!r4h!+VQ3yc*YrPE4ZW+?K~;@(CpkhFHHYa3 ywaYZsR##s3xiIMxv>5W*_$&OWMBCclUKfH!k&tnzM*IOcLfiiU delta 2346 zcmeHIUrbw77{BM1!j;}aVeR;jir7`~LQA`@x3IOBmLgkSy8#muqU&tSYBpHtg$Ep2 zmzns&gWS(2;}cJNaM`3L&IjX?8e@DAjK28f;)72c5 zch2|kch32~)50bzY_dC-_`3IV`HgFz{=i%u$Aw#Ai64WK$-^$fHlGk_(TsS61FpAp zSNGuLT@x&smZ~RF{Dr|oQ{Ov7*ex zWG}Obv%Sm>C{@VpV!NN5>1SH){UP!q45H-_;yB_2B7_Jd6vQwhf*`vQ{KgQY2xzys z;DcJ5?Ho>CLrfvgBOq;YImkMLlPqEep=Vk>^QI=MYyuX2+deDR18NJc}FBT zJ(#p5iIY*aqHM zyJKpD9zKqnMr~$(F}1I?Daxy<`CRIDCc zSN2tbG8TgRK_}LG55&`M&H0T`gJF$=GDmTegB|Ce)zk@#JUr|@Ezs+0Sb!SGp6tLC z+RMSJw6g9hG5Rkszm5t4c6t*6)yRR#%Sn-5CVKVv7w`G2;6XetRtUIZWn^}w++1}g z_2 0) { - - const logText = "[WARN] 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] 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] 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; - logger.write(logText); - return; - } - - return; - - } else { - - /* - - This is what we receive from the exchange. We will convert this to our standard format for later use. - - [ { orderNumber: '151918418632', - type: 'sell', - rate: '20000.00000000', - startingAmount: '0.00010000', - amount: '0.00010000', - total: '2.00000000', - date: '2018-02-24 11:14:17', - margin: 0 } ] - - */ - - for (let i = 0; i < exchangeResponse.length; i++) { - - let openPosition = { - id: exchangeResponse[i].orderNumber, - type: exchangeResponse[i].type, - rate: exchangeResponse[i].rate, - amountA: exchangeResponse[i].total, - amountB: exchangeResponse[i].amount, - date: (new Date(exchangeResponse[i].date)).valueOf() - }; - - exchangePositions.push(openPosition); - } + function onResponse(err, pExchangePositions) { + switch (err) { + case null: { // Everything went well, we have the information requested. + exchangePositions = pExchangePositions; readStatusAndContext(); } - } - catch (err) { - const logText = "[ERROR] 'onExchangeCallReturned' - ERROR : " + err.message; - logger.write(logText); - + break; + case 'Retry Later.': { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write("[ERROR] getPositionsAtExchange -> onResponse -> Retry Later. Requesting Interval Retry."); + callBackFunction(true, nextIntervalLapse); + return; + } + break; + case 'Operation Failed.': { // This is an unexpected exception that we do not know how to handle. + logger.write("[ERROR] getPositionsAtExchange -> onResponse -> Operation Failed. Aborting the process."); + return; + } + break; } } } @@ -517,7 +433,7 @@ } else { - getOrderTradesAtExchange(executionContext.positions[i].id, confirmOrderWasPartiallyExecuted); + getPositionTradesAtExchange(executionContext.positions[i].id, confirmOrderWasPartiallyExecuted); return; function confirmOrderWasPartiallyExecuted(trades) { @@ -597,7 +513,7 @@ /* Position not found: we need to know if the order was executed. */ - getOrderTradesAtExchange(executionContext.positions[i].id, confirmOrderWasExecuted); + getPositionTradesAtExchange(executionContext.positions[i].id, confirmOrderWasExecuted); function confirmOrderWasExecuted(trades) { @@ -692,109 +608,34 @@ } } - function getOrderTradesAtExchange(orderId, callBackFunction) { + function getPositionTradesAtExchange(pPositionId, callBack) { /* - Given one order, we request all the associated trades to it. + Given one position, we request all the associated trades to it. */ - poloniexApiClient.returnOrderTrades(orderId, onExchangeCallReturned); + exchangeAPI.getExecutedTrades(pPositionId, onResponse); - function onExchangeCallReturned(err, exchangeResponse) { + function onResponse(err, pTrades) { - try { - - if (err || exchangeResponse.error !== undefined) { - try { - - if (err.message.indexOf("ETIMEDOUT") > 0) { - - 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. */ - - callBackFunction(true, nextIntervalLapse); - return; - - } else { - - if (err.message.indexOf("ECONNRESET") > 0) { - - 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. */ - - callBackFunction(true, nextIntervalLapse); - return; - - } else { - - - 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] getOrderTradesAtExchange -onExchangeCallReturned : ERROR : exchangeResponse.error = " + exchangeResponse.error; - logger.write(logText); - return; - } - - return; - - } else { - - /* - - This is what we receive from the exchange. We will convert this to our standard format for later use. - - [ - { - "globalTradeID": 20825863, - "tradeID": 147142, - "currencyPair": - "BTC_XVC", - "type": "buy", - "rate": "0.00018500", - "amount": "455.34206390", - "total": "0.08423828", - "fee": "0.00200000", - "date": "2016-03-14 01:04:36" - }, - ...] - - */ - - let trades = []; - - for (let i = 0; i < exchangeResponse.length; i++) { - - let trade = { - id: exchangeResponse[i].tradeID, - type: exchangeResponse[i].type, - rate: exchangeResponse[i].rate, - amountA: exchangeResponse[i].total, - amountB: exchangeResponse[i].amount, - fee: exchangeResponse[i].fee, - date: (new Date(exchangeResponse[i].date)).valueOf() - } - - trades.push(trade); - } - - callBackFunction(trades); + switch (err) { + case null: { // Everything went well, we have the information requested. + callBack(pTrades); } - } - catch (err) { - const logText = "[ERROR] 'getOrderTradesAtExchange - onExchangeCallReturned' - ERROR : " + err.message; - logger.write(logText); - + break; + case 'Retry Later.': { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write("[ERROR] getPositionTradesAtExchange -> onResponse -> Retry Later. Requesting Interval Retry."); + callBackFunction(true, nextIntervalLapse); + return; + } + break; + case 'Operation Failed.': { // This is an unexpected exception that we do not know how to handle. + logger.write("[ERROR] getPositionTradesAtExchange -> onResponse -> Operation Failed. Aborting the process."); + return; + } + break; } } }