Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
\build
.vscode/
\googletest
/Generate_Orders/orders.txt
googletest/
/Generate_Orders/orders.txt

# Zig
.zig-cache/
zig-out/
70 changes: 70 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Build Commands

```bash
# Clone GoogleTest (first time only)
git clone --depth 1 https://github.com/google/googletest.git

# Build
zig build

# Run tests
zig build test

# Run executable
./zig-out/bin/LimitOrderBook
```

The project uses Zig's build system with C++20 and -O2 optimization. GoogleTest is integrated as a subdirectory.

## Architecture

This is a high-frequency trading limit order book matching engine implementing FIFO/Price Priority algorithm.

### Core Data Structures (Limit_Order_Book/)

**Three-class hierarchy with hybrid data structure design:**

1. **Book** - Main order book engine
- Dual AVL trees for buy/sell sides (regular orders)
- Dual AVL trees for stop orders (stop buys/sells)
- Hash maps for O(1) lookups: `orderMap`, `limitBuyMap`, `limitSellMap`, `stopMap`
- Maintains `highestBuy`/`lowestSell` pointers for O(1) best bid/ask access

2. **Limit** - Price level node
- AVL tree node (parent/leftChild/rightChild pointers)
- Contains doubly-linked list of orders at this price (headOrder/tailOrder)
- Tracks totalVolume and size (order count)

3. **Order** - Individual order
- Doubly-linked list node within a Limit (nextOrder/prevOrder)
- Back-reference to parentLimit for O(1) limit updates

### Time Complexities
- Add/Cancel/Modify/Execute: O(1) for existing limits, O(log M) for new price levels
- GetBestBid/Offer: O(1)
- AVL rebalancing: O(log M) when triggered

### Supporting Modules

- **OrderPipeline** (Process_Orders/) - Dispatcher pattern for order processing with nanosecond latency measurement
- **GenerateOrders** (Generate_Orders/) - Test data generation with normal distribution

### Order Types Supported
- Market orders
- Limit orders
- Stop orders
- Stop-limit orders

### Key Design Decisions

1. **AVL + Doubly-Linked List Hybrid** - AVL for O(log M) price navigation, doubly-linked list at each level for O(1) FIFO ordering

2. **Single-threaded matching** - Intentional design for FIFO/price priority consistency

3. **Manual memory management** - Uses new/delete with proper cleanup in destructors

4. **Separate stop order trees** - Parallel data structures isolate regular and stop order handling
41 changes: 0 additions & 41 deletions CMakeLists.txt

This file was deleted.

21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,34 @@ Limit_Order_Book/
│ ├── data_visualisation.py
│ └── order_processing_times.csv
├── test/ *unit tests
│ ├── CMakeLists.txt
│ ├── ExampleOrdersTests.cpp
│ └── LimitOrderBookTests.cpp
├── figures/
├── googletest/
├── main.cpp
├── .gitignore
├── CMakeLists.txt
├── build.zig
└── README.md
```

## Build Instructions

### Prerequisites

- [Zig](https://ziglang.org/download/) (0.13+)
- Clone GoogleTest (first time only):
```bash
git clone --depth 1 https://github.com/google/googletest.git
```

### Build and Run

```bash
zig build # Build
zig build test # Run tests
./zig-out/bin/LimitOrderBook # Run executable
```

## Architecture

<img src="./figures/architecture.png" alt="Architecture" width="800"/>
Expand Down
134 changes: 134 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Common C++ flags
const cpp_flags: []const []const u8 = &.{
"-std=c++20",
"-O2",
};

// =========================================
// Static Library: LimitOrderBook_lib
// =========================================
const lib_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libcpp = true,
});

lib_module.addCSourceFiles(.{
.files = &.{
"Limit_Order_Book/Book.cpp",
"Limit_Order_Book/Limit.cpp",
"Limit_Order_Book/Order.cpp",
"Process_Orders/OrderPipeline.cpp",
"Generate_Orders/GenerateOrders.cpp",
},
.flags = cpp_flags,
});

const lib = b.addLibrary(.{
.name = "LimitOrderBook_lib",
.root_module = lib_module,
});
b.installArtifact(lib);

// =========================================
// Main Executable: LimitOrderBook
// =========================================
const exe_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libcpp = true,
});

exe_module.addCSourceFiles(.{
.files = &.{"main.cpp"},
.flags = cpp_flags,
});

exe_module.linkLibrary(lib);

const exe = b.addExecutable(.{
.name = "LimitOrderBook",
.root_module = exe_module,
});
b.installArtifact(exe);

// Run command: zig build run
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run the limit order book");
run_step.dependOn(&run_cmd.step);

// =========================================
// GoogleTest Library
// =========================================
const gtest_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libcpp = true,
});

gtest_module.addCSourceFiles(.{
.files = &.{
"googletest/googletest/src/gtest-all.cc",
"googletest/googletest/src/gtest_main.cc",
},
.flags = cpp_flags,
});

gtest_module.addIncludePath(b.path("googletest/googletest/include"));
gtest_module.addIncludePath(b.path("googletest/googletest"));

const gtest = b.addLibrary(.{
.name = "gtest",
.root_module = gtest_module,
});

// =========================================
// Test Executable: LimitOrderBookTests
// =========================================
const test_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libcpp = true,
});

test_module.addCSourceFiles(.{
.files = &.{
"test/LimitOrderBookTests.cpp",
"test/ExampleOrdersTests.cpp",
},
.flags = cpp_flags,
});

test_module.addIncludePath(b.path("googletest/googletest/include"));
test_module.linkLibrary(lib);
test_module.linkLibrary(gtest);

const tests = b.addExecutable(.{
.name = "LimitOrderBookTests",
.root_module = test_module,
});
b.installArtifact(tests);

// Run tests: zig build test
const run_tests = b.addRunArtifact(tests);
run_tests.step.dependOn(b.getInstallStep());

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);

// Clean step: zig build clean
const clean_step = b.step("clean", "Remove build artifacts");
clean_step.dependOn(&b.addRemoveDirTree(b.path(".zig-cache")).step);
clean_step.dependOn(&b.addRemoveDirTree(b.path("zig-out")).step);
}
19 changes: 0 additions & 19 deletions test/CMakeLists.txt

This file was deleted.