diff --git a/libs/apiMintpal.js b/libs/apiMintpal.js index bd0610c..fec22e6 100644 --- a/libs/apiMintpal.js +++ b/libs/apiMintpal.js @@ -108,6 +108,16 @@ module.exports = function() { return this._request(options, callback); }, + getBuyOrderBook: function(currencyA, currencyB, callback){ + var options = { + method: 'GET', + url: PUBLIC_API_URL + '/orders/' + currencyB + '/' + currencyA + '/BUY', + qs: null + }; + + return this._request(options, callback); + }, + getOrderBook: function(currencyA, currencyB, callback){ var parameters = { command: 'returnOrderBook', diff --git a/libs/profitSwitch.js b/libs/profitSwitch.js index d8dac3d..a17f640 100644 --- a/libs/profitSwitch.js +++ b/libs/profitSwitch.js @@ -286,10 +286,49 @@ module.exports = function(logger){ marketData['BTC'].bid = new Number(market.top_bid); } + if (market.exchange == 'LTC' && market.code == symbol) { + if (!marketData.hasOwnProperty('LTC')) + marketData['LTC'] = {}; + + marketData['LTC'].last = new Number(market.last_price); + marketData['LTC'].baseVolume = new Number(market['24hvol']); + marketData['LTC'].quoteVolume = new Number(market['24hvol'] / market.last_price); + marketData['LTC'].ask = new Number(market.top_ask); + marketData['LTC'].bid = new Number(market.top_bid); + } + }); }); taskCallback(); }); + }, + function(taskCallback){ + var depthTasks = []; + Object.keys(symbolToAlgorithmMap).forEach(function(symbol){ + var marketData = profitStatus[symbolToAlgorithmMap[symbol]][symbol].exchangeInfo['Mintpal']; + if (marketData.hasOwnProperty('BTC') && marketData['BTC'].bid > 0){ + depthTasks.push(function(callback){ + _this.getMarketDepthFromMintpal('BTC', symbol, marketData['BTC'].bid, callback) + }); + } + if (marketData.hasOwnProperty('LTC') && marketData['LTC'].bid > 0){ + depthTasks.push(function(callback){ + _this.getMarketDepthFromMintpal('LTC', symbol, marketData['LTC'].bid, callback) + }); + } + }); + + if (!depthTasks.length){ + taskCallback(); + return; + } + async.series(depthTasks, function(err){ + if (err){ + taskCallback(err); + return; + } + taskCallback(); + }); } ], function(err){ if (err){ @@ -298,7 +337,31 @@ module.exports = function(logger){ } callback(null); }); - + }; + this.getMarketDepthFromMintpal = function(symbolA, symbolB, coinPrice, callback){ + mintpalApi.getBuyOrderBook(symbolA, symbolB, function(err, response){ + if (err){ + callback(err); + return; + } + var depth = new Number(0); + if (response.hasOwnProperty('data')){ + response['data'].forEach(function(order){ + var price = new Number(order.price); + var limit = new Number(coinPrice * portalConfig.profitSwitch.depth); + var qty = new Number(order.amount); + // only measure the depth down to configured depth + if (price >= limit){ + depth += (qty * price); + } + }); + } + + + var marketData = profitStatus[symbolToAlgorithmMap[symbolB]][symbolB].exchangeInfo['Mintpal']; + marketData[symbolA].depth = depth; + callback(); + }); }; @@ -386,7 +449,6 @@ module.exports = function(logger){ logger.error(logSystem, 'Check', 'Error while checking profitability: ' + err); return; } - logger.debug(logSystem, 'Check', JSON.stringify(profitStatus)); }); }; setInterval(checkProfitability, portalConfig.profitSwitch.updateInterval * 1000);