Skip to content

A C++ implementation of an immutable financial order book. This project demonstrates a functional approach to state management in a financial context using a copy-on-write strategy with shared pointers for efficiency.

Sithabo/Immutable-Financial-Order-Book

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Immutable Financial Order Book in C++

This project provides a simple yet effective implementation of an immutable financial order book using modern C++. It demonstrates how to manage state changes without mutation, using a copy-on-write strategy with shared pointers for efficiency.

This approach is common in functional programming and offers several advantages in concurrent or state-sensitive systems, such as providing a clear and safe history of the order book's state over time.

Core Concepts

  • Immutability: The OrderBook class is immutable. Methods like add and match do not modify the object they are called on. Instead, they return a new OrderBook instance representing the state after the operation. This makes the system's state history explicit and thread-safe by design.

  • Copy-on-Write with std::shared_ptr: To avoid the performance cost of deep-copying the entire order book for every small change, the implementation uses std::shared_ptr to manage the underlying bid and ask maps. When a new order is added, only the relevant map (bids or asks) is deep-copied, while the other is shared with the previous version of the book. This is an efficient "copy-on-write" strategy.

  • State Versions: The main() function demonstrates how each operation creates a new, distinct version of the book (book_v0, book_v1, book_v2, etc.), allowing you to inspect or reuse any previous state of the order book at any time without fear of it being changed.

How to Build and Run

This project is a single-file C++ application with no external dependencies. You can compile and run it using any modern C++ compiler (C++11 or newer).

Using g++

# Compile the source code
g++ -std=c++11 -o orderbook project.cpp

# Run the executable
./orderbook

Using MSVC (Visual Studio)

# Compile the source code
cl.exe /std:c++17 /EHsc project.cpp

# Run the executable
.\project.exe

Example Walkthrough

The main() function in project.cpp provides a clear demonstration of the immutable workflow:

  1. Initial State (v0): An empty order book is created.
  2. Adding Orders (v1-v4): Four orders are added sequentially. Each add call returns a new OrderBook instance, creating versions v1 through v4. The original v0 book remains empty, proving immutability.
  3. Triggering a Match (v5): A new buy order is added at a price that can be matched with an existing sell order. This creates book_v5.
  4. Matching Engine (v6): The match() method is called on book_v5. This function finds and executes trades, returning a pair containing the final state of the book (book_v6) and a list of trades that occurred.
  5. Post-Match State: The program prints the trades and the new state of the book (book_v6). It also shows that book_v5 (the state right before the match) remains unchanged, highlighting that the match operation is also immutable.

About

A C++ implementation of an immutable financial order book. This project demonstrates a functional approach to state management in a financial context using a copy-on-write strategy with shared pointers for efficiency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages