Skip to main content

Stream Binance Level 2 order book and trade data to local disk or GCS.

Project description

crypto-lob-stream

Stream Binance Level 2 order book and trade data to local disk or Google Cloud Storage.

High-quality, granular LOB data is difficult to access openly. Most sources are paywalled, time-limited, or never publicly released. This package makes it easy for anyone to collect continuous, fully reconstructable order book and trade data for any Binance pair and store it in an open, analysis-ready format. Monthly snapshots are published freely on Hugging Face for the research community.


Install

# Local output only
pip install crypto-lob-stream

# With Google Cloud Storage support
pip install crypto-lob-stream[gcs]

Requires Python 3.10+.

Windows users: if you see a warning that the Scripts directory is not on PATH, add it via System Settings > Environment Variables > Path. Until then, run with python -m crypto_lob_stream.cli instead of crypto-lob-stream.


Quickstart

Stream to local disk

from crypto_lob_stream import LOBStreamer

streamer = LOBStreamer(
    assets=["BTCUSDT", "ETHUSDT", "SOLUSDT"],
    output="local",
    output_dir="./lob_data",
)
streamer.run()

Stream to GCS

streamer = LOBStreamer(
    assets=["BTCUSDT"],
    output="gcs",
    bucket="my-gcs-bucket",
)
streamer.run()

Real-time callback

def on_trade(record):
    print(f"{record['asset']} {record['price']} x {record['quantity']}")

streamer = LOBStreamer(
    assets=["BTCUSDT"],
    output="local",
    on_trade=on_trade,
)
streamer.run()

CLI

# Local
crypto-lob-stream --assets BTCUSDT,ETHUSDT --output local --output-dir ./data

# GCS (after running setup)
crypto-lob-stream --assets BTCUSDT,ETHUSDT,SOLUSDT --output gcs

# Custom flush interval (seconds)
crypto-lob-stream --assets BTCUSDT --output local --flush-interval 60

GCS setup

If you want to stream to Google Cloud Storage, run the setup wizard once before your first stream. It saves your credentials so you never have to configure them again.

Step 1 -- Create a GCS bucket

Go to console.cloud.google.com, create a project, and create a bucket in your preferred region.

Step 2 -- Create a service account

In the GCP Console, go to IAM & Admin > Service Accounts. Create a new service account, assign it the Storage Object Admin role on your bucket, and download the JSON key file.

Step 3 -- Run the setup wizard

crypto-lob-stream setup

You will be prompted for your bucket name and the path to your JSON key file:

crypto-lob-stream -- GCS Setup
----------------------------------
GCS bucket name: my-bucket
Path to service account JSON key: ~/Downloads/my-key.json

Saved config to ~/.crypto_lob_stream/config.json
Testing connection to gs://my-bucket ... OK

Setup complete. You can now run:
  crypto-lob-stream --assets BTCUSDT,ETHUSDT --output gcs

Config is saved to ~/.crypto_lob_stream/config.json. From this point on, just run with --output gcs and credentials are applied automatically.

Check saved config

crypto-lob-stream config

Override credentials per run

crypto-lob-stream --assets BTCUSDT --output gcs --bucket other-bucket --credentials /path/to/other-key.json

Output structure

{output_dir}/
  trades/{asset}/YYYY-MM-DD-HH.parquet
  depth/{asset}/YYYY-MM-DD-HH.parquet
  snapshots/{asset}/YYYY-MM-DD-HHmmss.parquet

All files are Snappy-compressed Parquet. Files are flushed to disk or uploaded to GCS every 5 minutes by default.


Schemas

trades

Field Type Notes
timestamp_ms int64 Event time (Unix ms)
asset string e.g. BTCUSDT
trade_id int64 Binance trade ID
price float64
quantity float64
buyer_maker bool True if the buyer is the market maker

depth (diff events)

Field Type Notes
timestamp_ms int64 Receipt time (Unix ms)
asset string
side string bid or ask
price float64
quantity float64 0.0 means the level was removed
first_update_id int64 U field from Binance diff event
last_update_id int64 u field -- sequence number for replay ordering

snapshots

Field Type Notes
timestamp_ms int64 Fetch time (Unix ms)
asset string
side string bid or ask
price float64
quantity float64
last_update_id int64 REST snapshot lastUpdateId -- anchor for diff replay

A REST snapshot is fetched from Binance (1000 levels per side) immediately after every WebSocket connection, including reconnects. This ensures there is always a valid anchor point for book reconstruction.


LOB reconstruction

To reconstruct the full order book at any point in time from stored data:

  1. Load the nearest snapshots/ file whose timestamp_ms precedes your target window. The last_update_id column is the snapshot anchor.
  2. Discard all depth diff rows where last_update_id <= snapshot_last_update_id.
  3. Verify the first retained diff satisfies first_update_id <= snapshot_last_update_id + 1. If not, a gap exists -- use the next available snapshot.
  4. Apply diffs in ascending last_update_id order. For each price level, set quantity to the diff value. Remove the level if quantity == 0.0.

To detect the schema version programmatically:

import pyarrow.parquet as pq

schema = pq.read_schema("depth/btcusdt/2026-06-03-15.parquet")
is_reconstructable = "last_update_id" in schema.names

LOBStreamer parameters

Parameter Type Default Description
assets list[str] required Binance symbols, e.g. ["BTCUSDT", "ETHUSDT"]
output str "local" "local" or "gcs"
output_dir str "./lob_data" Base directory for local output
bucket str None GCS bucket name (required for GCS output if not saved via setup)
fallback_dir str "./lob_fallback" Local directory for failed GCS uploads
flush_interval int 300 Seconds between buffer flushes
on_trade callable None Optional callback invoked per trade record
on_depth callable None Optional callback invoked per depth record
log_dir str "./logs" Directory for rotating log files

Data limitations

Depth files collected before 2026-06-03 14:38:49 UTC do not contain first_update_id or last_update_id and cannot be used for LOB reconstruction. They may still be useful for analysing the distribution of price-level updates and trade activity. Files from that timestamp onward are fully reconstructable.


Hugging Face dataset

Monthly snapshots of data collected by this package are published at Hugging Face for free public use. Each release includes a dataset card noting the schema cutoff date and any known gaps.


Contributing

Issues and pull requests are welcome. If you add support for a new exchange or output target, please include tests.


License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crypto_lob_stream-0.2.3.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crypto_lob_stream-0.2.3-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file crypto_lob_stream-0.2.3.tar.gz.

File metadata

  • Download URL: crypto_lob_stream-0.2.3.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for crypto_lob_stream-0.2.3.tar.gz
Algorithm Hash digest
SHA256 6caf0d58b954d768ec07f866ee1db66b4c1c99dad3a5e05e3e15542ce64be9b9
MD5 051e9d39873dd9f452ffffbf96c1a8e0
BLAKE2b-256 990e67ce33d32957f501333c9a2f9c65d0707c44f397851d460b94a79ff5144c

See more details on using hashes here.

Provenance

The following attestation bundles were made for crypto_lob_stream-0.2.3.tar.gz:

Publisher: publish.yml on Goodie-Goody/crypto_lob_stream_pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file crypto_lob_stream-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for crypto_lob_stream-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d8f90644fee667135d1cd7b8a183909f8ef950844ed5ba2dc448ca0267a69297
MD5 4710f39556a2d0eb8e01ff06e948cc4f
BLAKE2b-256 6f64d2333677fbcb52563a0f04539ec5c904b12defaef7d0cea2441cc86679a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for crypto_lob_stream-0.2.3-py3-none-any.whl:

Publisher: publish.yml on Goodie-Goody/crypto_lob_stream_pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page