Skip to main content

Python wrapper for a high-performance Rust orderbook CLI

Project description

hft-lob

PyPI version Python License: MIT Platform

High-performance Python library for reading NSE binary market feed files and reconstructing a full 5-level Limit Order Book (LOB). Powered by a compiled Rust binary — zero Python overhead on the critical path.


Features

  • Reconstruct LOB from NSE CM binary feed files
  • Support for single or multiple instrument tokens
  • Three simple CLI commands — no arguments, no paths, no tokens on the command line
  • Clean Python Reader API for scripting and backtesting
  • 23-field CSV output per tick (timestamps, mid-price, 5-level bid/ask)
  • No dependencies — Rust binary is bundled

Architecture

hft-lob
├── hft_lob/
│   ├── cli.py            # Reader class + CLI entry point
│   └── bin/
│       └── orderbook-linux-x86_64   # Compiled Rust binary
~/.hft_lob                # User config (FILE + TOKEN)

Data flow:

NSE .bin feed file
       │
       ▼
 Rust binary (subprocess)
  orderbook-linux-x86_64
       │
       ▼
  CSV lines (stdout)
       │
       ▼
  Reader._messages[]
       │
    ┌──┴──────────────────────┐
    ▼                         ▼
get_next_message()     get_all_messages()
 (streaming)              (bulk load)

The Rust binary is invoked once per token. For multiple tokens, outputs are merged and exposed through a unified Reader interface.


Install

pip install hft-lob

Configure (once)

Create ~/.hft_lob — this is the only setup you ever need to do:

cat > ~/.hft_lob << 'EOF'
FILE=/nas/50.30/NSE_CM/Feed_CM_StreamID_2_29_12_2025.bin
TOKEN=1333,2885,5900
EOF
Key Description
FILE Absolute path to the NSE binary feed file
TOKEN Instrument token(s). Comma-separated for multiple.

CLI Usage

No arguments. No file path. No token. Just run:

hft-lob get_next

Prints the next LOB tick as a single CSV row.

hft-lob get_all

Prints every LOB tick, one CSV row per line (includes header on first line).

hft-lob eof

Prints True if no messages are available, False if data exists.

Pipe examples:

# Count total messages
hft-lob get_all | wc -l

# Preview first 5 rows
hft-lob get_all | head -6

# Save to CSV
hft-lob get_all > lob_data.csv

Python API

from hft_lob.cli import Reader

# Single token
r = Reader("/path/to/feed.bin", tokens=1333)

# Multiple tokens — merged into one stream
r = Reader("/path/to/feed.bin", tokens=[1333, 2885, 5900])
Method / Attribute Returns Description
r.get_next_message() str | None Next CSV row, or None at EOF
r.get_all_messages() list[str] All CSV rows as a list
r.is_end_of_file() bool True after all messages are consumed
r.header str Comma-separated column names

Streaming pattern:

r = Reader("/path/to/feed.bin", tokens=1333)
while not r.is_end_of_file():
    row = r.get_next_message()
    if row:
        print(row)

CSV Output Format

23 fields per row:

Field Description
local_ts Local timestamp (nanoseconds epoch)
exch_ts Exchange timestamp (nanoseconds epoch)
mid_price (best_bid + best_ask) / 2
bid_price_0bid_price_4 Bid price at depth levels 0–4
bid_qty_0bid_qty_4 Bid quantity at depth levels 0–4
ask_price_0ask_price_4 Ask price at depth levels 0–4
ask_qty_0ask_qty_4 Ask quantity at depth levels 0–4

Load into pandas

import io
import pandas as pd
from hft_lob.cli import Reader

r = Reader("/path/to/feed.bin", tokens=[1333, 2885, 5900])
msgs = r.get_all_messages()

df = pd.read_csv(io.StringIO(r.header + "\n" + "\n".join(msgs)))
df["exch_ts"] = pd.to_datetime(df["exch_ts"], unit="ns")
print(df.head())
print(f"Total rows: {len(df)}")

Requirements

Item Requirement
OS Linux x86_64
Python 3.7+
Dependencies None

The Rust binary (orderbook-linux-x86_64) is bundled inside the package — no separate install, no Rust toolchain needed.


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

hft_lob-0.2.4.tar.gz (223.1 kB view details)

Uploaded Source

Built Distribution

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

hft_lob-0.2.4-py3-none-any.whl (221.7 kB view details)

Uploaded Python 3

File details

Details for the file hft_lob-0.2.4.tar.gz.

File metadata

  • Download URL: hft_lob-0.2.4.tar.gz
  • Upload date:
  • Size: 223.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for hft_lob-0.2.4.tar.gz
Algorithm Hash digest
SHA256 e7f7c91d75abdd438289976a884370976f69a10ddb4fcbd7e8adacfd1ca19507
MD5 eb0a2fb9cda8310e57b0cf1725aa2c77
BLAKE2b-256 33c5163962dea8bb9907dd6927b38f910eed2d9b2a35e169c2e5b89f034b63d1

See more details on using hashes here.

File details

Details for the file hft_lob-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: hft_lob-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 221.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for hft_lob-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 506c0808a06dd18c28e0f3a31424d291b7d357cbc1ef7dd763cc4eda8daf7d1c
MD5 ea2220218d1081e7d993f59edb598cce
BLAKE2b-256 17be9fb721d377198c64c7b2c124dad0366a8f88c2ccc09634048c527c6d7e55

See more details on using hashes here.

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