I am trying to use cex.io API to construct a live order book, but I am struggling understanding the message from the API.
I am subscribing with the following JSON:
{
"e": "order-book-subscribe",
"data": {
"pair": [
"BTC",
"USD"
],
"subscribe": false,
"depth": -1
},
"oid": "1435927928274_3_order-book-subscribe"
}
The first message is a snapshot of the order book, this one its ok.
But next, the messages are "just" updates, same as this one :
{
'e': 'md_update',
'data': {
'id': 92276361,
'pair': 'BTC:USD',
'time': 1505337293621,
'bids': [],
'asks': [
[4078.1692, 0.0]
]
}
}
How do I update the snapshot first received with the updates lines ?
How do I know, if some of the lines if there was match in the book ?
And also if I subscribe with depth = 1
, would the updates only be for best bid / best ask ?
You simply match the updates with the current state of your internal orderbook. The API sends you the changes which are made to the orderbook on server side.
Example:
The update message is:
We see the ask with 4070 price has now an remaining amount of 0. So delete that entry from your orderbook.
Ask 4080 now has an remaining amount of 1. You check if there is already an ask with price 4080 in your orderbook. If so, update the amount to 1. If there is no ask with 4080 in your orderbook, add an entry with price 4080 and amount 1 to your orderbook.