Every lesson in this folder is a single, runnable C++ file. The comments in the code are the textbook — read each file top to bottom, build it, run it, then do the exercises at the bottom of the file.
Trading firms use C++ on the latency-critical path: market-data handlers, order books, matching engines, execution gateways, and strategy engines where reacting a microsecond faster than a competitor is the whole business. Python is used for research/backtesting; C++ is used where speed is the product. The three properties that matter:
- No garbage collector — no surprise pauses. You control exactly when memory is allocated and freed (RAII, lesson 01).
- Zero-cost abstractions — classes, templates and lambdas compile down to the same machine code as hand-written C.
- Mechanical sympathy — you can reason about cache lines, branches and allocations (lesson 05), which dominate real-world latency.
| Tool | What it is | Command |
|---|---|---|
| clang++ | Apple's C++ compiler (LLVM) | clang++ --version |
| make | Classic build tool, drives the compiler | make |
| lldb | Debugger | lldb ./bin/01_foundations |
Note: on macOS g++ is just an alias for clang. On Linux trading servers
you'd usually find real GCC; the code here works on both.
cd ~/Desktop/C++\ algos
make # builds every lesson into bin/
make run # builds and runs all lessons in order
make 03 # build just lesson 03
./bin/03_order_book
make debug # rebuild everything with sanitizers + no optimisation
make clean-std=c++20 # language version. Use the newest your firm allows.
-Wall -Wextra # warnings. Treat every warning as a bug.
-O2 # optimise. NEVER benchmark without this (lesson 05).
-O0 -g # debug build: no optimisation, keep symbols for lldb.
-fsanitize=address,undefined # runtime bug detectors — run tests with these ON.
Common practice at trading firms: two build profiles. A debug build
(-O0 -g -fsanitize=...) that you develop and test against, and a release
build (-O2 or -O3 -march=native) that goes to production. The Makefile
here does exactly that.
| # | File | Teaches |
|---|---|---|
| 01 | 01_foundations.cpp |
Compilation model, value semantics, const, references, RAII, and why prices are integers, never doubles |
| 02 | 02_market_data.cpp |
Structs, parsing tick data from CSV, std::vector, std::chrono timestamps, iterating safely |
| 03 | 03_order_book.cpp |
The limit order book — the core data structure of all trading. std::map, iterators, big-O reasoning |
| 04 | 04_strategy_backtest.cpp |
Classes and interfaces (virtual functions), a moving-average crossover strategy, P&L accounting |
| 05 | 05_low_latency.cpp |
Measuring latency correctly, cache locality, reserve(), allocation on the hot path, branch prediction |
| 06 | 06_capstone_engine.cpp |
A mini event-driven trading engine tying everything together: feed → book → strategy → fills → P&L |
Work through them in order — each assumes the previous ones.
Worked answers to every lesson's exercises live in solutions/ (spoilers —
attempt them yourself first). Build with make solutions; each runs as
./bin/sol_<lesson> and demonstrates its answers live.
A second track: tutorials + coded solutions covering the topic map of the
"green book" (Zhou, A Practical Guide to Quantitative Finance Interviews —
not included here; grab your own copy). Start at quant/QUANT_GUIDE.md. Build the coded
solutions with make quant; they land in bin/ alongside the lessons.
- Warnings clean. Code must compile silently under
-Wall -Wextra. - RAII everywhere. No naked
new/delete. Resources live in objects whose destructors clean up. You'll rarely even needunique_ptr— most things live in containers or on the stack. - Integer prices. Prices are
int64_tcounts of ticks/cents.doublecannot represent 0.1 exactly and rounding errors compound into real money. constby default. Mark everythingconstunless it must mutate.- Measure before optimising.
-O2, warmed-up loops, many iterations, look at the distribution not the mean (lesson 05). - Sanitizers in CI. Address + UB sanitizers catch the memory bugs that C++ is infamous for, before production does.
- No allocation on the hot path.
reserve()up front; reuse buffers.
- Practice: rewrite lesson 03's book with price-indexed arrays instead of
std::mapand benchmark the difference with lesson 05's harness. - Read: "Effective Modern C++" (Meyers), then cppreference.com as your daily reference.
- Real-world: look at open-source exchange connectivity (e.g. libwebsockets or Boost.Asio based feeds) and CME/Nasdaq ITCH protocol specs — parsing binary market data is the classic first job of a junior trading dev.
- Build systems: this folder uses
makefor transparency; industry uses CMake (brew install cmake) for anything multi-file/multi-platform.