Skip to content

Commit

Permalink
Increase coverage and improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
balthazar committed Oct 21, 2017
1 parent 3ec2314 commit 9766fe0
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 83 deletions.
147 changes: 142 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ console.log(await client.getOrder({
<summary>Output</summary>

```js
{
symbol: 'ENGETH',
orderId: 191938,
clientOrderId: '1XZTVBTGS4K1e',
price: '0.00138000',
origQty: '1.00000000',
executedQty: '1.00000000',
status: 'FILLED',
timeInForce: 'GTC',
type: 'LIMIT',
side: 'SELL',
stopPrice: '0.00000000',
icebergQty: '0.00000000',
time: 1508611114735
}
```

</details>
Expand Down Expand Up @@ -428,6 +443,21 @@ console.log(await client.allOrders({
<summary>Output</summary>

```js
[{
symbol: 'ENGETH',
orderId: 191938,
clientOrderId: '1XZTVBTGS4K1e',
price: '0.00138000',
origQty: '1.00000000',
executedQty: '1.00000000',
status: 'FILLED',
timeInForce: 'GTC',
type: 'LIMIT',
side: 'SELL',
stopPrice: '0.00000000',
icebergQty: '0.00000000',
time: 1508611114735
}]
```

</details>
Expand All @@ -437,7 +467,7 @@ console.log(await client.allOrders({
Get current account information.

```js
console.log(await client.openOrders())
console.log(await client.accountInfo())
```

|Param|Type|Required|
Expand All @@ -448,6 +478,19 @@ console.log(await client.openOrders())
<summary>Output</summary>

```js
{
makerCommission: 10,
takerCommission: 10,
buyerCommission: 0,
sellerCommission: 0,
canTrade: true,
canWithdraw: true,
canDeposit: true,
balances: [
{ asset: 'BTC', free: '0.00000000', locked: '0.00000000' },
{ asset: 'LTC', free: '0.00000000', locked: '0.00000000' },
]
}
```

</details>
Expand All @@ -473,6 +516,18 @@ console.log(await client.myTrades({
<summary>Output</summary>

```js
[{
id: 9960,
orderId: 191939,
price: '0.00138000',
qty: '10.00000000',
commission: '0.00001380',
commissionAsset: 'ETH',
time: 1508611114735,
isBuyer: false,
isMaker: false,
isBestMatch: true
}]
```

</details>
Expand All @@ -481,34 +536,99 @@ console.log(await client.myTrades({

#### depth

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

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

<details>
<summary>Output</summary>

```js
{
eventType: 'depthUpdate',
eventTime: 1508612956950,
symbol: 'ETHBTC',
updateId: 18331140,
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>

#### candles

Live candle data feed for a given symbol and interval.
Live candle data feed for a given interval. You can pass either a symbol string
or a symbol array.

```js
client.ws.candles('ETHBTC', '1m', candle => {
console.log(candle)
})
```

<details>
<summary>Output</summary>

```js
{
eventType: 'kline',
eventTime: 1508613366276,
symbol: 'ETHBTC',
open: '0.04898000',
high: '0.04902700',
low: '0.04898000',
close: '0.04901900',
volume: '37.89600000',
trades: 30,
interval: '5m',
isFinal: false,
quoteVolume: '1.85728874',
buyVolume: '21.79900000',
quoteBuyVolume: '1.06838790'
}
```

</details>

#### trades

Live trade data feed for a given symbol.
Live trade data feed. Pass either a single symbol string or an array of symbols.

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

<details>
<summary>Output</summary>

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

</details>

#### user

Live user messages data feed.
Expand All @@ -523,3 +643,20 @@ const clean = await client.ws.user(msg => {

Note that this method returns a promise returning a `clean` callback, that will clear
the keep-alive interval and close the data stream.

<details>
<summary>Output</summary>

```js
{
eventType: 'account',
eventTime: 1508614885818,
balances: {
'123': { available: '0.00000000', locked: '0.00000000' },
'456': { available: '0.00000000', locked: '0.00000000' },
BTC: { available: '0.00000000', locked: '0.00000000' },
]
}
```
</details>
148 changes: 82 additions & 66 deletions src/websocket.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,102 @@
import WebSocket from 'ws'
import zip from 'lodash.zipobject'

import httpMethods from 'http'

const BASE = 'wss://stream.binance.com:9443/ws'

const depth = (symbol, cb) => {
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@depth`)
w.on('message', msg => {
const {
e: eventType,
E: eventTime,
s: symbol,
u: updateId,
b: bidDepth,
a: askDepth,
} = JSON.parse(msg)
const depth = (payload, cb) =>
(Array.isArray(payload) ? payload : [payload]).forEach(symbol => {
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@depth`)
w.on('message', msg => {
const {
e: eventType,
E: eventTime,
s: symbol,
u: updateId,
b: bidDepth,
a: askDepth,
} = JSON.parse(msg)

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

const candles = (symbol, interval, cb) => {
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@kline_${interval}`)
w.on('message', msg => {
const { e: eventType, E: eventTime, s: symbol, k: tick } = JSON.parse(msg)
const {
o: open,
h: high,
l: low,
c: close,
v: volume,
n: trades,
i: interval,
x: isFinal,
q: quoteVolume,
V: buyVolume,
Q: quoteBuyVolume,
} = tick
const candles = (payload, interval, cb) => {
if (!interval || !cb) {
throw new Error('Please pass a symbol, interval and callback.')
}

cb({
eventType,
eventTime,
symbol,
open,
high,
low,
close,
volume,
trades,
interval,
isFinal,
quoteVolume,
buyVolume,
quoteBuyVolume,
;(Array.isArray(payload) ? payload : [payload]).forEach(symbol => {
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@kline_${interval}`)
w.on('message', msg => {
const { e: eventType, E: eventTime, s: symbol, k: tick } = JSON.parse(msg)
const {
o: open,
h: high,
l: low,
c: close,
v: volume,
n: trades,
i: interval,
x: isFinal,
q: quoteVolume,
V: buyVolume,
Q: quoteBuyVolume,
} = tick

cb({
eventType,
eventTime,
symbol,
open,
high,
low,
close,
volume,
trades,
interval,
isFinal,
quoteVolume,
buyVolume,
quoteBuyVolume,
})
})
})
}

const trades = (symbol, cb) => {
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@aggTrades`)
w.on('message', msg => {
const {
e: eventType,
E: eventTime,
s: symbol,
p: price,
q: quantity,
m: maker,
a: tradeId,
} = JSON.parse(msg)
const trades = (payload, cb) =>
(Array.isArray(payload) ? payload : [payload]).forEach(symbol => {
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@aggTrade`)
w.on('message', msg => {
const {
e: eventType,
E: eventTime,
s: symbol,
p: price,
q: quantity,
m: maker,
a: tradeId,
} = JSON.parse(msg)

cb({
eventType,
eventTime,
symbol,
price,
quantity,
maker,
tradeId,
cb({
eventType,
eventTime,
symbol,
price,
quantity,
maker,
tradeId,
})
})
})
}

const userTransforms = {
outboundAccountInfo: m => ({
Expand Down
Loading

0 comments on commit 9766fe0

Please sign in to comment.