Crypto-toolbox/HFT-Orderbook logo

Crypto-toolbox/HFT-Orderbook

Free

Limit Order Book for high-frequency trading (HFT), as described by WK Selph, implemented in Python3 and C

FreeFree tier
Type
Open Source

About Crypto-toolbox/HFT-Orderbook

HFT-Orderbook is an open-source implementation of a high-frequency trading (HFT) limit order book, originally described by WK Selph. Written in C for maximum performance, with an accompanying Python3 version (lob.py), it provides O(1) time complexity for core operations: add, cancel, and execute orders. The data structure uses a binary tree of Limit objects, each containing a doubly linked list of Order objects, plus hash maps for O(1) lookup by order ID and limit price. It efficiently supports queries like best bid/offer (top of book) and volume at a given price level. This library is ideal for developers building algorithmic trading systems or studying order book mechanics.

Key Features

O(1) add, cancel, and execute operations
O(1) GetBestBid/Offer and GetVolumeAtLimit
Binary tree of Limit objects sorted by price
Doubly linked list of orders per limit price
Hash map for O(1) order lookup by ID
Separate buy and sell trees for inside book tracking
Implemented in C for high performance
Python3 version available (lob.py)
Based on WK Selph's widely referenced blog post

Pros & Cons

Pros
  • Core operations (add, cancel, execute) are O(1) - highly efficient
  • Efficient querying of best bid/offer and volume at any limit
  • Clean, well-documented design based on industry knowledge
  • Open source and free to use, modify, and integrate
  • C implementation ensures low latency
  • Python3 version available for rapid prototyping
  • Includes unit tests (orderbook_tests.py)
Cons
  • Does not include networking, market data feeds, or trading strategy components
  • Limited to limit order book functionality only; no support for other order types
  • Python version may be slower than the C implementation
  • Requires adaptation and integration for live trading environments
  • No built-in persistence or logging mechanisms

Best For

High-frequency trading (HFT) algorithmsMarket making and arbitrage strategiesOrder book simulation and backtestingEducation and research on limit order book data structuresBuilding custom trading platforms

FAQ

What operations does the limit order book support?
It supports add, cancel, and execute operations, all in O(1) time, plus queries for best bid/offer and volume at a given limit price.
What data structures are used internally?
The order book uses a binary tree of Limit objects sorted by price, each containing a doubly linked list of Order objects. Hash maps provide O(1) lookup by order ID and limit price.
Is there a Python version available?
Yes, the repository includes a Python3 implementation (lob.py) alongside the primary C implementation.
Is this ready for production trading?
The library provides the core order book logic but does not include networking, market data handling, or risk management. It is intended as a component for building larger trading systems.