# gkavinrajancodes/orderbook architecture

> Architecture diagram of the GitHub repository gkavinrajancodes/orderbook, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gkavinrajancodes/orderbook
- Repository: https://github.com/gkavinrajancodes/orderbook

## Overview

This repository contains two generations of a C++ trading orderbook: a feature-rich V1 implementation and a lower-allocation, single-writer V2 engine. The main workflow is order submission, matching, and trade generation; V1’s example adds advanced-order handling, risk tracking, metrics, and snapshots. V2 adds a queued matching thread, an exchange-feed integration, recording, and an optional tamper-evident audit wrapper. Relationships are shown only where the README or excerpts support them; several V2 capabilities are represented without inferred connections.

Main entry points: [`v1/EnhancedOrderbookExample.cpp`](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/EnhancedOrderbookExample.cpp) (Order Workflow), [`v2/core/OrderbookThread.h`](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/OrderbookThread.h) (Matching Thread), [`v2/core/Orderbook.cpp`](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/Orderbook.cpp) (Orderbook Engine), [`v2/core/BinanceFeed.cpp`](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/BinanceFeed.cpp) (Binance Feed)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_v1["V1 Trading"]
  node_v1example["Order Workflow"]
  node_v1book["Orderbook<br/>[Orderbook.cpp]"]
  node_v1orders["Order Types<br/>[Order.h]"]
  node_advanced["Advanced Orders"]
  node_risk["Risk Manager<br/>[RiskManager.cpp]"]
  node_metrics["Book Metrics"]
  node_snapshot["Book Snapshot"]
  node_multi["Multi-Instrument"]
end

subgraph group_v2engine["V2 Matching"]
  node_thread["Matching Thread<br/>[OrderbookThread.h]"]
  node_v2book["Orderbook Engine<br/>[Orderbook.cpp]"]
end

subgraph group_v2support["V2 Support"]
  node_feed["Binance Feed<br/>[BinanceFeed.cpp]"]
  node_recorder["Data Recorder<br/>[DataRecorder.cpp]"]
  node_audit[("Audit Log<br/>[AuditLog.h]")]
  node_auction["Batch Auction<br/>[BatchAuction.h]"]
end

subgraph group_internals["Matching Internals"]
  node_pool["Order Pool<br/>[OrderPool.h]"]
  node_levels["Price Levels<br/>[PriceLevel.h]"]
  node_bitmap["Price Bitmap<br/>[PriceBitmap.h]"]
  node_lookup["Order Lookup<br/>[FlatHashMap.h]"]
  node_queue["SPSC Queues<br/>[SPSCQueue.h]"]
  node_sha["SHA-256<br/>[SHA256.h]"]
  node_cpu["CPU Utilities<br/>[CpuUtils.h]"]
end

node_client(("Order Client"))
node_v2client(("Command Producer"))

node_client -->|"submits orders"| node_v1example
node_v1example -->|"processes orders"| node_v1book
node_v1example -->|"updates positions"| node_risk
node_v1example -->|"records trades"| node_metrics
node_v1example -->|"dispatches orders"| node_advanced
node_v1example -.->|"creates snapshots"| node_v1book
node_client -.->|"sends commands"| node_v2client
node_v2client -.->|"queues commands"| node_thread
node_thread -->|"reads commands"| node_queue
node_thread -->|"writes results"| node_queue
node_thread -->|"dispatches operations"| node_v2book
node_thread -->|"pins thread"| node_cpu
node_v2book -->|"allocates orders"| node_pool
node_v2book -->|"manages levels"| node_levels
node_v2book -->|"tracks best prices"| node_bitmap
node_v2book -->|"indexes orders"| node_lookup
node_feed -.->|"integration uses"| node_thread
node_feed -.->|"integration uses"| node_recorder
node_audit -->|"wraps operations"| node_v2book
node_audit -->|"hashes events"| node_sha

click node_v1example "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/EnhancedOrderbookExample.cpp"
click node_v1book "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/Orderbook.cpp"
click node_v1orders "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/Order.h"
click node_advanced "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/AdvancedOrderTypes.cpp"
click node_risk "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/RiskManager.cpp"
click node_metrics "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/OrderbookMetrics.cpp"
click node_snapshot "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/OrderbookSnapshot.cpp"
click node_multi "https://github.com/gkavinrajancodes/orderbook/blob/main/v1/MultiInstrumentOrderbook.cpp"
click node_thread "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/OrderbookThread.h"
click node_v2book "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/Orderbook.cpp"
click node_feed "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/BinanceFeed.cpp"
click node_recorder "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/DataRecorder.cpp"
click node_audit "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/AuditLog.h"
click node_auction "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/BatchAuction.h"
click node_pool "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/OrderPool.h"
click node_levels "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/PriceLevel.h"
click node_bitmap "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/PriceBitmap.h"
click node_lookup "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/FlatHashMap.h"
click node_queue "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/SPSCQueue.h"
click node_sha "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/SHA256.h"
click node_cpu "https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/CpuUtils.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_v1example,node_v1book,node_v1orders,node_advanced,node_risk,node_metrics,node_snapshot,node_multi,node_client toneBlue
class node_thread,node_v2book toneAmber
class node_feed,node_recorder,node_audit,node_auction toneMint
class node_pool,node_levels,node_bitmap,node_lookup,node_queue,node_sha,node_cpu toneRose
class node_v2client toneIndigo
```

## Components

### V1 Trading

- [Order Workflow](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/EnhancedOrderbookExample.cpp): `v1/EnhancedOrderbookExample.cpp`
- [Orderbook](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/Orderbook.cpp): `v1/Orderbook.cpp`
- [Order Types](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/Order.h): `v1/Order.h`
- [Advanced Orders](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/AdvancedOrderTypes.cpp): `v1/AdvancedOrderTypes.cpp`
- [Risk Manager](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/RiskManager.cpp): `v1/RiskManager.cpp`
- [Book Metrics](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/OrderbookMetrics.cpp): `v1/OrderbookMetrics.cpp`
- [Book Snapshot](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/OrderbookSnapshot.cpp): `v1/OrderbookSnapshot.cpp`
- [Multi-Instrument](https://github.com/gkavinrajancodes/orderbook/blob/main/v1/MultiInstrumentOrderbook.cpp): `v1/MultiInstrumentOrderbook.cpp`

### V2 Matching

- [Matching Thread](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/OrderbookThread.h): `v2/core/OrderbookThread.h`
- [Orderbook Engine](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/Orderbook.cpp): `v2/core/Orderbook.cpp`

### V2 Support

- [Binance Feed](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/BinanceFeed.cpp): `v2/core/BinanceFeed.cpp`
- [Data Recorder](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/DataRecorder.cpp): `v2/core/DataRecorder.cpp`
- [Audit Log](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/AuditLog.h): `v2/core/AuditLog.h`
- [Batch Auction](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/BatchAuction.h): `v2/core/BatchAuction.h`

### Matching Internals

- [Order Pool](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/OrderPool.h): `v2/core/OrderPool.h`
- [Price Levels](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/PriceLevel.h): `v2/core/PriceLevel.h`
- [Price Bitmap](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/PriceBitmap.h): `v2/core/PriceBitmap.h`
- [Order Lookup](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/FlatHashMap.h): `v2/core/FlatHashMap.h`
- [SPSC Queues](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/SPSCQueue.h): `v2/core/SPSCQueue.h`
- [SHA-256](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/SHA256.h): `v2/core/SHA256.h`
- [CPU Utilities](https://github.com/gkavinrajancodes/orderbook/blob/main/v2/core/CpuUtils.h): `v2/core/CpuUtils.h`

### Other components

- **Order Client**
- **Command Producer**

## Connections

- Order Client → Order Workflow: submits orders (evidence: `README.md`)
- Order Workflow → Orderbook: processes orders (evidence: `v1/EnhancedOrderbookExample.cpp`)
- Order Workflow → Risk Manager: updates positions (evidence: `v1/EnhancedOrderbookExample.cpp`)
- Order Workflow → Book Metrics: records trades (evidence: `v1/EnhancedOrderbookExample.cpp`)
- Order Workflow → Advanced Orders: dispatches orders (evidence: `v1/EnhancedOrderbookExample.cpp`)
- Order Workflow → Orderbook: creates snapshots
- Order Client → Command Producer: sends commands
- Command Producer → Matching Thread: queues commands
- Matching Thread → SPSC Queues: reads commands (evidence: `v2/core/OrderbookThread.h`)
- Matching Thread → SPSC Queues: writes results (evidence: `v2/core/OrderbookThread.h`)
- Matching Thread → Orderbook Engine: dispatches operations (evidence: `v2/core/OrderbookThread.h`)
- Matching Thread → CPU Utilities: pins thread (evidence: `v2/core/OrderbookThread.h`)
- Orderbook Engine → Order Pool: allocates orders
- Orderbook Engine → Price Levels: manages levels
- Orderbook Engine → Price Bitmap: tracks best prices
- Orderbook Engine → Order Lookup: indexes orders
- Binance Feed → Matching Thread: integration uses
- Binance Feed → Data Recorder: integration uses
- Audit Log → Orderbook Engine: wraps operations (evidence: `v2/core/AuditLog.h`)
- Audit Log → SHA-256: hashes events (evidence: `v2/core/AuditLog.h`)
