Skip to content

maniac0112/limitorderbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Limit Order Book

A simple Limit Order Book implementation in C++. Maintains two side books (bids and asks) with support for:

  • Limit order placement with automatic matching
  • Partial and full fills
  • Order cancellation in O(1) via hashmap of iterators
  • Volume tracking per price level
  • Text-based printing of book state

Features

  • Two sides: Bids (descending prices) and Asks (ascending prices) using std::map with custom comparators.
  • Fast operations: O(log n) price-level lookup, O(1) insert/delete within price level via std::list.
  • Order cancellation: Immediate removal by OrderId using std::unordered_map<OrderId, iterator>.
  • Recursive matching: New orders cross spread and match until exhausted.
  • VolumeMap: Tracks total quantity at each price.

Design Overview

  • Order: Represents an individual order with price, quantity, ID, side, and timestamp.

  • OrderQueue: std::list<Order> for each price level, enabling stable iterators.

  • LimitOrderBook: Manages Bids and Asks maps:

    • std::map<Price, OrderQueue, std::greater<Price>> Bids
    • std::map<Price, OrderQueue, std::less<Price>> Asks
  • OrderMap: std::unordered_map<OrderId, OrderQueue::iterator> for cancellation.

  • VolumeMap: std::unordered_map<Price, Quantity> for level volume.


Prerequisites

  • C++17 or later
  • Clang++ or any C++17-compatible compiler

Building

# Compile with clang++
clang++ -std=c++17 -O2 -Wall -Wextra -o orderbook main.cpp

You can also use g++:

g++ -std=c++17 -O2 -Wall -Wextra -o orderbook main.cpp

Running

./orderbook

This runs the built-in test driver, demonstrating:

  1. Initial limit order placements
  2. Matching behavior for crossed orders
  3. Partial and full fills
  4. Order cancellation
  5. Final book state printing

Usage Example

LimitOrderBook book;
auto now = std::chrono::steady_clock::now();

// Place bids
book.PlaceOrder(100, 10, 1, OrderSide::BUY, now);
book.PlaceOrder(102,  5, 2, OrderSide::BUY, now);

// Place asks
book.PlaceOrder(105,  7, 3, OrderSide::SELL, now);
book.PlaceOrder(101,  3, 4, OrderSide::SELL, now);

// Print
book.PrintBook();

// Place an order that crosses the spread
book.PlaceOrder(103, 10, 5, OrderSide::BUY, now);

book.PrintBook();

Project Structure

├── main.cpp       # Implementation and test driver
└── README.md      # This file

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Please label issues and PRs appropriately. New contributors can look for issues tagged good first issue.


About

Limit Order Book Implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages