Skip to content
All library documents

Aggregating Binance Bitcoin Order Book Levels into Price Buckets

Article Quant Q&A · Author: Kevin Kim

Summary

The document explains how to create wider price buckets from raw Binance BTCUSDT order book levels. The exchange provides individual price levels, while wider increments shown in a user interface are aggregated display buckets. For a custom bucket width, sum the quantities of raw bid and ask levels whose prices fall within each range. For a reliable live book, initialize from a sufficiently deep snapshot and keep it synchronized with incremental depth updates rather than repeatedly polling snapshots. The discussion also distinguishes live aggregation from historical research: arbitrary historical full-depth states may require recording updates going forward or obtaining replay data from a vendor. A maintained local depth cache can serve strategies with a synchronized live book, but it does not itself supply historical data. The guidance focuses on data handling; it does not evaluate strategy performance or specify market impact and execution considerations.

Key ideas

  • Raw exchange order book levels can be grouped into custom price ranges by summing quantities.
  • A snapshot can initialize a local book, which is then updated using depth stream changes.
  • Repeated snapshot polling is not presented as a reliable way to maintain a live order book.
  • Historical order book replay requires recorded updates, an available data product, or a vendor.

Tags

Full text
# where can i get all the depth levels of orderbook data for BTC?


# where can i get all the depth levels of orderbook data for BTC?












Right now, I was able to get an orderbook snapshots data for BTCUSDT in Binance. The problem with this data is that each depth level of the orderbook increases every 5 cents or so. For total of 20 depth levels, it amounts to only 1 dollar higher or lower from the mid price.

Because i intend to hold the position for longer (around a few hours to a couple days), I want to see the bigger picture, i.e. in Binance website, you can select, for example, "10" dollars for the orderbook and the orderbook depth levels are in increments of 10 dollars. I want this type of data, and not the orderbook data where the depth levels increase in increments of 5 cents. Is there a way to obtain such data?

## Answer by autoencoder (score 0, accepted)

https://quant.stackexchange.com/a/79856

This answer does not give you orderbook depth levels with 10 dollar increments directly, but does give you a broader range of depth:

So to have more levels than 20, you could simply use api provided by binance:

https://fapi.binance.com/fapi/v1/depth?symbol=BTCUSDT&limit=1000

and you can get levels up to 1000.

You may also want to manage a local orderbook based on this, see How to manage a local order book correctly from binance, and they provide a official python script here:

https://github.com/binance/binance-toolbox-python/blob/master/manage_local_order_book.py

With the above setting you could then do your own calculations for any arbitrary price increment.

## Answer by Oliver Zehentleitner (score 1)

https://quant.stackexchange.com/a/85642

There are two different concepts here that are easy to mix up:

- raw exchange order book levels

- aggregated display buckets

Binance does not usually give you a separate "10 USD order book" feed in the same sense as the UI shows it. The exchange API gives you raw price levels. The "10 USD" view in the UI is an aggregation/display layer on top of those raw levels.

So the usual approach is:

- get a sufficiently deep snapshot

- keep the book updated with diff depth streams

- aggregate the raw levels yourself into whatever bucket size you need

For example, if you want 10 USD buckets, you group all bid/ask levels into price ranges such as:

- 65000-65010

- 65010-65020

- 65020-65030

and sum the quantities inside each bucket.

For live data, do not repeatedly poll snapshots if you need a reliable book over time. Use the snapshot only to initialize the book, then apply websocket depth updates. This is also the model described in Binance's own local order book documentation.

A simplified aggregation example:

```
from decimal import Decimal, ROUND_FLOOR

def bucket_price(price: str, bucket_size: str = "10"):
    price = Decimal(price)
    bucket = Decimal(bucket_size)
    return (price / bucket).to_integral_value(rounding=ROUND_FLOOR) * bucket

def aggregate_side(levels, bucket_size="10"):
    buckets = {}

    for price, qty in levels:
        b = bucket_price(price, bucket_size)
        buckets[b] = buckets.get(b, Decimal("0")) + Decimal(qty)

    return sorted(buckets.items())

# levels = [["65001.20", "0.5"], ["65008.40", "0.2"], ["65015.00", "1.1"]]
# aggregate_side(levels, "10")
```

For historical data, the answer is different: Binance does not give you a simple free public endpoint where you can request arbitrary historical full-depth order book states for any time range. You either need to:

- record the diff depth stream yourself going forward,

- use Binance historical/order-book delta products where available and permitted,

- or buy historical L2/order book replay data from a data vendor.

If your goal is live usage, one practical option is to maintain a local depth cache service and query that instead of rebuilding the book inside every strategy process.

Disclosure: I maintain an open-source project called UNICORN Binance DepthCache Cluster (UBDCC). It runs synchronized Binance DepthCaches as a local service, supports replicas/failover, and exposes the book over HTTP/JSON, so applications in Python, Node.js, Go, Rust, Java, etc. can query the same maintained book and then aggregate it into any bucket size they need.

It does not magically provide historical data. It is mainly for live order book infrastructure. For backtesting, you still need recorded or purchased historical L2 data.

## Answer by Mate Hegedus (score 0)

https://quant.stackexchange.com/a/85618

If you are also looking for historical orderbook data to backtest your strategy I would recommend my own site: https://www.cryptohftdata.com/

From here you can download historical tick by ticker L2 orderbook replays for 9+ crypto exchanges (including Binance).

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.