Skip to content

Commit

Permalink
Fixed historicalTrades, recentTrades, aggTrades functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Eyrick authored Jan 25, 2018
1 parent e8c7823 commit 8a47409
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions node-binance-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ module.exports = function() {
return callback( null, JSON.parse(body) );
});
};

const marketRequest = function(url, data, callback, method = 'GET') {
if ( !data ) data = {};
let query = Object.keys(data).reduce(function(a,k){a.push(k+'='+encodeURIComponent(data[k]));return a},[]).join('&');
let opt = {
url: url+'?'+query,
method: method,
timeout: options.recvWindow,
agent: false,
headers: {
'User-Agent': userAgent,
'Content-type': contentType,
'X-MBX-APIKEY': options.APIKEY
}
};
request(opt, function(error, response, body) {
if ( !callback ) return;

if ( error )
return callback( error, {} );

if ( response && response.statusCode !== 200 )
return callback( response, {} );

return callback( null, JSON.parse(body) );
});
};

const signedRequest = function(url, data, callback, method = 'GET') {
if ( !options.APISECRET ) throw 'signedRequest: Invalid API Secret';
Expand Down Expand Up @@ -383,7 +410,7 @@ LIMIT_MAKER
return depthVolume(symbol);
},
roundStep: function roundStep(number, stepSize) {
return ((number / stepSize) | 0) * stepSize;
return ( (number / stepSize) | 0 ) * stepSize;
},
percent: function(min, max, width = 100) {
return ( min * 0.01 ) / ( max * 0.01 ) * width;
Expand Down Expand Up @@ -606,11 +633,15 @@ LIMIT_MAKER
time: function(callback) {
apiRequest(base+'v1/time', callback);
},
aggTrades: function(symbol, options = {}, callback = false) { //fromId startTime endTime limit
let parameters = Object.assign({symbol}, options);
marketRequest(base+'v1/aggTrades', parameters, callback);
},
recentTrades: function(symbol, callback, limit = 500) {
signedRequest(base+'v1/trades', {symbol:symbol, limit:limit}, callback);
marketRequest(base+'v1/trades', {symbol:symbol, limit:limit}, callback);
},
historicalTrades: function(symbol, callback, limit = 500) {
signedRequest(base+'v1/historicalTrades', {symbol:symbol, limit:limit}, callback);
marketRequest(base+'v1/historicalTrades', {symbol:symbol, limit:limit}, callback);
},
// convert chart data to highstock array [timestamp,open,high,low,close]
highstock: function(chart, include_volume = false) {
Expand Down

0 comments on commit 8a47409

Please sign in to comment.