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

Commit

Permalink
Fixed depth cache reconnect with multiple symbols (Thanks mstijak)
Browse files Browse the repository at this point in the history
Update from development branch
  • Loading branch information
Jon Eyrick authored Jan 8, 2018
2 parents 23325c4 + d799677 commit e2c7eef
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 47 deletions.
44 changes: 4 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,8 @@ Market Buy response {
transactTime: 1509049376261,
price: '0.00000000',
origQty: '1.00000000',
executedQty: '1.00000000',
utedQty: '1.00000000',
status: 'FILLED',
timeInForce: 'GTC',
type: 'MARKET',
Expand Down Expand Up @@ -1255,45 +1256,6 @@ binance.websockets.trades(['BNBBTC', 'ETHBTC'], function(trades) {
});
```

#### User Data: Account Balance Updates, Trade Updates, New Orders, Filled Orders, Cancelled Orders via WebSocket
```javascript
function balance_update(data) {
console.log("Balance Update");
for ( let obj of data.B ) {
let { a:asset, f:available, l:onOrder } = obj;
if ( available == "0.00000000" ) continue;
console.log(asset+"\tavailable: "+available+" ("+onOrder+" on order)");
}
}
function execution_update(data) {
let { x:executionType, s:symbol, p:price, q:quantity, S:side, o:orderType, i:orderId, X:orderStatus } = data;
if ( executionType == "NEW" ) {
if ( orderStatus == "REJECTED" ) {
console.log("Order Failed! Reason: "+data.r);
}
console.log(symbol+" "+side+" "+orderType+" ORDER #"+orderId+" ("+orderStatus+")");
console.log("..price: "+price+", quantity: "+quantity);
return;
}
//NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED
console.log(symbol+"\t"+side+" "+executionType+" "+orderType+" ORDER #"+orderId);
}
binance.websockets.userData(balance_update, execution_update);
```
<details>
<summary>View Response</summary>

```
BNBBTC NEW BUY LIMIT ORDER #6407865 (NEW)
..price: 0.00035595, quantity: 5.00000000
Balance Update
BTC available: 0.77206464 (0.00177975 on order)
ETH available: 1.14109900 (0.00000000 on order)
BNB available: 41.33761879 (0.00000000 on order)
SNM available: 0.76352833 (0.00000000 on order)
```
</details>

#### Get 24h Price Change Statistics via WebSocket
```js
// For all symbols:
Expand Down Expand Up @@ -1449,6 +1411,8 @@ binance.withdraw("ETH", "0x1d2034348c851ea29c7d03731c7968a5bcc91564", 1, false,
binance.withdraw("BTC", "1C5gqLRs96Xq4V2ZZAR1347yUCpHie7sa", 0.2);
```

#### [Advanced examples (exchangeInfo, websocket balance updates, websocket order execution updates)](https://github.com/jaggedsoft/node-binance-api/blob/master/examples/advanced.md)

### Troubleshooting
Verify that your system time is correct. If you have any suggestions don't hestitate to file an issue.

Expand Down
80 changes: 80 additions & 0 deletions examples/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,83 @@ binance.exchangeInfo(function(data) {
});
```
![example](https://image.ibb.co/bz5KAG/notationals.png)


#### Enable Test Mode for orders
```js
const binance = require('node-binance-api');
binance.options({
'APIKEY':'<key>',
'APISECRET':'<secret>',
'test':true
});
```


#### Terminate WebSocket connections
First disable automatic reconnection of websockets

```js
binance.options({
'APIKEY': '<your key>',
'APISECRET': '<your secret>',
'reconnect': false
});
```

Now you can terminate each websocket endpoint by the id:
```js
binance.websockets.terminate('ethbtc@ticker'); // for prevday
binance.websockets.terminate('ethbtc@kline_1m'); // for candlestick charts
```

You can store a reference to each `ws` object or view a list of all of them:
```js
// List all endpoints
let endpoints = binance.websockets.subscriptions();
for ( let endpoint in endpoints ) {
console.log(endpoint);
//binance.websockets.terminate(endpoint);
}
```


#### User Data: Account Balance Updates, Trade Updates, New Orders, Filled Orders, Cancelled Orders via WebSocket
```javascript
function balance_update(data) {
console.log("Balance Update");
for ( let obj of data.B ) {
let { a:asset, f:available, l:onOrder } = obj;
if ( available == "0.00000000" ) continue;
console.log(asset+"\tavailable: "+available+" ("+onOrder+" on order)");
}
}
function execution_update(data) {
let { x:executionType, s:symbol, p:price, q:quantity, S:side, o:orderType, i:orderId, X:orderStatus } = data;
if ( executionType == "NEW" ) {
if ( orderStatus == "REJECTED" ) {
console.log("Order Failed! Reason: "+data.r);
}
console.log(symbol+" "+side+" "+orderType+" ORDER #"+orderId+" ("+orderStatus+")");
console.log("..price: "+price+", quantity: "+quantity);
return;
}
//NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED
console.log(symbol+"\t"+side+" "+executionType+" "+orderType+" ORDER #"+orderId);
}
binance.websockets.userData(balance_update, execution_update);
```
<details>
<summary>View Response</summary>

```
BNBBTC NEW BUY LIMIT ORDER #6407865 (NEW)
..price: 0.00035595, quantity: 5.00000000
Balance Update
BTC available: 0.77206464 (0.00177975 on order)
ETH available: 1.14109900 (0.00000000 on order)
BNB available: 41.33761879 (0.00000000 on order)
SNM available: 0.76352833 (0.00000000 on order)
```
</details>

13 changes: 7 additions & 6 deletions node-binance-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ LIMIT_MAKER
}
}
};
const getDepthCache = function(symbol) {
if ( typeof depthCache[symbol] === 'undefined' ) return {bids: {}, asks: {}};
return depthCache[symbol];
};
const depthVolume = function(symbol) { // Calculate Buy/Sell volume from DepthCache
let cache = getDepthCache(symbol), quantity, price;
let bidbase = 0, askbase = 0, bidqty = 0, askqty = 0;
Expand All @@ -350,10 +354,6 @@ LIMIT_MAKER
}
return {bids: bidbase, asks: askbase, bidQty: bidqty, askQty: askqty};
};
const getDepthCache = function(symbol) {
if ( typeof depthCache[symbol] === 'undefined' ) return {bids: {}, asks: {}};
return depthCache[symbol];
};
////////////////////////////
return {
depthCache: function(symbol) {
Expand Down Expand Up @@ -639,13 +639,14 @@ LIMIT_MAKER
}
},
depthCache: function depthCacheFunction(symbols, callback, limit = 500) {
if ( typeof symbols === 'string' ) symbols = [symbols]; // accept both strings and arrays
for ( let symbol of symbols ) {
if ( typeof info[symbol] === 'undefined' ) info[symbol] = {};
info[symbol].firstUpdateId = 0;
depthCache[symbol] = {bids: {}, asks: {}};
messageQueue[symbol] = [];
let reconnect = function() {
if ( options.reconnect ) depthCacheFunction(symbols, callback);
if ( options.reconnect ) depthCacheFunction([symbol], callback);
};
subscribe(symbol.toLowerCase()+'@depth', function(depth) {
if ( !info[symbol].firstUpdateId ) {
Expand Down Expand Up @@ -737,4 +738,4 @@ LIMIT_MAKER
}
};
}();
//https://github.com/binance-exchange/binance-official-api-docs
//https://github.com/binance-exchange/binance-official-api-docs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-binance-api",
"version": "0.3.2",
"version": "0.3.3",
"description": "Binance API for node https://github.com/jaggedsoft/node-binance-api",
"main": "node-binance-api.js",
"dependencies": {
Expand Down

0 comments on commit e2c7eef

Please sign in to comment.