Skip to content

Design Review

The instrument. toy_quant replays market data through the whole trading loop and reports what a market maker would experience, with the queue in front of a quote modelled instead of assumed. Every number in this document comes from it, and every number here is re-derived by the gate on every edit; the markets it was pointed at, and what closed each one, are in the Retrospective.

Deribit L2 depth ─► L2OrderBook ─► L2MarketView ─┐
Deribit trades   ─► MarketEvent ────────────────┼─► Pipeline ─► Strategy ─► MatchingEngine ─► reports ─► Portfolio
legacy CSV / UDP / Binance ─────────────────────┘
Size 147 files, 34,462 lines: accounting 778, app 11,450, backtest 622, bench 563, common 1,262, concurrency 176, exchange 1,536, legacy 2,253, lines 7,829, main 39, market 4,191, orderbook 598, strategy 2,677, utils 488
Tests test binaries, at least one per module, run by tier 0 of the gate (38 in the default build; the two L1 ones only with -DBUILD_LEGACY_L1=ON)
Tools the scripts under tools/ (*.py and *.sh, grouped by what they do) and one C harness: the gate and its six lints in tools/gate/, the measurement sweeps, the source fetches, the one-off studies, and the live harnesses. docs/TOOLS.md is the inventory, and the lint requires a line there for every file
Build CMake + presets, C++20, standard library only (zlib for .gz inputs)
Numbers every figure below is re-derived by bash tools/gate/verify_l2.sh fast on every change

This is an instrument, not a trading system. No persistence across runs beyond the session snapshot, no market impact, no risk gateway -- the in-process pre-trade pipeline is not a broker's. The order leg can leave the process: --live-orders makes the venue a socket whose relay signs the venue's own REST calls, and the venue's order state comes back on its private orders channel (ADR 0005), so connectivity exists as one channel to a demo venue and no number in this document was measured on it. The loop is meant to be readable in one sitting, because a cost that is not modelled cannot be the reason for a result. What is worth reading is the engineering in sections 2 to 4.

What this document is not. It is not the module reference — Architecture is — and it is not the measurement record: the numbers here are summaries of records that live in Performance History and Performance. Its job is the argument, and the boundaries between the documents are listed in the documentation map.

1. The loop, in code

Two interfaces carry the design. Strategy has one class per strategy name; IMatchingEngine has four implementations -- three engines a run can be configured with, and one reader -- and the pipeline drives whichever it was given without knowing which:

Interface Implementations Why it is an interface
Strategy — src/strategy/strategy.h one class per strategy name, built by make_strategy the pipeline drives any decision module without knowing which
IMatchingEngine — src/exchange/matching_engine.h MatchingEngine (the matcher), PaperBroker (--paper-broker) and LiveVenue (--live-orders) are the three engines; RecordedVenue (src/lines/recorded_venue.h) replays a recorded order leg as if it were a venue, and --replay-orders is the flag that drives it reports arrive through a callback, so the pipeline never reaches into the engine -- and a venue is another engine rather than a branch in the loop

IOrderBook was the third, and the coordinator no longer takes one. It was there for the single branch that turns a BboQuote into a top of book, which made every caller supply a book, and the L2 path supplied one it never read. That conversion belongs to the pipeline, so the interface now belongs to the frozen line alone.

One L2 event, top to bottom. Pipeline::process_l2_market_view (src/app/pipeline.cpp), trimmed:

void Pipeline::process_l2_market_view(const L2MarketView& view)
{
    begin_event(view.ts);                       // funding settlement + the event clock
    ...
    latest_quotes_.insert_or_assign(view.symbol, quote);

    engine_.process_l2_top(quote);              // the report callback fires inside this call

    // The report callback runs inside process_l2_top and reads last_mid_, so this
    // update has to stay after that call.
    advance_mid((view.top.bid_price + view.top.ask_price) / 2.0);
    record_market_prints();                     // L2-only: the venue's prints, same horizon

    submit_strategy_actions(view.ts, strategy_.on_l2_market_view(view));
    ledger_.sample_resting(engine_);            // queue distribution: one lookup per open order
}

Two decisions are visible in that excerpt rather than in prose. The strategy runs last, so it sees the book the engine just applied instead of the previous one. The queue sampling is unconditional, so the distribution covers every order rather than only the ones a trace was asked for. begin_event() and advance_mid() are the two pieces every event path shares, so the L1, L2-view and L2-trade paths cannot drift in the order they advance the clock, publish the midpoint or drain a mature markout.

2. C++ decisions

Decision Where Why Measured effect
Blocked reader instead of std::getline / gzgets src/market/line_reader.h both stream a std::string and walk bytes; the feed is 1.5 GB of ~56-byte rows line layer 0.115 → 0.080 s, within 10% of the raw memchr floor; end-to-end -16.0%
Flat array indexed by tick instead of std::map src/orderbook/tick_ladder.h a tree descends ~10 pointer levels, a sorted vector moves kilobytes per insert; this feed keeps >1000 live levels over ~20,000 ticks book-apply p50 193 → 51 ns; end-to-end -9.5%
std::variant + std::visit for market events instead of a base class src/app/pipeline.cpp, src/common/market_event.h the event set is closed and known at compile time jump-table dispatch, no vtable on the hot path, and the compiler forces every alternative to be handled
Integer tick prices everywhere, one conversion function src/common/types.h float price comparison is a bug generator the matcher compares PriceTick, so no epsilon logic exists anywhere in the engine
std::stable_sort for in-flight orders src/exchange/matching_engine.cpp delivery times collide and submission order has to survive a replay stays reproducible when two orders are due in the same microsecond
Named config structs, designated initializers src/app/strategy_factory.h, src/strategy/maker/l2_market_maker_base.h the factory took 11 positional arguments, seven of them double a transposed pair of fractions can no longer compile
Rebuild aside, validate, then std::move the result in src/orderbook/l2_orderbook.cpp a snapshot that fails a check must not leave half a book behind every rejection path leaves the previous book live, which is what the ordering, cross and symbol checks depend on

The line reader: blocks, memchr, and a stated lifetime contract

// src/market/line_reader.h
class LineReader
{
   public:
    static constexpr std::size_t kDefaultBlockSize = std::size_t{1} << 20;  // 1 MiB
    explicit LineReader(const std::string& path, std::size_t block_size = kDefaultBlockSize);
    LineReader(const LineReader&) = delete;                 // owns an ifstream or gzFile
    LineReader& operator=(const LineReader&) = delete;
    bool next_line(std::string_view& line);                 // view into the block: no allocation
    std::uint64_t block_reads() const { return block_reads_; }
};

Three things here are deliberate. The header states the contract that makes the string_view safe — it is valid until the next call to next_line() — and copy construction is deleted rather than shared. A row crossing a block boundary is compacted to the front of the block, so an owned copy happens only for a line larger than 1 MiB. And block_reads() is exposed so a test can assert that a large file is read in blocks rather than per line: the counter is part of the interface, not a debugging leftover.

The book container: a flat array, and the alternative that lost

TickLadder stores one side as an array indexed by tick, and its own header carries the argument:

/*
 A std::map and a sorted vector both walk a level in a number of dependent steps: a red-black tree
 descends about ten pointer levels, and a sorted vector moves several kilobytes of elements per
 insertion once the book holds a thousand levels. ... Indexing by tick turns a level change into one
 computed address.
*/

The sorted std::vector was implemented and measured rather than argued away: 1.02 to 1.11x over the std::map in isolation, which does not justify a kilobyte-scale memmove per insertion. That is the point of the exercise — the rejected alternative is a result, and it also says the two structures pay for the same shape. Two smaller choices inside the class earned their place the same way: set() returns early when the slot already holds the requested quantity, which an incremental feed does constantly, and top_levels() produces the touch and the summed depth in one walk rather than one walk per level.

TickLadder in full: one array, one hint, one growth margin

The container is a single std::vector<uint64_t> of quantities indexed by tick - base_, plus the remembered best slot. Every lookup is an address computation:

// src/orderbook/tick_ladder.h
std::size_t slot_of(PriceTick tick) const { return static_cast<std::size_t>(tick - base_); }
bool in_range(PriceTick tick) const
{
    return tick >= base_ && tick - base_ < static_cast<PriceTick>(quantity_.size());
}

Growth is where a flat array goes wrong, so it is written to be rare and to be cheap when it happens. The range starts at 2048 slots centred on the first price seen, and each growth adds a 1024-slot margin on both sides, so a drifting market does not reallocate on consecutive updates:

// src/orderbook/tick_ladder.cpp
constexpr std::size_t kInitialSlots = 2048;
constexpr std::size_t kGrowthMargin = 1024;
...
new_base = std::min(base_, tick) - static_cast<PriceTick>(kGrowthMargin);
const PriceTick new_top = std::max(old_top, tick + 1) + static_cast<PriceTick>(kGrowthMargin);
new_slots = static_cast<std::size_t>(new_top - new_base);
if (new_base < 0)
{
    // Ticks are derived from positive prices, but keep the base non-negative so that slot
    // arithmetic stays in unsigned range.
    new_slots += static_cast<std::size_t>(-new_base);
    new_base = 0;
}
std::vector<uint64_t> grown(new_slots, uint64_t{0});
...copy the old contents at an offset...
quantity_.swap(grown);

Three details there are the design. The margin turns growth from a per-update event into a per-run one. The clamp at zero is what makes slot_of legal as unsigned arithmetic: without it, tick - base_ would wrap for a tick below the base, and the slot index would become enormous instead of invalid. And rebuilding into a new vector and calling swap keeps the old contents alive until the new buffer is complete, so a failure during growth cannot leave a half-copied ladder behind.

Finding the next best level when the best one empties is the other hot path. It walks one slot at a time from the emptied slot rather than scanning, which is the shape assumption written into code:

void TickLadder::advance_best_hint()
{
    std::size_t slot = slot_of(best_);
    if (descending_) { while (slot > 0) { --slot; if (quantity_[slot] != 0) { best_ = base_ + slot; return; } } }
    else             { while (++slot < quantity_.size()) { ... } }
    best_ = kNoBest;   // the side is empty
}

Real books are dense near the touch, so the next populated level is usually the next slot. The same reasoning is why top_levels() exists next to nth(): asking for each of five levels separately restarted the walk from the best slot every time and crossed the near-touch slots about five times over.

Why not std::pmr

The usual answer for a hot path with allocations is a pool — std::pmr::monotonic_buffer_resource and containers that take an allocator. This project has no allocator plumbing at all (grep -rn 'pmr\|memory_resource\|allocator' src/ returns nothing), and the reason is a measurement rather than a preference.

What the profile of the pinned run says about allocation: 279 read syscalls per run (the 1 MiB block reader) with 3.4% system time, 0 major page faults, and 8 MB peak RSS while streaming 1.5 GB. There is no allocator cost in the profile to reclaim, because the design already removed the allocations a pool would have served:

Where an allocation would naturally happen What it does instead
per input line one 1 MiB block, reused; next_line() hands back a string_view into it
per field split a caller-owned std::vector<std::string_view> that is clear()ed and re-reserve()d
per book level change a slot in an array that grows a handful of times per run
per resting order one std::list node: cancel is O(1) and nothing shifts, at the cost of one allocation that 406 orders over 117,288 batches makes unmeasurable
per strategy one std::unique_ptr<Strategy> at start-up; 16 make_unique calls in the repository

There is a second cost that pmr would add: types. std::pmr::vector is a different type from std::vector, so the allocator would spread into the book's interface, the engine's containers and the tests, in exchange for a cost the RSS number says is not there. The rule the project already follows is the deciding factor: no change without a measurement behind it. If a future workload allocated per event — a map per update, or an object per order — the same profile would show it, and this is the tool to reach for then.

What the C++ actually uses, and what it deliberately does not

Counts from the repository, so this is an inventory rather than a slogan:

Feature Where it earns its place
std::string_view — 66 uses the whole data path: the block reader, the field split, the parsers, symbol lookup. The lifetime contract is written down in LineReader rather than assumed
constexpr — 46 the block size, the parse bounds (kMaxExactMantissa, kMaxFastDecimals), the tick conversion, the ladder's growth constants
if constexpr — 10, with std::variant + std::visit — 9 one Pipeline::process_event for four input formats, resolved at compile time, no vtable
std::unique_ptr / make_unique — 18 / 16 the strategy factory hands ownership to the caller; nothing else in the loop is heap-owned by policy
enum class — 11 Side, ExecType, QueueModel, LiquidityRole, AppMode, Stage: scoped, and a mix-up does not compile. The four in common/types.h also name their underlying type, uint8_t, because a side, a run mode, an execution type and a queue model are each carried by structs that travel with every event, and naming the width keeps that a decision rather than a default
= delete — 2 LineReader copy construction and assignment: it owns a file handle, so a copy would double-close it
std::stable_sort — 2 vs std::sort — 4 where the order is the semantics (in-flight orders) against where any order is fine
std::deque — 8 rolling trade windows and the markout queue: append at the back, remove at the front, nothing shifts
std::list — one per price level one node per resting order: cancel is O(1) instead of moving the rest of the level
std::from_chars — 7 the boundary parsers, with an integer fast path in front of the double case
std::array — 9 fixed tables, such as the power-of-ten lookup the price parser indexes
[[nodiscard]] — 22 the five returns whose bool says whether their out-parameter was written (SpscRing::try_push and try_pop, LineReader::next_line, IMarketEventReader::next, TickLadder::nth), and the parsers and counters whose value is the whole point of the call. The distinction is the one worth stating: a dropped status is a silent wrong value, while a dropped query is only a call that did nothing

Deliberately not used, and why:

  • std::pmr and allocators — the profile has no allocation cost to reclaim, and std::pmr containers are different types, so the allocator would spread into the book's interface, the engine and the tests.
  • Concepts and ranges — nothing in this repository is generic. The containers have concrete element types and the algorithms are three-line loops; a constraint would document a template that does not exist.
  • Exceptions on the hot path — the parsers validate and fall back to a slow path instead of throwing, so a malformed field costs time rather than control flow. Exceptions stay at the boundaries, where a missing input file should stop the program.

Typed events without a vtable

// src/app/pipeline.cpp — one event path for every input format
std::visit([this](const) auto& value)
{
    using Event = std::decay_t<decltype(value)>;
    if constexpr (std::is_same_v<Event, MarketTrade>) { ... }
    else if constexpr (std::is_same_v<Event, MarketDepthSnapshot>) { ... }
    ...
}, event);

The event set is closed, so std::variant costs nothing that a virtual call would not cost more of: dispatch becomes a jump table, and a new alternative fails to compile until every branch handles it. The pipeline is still written once for all four input formats.

Prices are integers, and there is exactly one conversion

// src/common/types.h
using PriceTick = int64_t;
inline PriceTick to_price_tick(double price, double tick_size = PRICE_TICK_SIZE)
{
    return static_cast<PriceTick>(std::llround(price / tick_size));
}

// src/exchange/matching_engine.cpp — the matcher compares ticks, never doubles
if (incoming_price < best_price) break;

Every price in the engine, the books and the queue counters is PriceTick; a double price exists only at the boundary where a feed is parsed and where a report is rendered. That removes the entire class of "it should have crossed but did not" bugs, because there is no rounding disagreement left to have: the reader and the engine call the same function.

Zero is "nothing", and there is no other sentinel

The project does not use std::optional on the data path. "This field means nothing" is a zero, and a consumer checks the field rather than the type:

Where The zero What it means
TickLadder::kNoBest, src/orderbook/tick_ladder.h -1 this side holds no populated level
a ladder slot, quantity_[slot] 0 the level is not there, which is why no presence bitmap sits beside it
IncrementalBookUpdate::amount 0 the venue's delete message, applied by the same set() that adds a level
an L2MarketView on an empty or crossed book every field there is no usable touch, so nothing downstream may act on it
L2OrderBook::local_timestamp_ 0 no local timestamp has been seen yet, so the next one cannot be early

The fourth row is the one with consequences, and it is what makes "a trade without a usable BBO cannot create a synthetic fill" true. It is true because three callers test the same field, not because one of them raised: Pipeline::process_l2_market_view feeds the engine only when both prices are positive, L2MarketMakerBase::on_l2_market_view quotes at a mid of zero, which is a cancel, and the run's final mark stays at zero. The fifth row is the same idea for a timestamp: the check that the next batch is not early is written local_timestamp_ != 0 && batch.local_ts < local_timestamp_, so zero carries "nothing seen yet" without a second flag to keep in step with it.

The price of the convention is that a reader has to learn it, which is what this section is for. The alternative buys a branch and a wider struct on the path of every event, to express a distinction the fields already carry.

The same invariant can be an error on one path and normal on another

A crossed book, meaning a best bid above the best ask, is refused on the snapshot path and tolerated on the incremental one:

// src/orderbook/l2_orderbook.cpp
if (next_bids.best() > next_asks.best()) throw std::invalid_argument("L2 snapshot is crossed");

A snapshot is a venue asserting that its book is clean at one instant, so a crossed one is a broken input and the whole update is refused. An incremental stream makes no such assertion: its two sides arrive in separate messages, so a book that reads crossed between them is a normal intermediate state rather than a fault. The incremental path therefore checks nothing about the two sides together, and the crossed state is absorbed one layer up, where top_of_book() and market_view() return the empty view until the next update clears it. Validating there would reject a correct feed, and not validating anywhere would hide a defect on the snapshot path, so the two paths cannot share the check.

Delivery order is a correctness property, not a preference

// src/exchange/matching_engine.cpp
void MatchingEngine::advance_to(uint64_t ts)
{
    // The clock has to move even when nothing is in flight, or a cancel latency would never expire.
    now_us_ = std::max(now_us_, ts);
    if (in_flight_orders_.empty()) return;
    std::stable_sort(in_flight_orders_.begin(), in_flight_orders_.end(),
                     [](const) auto& left, const auto& right) { return left.first < right.first; });
    ...
}

Two orders due in the same microsecond have to land in submission order or the replay stops being reproducible, which is why the sort is stable rather than fast. The comment on the clock line is the fix for a real bug: while the engine only moved its clock when something was pending, a cancel latency could never expire on a quiet market.

The strategy factory takes a struct, not eleven arguments

// src/app/strategy_factory.h
struct L1StrategyConfig
{
    FlowAwareMarketMakerConfig flow_config{}; // the frozen line's own knobs, in one struct
    double risk_threshold = 0.60;             // four of the old arguments were fractions
    double stress_spread_multiplier = 2.5;    // of the same type, in the same position
    ...
};
struct StrategyFactoryConfig
{
    const InstrumentSpec* instrument = nullptr;
    L1StrategyConfig l1{};                    // the only place an L1 type is still named
    ...
    uint64_t order_size_override = 0;         // ... and two were counts of the same type
};
std::unique_ptr<Strategy> make_strategy(const std::string& strategy_name,
                                        const StrategyFactoryConfig& config = {});

The old signature was eleven positional arguments, seven of them double, so at a call site a risk threshold and a fee multiplier could be swapped and still compile. Named fields with designated initializers make that class of mistake unwritable, and the C++20 rule that initializers follow declaration order is what makes a call site readable in review.

One place per fact, and the check that guards it

The factory's decision is the general one, and the repository now applies it to everything that had been written out twice. This is not a wish list: each row was a fact living in more than one place, and the right column says what fails when the row stops being true -- or that nothing does yet.

fact where it lives now what fails when it stops being true
the engine's nine settings MatchingEngineConfig the 34 call sites, which name the knobs they set
the venue's schedule for an instrument fee_schedule_of the recorded runs, through the numbers they quote
the L1 settings a run uses L1StrategyConfig tests/strategy_benchmark_test.cpp, which passes {} where it used to repeat four values
the flags the binary accepts parse_config and its print_usage tools/gate/lint_prose.py, in both directions
the size of the tree, per directory the table above tools/gate/lint_prose.py, which counts it
the number of test binaries CMakeLists.txt tools/gate/lint_prose.py, which counts add_toy_test
the tools that exist the files under tools/ tools/gate/lint_prose.py, which requires docs/TOOLS.md to name every one
a field of a run's output tools/gate/lib.sh's field_of nothing yet: the agreement with tools/gate/check_docs.py's rule was measured once, on a 115-field run
the interval a mean carries tools/study/pnl_interval.py, and the window list it replays its own self-test in tier 1 and a tool: block in docs/PERFORMANCE_HISTORY.md, so the quoted interval is re-derived and the tool's arithmetic is checked

Two of these earned their place twice. The engine's struct was the last positional configuration in the codebase, and replacing its argument list dropped a bare 1 -- the one-event cancel delay that no flag names -- which moved every recorded L2 and L1 number until the gate said so and the value got a name. And the field reader had been written in five scripts with two different boundary rules, which agreed on every field of a real run: that is the state a rule drifts out of, not the state it drifts into.

3. Where the time went

One line per attempt, including the ones that paid nothing. "Effect" is a paired measurement on a fixed workload; the full table and the derivations are in Performance. What this section does not do is narrate the line: the order the attempts came in, the condition that ended it and what it never measured are in the low-latency retrospective.

Change Effect Status
Buffered line reader (1 MiB blocks, memchr, string_view) -16.0% end to end; the line layer alone within 10% of the raw scan floor kept
TickLadder flat array in the market book book-apply p50 193 → 51 ns; -9.5% end to end kept
Symbol ids keyed by integer in the engine engine p50 141 → 111 ns, 5/5 pairs kept
One bounded top-level walk instead of nth() per level market-view p50 95.7 → 68.4 ns; output byte-identical over 11,403,032 batches kept
Integer price and quantity parsing instead of std::from_chars<double> read+parse p50 -65.5 ns (-11.3%) kept
Sorted std::vector for the book 1.02-1.11x over the map, in isolation rejected
Four inline update slots, column-role jump table, boolean dispatch no measurable effect (+5.4 ns ± 12.4) kept, unproven

read+parse is still 61% of the event and the last two attempts to reduce it both measured as no effect, which is the useful conclusion: the remaining cost is not in the row loop's micro-structure. Cost intuition on this codebase was wrong by 3 to 10x in a consistent direction, which is why the ledger is a table of attempts rather than a list of wins.

4. The gate

One script, four tiers, run on every change. tools/gate/verify_l2.sh all:

Tier Time What it checks
0 ~3 s CTest: 40 unit-test binaries
1 ~1-2 min pinned invariants on an L2 and an L1 run, every number quoted in the documents, the prose lints, the site build, and the IEX and futures registries' own checks -- the code half is 26 s and the document half is the rest
2 ~26 s the SHA-256 of the full 1.5 GB standard output against tests/fixtures/l2_full_ref.sha256, taken with the mode line filtered out so the reference describes the code rather than one checkout's absolute paths
3 ~10 s built, 632 s cold the same test binaries and one 200k-row replay under AddressSanitizer and UndefinedBehaviorSanitizer, with leak detection on -- 6 s of tests and 4 s of replay once the sanitizer tree exists

Most of tier 1 is the document check: it re-runs the 53 recorded runs the documents quote, which include the F4 windows, the latency sweep on converted Databento data and the interval a five-window series carries, so a claim about a market or about a millisecond moves the per-edit loop from seconds to about a minute. That is the cost of the rule that a documented number is a claim about the code.

Tier 3 is the one standard C++ check this project did not run until now, and it is checked to be live rather than assumed: the sanitizer binary links libasan and libubsan and contains 34 __asan references, so tier3 OK means the probes were on. A clean run under a probe that never armed is worth nothing, which is the rule the rest of this document follows too. The build is parallel, cmake --build ... -j$(nproc), and it was serial until 2026-11-15: a cold tier now measures 632 s end to end on eight cores, against the roughly fifteen minutes the same tier took before the flag. Measured apart on a built tree, the tier is 6 s of tests and 4 s of replay, so a cold run is the compiler and nothing else; the tier rebuilds incrementally, which is why accepting a change pays ~10 s rather than ten minutes.

The tier function, as it is written
# tools/gate/verify_l2.sh — tiers 0 and 1
tier0()  { ctest --test-dir "$build_dir" ...; }                       # a red test cannot pass
tier1()  {
  python3 tools/gate/check_written_fields.py   # every summary field is actually computed
  python3 tools/gate/lint_prose.py docs   # flags, queue models, tool paths, size figures, the tool inventory
  python3 tools/gate/check_links.py            # every link and every #anchor resolves
  python3 tools/gate/check_layering.py         # the core includes nothing that belongs to a line
  python3 tools/gate/check_adr.py              # a decision and the index row that lists it
  python3 tools/study/viability.py             # every quotient that closed a line, re-derived
  mkdocs build --strict                        # the documents as a site: no unresolved link, no old repository name
  python3 tools/fetch/fetch_iex_hist.py --self-test   # the pcap/IEX-TP walk, on a file it builds itself (one of seven)
  python3 tools/study/pnl_interval.py --self-test      # the interval's arithmetic, on hand-checked samples
  "$BIN" l2_replay ... | grep -oE 'submitted_orders=406|trade_reports=1|...'   # pinned invariants
  python3 tools/gate/check_docs.py --binary "$BIN" docs
}

Six lints stand behind the four tiers, and each owns one kind of claim. check_written_fields.py says every field of every summary struct is written somewhere. lint_prose.py re-counts what a paragraph states — the flags the binary accepts, the queue models that exist, the tool paths, the size figures and the tools that exist — so prose cannot describe a model set that changed. check_links.py follows every link and every anchor, because a reader moves between documents by link and a document that is split breaks them silently — and it follows an absolute GitHub URL back to the path it names as well, so the handful of links that mkdocs forces out of the tree are checked rather than skipped. check_layering.py reads the #include lines, so the core cannot reach into a feed family or a closed line. check_adr.py holds each decision record to the index row that lists it. check_docs.py re-runs the command behind every quoted number.

The seventh check in that tier is not a lint: mkdocs build --strict builds the documents into the site a reader opens, and it refuses a link that resolves outside docs_dir. Such a link is legal on GitHub and passes check_links.py, which is satisfied by the file existing — what it violates is the site's rule that every page lives under docs/. So a paragraph could point outside the published tree and stay green locally, which is how a relative ../CHANGELOG.md survived four green tiers and went red in CI. The check is skipped when mkdocs is not installed — the C++ tiers must not depend on a Python package — and it is not skipped in CI, which installs one.

It also greps the pages it just built for the repository's old name. site_name and repo_name are the only source of the site's title and of its header link, so a value left behind there publishes a page that names a repository which does not exist — which is what the published site did for two weeks after the rename, because nothing looked at the built pages rather than at the source they came from.

The last line is the part worth stealing. A table in a document can carry a marker — the first line below is written so that it is not a marker itself, which matters because the checker matches a line that is exactly the marker text:

<!-- toyquant:check            <- this line and the next two form a marked block
run: l2_replay <the full command, one line>
then: submitted_orders=182 trade_reports=21 fill_rate=0.115385 captured_edge_per_unit_ticks=0.0952381
-->

A block whose command is not the main binary names it on a binary: line, resolved beside the binary the gate passes in, which is how the ring hand-off benchmark's message count and checksum are re-derived without a nanosecond being asserted.

check_docs.py re-runs the command and fails the build if any value has moved. A documented number is a claim about the code, so it is treated as one. lint_prose.py covers the other half: it reads the flag literals out of the parser and fails when a document omits a flag the binary accepts, invents one it does not, or names a tools/ script that is missing. Both lints have caught real drift here, twice on flags.

The IEX line is the third shape, for a tool whose real input cannot be fetched in a test at all: the archive is behind an account, so the self-test builds a pcap in the container shape and checks that the walk reads back what went in. It is a statement about the walk and not about the feed, which is why the tool decodes no message and says so on every run.

Two habits come out of this design, and both are in the history as failures:

  • The default run is byte-identical. Every knob defaults to inert, so a feature lands without moving the pinned output. When a change does alter the default — five new summary fields, once — the reference is re-recorded only after proving the difference is exactly those fields, by stripping them from the new output and hashing the remainder against the old reference. It matched: the change really was five fields.
  • Tier 0 exists because the gate lied once. A commit landed carrying a failing assertion, because the script compared replay outputs and never ran the unit tests. The fix was to put CTest first, and the first change made under the new tier was caught by it.

5. What it measures

Five results, one line each. The first four come from the marked block above and its neighbours; the strategy rows are in Performance History.

Result The number
A fill is a queue event, not a price event the median quote never came within 3.9% of the front of its queue, and almost every quote that reached the front traded: 21 of 21 on the mainline window, within a few percent on two others
Adverse selection takes most of the quoted spread 0.095 ticks per contract captured out of the 0.5 the quote was placed at
The fee is the binding constraint 2 basis points on a 10 USD contract is 2.57 ticks, against 0.095 captured: the policy earns 3.7% of the fee it pays, and 29 times less than it at 267 fills
Latency has two legs with opposite signs +20 ms of arrival latency: fills 21 → 11 and captured edge 0.095 → 4.09 ticks. +20 ms of cancel latency: fills → 25 and captured edge → -0.440 ticks
Widening does not create value, it reduces participation captured edge per contract rises 100x while total captured value falls; the loss shrinks only because the strategy stops trading (1 fill instead of 267)

6. What it deliberately does not do

  • No FIX, no recovery matrix of its own, no risk gateway, no persistence. Connectivity is one order channel to a demo venue (ADR 0005): the relay holds the login, the subscription and the reconnect -- ADR 0006 is moving that into the engine slice by slice, with the authentication already there -- and no number on this page was measured through it. What it deliberately does not do is the rest of that list, and the loop is meant to be readable in one sitting.
  • No market impact and no feedback from our own resting size into the queue in front of us. The optional participation cap is a capacity limit on the displayed touch, not a price-impact model.
  • One venue, one instrument, one day. Deribit BTC-PERPETUAL on 2020-04-01, in windows of 200,000 to 2,000,000 rows.
  • The queue is a model, not a reconstruction. The feed carries aggregated quantities, not order identities, so the queue in front of a quote is inferred. The mitigation is bounds rather than a single calibrated rule: conservative, prorata (the default), lumpy and optimistic, plus --arrival-share for how much of the displayed size a new quote starts behind.
  • Fill counts below thirty are not conclusions. Several tables here report one to five fills, and every one of them says so next to the row rather than in a footnote.
  • The L1 path is the project's first iteration, kept as a regression fixture. It models no queue in front of a quote, so a fill there is a price event; the history of why the mainline moved to L2 is in Performance History.

7. The questions a reader asks, with the answers

In one paragraph

A measurement project with a C++20 instrument inside it. The question was whether a personal account's programmatic quote can pay for itself; the answer is no in eight markets — crypto perpetuals, the micro E-mini, SPY, US large caps, two option tenors, a funding carry and a reversion rule — and each one is closed by a quotient rather than an opinion, from the fee against the tick to the inventory's own move against the edge. The instrument exists because those quotients need a queue that a quote actually waits behind and a fee model that charges what the venue charges. At a one-tick spread the strategy captures 0.095 ticks per contract against a fee worth 2.57, so it earns 4% of the fee it pays — and every number in that sentence is re-computed by the build.

The questions

Question The answer
"Isn't the queue model arbitrary?" It is a model, and it is bounded: four rules from a conservative lower bound to an optimistic upper bound, plus an arrival share. Measuring the share showed it barely matters, because the pro-rata share is a fixed point of the update rule, and only trades move a level's queue materially (0.04% of quantity changes). So the dynamics decide the queue, not the calibration.
"How do you know the numbers are right?" The build re-runs the command behind every documented figure and fails if a value moved; prose is checked for flags, queue models and tool paths that do not exist; the unit tests run first; and the default run is hashed against a reference, so a faster-but-different change cannot slip through. When the default did change once, I proved the difference was exactly the new fields by stripping them and hashing the rest.
"What was the hardest bug?" A fee model that charged price * quantity on an inverse contract, about 630x too much, which had turned an entire strategy comparison into a measurement of the overcharge. It was found by writing down what the fee should be, not by reading the code. The second one is a silent order rejection: a strategy that believed it was quoting had nothing resting, so the position gate leaked and the strategy's bookkeeping drifted.
"How do you profile?" An in-process stage profiler (StageProfiler, null-gated so it costs nothing when off) reports per-stage percentiles; changes are accepted on paired repeated runs with a t-statistic, never on a single reading. Absolute numbers do not travel between sessions here — one unchanged binary measured a stage at 581 ns and 834 ns twenty minutes apart — so only paired deltas are quoted as evidence.
"Why C++, and why not Python?" The workload is 1.5 GB of parsing plus a book update per batch, and the parts worth deciding are the container, the allocation behaviour and the memory access pattern. A profiler inside the process is also what made the 3-to-10x-wrong cost intuition visible.
"What would you do differently?" Build the gate before the features: it caught two undocumented flags, a paragraph describing a model set that had changed, and a failing test that shipped in a commit of mine. I would also start from the fee model and the queue model, because everything downstream is a measurement of those two.
"What next?" The out-of-sample check is five days and gated: four more days, replayed through the same depth format, keep the fill rate between 8 and 15% and the fee between 9.6 and 102 times the captured edge, with every realized PnL negative. The first three-window version of that claim said the fee was never below 27 times, and days four and five broke it, which is why the table carries one block per day. What is left is the block-parse experiment, because parsing is 61% of the event and the row loop has already paid nothing twice.