Skip to content

lyqsbf/Limit-Order-Book

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Limit Order Book (C++) Work in Progress

This repository contains a simple implementation of a Limit Order Book (LOB) in C++, with automatic matching logic for buy and sell orders. It is my first hands-on project in the trading domain, and I intend to keep improving it step by step.

At the moment this project demonstrates the core matching workflow used by exchanges:

  • Add limit orders
  • Match aggressive orders against the opposite side
  • Keep price-time priority (FIFO inside each price level)
  • Cancel and modify existing orders
  • Inspect top-of-book and spread

Motivation

This project steams from my curiosity about algorithmic trading and financial systems. It is my very first practical contact with building an order book, and as such, I consider it a work-in-progress prototype. My goal is to learn the fundamentals of order matching, efficient data management, and gradually incorporate features found in real-world trading engines.

I am aware that there is plenty of room for optimization and refinement, but that's precisely what makes this project exciting: the chance to experiment, make mistakes, and learn along the way. Any feedback, suggestions, or contributions are more than welcome!

Overview

The implementation is in lob.cc and uses:

  • std::map for price levels
    • Bids: descending prices (greater<ll>)
    • Asks: ascending prices
  • std::list<Order> per price level to preserve FIFO order
  • std::unordered_map<orderId, OrderEntry> for O(1)-style order lookup on cancel/modify

Order Model

Each order contains:

  • id: unique order identifier
  • price: limit price
  • quantity: remaining quantity
  • side: Buy or Sell

Matching Logic

When a new order arrives:

  1. It tries to match the best prices on the opposite side.
  2. Trades are executed while prices cross and quantity remains.
  3. If quantity is still left, the order is inserted into its own side of the book.

Matching is done with price-time priority:

  • Best price first
  • Oldest order first within that price

Supported Operations

  • addOrder(Order)

    • Rejects duplicate IDs
    • Matches immediately if possible
    • Inserts residual quantity if not fully filled
  • cancelOrder(orderId)

    • Removes an existing order from the book
  • modifyOrder(orderId, newQuantity)

    • Updates quantity for an existing order
    • If newQuantity == 0, it cancels the order
  • display()

    • Prints aggregated quantity at each price level
  • getBestBid(), getBestAsk(), getSpread(), empty()

    • Basic top-of-book queries

Build and Run

g++ -std=c++17 -O2 -Wall -Wextra -o lob lob.cc
./lob

Example Scenario in main()

The demo sequence does the following:

  1. Places initial bid/ask orders
  2. Sends an aggressive buy order that matches existing asks
  3. Modifies one remaining ask
  4. Cancels that ask
  5. Attempts to add a duplicate order ID (error path)
  6. Prints best bid/ask and spread

Notes and Next Steps

Since this is my first approach to the domain, I have planned several enhancements to deepen my understanding and make the system more realistic.

Current limitations:

  • No decimal/fixed-point support.
  • Only limit orders are implemented.
  • Single-threaded; no concurrency control.
  • No persistence (book state is lost after program ends).
  • Limited testing (no automated unit tests yet).

Planned Enhancements (Roadmap):

  • Support for decimal prices (fixed-point) and tick sizes.
  • Implementation of market orders, stop-loss, and iceberg orders.
  • Real-time market depth and statistics.
  • Partial cancel (reduce quantity).
  • Trade history storage.
  • Input-driven simulator (from file/socket).
  • Unit tests for matching/cancel/modify edge cases.
  • Performance benchmarks and profiling.
  • Python binding for visualization becktesting.

If you're interested in any of these areas or would like to collaborate, feel free to reach out!

Contributing

This project is a learning journey, so all suggestions, bug reports, and pull requests are appreciated. Please open an issue or submit a PR if you'd like to help.

License

This repository is provided for educational use.

About

πŸ“ˆ Limit Order Book in C++ (work in progress). My first approach to algorithmic trading. Supports add, match, cancel, modify orders with price-time priority.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages