Skip to content

Commit 63ddfb4

Browse files
author
Scripnic Alexandru
committed
Add bulkDepth functionality
1 parent 3bc9479 commit 63ddfb4

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Following examples will use the `await` form, which requires some configuration
6767
- [depositAddress](#depositaddress)
6868
- [Websockets](#websockets)
6969
- [depth](#depth)
70+
- [bulkDepth](#bulkdepth)
7071
- [partialDepth](#partialdepth)
7172
- [ticker](#ticker)
7273
- [allTickers](#alltickers)
@@ -910,6 +911,42 @@ client.ws.depth('ETHBTC', depth => {
910911

911912
</details>
912913

914+
#### bulkDepth
915+
916+
Live depth market data feed. The first parameter can either
917+
be a single symbol string or an array of symbols.
918+
919+
This method creates only one connection, that prevents from overpopulating the server.
920+
921+
```js
922+
client.ws.bulkDepth('ETHBTC', depth => {
923+
console.log(depth);
924+
})
925+
```
926+
927+
<details>
928+
<summary>Output</summary>
929+
930+
```js
931+
{
932+
eventType: 'depthUpdate',
933+
eventTime: 1508612956950,
934+
symbol: 'ETHBTC',
935+
firstUpdateId: 18331140,
936+
finalUpdateId: 18331145,
937+
bidDepth: [
938+
{ price: '0.04896500', quantity: '0.00000000' },
939+
{ price: '0.04891100', quantity: '15.00000000' },
940+
{ price: '0.04891000', quantity: '0.00000000' } ],
941+
askDepth: [
942+
{ price: '0.04910600', quantity: '0.00000000' },
943+
{ price: '0.04910700', quantity: '11.24900000' }
944+
]
945+
}
946+
```
947+
948+
</details>
949+
913950
#### partialDepth
914951

915952
Top levels bids and asks, pushed every second. Valid levels are 5, 10, or 20.

src/websocket.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,38 @@ const depth = (payload, cb) => {
3636
return (options) => cache.forEach(w => w.close(1000, 'Close handle was called', { keepClosed: true, ...options }))
3737
}
3838

39+
const bulkDepth = (payload, cb) => {
40+
const _BASE = BASE.replace(/ws$/gi, 'stream');
41+
const streams = (Array.isArray(payload) ? payload : [payload])
42+
.map(symbol => `${symbol}@depth`)
43+
.join('/');
44+
45+
const w = openWebSocket(`${_BASE}?streams=${streams.toLowerCase()}`);
46+
w.onmessage = msg => {
47+
const {
48+
e: eventType,
49+
E: eventTime,
50+
s: symbol,
51+
U: firstUpdateId,
52+
u: finalUpdateId,
53+
b: bidDepth,
54+
a: askDepth,
55+
} = JSON.parse(msg.data).data
56+
57+
cb({
58+
eventType,
59+
eventTime,
60+
symbol,
61+
firstUpdateId,
62+
finalUpdateId,
63+
bidDepth: bidDepth.map(b => zip(['price', 'quantity'], b)),
64+
askDepth: askDepth.map(a => zip(['price', 'quantity'], a)),
65+
});
66+
}
67+
68+
return (options) => w.close(1000, 'Close handle was called', { keepClosed: true, ...options });
69+
}
70+
3971
const partialDepth = (payload, cb) => {
4072
const cache = (Array.isArray(payload) ? payload : [payload]).map(({ symbol, level }) => {
4173
const w = openWebSocket(`${BASE}/${symbol.toLowerCase()}@depth${level}`)
@@ -272,6 +304,7 @@ const user = opts => cb => {
272304

273305
export default opts => ({
274306
depth,
307+
bulkDepth,
275308
partialDepth,
276309
candles,
277310
trades,

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ test('[WS] depth', t => {
130130
})
131131
})
132132

133+
test('[WS] bulk depth', t => {
134+
return new Promise(resolve => {
135+
const symbols = ['ETHBTC', 'LTCBTC'];
136+
const connection = client.ws.bulkDepth(symbols, depth => {
137+
checkFields(t, depth, [
138+
'eventType',
139+
'eventTime',
140+
'firstUpdateId',
141+
'finalUpdateId',
142+
'symbol',
143+
'bidDepth',
144+
'askDepth',
145+
]);
146+
147+
connection();
148+
resolve();
149+
});
150+
});
151+
});
152+
133153
test('[WS] partial depth', t => {
134154
return new Promise(resolve => {
135155
client.ws.partialDepth({ symbol: 'ETHBTC', level: 10 }, depth => {

0 commit comments

Comments
 (0)