Skip to main content

Python wrapper for a high-performance Rust orderbook CLI

Project description

hft-lob

hft-lob is a Python library that wraps a high-performance Rust binary to read and decode NSE (National Stock Exchange) binary market feed files and reconstruct a Limit Order Book (LOB) for any instrument token. It gives you a clean Python API and a one-word CLI — no file paths or tokens on the command line, ever.


Features

# Feature Description
1 get_all Load all LOB messages at once into a Python list
2 get_next Stream messages one at a time using a cursor
3 eof Check whether all messages have been read

Installation

pip install hft-lob

Requirements:

  • Linux x86_64
  • Python >= 3.7
  • No additional dependencies

Setup (one time only)

Create a config file ~/.hft_lob with your file path and token:

cat > ~/.hft_lob << 'EOF'
FILE=/path/to/Feed_CM_StreamID_2_29_12_2025.bin
TOKEN=1333
EOF

That's it. You never need to type the path or token again.


CLI Usage (shortest commands)

After setup, run any function with a single word:

hft-lob get_next
hft-lob get_all
hft-lob eof

No file path. No token. Everything is read from ~/.hft_lob automatically.


Feature Details

1. get_all

What it does: Loads every LOB message from the file into memory and prints them all. Best when you want the full dataset.

hft-lob get_all

Python equivalent:

from hft_lob.cli import Reader

r = Reader("/path/to/feed.bin", tokens=1333)
messages = r.get_all_messages()

print(f"Total: {len(messages)}")
print(messages[0])   # first row
print(messages[-1])  # last row

Returns: list of CSV strings, one per LOB update After calling: eof becomes True


2. get_next

What it does: Returns one message at a time. Each call advances an internal cursor. Returns None when there are no more messages. Best for processing row by row or stopping early.

hft-lob get_next

Python equivalent:

from hft_lob.cli import Reader

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

# Single message
print(r.get_next_message())

# Or loop one by one
while True:
    msg = r.get_next_message()
    if msg is None:
        break
    print(msg)

Returns: one CSV string per call, or None when done After last message: eof becomes True


3. eof

What it does: Checks whether all messages have been read. Returns True when done, False if messages remain.

hft-lob eof

Python equivalent:

from hft_lob.cli import Reader

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

print(r.is_end_of_file())   # False — nothing read yet
r.get_all_messages()
print(r.is_end_of_file())   # True — all messages read

Returns: True or False


Python API

If you prefer using Python directly instead of the CLI:

from hft_lob.cli import Reader

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

r.get_all_messages()    # list of all CSV rows
r.get_next_message()    # one CSV row, or None
r.is_end_of_file()      # True / False
r.header                # column names string
r.close()               # no-op, safe to call

Reader(file_path, tokens) parameters

Parameter Type Description
file_path str Path to the NSE binary feed .bin file
tokens int or list[int] Instrument token(s) to extract

CSV Output Format

Each message row has 23 comma-separated fields:

local_ts, exch_ts, mid_price,
bid_price_0, bid_qty_0, ask_price_0, ask_qty_0,
bid_price_1, bid_qty_1, ask_price_1, ask_qty_1,
bid_price_2, bid_qty_2, ask_price_2, ask_qty_2,
bid_price_3, bid_qty_3, ask_price_3, ask_qty_3,
bid_price_4, bid_qty_4, ask_price_4, ask_qty_4
Field Description
local_ts Local timestamp (nanoseconds) when message was received
exch_ts Exchange timestamp (nanoseconds) from NSE
mid_price (best bid + best ask) / 2
bid_price_N Bid price at depth level N (0 = best bid)
bid_qty_N Total quantity at bid level N
ask_price_N Ask price at depth level N (0 = best ask)
ask_qty_N Total quantity at ask level N

Examples

Parse into a dict

from hft_lob.cli import Reader

r = Reader("/path/to/feed.bin", tokens=1333)
messages = r.get_all_messages()
columns = r.header.split(",")

first = dict(zip(columns, messages[0].split(",")))
print(f"Best bid: {first['bid_price_0']} x {first['bid_qty_0']}")
print(f"Best ask: {first['ask_price_0']} x {first['ask_qty_0']}")

Load into pandas

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

r = Reader("/path/to/feed.bin", tokens=1333)
messages = r.get_all_messages()

df = pd.read_csv(io.StringIO(r.header + "\n" + "\n".join(messages)))
print(df[["local_ts", "mid_price", "bid_price_0", "ask_price_0"]].head())

Multiple tokens

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

How It Works (Architecture)

hft-lob get_next
      |
      v
 ~/.hft_lob (config)         <-- FILE and TOKEN read from here
      |
      v
 hft_lob.cli.Reader          <-- Python class
      |
      v
 Rust Binary (subprocess)    <-- orderbook-linux-x86_64 (bundled)
      |
      v
 NSE Binary Feed File        <-- your .bin file
      |
      v
 CSV Output                  <-- printed to terminal / returned to Python

Package Structure

hft_lob/
├── __init__.py                  <- package marker
├── cli.py                       <- Reader class + CLI main()
└── bin/
    └── orderbook-linux-x86_64   <- Rust binary (bundled, no install needed)

Platform Support

Platform Supported
Linux x86_64 Yes
macOS No
Windows No

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.2.tar.gz (223.9 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.2-py3-none-any.whl (222.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hft_lob-0.2.2.tar.gz
  • Upload date:
  • Size: 223.9 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.2.tar.gz
Algorithm Hash digest
SHA256 97ff19371f45b2fc81be5cb5515888312887c820c65afb1f88a6d0502087d814
MD5 0974de254a96be567d583499460373bd
BLAKE2b-256 44b97c936f87ecc23f643f76a9a7d7c671634b28832dfb9206e4da9733deea83

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hft_lob-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 222.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6bdac156b325d10b8b38eeb1ca0cbf9a10ba5a4f0cc5d4c552cf1bd0c65d0a15
MD5 e522ae14d62264ad3238a93dd679d1f0
BLAKE2b-256 d14b6e05b73552b96d87a00c2ba919937b98f16b75a2e02573b5082fb2bb7514

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