Skip to content

Repository files navigation

C++ for Algorithmic Trading — a hands-on curriculum

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.

Why C++ for trading?

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:

  1. No garbage collector — no surprise pauses. You control exactly when memory is allocated and freed (RAII, lesson 01).
  2. Zero-cost abstractions — classes, templates and lambdas compile down to the same machine code as hand-written C.
  3. Mechanical sympathy — you can reason about cache lines, branches and allocations (lesson 05), which dominate real-world latency.

Your toolchain (already on this Mac)

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.

Build & run everything

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

The compiler flags you'll use for the rest of your life

-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.

Curriculum

# 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.

Quant interview prep (quant/)

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.

Common practice checklist (what "professional C++" means)

  • 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 need unique_ptr — most things live in containers or on the stack.
  • Integer prices. Prices are int64_t counts of ticks/cents. double cannot represent 0.1 exactly and rounding errors compound into real money.
  • const by default. Mark everything const unless 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.

Where to go next

  • Practice: rewrite lesson 03's book with price-indexed arrays instead of std::map and 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 make for transparency; industry uses CMake (brew install cmake) for anything multi-file/multi-platform.

About

Hands-on C++20 curriculum for algorithmic trading (order books, backtesting, low latency) + quant interview prep with Monte Carlo-verified solutions

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages