Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bulkDepth functionality #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Following examples will use the `await` form, which requires some configuration
- [depositAddress](#depositaddress)
- [Websockets](#websockets)
- [depth](#depth)
- [bulkDepth](#bulkdepth)
- [partialDepth](#partialdepth)
- [ticker](#ticker)
- [allTickers](#alltickers)
Expand Down Expand Up @@ -910,6 +911,42 @@ client.ws.depth('ETHBTC', depth => {

</details>

#### bulkDepth

Live depth market data feed. The first parameter can either
be a single symbol string or an array of symbols.

This method creates only one connection, that prevents from overpopulating the server.

```js
client.ws.bulkDepth('ETHBTC', depth => {
console.log(depth);
})
```

<details>
<summary>Output</summary>

```js
{
eventType: 'depthUpdate',
eventTime: 1508612956950,
symbol: 'ETHBTC',
firstUpdateId: 18331140,
finalUpdateId: 18331145,
bidDepth: [
{ price: '0.04896500', quantity: '0.00000000' },
{ price: '0.04891100', quantity: '15.00000000' },
{ price: '0.04891000', quantity: '0.00000000' } ],
askDepth: [
{ price: '0.04910600', quantity: '0.00000000' },
{ price: '0.04910700', quantity: '11.24900000' }
]
}
```

</details>

#### partialDepth

Top levels bids and asks, pushed every second. Valid levels are 5, 10, or 20.
Expand Down
33 changes: 33 additions & 0 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ const depth = (payload, cb) => {
return (options) => cache.forEach(w => w.close(1000, 'Close handle was called', { keepClosed: true, ...options }))
}

const bulkDepth = (payload, cb) => {
const _BASE = BASE.replace(/ws$/gi, 'stream');
const streams = (Array.isArray(payload) ? payload : [payload])
.map(symbol => `${symbol}@depth`)
.join('/');

const w = openWebSocket(`${_BASE}?streams=${streams.toLowerCase()}`);
w.onmessage = msg => {
const {
e: eventType,
E: eventTime,
s: symbol,
U: firstUpdateId,
u: finalUpdateId,
b: bidDepth,
a: askDepth,
} = JSON.parse(msg.data).data

cb({
eventType,
eventTime,
symbol,
firstUpdateId,
finalUpdateId,
bidDepth: bidDepth.map(b => zip(['price', 'quantity'], b)),
askDepth: askDepth.map(a => zip(['price', 'quantity'], a)),
});
}

return (options) => w.close(1000, 'Close handle was called', { keepClosed: true, ...options });
}

const partialDepth = (payload, cb) => {
const cache = (Array.isArray(payload) ? payload : [payload]).map(({ symbol, level }) => {
const w = openWebSocket(`${BASE}/${symbol.toLowerCase()}@depth${level}`)
Expand Down Expand Up @@ -272,6 +304,7 @@ const user = opts => cb => {

export default opts => ({
depth,
bulkDepth,
partialDepth,
candles,
trades,
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ test('[WS] depth', t => {
})
})

test('[WS] bulk depth', t => {
return new Promise(resolve => {
const symbols = ['ETHBTC', 'LTCBTC'];
const connection = client.ws.bulkDepth(symbols, depth => {
checkFields(t, depth, [
'eventType',
'eventTime',
'firstUpdateId',
'finalUpdateId',
'symbol',
'bidDepth',
'askDepth',
]);

connection();
resolve();
});
});
});

test('[WS] partial depth', t => {
return new Promise(resolve => {
client.ws.partialDepth({ symbol: 'ETHBTC', level: 10 }, depth => {
Expand Down