orderbook_full channel (hip3_orderbook_full for HIP-3) sends every price level once, then the level changes from each block. The script below keeps the total size at every BTC price level from that one subscription and prints the best bid, best ask and level counts.
The script
live_book.py
live_book.py and run it with your API key in OXARCHIVE_API_KEY, as the top comments show. It needs Python 3.9 or later and websockets 14 or later. For a HIP-3 market, change CHANNEL and SYMBOL as the comments show.
How it works
1
Connect with your key
Send your API key in the opening handshake as
Authorization: Bearer <key>. An invalid key is refused with HTTP 401 before the socket opens. Connection covers the full handshake.2
Subscribe
Send one subscribe message per book. The server confirms with a HIP-3 symbols keep the builder prefix and its case, such as
subscribed message.xyz:XYZ100.3
Load the snapshot
The first data message is an
l4_snapshot holding every level: data.bids and data.asks, each level {px, sz, n} with price, total size and order count. Replace the book you hold with it. The top-level last_block_number is the block the snapshot reflects.4
Apply each batch in order
Every
l4_batch carries a list of level changes {side, px, sz, n, bn}. side is B for bids and A for asks, and bn is the block number. Set the level at px to size sz and order count n, and delete it when sz is 0. The script keeps sizes only. Apply the changes in the order they arrive.Stay connected
The server sends a ping frame every 30 seconds, andwebsockets answers it automatically. The script also sends {"op":"ping"} every 25 seconds, and the server replies {"type":"pong"}. A connection with active subscriptions closes after 60 seconds without activity; Keep-alive has the details.
A BTC snapshot is about 830 KB, and a busy batch can pass 200 KB. websockets caps messages at 1 MiB unless you set max_size, so the script turns the cap off.
When the connection drops, the script clears the book and reconnects. The new subscription opens with a fresh snapshot. A handshake refused with a 4xx status, such as 401 for an invalid key, stops the script instead of retrying.
Keep up with the stream
A BTC book receives about 8 batches and 3,000 level changes a second. Keep the receive loop to the dictionary updates above, and run display, storage and signal work outside it. Read the book only while it holds bothB and A. The script prints once every 40 batches for the same reason. To read the best bid and ask on every update, keep each side in a sorted structure such as SortedDict from sortedcontainers instead of calling max and min.
If the client falls behind, changes are dropped: the server sends an error with error_code slow_consumer and stops that subscription. The script then reconnects and starts again from a fresh snapshot.
Credits
WebSocket data is billed per delivered message, at no more than 1 credit a message. The snapshot is one message, and eachl4_batch is one message however many level changes it carries. A BTC book at about 8 batches a second costs at most about 8 credits a second. Credits covers estimating a job, and Rate limits lists each plan’s monthly credits.