Order Book
Order Book is used to subscribe to real-time updates for the order book of a specific trading pair on our platform.
Information
Update Speed: 1000ms
Initial Limit: 1000 data points for both bid and ask sides
Message Structure
Send Message
- type: Specifies the type of WebSocket message.
- Type: String.
- Value: "subscribe"
- channel: Indicates the type of data to subscribe to (e.g., order book updates).
- Type: String.
- Value: "orderbook"
- id: A unique identifier for the subscription request. Write randomly or use any unique identifier.
- Type: String.
- Example. "random string 001"
- symbol: Specifies the trading pair for which the subscriber wants to receive order book updates.
- Type: String.
- Example: "WBTCUSDT"
Subscribe Example
{
"type": "subscribe",
"channel": "orderbook",
"id": "random string",
"symbol": "WBTCUSDT"
}
Unsubscribe Example
{
"type": "unsubscribe",
"channel": "orderbook",
"id": "random string",
"symbol": "WBTCUSDT"
}
Initial Message
- type: Specifies the type of WebSocket message.
- Type: String
- Value: "init"
- channel: Indicates the type of data being updated (e.g., order book updates).
- Type: String
- Value: "orderbook"
- symbol: Specifies the trading pair for which the order book is being updated.
- Type: String
- Example: "WBTCUSDT"
- timestamp: Specifies the timestamp of the order book is subscribed.
- Type: Integer (UNIX timestamp in milliseconds & UTC)
- Example: 1704433004692
- data: Contains the actual order book update information.
- bids: Array of bid orders (buy orders) in the order book. Each bid is an object with price, volume, and orderCount fields.
- Type: Array of Objects
- asks: Array of ask orders (sell orders) in the order book. Each ask is an object with price, volume, and orderCount fields.
- Type: Array of Objects
- timestamp: The current timestamp when the update message is sent.
- Type: Integer (UNIX timestamp in milliseconds & UTC)
Response Message Example
- Type: Integer (UNIX timestamp in milliseconds & UTC)
- bids: Array of bid orders (buy orders) in the order book. Each bid is an object with price, volume, and orderCount fields.
{
"error": false,
"id": "random string",
"type": "init",
"channel": "orderbook",
"symbol": "WBTCUSDT",
"timestamp": 1704433004692,
"data": {
"timestamp": 1704433004336,
"asks": [
{
"price": 43694.08,
"volume": 0.85,
"orderCount": 1
},
{
"price": 43702.88,
"volume": 0.7,
"orderCount": 1
},
{
"price": 43703.8,
"volume": 0.98,
"orderCount": 1
},
{
"price": 43704.3,
"volume": 0.87,
"orderCount": 1
},
{
"price": 43706.99,
"volume": 0.78,
"orderCount": 1
}
],
"bids": [
{
"price": 43665.09,
"volume": 0.73,
"orderCount": 1
},
{
"price": 43663.64,
"volume": 0.87,
"orderCount": 1
},
{
"price": 43662.53,
"volume": 0.53,
"orderCount": 1
}
]
}
}
Update Message
type: Specifies the type of WebSocket message.
- Type: String
- Value: "update"
channel: Indicates the type of data being updated (e.g., order book updates).
- Type: String
- Value: "orderbook"
symbol: Specifies the trading pair for which the order book is being updated.
- Type: String
- Example: "WBTCUSDT"
data: Contains the actual order book update information.
- bids: Array of bid orders (buy orders) in the order book. Each bid is an object with price, volume, and orderCount fields.
- Type: Array of Objects
- asks: Array of ask orders (sell orders) in the order book. Each ask is an object with price, volume, and orderCount fields.
- Type: Array of Objects
- lastTimestamp: The timestamp of the last order book.
- Type: Integer (UNIX timestamp in milliseconds & UTC)
- timestamp: The timestamp of the current order book.
- Type: Integer (UNIX timestamp in milliseconds & UTC)
Update Message Example
{ "type": "update", "channel": "orderbook", "symbol": "WBTCUSDT", "data": { "bids": [ { "price": 32680.18, "volume": 0.09, "orderCount": 1 }, { "price": 32571.69, "volume": 0.1, "orderCount": 1 }, { "price": 32142.54, "volume": 0.1, "orderCount": 1 } ], "asks": [ { "price": 34915.97, "volume": 0.1, "orderCount": 1 }, { "price": 32998.83, "volume": 0.0, "orderCount": 0 } ], "lastTimestamp": 1700809758368, "timestamp": 1700809759368 } }
Maintain a Local Orderbook
To maintain a local order book correctly, follow these steps:
- Type: Integer (UNIX timestamp in milliseconds & UTC)
- bids: Array of bid orders (buy orders) in the order book. Each bid is an object with price, volume, and orderCount fields.
Subscribe to the WebSocket "orderbook" Channel
- Initiate a connection to the WebSocket order book channel for real-time updates.
Initialize with Snapshot
- Upon subscribing, you'll receive an initial snapshot message which type is
"init"
. Buffer and store this data to create your local order book. - This snapshot gives you the current state of the order book up to a certain depth limit (1000 data points for both bid and ask sides).
- Upon subscribing, you'll receive an initial snapshot message which type is
Process Updates
- Continuously listen to the stream of updates which type is
"uptade"
. - For each update, check that the
lastTimestamp
matches thetimestamp
in previous message from your local order book to ensure updates are in sequence. - If the timestamps do not match, it indicates that some messages have been lost. In such cases, you should reconnect and reinitialize your order book to ensure accuracy.
- Continuously listen to the stream of updates which type is
Apply Updates
- The data in each update represents the absolute quantity at a specific price level.
- If the quantity is greater than 0 at a price level, update your local order book with this new quantity.
- If the quantity is 0, remove the corresponding price level from your local order book.
Handling 1000 Price Levels
- Your local order book might not include all price levels due to the initial snapshot's depth limit of 1000 levels.
- Vessel's WebSocket only maintains the most recent 1000 levels in the order book. Specifically, when an update message is received, the system retrieves the latest 1000 price levels. If there are price levels in the previous order book that are no longer within the latest 1000 price levels, their quantities will be updated to 0. If there is any discrepancy between the previous order book and the latest one, the system will update the quantities based on the latest price levels.
- However, the depth limit of 1000 levels generally provides enough information to understand the market and trade effectively.
By following these steps, you can maintain a local order book that is sufficiently accurate for most trading activities, even though it may not perfectly match the full order book on the exchange.