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
Example OrderBookUpdate message
{
"marketUpdate": {
"market": {
"exchangeId": "4",
"currencyPairId": "180",
"marketId": "87"
},
"orderBookUpdate": {
"seqNum": 3143,
"bids": [
{
"priceStr": "8087",
"amountStr": "0.04",
},
{
"priceStr": "8086.2",
"amountStr": "0.089",
},
// ...
],
"asks": [
{
"priceStr": "8087.2",
"amountStr": "1.15590988",
},
{
"priceStr": "8090",
"amountStr": "1",
},
// ...
]
}
}
}

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
Example OrderBookDeltaUpdate Message
{
"marketUpdate": {
"market": {
"exchangeId": "4",
"currencyPairId": "180",
"marketId": "87"
},
"orderBookDeltaUpdate": {
"seqNum": 3922,
"bids": {
"removeStr": [
"8048.4"
// ...
]
},
"asks": {
"set": [
{
"priceStr": "8093.7",
"amountStr": "0.8",
}
// ...
]
}
}
}
}

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
Example OrderBookSpreadUpdate message
{
"marketUpdate": {
"market": {
"exchangeId": "4",
"currencyPairId": "180",
"marketId": "87"
},
"orderBookSpreadUpdate": {
"timestamp": "1571337091678",
"bid": {
"priceStr": "8086.2",
"amountStr": "2.32930209",
"price": 8086.2, // DEPRECATED
"amount": 2.329302 // DEPRECATED
},
"ask": {
"priceStr": "8086.3",
"amountStr": "1.29659337",
"price": 8086.3, // DEPRECATED
"amount": 1.2965934 // DEPRECATED
}
}
}
}

Example Code

Node.js
Go
Node.js
import { StreamClient, RESTClient, createOrderBookWatcher } from 'cw-sdk-node';
const restClient = new RESTClient();
const streamClient = new StreamClient({
creds: {
apiKey: '<your api key>',
secretKey: '<your secret key>'
}
});
let orderBookWatcher = null;
function run() {
// Get the ID of the market you want to watch
createOrderBookWatcher(
// a market ID can also be supplied instead of exchange/base/quote
{
exchange: 'kraken',
base: 'btc',
quote: 'usd'
},
streamClient,
restClient
).then((orderBookWatcher) => {
orderBookWatcher.onUpdate((marketId, snapshot) => {
console.log(`Market ${marketId} OrderBook updated!`, snapshot);
});
orderBookWatcher.onError((marketID, error) => {
console.error(`Error updating OrderBook on Marked "${marketID}"!`, error);
});
// Connect to stream
streamClient.connect();
});
}
run();
// When finished with OrderBookWatcher, be sure to destroy it!
// This unsubscribes from the orderbook stream and prevents it from retrieving more snapshot data.
function cleanUp() {
streamClient.disconnect();
orderBookWatcher.destroy();
}
Go
package main
import (
"log"
"os"
"os/signal"
"syscall"
"code.cryptowat.ch/cw-sdk-go/client/rest"
"code.cryptowat.ch/cw-sdk-go/client/websocket"
"code.cryptowat.ch/cw-sdk-go/common"
"code.cryptowat.ch/cw-sdk-go/orderbooks"
)
func main() {
restClient := rest.NewRESTClient(nil)
streamClient, err := websocket.NewStreamClient(nil)
if err != nil {
log.Fatal(err)
}
streamClient.Connect()
orderbook, err := orderbooks.NewOrderBookWatcher(orderbooks.OrderBookWatcherParams{
RESTClient: restClient,
StreamClient: streamClient,
Market: common.MarketParams{
Symbol: common.MarketSymbol{
Exchange: "kraken",
Base: "btc",
Quote: "usd",
},
},
})
if err != nil {
log.Fatal(err)
}
orderbook.OnUpdate(func(update orderbooks.Update) {
if ob := update.OrderBookUpdate; ob != nil {
log.Println("Order book update")
} else if err := update.GetSnapshotError; err != nil {
log.Println("Error", err)
}
})
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
// Wait until the OS signal is received, at which point we'll close the connection and quit.
<-signals
log.Print("Exiting")
}