Order Books
Order book data is separated into three streams for snapshots, deltas, and spread. Order book snapshots are broadcast once per minute, while the deltas are real-time. To maintain a real-time order book, apply deltas to the most recent snapshot. Both deltas and snapshots contain a sequence number, so you can determine if the data is received in the correct order.

Snapshots

You can subscribe to order book snapshots in a variety of ways. Each will result in OrderBookUpdate messages for each resource.
Subscription
Description
markets:68:book:snapshots
A feed of order book snapshots for a single market
exchanges:4:book:snapshots
A feed of all order book snapshots for a given exchange
instruments:232:book:snapshots
A feed of all order book snapshots for a given instrument (like BTC/USD)
assets:60:book:snapshots
A feed of all order book snapshots for all instruments for a given asset (like BTC/*)
Example OrderBookUpdate message
1
{
2
"marketUpdate": {
3
"market": {
4
"exchangeId": "4",
5
"currencyPairId": "180",
6
"marketId": "87"
7
},
8
"orderBookUpdate": {
9
"seqNum": 3143,
10
"bids": [
11
{
12
"priceStr": "8087",
13
"amountStr": "0.04",
14
},
15
{
16
"priceStr": "8086.2",
17
"amountStr": "0.089",
18
},
19
// ...
20
],
21
"asks": [
22
{
23
"priceStr": "8087.2",
24
"amountStr": "1.15590988",
25
},
26
{
27
"priceStr": "8090",
28
"amountStr": "1",
29
},
30
// ...
31
]
32
}
33
}
34
}
Copied!
These are very large messages, so the order arrays have been truncated.

Deltas

Deltas are used to update an order book without sending the entire snapshot over the wire.
Subscription
Description
markets:68:book:deltas
A feed of order book deltas for a single market
exchanges:4:book:deltas
A feed of all order book deltas for a given exchange
instruments:232:book:deltas
A feed of all order book deltas for a given instrument (like BTC/USD)
assets:60:book:deltas
A feed of all order book deltas for all instruments for a given asset (like BTC/*)
Example OrderBookDeltaUpdate Message
1
{
2
"marketUpdate": {
3
"market": {
4
"exchangeId": "4",
5
"currencyPairId": "180",
6
"marketId": "87"
7
},
8
"orderBookDeltaUpdate": {
9
"seqNum": 3922,
10
"bids": {
11
"removeStr": [
12
"8048.4"
13
// ...
14
]
15
},
16
"asks": {
17
"set": [
18
{
19
"priceStr": "8093.7",
20
"amountStr": "0.8",
21
}
22
// ...
23
]
24
}
25
}
26
}
27
}
Copied!

Spread

You can also subscribe to book:spread to get updates to only the best bid & ask.
Subscription
Description
markets:68:book:spread
A feed of order book spread for a single market
exchanges:4:book:spread
A feed of all order book spreads for a given exchange
instruments:232:book:spread
A feed of all order book spreads for a given instrument (like BTC/USD)
assets:60:book:spread
A feed of all order book spreads for all instruments for a given asset (like BTC/*)
Example OrderBookSpreadUpdate message
1
{
2
"marketUpdate": {
3
"market": {
4
"exchangeId": "4",
5
"currencyPairId": "180",
6
"marketId": "87"
7
},
8
"orderBookSpreadUpdate": {
9
"timestamp": "1571337091678",
10
"bid": {
11
"priceStr": "8086.2",
12
"amountStr": "2.32930209",
13
"price": 8086.2, // DEPRECATED
14
"amount": 2.329302 // DEPRECATED
15
},
16
"ask": {
17
"priceStr": "8086.3",
18
"amountStr": "1.29659337",
19
"price": 8086.3, // DEPRECATED
20
"amount": 1.2965934 // DEPRECATED
21
}
22
}
23
}
24
}
Copied!

Example Code

Node.js
Go
1
import { StreamClient, RESTClient, createOrderBookWatcher } from 'cw-sdk-node';
2
const restClient = new RESTClient();
3
const streamClient = new StreamClient({
4
creds: {
5
apiKey: '<your api key>',
6
secretKey: '<your secret key>'
7
}
8
});
9
10
let orderBookWatcher = null;
11
12
function run() {
13
// Get the ID of the market you want to watch
14
createOrderBookWatcher(
15
// a market ID can also be supplied instead of exchange/base/quote
16
{
17
exchange: 'kraken',
18
base: 'btc',
19
quote: 'usd'
20
},
21
streamClient,
22
restClient
23
).then((orderBookWatcher) => {
24
orderBookWatcher.onUpdate((marketId, snapshot) => {
25
console.log(`Market ${marketId} OrderBook updated!`, snapshot);
26
});
27
28
orderBookWatcher.onError((marketID, error) => {
29
console.error(`Error updating OrderBook on Marked "${marketID}"!`, error);
30
});
31
32
// Connect to stream
33
streamClient.connect();
34
});
35
}
36
37
run();
38
39
// When finished with OrderBookWatcher, be sure to destroy it!
40
// This unsubscribes from the orderbook stream and prevents it from retrieving more snapshot data.
41
function cleanUp() {
42
streamClient.disconnect();
43
orderBookWatcher.destroy();
44
}
Copied!
1
package main
2
3
import (
4
"log"
5
"os"
6
"os/signal"
7
"syscall"
8
9
"code.cryptowat.ch/cw-sdk-go/client/rest"
10
"code.cryptowat.ch/cw-sdk-go/client/websocket"
11
"code.cryptowat.ch/cw-sdk-go/common"
12
"code.cryptowat.ch/cw-sdk-go/orderbooks"
13
)
14
15
func main() {
16
restClient := rest.NewRESTClient(nil)
17
streamClient, err := websocket.NewStreamClient(nil)
18
if err != nil {
19
log.Fatal(err)
20
}
21
22
streamClient.Connect()
23
24
orderbook, err := orderbooks.NewOrderBookWatcher(orderbooks.OrderBookWatcherParams{
25
RESTClient: restClient,
26
StreamClient: streamClient,
27
Market: common.MarketParams{
28
Symbol: common.MarketSymbol{
29
Exchange: "kraken",
30
Base: "btc",
31
Quote: "usd",
32
},
33
},
34
})
35
if err != nil {
36
log.Fatal(err)
37
}
38
39
orderbook.OnUpdate(func(update orderbooks.Update) {
40
if ob := update.OrderBookUpdate; ob != nil {
41
log.Println("Order book update")
42
} else if err := update.GetSnapshotError; err != nil {
43
log.Println("Error", err)
44
}
45
})
46
47
signals := make(chan os.Signal, 1)
48
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
49
50
// Wait until the OS signal is received, at which point we'll close the connection and quit.
51
<-signals
52
log.Print("Exiting")
53
}
Copied!
Last modified 1yr ago