Skip to content

About

An orderbook written in C++ from scratch.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Order Book (C++)

A limit order book and matching engine implemented in C++20.

The order book maintains buy and sell orders, matches compatible orders using price-time priority, and generates trades when orders cross.

Features

  • Buy and sell order books
  • Price-time priority matching
  • FIFO ordering within each price level
  • Full and partial fills
  • Add, cancel, and modify operations
  • Multiple matches from a single order
  • Aggregated price-level information
  • Thread-safe order book operations
  • Good-for-Day order expiry

Order Types

  • GoodTillCancel — remains in the book until filled or cancelled.
  • FillAndKill — immediately fills as much as possible and cancels the remaining quantity.
  • FillOrKill — executes only if the entire quantity can be filled immediately.
  • GoodForDay — remains active until the end of the trading day.
  • Market — executes against available liquidity without requiring a limit price.

Matching

The book maintains separate price levels for bids and asks.

  • Bids are ordered from highest to lowest price.
  • Asks are ordered from lowest to highest price.
  • Orders at the same price are stored in FIFO order.
  • Orders match while the best bid price is greater than or equal to the best ask price.
  • A single order can match against multiple resting orders and price levels.

Each successful match produces a Trade containing the bid and ask trade information.

Core Data Structures

bids_   -> map<Price, OrderPointers>
asks_   -> map<Price, OrderPointers>

orders_ -> unordered_map<OrderId, OrderEntry>
data_   -> unordered_map<Price, LevelData>

Each price level contains a list of orders in arrival order.

orders_ maps an order ID to the order and its position within its price level, allowing orders to be located directly for cancellation.

data_ maintains aggregated quantity and order-count information for price levels.

Public API

Trades AddOrder(OrderPointer order);
void CancelOrder(OrderId orderId);
Trades ModifyOrder(OrderModify order);

std::size_t Size() const;
OrderbookLevelInfos GetOrderInfos() const;

Build

Requires a C++20-compatible compiler.

Build the project:

make

Build and run:

make run

Remove the generated executable:

make clean

Tests

main.cpp contains tests for:

  • Adding orders
  • Cancelling orders
  • Non-crossing orders
  • Exact matches
  • Partial fills
  • Multiple matches
  • Duplicate order IDs
  • Aggregated price-level information

Run them with:

make run

About

An orderbook written in C++ from scratch.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages