Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit ab0e0d0

Browse files
author
Jon Eyrick
authored
Adjustable limit for chart function
2 parents 39e969d + 2ad3da7 commit ab0e0d0

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

examples/experiments.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Connect to all websocket endpoints without using the library
2+
(async () => {
3+
const axios = require('axios');
4+
const WebSocket = require('ws');
5+
let streams = [];
6+
let markets = [];
7+
let prevDay = await axios.get('https://api.binance.com/api/v1/ticker/24hr');
8+
for ( let obj of prevDay.data ) {
9+
markets.push(obj.symbol);
10+
streams.push(obj.symbol.toLowerCase()+'@aggTrade');
11+
}
12+
console.log(markets);
13+
14+
const ws = new WebSocket('wss://stream.binance.com:9443/stream?streams='+streams.join('/'));
15+
ws.on('message', function(payload) {
16+
let json = JSON.parse(payload), stream = json.stream, data = json.data;
17+
if ( data.e == 'aggTrade' ) {
18+
console.log(data.s+' price: '+data.p);
19+
} else console.log(stream, data);
20+
});
21+
})();

node-binance-api.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,11 +1865,12 @@ let api = function Binance() {
18651865
* @param {array/string} symbols - an array or string of symbols to query
18661866
* @param {string} interval - the time interval
18671867
* @param {function} callback - callback function
1868+
* @param {int} limit - maximum results, no more than 1000
18681869
* @return {string} the websocket endpoint
18691870
*/
1870-
chart: function chart(symbols, interval, callback) {
1871+
chart: function chart(symbols, interval, callback, limit = 500) {
18711872
let reconnect = function () {
1872-
if (Binance.options.reconnect) chart(symbols, interval, callback);
1873+
if (Binance.options.reconnect) chart(symbols, interval, callback, limit);
18731874
};
18741875

18751876
let symbolChartInit = function (symbol) {
@@ -1897,8 +1898,8 @@ let api = function Binance() {
18971898
}
18981899
};
18991900

1900-
let getSymbolKlineSnapshot = function (symbol) {
1901-
publicRequest(base + 'v1/klines', { symbol: symbol, interval: interval }, function (error, data) {
1901+
let getSymbolKlineSnapshot = function (symbol, limit = 500) {
1902+
publicRequest(base + 'v1/klines', { symbol: symbol, interval: interval, limit: limit }, function (error, data) {
19021903
klineData(symbol, interval, data);
19031904
//Binance.options.log('/klines at ' + Binance.info[symbol][interval].timestamp);
19041905
if (typeof Binance.klineQueue[symbol][interval] !== 'undefined') {
@@ -1917,12 +1918,12 @@ let api = function Binance() {
19171918
return symbol.toLowerCase() + '@kline_' + interval;
19181919
});
19191920
subscription = subscribeCombined(streams, handleKlineStreamData, reconnect);
1920-
symbols.forEach(getSymbolKlineSnapshot);
1921+
symbols.forEach(element => getSymbolKlineSnapshot(element, limit));
19211922
} else {
19221923
let symbol = symbols;
19231924
symbolChartInit(symbol);
19241925
subscription = subscribe(symbol.toLowerCase() + '@kline_' + interval, handleKlineStreamData, reconnect);
1925-
getSymbolKlineSnapshot(symbol);
1926+
getSymbolKlineSnapshot(symbol, limit);
19261927
}
19271928
return subscription.endpoint;
19281929
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-binance-api",
3-
"version": "0.8.5",
3+
"version": "0.8.6",
44
"description": "Binance API for node https://github.com/jaggedsoft/node-binance-api",
55
"main": "node-binance-api.js",
66
"dependencies": {

0 commit comments

Comments
 (0)