From da0996afec60c5f9b077d7b4543afa12db58b053 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 5 Mar 2018 20:22:46 +0100 Subject: [PATCH] Exchange API migration completed --- .vs/AACloudPlatform/v15/.suo | Bin 59392 -> 60416 bytes AACloudPlatform.njsproj | 3 ++ Context.js | 54 +++++++++++++++++++++++++ ExchangeAPI.js | 75 ++++++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 Context.js diff --git a/.vs/AACloudPlatform/v15/.suo b/.vs/AACloudPlatform/v15/.suo index fa3a099cbfea4ab1c1b7b217ac2efd32cf24d9fb..f96bcadb7aecb8e20b9a7181018abfd3af7ad602 100644 GIT binary patch delta 1181 zcmZvbdrZ?;6vyxVwLFTo6oIrRm7+6>Vh~@bS&U2?UlUjGfe)f~7I2K!s&$%b2I?Ns zs8Eht>olo9M5CesE>aRT@(0OAvbh?wY20)YoB4e@ayk$YC?Ixciw>~GY;#C@$c&WHJ z|BA}Av-t*U)K*lNBbo=0JFpfH(uL4pfMQ?=MPMmd0+xeipaiS~E5Tb-S_QiX3>1a4 z7TN*61nYs5vAL`QP8pEhkAvL=Qo$ndHK+pL02kN@++YjX3}lW+TAyQLy;2ny{4*;h zBsRs@YJ6_3PT{y0fautC1Ds@F1Swz`Kw!0QJe+JPS{zGwtKOr;SE8a~B(K5CReE8l z<_BnNay8#CUL;SSte4-}56!UH<9db9VbwJu??PWUR$V)sP@h;&`6DdTkk-5IVDoPo zfVvBN5BwX`u_Mt3&=K&|S7b_m1m}P77|0~Kq|(}aCNAX@^wGcO;CILo9hc_owT#(L$y^a_xXaZpMX4y0%~1FG zZPeK~lALW8%4nQI?q(xZ_=oq8Ks`4soXM+ZtTq- zGDr{HgQLwV>!qPDwgBjXCRUqnbj=hc=gxBKY|j_@^_`p)`xt7zW~1=s(c)luGpAik zO=LQ9HgPCR>dm6y-WjqMBS#=p-fJpa*tv7Zq^Y(^(^vcvJ9Hvo7VG?(C>+Y7p7Um- zyW{%dA3A^f{X1{{tvyf7vqbo4F6Wt48(y7Wfal?29z3S)_{?FI@|jTd>Sy$%%R;5s zBGh~(k4#6?#KNAJoOI(BlcD=Lxl0DM`u%@)VUIlH>O&k_RxZjrvNW{2dlYR5ndy4B XSrkVuYQ(|Iib8)?x#)hC^F!VP!lJt~$AlEN4? zIhDnp(QoonmUK}Mpc*qE1}cXG&542Hn!;#;!9ac(l2`(i{dZ!bd}I=olLDnvfpi)W zXF&OxKspPE^ME)9h;xA$!+zKn%0K1jsH0;xZu4o*1Y;i7AJ*;kMDlg&_Ac zFXCa^#8bkwxkO+E<7OEF0i(@2GFKQkxs*t3E>Y`Y+!WWKJ-K0G!=f&Z$qkc?CWlSt z+w3sq0OO)5T$39^#5apid&#(H0o&w@*R?lm&$!CGXcfz3!+EZg`;VJ#uAbA%xM{<3 zRzVY>%`or<$WYq6W2*n~m-| LFl}~tc7PE8i>J>{ diff --git a/AACloudPlatform.njsproj b/AACloudPlatform.njsproj index d38aaf60f..081717a47 100644 --- a/AACloudPlatform.njsproj +++ b/AACloudPlatform.njsproj @@ -22,6 +22,9 @@ + + Code + Code diff --git a/Context.js b/Context.js new file mode 100644 index 000000000..b72c2c22e --- /dev/null +++ b/Context.js @@ -0,0 +1,54 @@ +exports.newExchangeAPI = function newExchangeAPI(BOT) { + + /* + + This module allows trading bots to have a context between executions. + + */ + + const MODULE_NAME = "Exchange API"; + + thisObject = { + initialize: initialize, + getOpenPositions: getOpenPositions, + getExecutedTrades: getExecutedTrades, + putPosition: putPosition, + movePosition: movePosition + }; + + let bot = BOT; + + const DEBUG_MODULE = require('./Debug Log'); + const logger = DEBUG_MODULE.newDebugLog(); + logger.fileName = MODULE_NAME; + + const POLONIEX_CLIENT_MODULE = require('./Poloniex API Client'); + let poloniexApiClient; + + return thisObject; + + function initialize(callBackFunction) { + + try { + + let apiKey = readApiKey(); + poloniexApiClient = new POLONIEX_CLIENT_MODULE(apiKey.Key, apiKey.Secret); + + function readApiKey() { + + try { + return JSON.parse(fs.readFileSync('../' + 'API-Keys' + '/' + EXCHANGE_NAME + '.json', 'utf8')); + } + catch (err) { + logger.write("[ERROR] initialize -> readApiKey -> err = " + err); + callBackFunction("Operation Failed."); + } + } + + } catch (err) { + + logger.write("[ERROR] initialize -> err = " + err); + callBackFunction("Operation Failed."); + } + } +} \ No newline at end of file diff --git a/ExchangeAPI.js b/ExchangeAPI.js index f23e1e508..b32a13b7a 100644 --- a/ExchangeAPI.js +++ b/ExchangeAPI.js @@ -4,7 +4,6 @@ This module allows trading bots to connect to the exchange and do trding operations on it. So far it can only work with Poloniex. - */ const MODULE_NAME = "Exchange API"; @@ -320,10 +319,82 @@ } } - function movePosition() { + function movePosition(pPosition, pNewRate) { try { + 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) { + + logger.write("[WARN] movePosition -> onExchangeCallReturned -> Timeout reached while trying to access the Exchange API. Requesting new execution later. : ERROR = " + err.message); + callBackFunction("Retry Later."); + return; + + } else { + + if (err.message.indexOf("ECONNRESET") > 0) { + + logger.write("[WARN] movePosition -> onExchangeCallReturned -> The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message); + callBackFunction("Retry Later."); + return; + + } else { + logger.write("[ERROR] movePosition -> onExchangeCallReturned -> Unexpected error trying to contact the Exchange. This will halt this bot process. : ERROR = " + err.message); + callBackFunction("Operation Failed."); + return; + } + } + + } catch (err) { + logger.write("[ERROR] movePosition -> onExchangeCallReturned -> exchangeResponse.error = " + exchangeResponse.error); + callBackFunction("Operation Failed."); + return; + } + + return; + + } else { + + if (exchangeResponse.success !== 1) { + logger.write("[ERROR] movePosition -> onExchangeCallReturned -> exchangeResponse.success = " + exchangeResponse.success); + callBackFunction("Operation Failed."); + return; + } + /* + + This is what we can receive from the exchange. + + { + "orderNumber":31226040, + "resultingTrades": + [{ + "amount":"338.8732", + "date":"2014-10-18 23:03:21", + "rate":"0.00000173", + "total":"0.00058625", + "tradeID":"16164", + "type":"buy" + }] + } + + */ + + callBackFunction(null, exchangeResponse.orderNumber); + } + } + catch (err) { + logger.write("[ERROR] movePosition -> onExchangeCallReturned -> Error = " + err.message); + callBackFunction("Operation Failed."); + } + } } catch (err) { logger.write("[ERROR] movePosition -> err = " + err);