Skip to content
Merged
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
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Following examples will use the `await` form, which requires some configuration
- [ticker](#ticker)
- [allTickers](#alltickers)
- [candles](#candles-1)
- [trades](#trades)
- [aggTrades](#aggtrades-1)
- [trades](#trades-1)
- [user](#user)
- [ErrorCodes](#errorcodes)

Expand Down Expand Up @@ -1031,7 +1032,7 @@ client.ws.candles('ETHBTC', '1m', candle => {

#### trades

Live trade data feed. Pass either a single symbol string or an array of symbols.
Live trade data feed. Pass either a single symbol string or an array of symbols. The trade streams push raw trade information; each trade has a unique buyer and seller.

```js
client.ws.trades(['ETHBTC', 'BNBBTC'], trade => {
Expand All @@ -1042,6 +1043,33 @@ client.ws.trades(['ETHBTC', 'BNBBTC'], trade => {
<details>
<summary>Output</summary>

```js
{
eventType: 'trade',
eventTime: 1508614495052,
symbol: 'ETHBTC',
price: '0.04923600',
quantity: '3.43500000',
maker: false,
tradeId: 2148226
}
```

</details>

#### aggTrades

Live trade data feed. Pass either a single symbol string or an array of symbols. The aggregate trade streams push trade information that is aggregated for a single taker order.

```js
client.ws.aggTrades(['ETHBTC', 'BNBBTC'], trade => {
console.log(trade)
})
```

<details>
<summary>Output</summary>

```js
{
eventType: 'aggTrade',
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ declare module 'binance-api-node' {
allTickers: (callback: (tickers: Ticker[]) => void) => Function;
candles: (pair: string, period: string, callback: (ticker: Candle) => void) => Function;
trades: (pairs: string[], callback: (trade: Trade) => void) => Function;
aggTrades: (pairs: string[], callback: (trade: Trade) => void) => Function;
user: ( callback: (msg: OutboundAccountInfo|ExecutionReport) => void) => Function;
}

Expand Down
9 changes: 7 additions & 2 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ const allTickers = cb => {
return (options) => w.close(1000, 'Close handle was called', { keepClosed: true, ...options })
}

const trades = (payload, cb) => {
const tradesInternal = (payload, streamName, cb) => {
const cache = (Array.isArray(payload) ? payload : [payload]).map(symbol => {
const w = openWebSocket(`${BASE}/${symbol.toLowerCase()}@aggTrade`)
const w = openWebSocket(`${BASE}/${symbol.toLowerCase()}@${streamName}`)
w.onmessage = msg => {
const {
e: eventType,
Expand Down Expand Up @@ -195,6 +195,10 @@ const trades = (payload, cb) => {
return (options) => cache.forEach(w => w.close(1000, 'Close handle was called', { keepClosed: true, ...options }))
}

const aggTrades = (payload, cb) => tradesInternal(payload, 'aggTrade', cb)

const trades = (payload, cb) => tradesInternal(payload, 'trade', cb)

const userTransforms = {
outboundAccountInfo: m => ({
eventType: 'account',
Expand Down Expand Up @@ -271,6 +275,7 @@ export default opts => ({
partialDepth,
candles,
trades,
aggTrades,
ticker,
allTickers,
user: user(opts),
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ test('[WS] trades', t => {
})
})

test('[WS] aggregate trades', t => {
return new Promise(resolve => {
client.ws.aggTrades(['BNBBTC', 'ETHBTC', 'BNTBTC'], trade => {
checkFields(t, trade, ['eventType', 'tradeId', 'quantity', 'price', 'symbol'])
resolve()
})
})
})

test('[WS] userEvents', t => {
const accountPayload = {
e: 'outboundAccountInfo',
Expand Down