From 69c6fa841263946cafe9e92f4c735e530f8e001e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 5 Mar 2018 17:31:48 +0100 Subject: [PATCH] Two Functions Migrated to the Exchange API --- .vs/AACloudPlatform/v15/.suo | Bin 61952 -> 61440 bytes ExchangeAPI.js | 270 ++++++++++++++++++++++++++++++++++- 2 files changed, 265 insertions(+), 5 deletions(-) diff --git a/.vs/AACloudPlatform/v15/.suo b/.vs/AACloudPlatform/v15/.suo index c6ac4af2ec6bd185c9a5c1138956b4f18a9b6972..a15361a8fb1e9912bc2bcfc70ca59c8895505875 100644 GIT binary patch delta 1419 zcma)+X-pGQ5Xbx8Zf!v=6;w{sf+&@svH)+X(a#@%{^XBmn%3W-D1$Z%u` zG8!3)=#Y417{c`=Ac@EbWG*ro>Bc;x&?X}xW!$FVeJYZLj6u>64C{JK=%gbB$Yht@ zKw@3?MCjC7hmFHX3^EUyj}$^q#Kcj>=&eLnA-q@#+BFC-QVRPc^2iF8eUcKHC#s1C z<6z&H!5q3m5oycI4+&0mm){VdQB@MlG{Xwy~d4XmaRSmb23 z(zleQ6sHg^AAWwnlqE;yGOA?jDbsKKs+Ol}l?*B}I;We+6=Iku_%_P~R~*IcKvk6> zFQ#%eRf^`2Fp3$PkGAfz(7r-`L%@mnQdPS59DQ2(+|0%rPL8>}v_m_+L zm8E6!5i0Kyl@k&P{}~dV>A>tULHa2jv$}l*>f5qfp! zLzE{5n(K05ja&kr%LedxO`tj7z}8*ZLdfr$eMxs+X@nW)UO?=Ui+`p9X{iKO|7@1; zzfIt@e>@xCc#@1bj;cdzA3C!UTfZ`Un_D6yG0G2wt)91sO0vPZI2K}SHw;W&hH~v5 zAr|npRx|5$Y`EiWewg69bn8y{(=B>_Hu_h?1}Afq$nPnX*Ju8JILUGsg(qk1-oj?`m)V3j`@}gG8s6F9@tahp`?Q@5yi-qK gYy685S}t^^BB9o1=s@_G3}tsFu!Ub51i4H53r&U9nE(I) delta 2776 zcmb7GYj9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=!@%CKK`f3uNiylX zNooRi5}r&XfXrYk2jPSjtTFWE2>}isvtS0zg_@HV*w&_C`+SM=b?$?`?E1-5(hi!a xs!8-FM=mk;kxd2U%@eTNQ534y!0gv7pe$G^s=gYNvFlKyg0&F)d}-uM?k`oVmstP+ diff --git a/ExchangeAPI.js b/ExchangeAPI.js index b78e74df9..6e98b61ca 100644 --- a/ExchangeAPI.js +++ b/ExchangeAPI.js @@ -1,14 +1,274 @@ -exports.newExchangeAPI = function newExchangeAPI() { +exports.newExchangeAPI = function newExchangeAPI(BOT) { + + /* + + 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"; thisObject = { - bot: undefined, - fileName: undefined, - write: write + initialize: initialize, + getOpenPositions: getOpenPositions, + getExecutedTrades: getExecutedTrades, + putBuyPosition: putBuyPosition, + putSellPosition: putSellPosition, + 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."); + } + } + + function getOpenPositions(pMarket, callBackFunction) { + + try { + + poloniexApiClient.returnOpenOrders(pMarket.assetA, pMarket.assetB, onExchangeCallReturned); + + function onExchangeCallReturned(err, exchangeResponse) { + + try { + + if (err || exchangeResponse.error !== undefined) { + try { + + if (err.message.indexOf("ETIMEDOUT") > 0) { + + logger.write("[WARN] getOpenPositions -> 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] getOpenPositions -> onExchangeCallReturned -> The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message); + callBackFunction("Retry Later."); + return; + + } else { + logger.write("[ERROR] getOpenPositions -> 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] getOpenPositions -> onExchangeCallReturned -> exchangeResponse.error = " + exchangeResponse.error); + callBackFunction("Operation Failed."); + 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 } ] + + */ + + let exchangePositions = []; + + 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); + } + + callBackFunction(null, exchangePositions); + } + } + catch (err) { + logger.write("[ERROR] getOpenPositions -> onExchangeCallReturned -> Error = " + err.message); + callBackFunction("Operation Failed."); + } + } + } catch (err) { + logger.write("[ERROR] getOpenPositions -> onExchangeCallReturned -> Error = " + err.message); + callBackFunction("Operation Failed."); + } + } + + function getExecutedTrades(pPositionId, callBackFunction) { + + try { + + poloniexApiClient.returnOrderTrades(pPositionId, onExchangeCallReturned); + + function onExchangeCallReturned(err, exchangeResponse) { + + try { + + if (err || exchangeResponse.error !== undefined) { + try { + + if (err.message.indexOf("ETIMEDOUT") > 0) { + + logger.write("[WARN] getExecutedTrades -> 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] getExecutedTrades -> onExchangeCallReturned -> The exchange reseted the connection. Requesting new execution later. : ERROR = " + err.message); + callBackFunction("Retry Later."); + return; + + } else { + logger.write("[ERROR] getExecutedTrades -> 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] getExecutedTrades -> onExchangeCallReturned -> exchangeResponse.error = " + exchangeResponse.error); + callBackFunction("Operation Failed."); + 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(null, trades); + } + } + catch (err) { + logger.write("[ERROR] getExecutedTrades -> onExchangeCallReturned -> Error = " + err.message); + callBackFunction("Operation Failed."); + } + } + } catch (err) { + logger.write("[ERROR] getExecutedTrades -> onExchangeCallReturned -> Error = " + err.message); + callBackFunction("Operation Failed."); + } + } + + function putBuyPosition() { + + try { + + + } catch (err) { + logger.write("[ERROR] putBuyPosition -> err = " + err); + callBackFunction("Operation Failed."); + } + } + + function putSellPosition() { + + try { + + + } catch (err) { + logger.write("[ERROR] putSellPosition -> err = " + err); + callBackFunction("Operation Failed."); + } + } + + function movePosition() { + + try { + + + } catch (err) { + logger.write("[ERROR] movePosition -> err = " + err); + callBackFunction("Operation Failed."); + } + } }; \ No newline at end of file