Manage local order book
If you are keeping track of the order book locally, it is important to keep it in sync with Bitvavo. This prevents inaccurate trading caused by stale or out-of-sequence data and ensures your orders reflect the latest market conditions.
This is especially important for high-frequency trading strategies that rely on real-time data and trade on multiple exchanges.
Step 1: Subscribe to order book updates
-
Connect to the WebSocket API using the
wss://api.bitvavo.com/v2/
endpoint. -
Subscribe to the Book subscription channel for a market (for example,
BTC-EUR
).WebSocket subscription message{
"action": "subscribe",
"channels": [
{
"name": "book",
"markets": ["BTC-EUR"]
}
]
} -
Store (buffer) the incoming events ordered by the value of the
nonce
parameter. This is a sequential identifier of the order book version.Book subscription event{
"event": "book",
"market": "BTC-EUR",
"nonce": 123456789,
"bids": [
[ "30000.50", "0.1" ],
[ "30000.00", "0.5" ]
],
"asks": [
[ "30001.00", "0.2" ],
[ "30001.50", "0.3" ]
]
}
Step 2: Get a snapshot of the order book
-
To get a snapshot of the order book, make a Get order book REST API or WebSocket request for a market.
- REST
- WebSocket
cURL requestcurl -L -g 'https://api.bitvavo.com/v2/{market}/book' \
-H 'Accept: application/json' \
-H 'Bitvavo-Access-Key: YOUR_API_KEY' \
-H 'Bitvavo-Access-Timestamp: YOUR_GENERATED_SIGNATURE' \
-H 'Bitvavo-Access-Signature: CURRENT_TIMESTAMP' \
-H 'Bitvavo-Access-Window: 10000' \getBook message{
"action": "getBook",
"requestId": 1,
"market": "BTC-EUR",
"depth": 3
}The response for REST and WebSocket is the same:
Snapshot{
"market": "BTC-EUR",
"nonce": 123456788,
"bids": [
[ "30000.50", "0.1" ],
[ "30000.00", "0.6" ],
[ "29999.50", "0.2" ]
],
"asks": [
[ "30001.00", "0.2" ],
[ "30001.50", "0.3" ],
[ "30002.00", "0.1" ]
]
} -
Compare the
nonce
of the snapshot with thenonce
of the initial WebSocket update:- If the value
nonce
of the snapshot is greater than thenonce
from the first update, go to Step 3. - If the
nonce
of the snapshot is smaller than or equal to thenonce
from the first update, get a new snapshot and try again.
- If the value
-
Discard all stored updates where the
nonce
of the event is smaller than or equal to thenonce
from the snapshot.
Step 3: Update your local order book
- Update the
bids
andasks
in your order book to the state from the snapshot. - Set the update ID of your order book to the
nonce
of the snapshot. - Apply the remaining stored updates with a
nonce
greater than thenonce
of the snapshot. - Continue to apply new WebSocket updates, using the update procedure below.
Step 4: Discard outdated data
- If the
nonce
of the event is not greater exactly by 1 than thenonce
of your local order book, your local book is out of sync. Discard it and restart the process from Step 1 (re-subscribe and get a new snapshot). - For every price in
bids
andasks
:- If the price is not in your local book, add it with the provided size (for example, price level
"30000.50"
and size"0.1"
). - If the size is
0
, remove the price from your local book. - If the price already exists, update its size to the new value.
- If the price is not in your local book, add it with the provided size (for example, price level
- After applying all updates from the event, set the
nonce
of your local book to match thenonce
of the event.