Learning Path¶
This document is the market-making side of the project: the mechanisms the instrument has to model, in the order they matter, each with the measurement that supports it. Sections 1, 6 and 7 are one thread -- a fill is a queue statement, here is one order's trace, and here is what changes when the queue is harder to leave than to reach.
| you want | open |
|---|---|
| why the simulator is built this way, and what the evidence cannot support yet | Design Review |
| the measurements themselves | Performance History |
| the order to read the source in, with the result each step produced | Main Line |
| the mechanisms taken to market, one market at a time | Retrospective |
Everything below uses the recorded Deribit window; the arithmetic behind a term -- capture, markout, the fee floor, the breakeven rate -- is in the glossary. Every field a run prints is folded at the end of this page.
bash tools/gate/verify_l2.sh fast # cuts the slice and checks that the code still reproduces its numbers
1. Fill probability is a queue, not a price¶
An order fills when the trades at its price have consumed everything ahead of it. The strategy's own size is almost irrelevant: a ten-contract quote is 0.05% of the median level, so what decides a fill is where the queue went.
tools/sweep/workload_matrix.sh model # fills under each queue model, same window
tools/sweep/workload_matrix.sh placement # fills as the quote moves from behind the touch onto it
tools/sweep/workload_matrix.sh requote # fills as the quote is held instead of re-joined
tools/sweep/workload_matrix.sh arrival # fills as the assumed arrival position changes
Read trade_reports (fills) and quote_at_touch_orders. The measured ordering of the levers, largest first:
| lever | fills multiplier | so |
|---|---|---|
| requote threshold | 7.7x | change the requote policy if you want more fills |
| queue model | 5x | the model is the second-largest choice, not a detail |
| quote placement | 3x | moving onto the touch buys fills, not queue |
| arrival share | under 2x | the smallest of the four |
The arithmetic is in docs/PERFORMANCE_HISTORY.md, under "The queue model, as arithmetic": a level decrease of r has
one unknown -- how much of it was ahead of us -- and the models are claims about that number's mean, with
--queue-chunks controlling its variance and --arrival-share its starting point.
2. Adverse selection is what happens after the fill¶
A maker earns the spread at the fill and gives some of it back when the market keeps moving.
Read captured_edge_per_unit_ticks (what the fill was worth in ticks) against
markout_per_unit_ticks (what it was worth five quote cycles later). On the recorded window the first
is 0.4 to 2.5 ticks and the second is -0.5 for the fills that resolve, so a large part of the spread is
given back. markout_count says how many fills were old enough to be resolved, which matters because a
markout that never resolves is not evidence of anything.
3. Fees decide whether the spread was worth earning¶
A maker pays a fee on every fill, so the spread only matters if it exceeds that fee. On the mainline policy
it does not: the captured edge is 0.095 ticks per contract against a fee worth 2.57 ticks of the same
contract, so the strategy earns about 4% of the fee it pays. Widening the quote fixes the ratio and destroys
the fills: the two cross near five ticks, where the window holds one observation. Both tables, and the code
that turns the width into a price (src/strategy/maker/l2_market_maker_base.h:377), are in
Performance History.
One BTC-PERPETUAL contract has a fixed 10 USD face value at a price near 6,421, so a half-dollar tick
is worth 10 * 0.5 / 6421 = 0.0008 USD, while a 0.02% maker fee on the same contract is 0.002 USD.
Covering the fee takes about 2.5 ticks of capture per side; a quote at the touch earns 0.4 to 0.7.
Read captured_edge_usd against fees_paid in the summary. More fills raise the fees and barely move
the edge, so realized_pnl gets worse as the fill count rises. This is the honest result of the demo:
on this instrument and this fee, a passive quote at the touch cannot pay for itself, and no queue model
changes that.
4. Inventory is the risk that survives all of the above¶
Read max_abs_inventory, inventory_sign_changes, avg_abs_inventory, and in [PORTFOLIO]
unrealized_pnl, which is the open position marked at the last market price. max_abs_exposure_usd
against starting_cash_usd says whether the run ever held more than its collateral allowed, and
exposure_over_collateral_fills counts how often a fill pushed it past. --max-position=N turns that
observation into a pre-trade gate. strategy_position_mismatches compares the strategy's own position
against the pipeline's. It is zero when every fill the account books was attributed to an order this process
submitted -- a fill that arrives after the report that ended its order counts, because a live venue sends the
fill that way -- and a non-zero value means a callback was dropped or an order the process does not know (a
resumed session) filled.
5. Reading a comparison without fooling yourself¶
tools/sweep/compare_runs.sh "conservative" "l2_replay ... 1 conservative --fast-validation" \
"prorata" "l2_replay ... 1 prorata --fast-validation"
- Fewer than 30 fills supports no conclusion about PnL or per-fill edge. Every recorded window is
far below that, which is why this project reports a range across queue models instead of a number.
The benchmark marks each row with
fills_are_conclusiveand warns on stderr. - Steps do not add. Two changes that each give -6% are not a -12% change; interactions are real.
- A simulation is not a measurement below its own noise. Timing claims have the same rule, recorded
in
docs/PERFORMANCE.md.
6. Seeing the queue instead of trusting the statistic¶
Section 1 says a fill depends on the queue in front of a quote. That is easy to assert and hard to believe,
because every fill rate the project prints is a total: 182 orders, 21 fills, 11.5%. --trace-order=N
replaces the total with one order's sequence. Below is the complete trace of order 3 in the window the
performance tables use, at the policy with the best measured capture, with the timestamps quoted as
milliseconds after the order was sent and the rows narrowed to the two columns that matter. The submit row
has no queue to report and prints zero, because the order has not joined a level yet: the field belongs to a
resting order's wait, not to the price.
event queue_ahead change
submit -
rest 7200 <- the queue the order joined behind
view 7199 1
view 2135 5064 <- one cancellation ahead removed 70% of the queue
view 2134 1
view 2063 71
view 1995 68
view 1929 66
view 1865 64
view 1803 62
view 1743 60
view 1723 20
view 1704 19
view 588 1116 <- a second lump, the last big one
view 569 19
view 507 62
view 391 116
view 304 87
view 236 68
view 211 25
view 189 22
view 186 3
view 169 17
cancel 0 <- pulled with 169 contracts still standing in front
Four things are in there that no total can show.
The order waited behind 7,200 contracts to trade one. A new quote does not get to cut in: the model makes it join behind everything the venue was already displaying at that price, which for a Deribit perp level is thousands of contracts.
The queue does not erode, it collapses. 86% of everything that left went in two events, 5,064 at the first view and 1,116 later on. The remaining twenty-one views moved it by tens. A model that spread the same total reduction evenly over the same events would reproduce the average and get the distribution wrong, and it is the distribution that decides who fills first.
The quote was pulled while 169 contracts still stood in front of it. The strategy's own price rule moved and it cancelled, so the queue never reached zero and no fill was possible. That is the mechanical reading of the fill rate: 21 fills from 182 orders is less a statement about prices than about how long a queue takes to clear and how quickly the strategy changes its mind.
The first order of the run did not last a millisecond. Order 1 joined behind 208,300 contracts and was cancelled in the same millisecond it was submitted, which is what quoting one tick wide at a touch one tick away looks like when the strategy re-quotes immediately.
Two notes on building this, because both are the kind of thing that hides inside a plausible number. The
first version printed the cancel at the timestamp the order had been submitted: an order's own reports
carry the order's timestamp rather than the event's, so the sequence read backwards from its second row.
The trace now times every row with the clock of the event being processed. And --no-output silences the
summary, so the first traced run that used it printed nothing at all; the trace rides in the summary and is
meant to be read with grep QUEUE_TRACE.
The trace is one order; the distribution is all of them, and it is the version that carries the claim.
queue_profile_orders, queue_zero_orders and the two queue_..._fraction_p50 fields in the summary are
the same measurement aggregated over every quote the strategy posted, and on the window above the median
quote never came closer than 96.1% of the queue it joined behind. Section 1 of
Design Review is the claim and section 4 is the evidence behind it.
7. Latency is two costs with opposite signs¶
The natural way to model latency is a single number that makes everything slightly worse. Measured on this feed it is two numbers that pull in opposite directions, and they are not the same kind of cost.
| added latency | fills | captured ticks per contract | realized USD |
|---|---|---|---|
| none | 21 | +0.095 | -0.109 |
| 20 ms to arrive | 11 | +4.09 | -0.016 |
| 20 ms to cancel | 25 | -0.440 | -0.156 |
A late arrival quotes wider. The strategy chooses a price at one view and that price reaches the book milliseconds later, by which time the touch has usually moved. The order ends up where the strategy would no longer put it, which on this feed means further from the mid, and further from the mid is a better price for the side that gets filled. Fewer orders trade and each one that does is worth more: the capture per contract rises by a factor of forty, and the fill count halves.
A late cancel leaves a stale quote standing. The quote sits at a price the strategy has already decided to leave, so the trades that reach it are exactly the trades that were going through that price, which means buying above the mid. The fill count rises rather than falls, because a quote that is never pulled is always there to be hit, and the capture per contract turns negative: each fill is worth less than nothing before fees are counted.
The asymmetry is the lesson. Being slow to arrive costs fills; being slow to leave costs money. One latency
knob would have averaged the two into a mild cost, and the numbers above say the mild version is wrong in
both directions. It is also the reason the two are separate flags rather than one: a strategy that is only
slow on the way in and fast on the way out is a different business from the reverse, and this project can
now tell them apart. docs/PERFORMANCE_HISTORY.md records the five-point sweep of each, including the
markout column that shows the cancel's cost is realised at the fill rather than after it.
Field reference: every field a run prints, what it means and which line carries it
Field reference¶
The fields a reader needs, what they mean, and which line carries them. [EXECUTION] is authoritative
for execution quality; [STRATEGY_METRICS] carries only what a strategy owns.
| Field | Meaning |
|---|---|
submitted_orders, submitted_quantity |
Quotes sent to the engine, and their total size |
cancel_requests |
Cancellations requested, one per resting order pulled |
trade_reports, trade_report_quantity |
Fills, and the quantity filled |
fill_rate, cancel_rate |
Filled quantity over submitted, and cancels over orders |
queue_ahead_consumed |
Queue in front of us consumed by trades, measured exactly |
buy/sell_queue_from_quantity_changes |
Queue in front of us removed by level-decrease inference |
quote_at_touch_orders, quote_behind_touch_orders |
Where the quote sat relative to the venue touch |
captured_edge, adverse_selection |
Raw sums of price differences; the tick and USD fields below are the readable form |
captured_edge_per_unit_ticks, markout_per_unit_ticks |
Quantity-weighted averages in ticks, comparable with a half-spread. The second is resolved --markout-horizon quote cycles after the fill -- five by default in every recorded run -- so it is a measurement of what happened next rather than of the fill, and it is the only field here whose value a flag changes |
captured_edge_usd, markout_usd, fees_paid |
The same quantities in the unit fees are charged in |
queue_profile_orders, queue_zero_orders, queue_min_fraction_p50, queue_min_fraction_p10, queue_end_fraction_p50 |
The queue distribution over orders rather than over fills: how many orders were profiled, how many saw their queue reach zero, and the median and tenth-percentile closest approach and end-of-life queue, each as a fraction of the queue the order joined behind. Both fractions are zero on a path whose model gives a quote no queue at rest, which is the L1 replay. |
markout_count |
Fills old enough for the five-cycle markout to have resolved |
avg_abs_inventory, max_abs_inventory, inventory_sign_changes |
Inventory path statistics, updated where the position changes |
max_abs_exposure_usd, starting_cash_usd, exposure_over_collateral_fills |
Marked exposure against the collateral the run began with |
position_limit, risk_rejected_orders |
The pre-trade gate, and how many orders it refused |
strategy_net_position, strategy_position_mismatches |
The strategy's own position, and any divergence from the pipeline's |
working_orders |
Orders still resting when the input ended |
audited_rejected_orders |
Orders that ended in a venue, broker or timeout rejection, appended to [EXECUTION] only when non-zero. A Rejected report carries reject_reason and is terminal like a cancel: pending quantity is released and the order leaves the strategy's open set. A cancel the venue refuses (51400: filled, cancelled or gone) reaches the engine as a Rejected too, so an order it ended is counted here and its fills arrive afterwards as late_fill_reports |
invalid_order_transitions, orphan_order_reports, late_fill_reports/late_fill_quantity |
The state machine's health: a report that would move an open order backwards, a report the ledger could not attribute (a duplicate terminal one, or an order this process never placed), and a fill that arrived after its order's ending report -- the live venue's reply to a cancel for an order it has already filled precedes the fill. An acknowledgement of an order that is already working is not one of the first: the engine reports a marketable limit as a fill and then as resting its remainder, so the state stays where it is and nothing is counted. Printed only when non-zero, and a normal run leaves the first two at zero |
cancel_reason (on a Cancelled report) |
Why the order left: user, venue, ioc, fok, post_only, self_trade or timeout. The distinction is what lets a strategy tell its own cancel from the venue cancelling on it, and post_only is the venue declining a quote that would have traded on arrival |
[PORTFOLIO] cash, realized_pnl, unrealized_pnl, equity, fees_paid |
The account, marked to market at the last price. marks_complete says whether every open position was actually marked: when it is 0, equity and unrealized_pnl print n/a (and so do the mark-dependent [PNL] fields), because cash plus an unmarked position is not the account's value |
[FUNDING] funding_points, funding_span_hours, funding_sum_rate, funding_annualized_rate |
The rate series the run was given: how many settlements it holds, over what span, and what a long holding one unit of notional paid over it and over a year |
[FUNDING] funding_settlements, funding_flow_usd |
What the window did with it: settlements the event clock passed, and the USD they credited to cash. A settlement reached before the account holds a position is counted and moves nothing, so the two disagree by design. Printed only when --funding-rates supplied a series |
[CARRY] carry_gross_notional_usd, carry_net_notional_usd |
The two legs and their difference: a hedge holds both notionals and is exposed to neither, so only the second is the account's own risk and only the first is what a reader would call the size |
[CARRY] carry_leg_a_funding_usd, carry_leg_b_funding_usd, carry_funding_usd |
What each leg's funding paid or received and what is left after the hedge takes its side back, which is the carry |
[CARRY] carry_fees_usd, carry_net_usd |
The round trip at the venues' taker rates, and the carry minus it |
[CARRY] carry_annualized_carry_usd, carry_annualized_return, carry_to_fees, carry_breakeven_hours |
The carry as a yearly rate, as a fraction of one leg's notional, against the fees it paid, and the holding period that comparison implies |
[PNL] closed_pnl_usd, net_pnl_usd |
What the run completed, and the same plus the position it was still holding marked at the last price. The first is the quoting result and the second is the bet left open, so neither is called "the PnL" |
[PNL] gross_pnl_usd, gross_closed_pnl_usd |
The two above with the fees added back: a negative gross PnL is a loss before the venue charged anything, which no fee tier can fix |
[PNL] maker_notional_usd, breakeven_maker_rate |
The notional the maker fees were charged on, and the maker rate that would have made this run flat. A negative rate is the fee question's real answer: zero fees would still lose, and that rebate would be needed instead |
[PNL] net_per_fill_usd, gross_closed_per_fill_usd |
The same two quantities per trade report, which is the number a quote decision can be judged by rather than a total |
[EXPOSURE] legs, gross_usd, net_usd, gross_to_net, fees_usd |
The book at the last quote each symbol showed: one SYMBOL:quantity:notional per leg sorted by symbol, then the sum of the sizes, the size of the sum, their ratio, and the fees the account paid. Gross is what the fees are charged on and net is what the position risk is, which is the difference a one-symbol book cannot show and a hedged or multi-leg one lives or dies by. The tag is [EXPOSURE] and not [PORTFOLIO] because the account's own line already carries that one. In a run given a second book it is the only line that reads both legs at once, and each leg is valued with its own instrument's face value. A leg the run had no positive mark for prints n/a and the line ends unmarked=N; the totals are n/a too, because a zero-valued leg is not a leg worth zero |
[BOOK] symbol, events |
How many market events one book was given, in a run carrying several: the count that says the merge delivered both streams rather than one of them twice, because the run's own events in [L2 DATA] is the sum of these and nothing else. Printed once per book, directly under that book's [EXECUTION] line, and only in a two-book run |
[MARK] missing, reason |
Printed only when a run ends holding a position it could not mark: the symbol, the quantity and reason=no_positive_midpoint. It goes with equity=n/a marks_complete=0 on the account and unmarked=1 on the exposure, and it is the line that says the run's cash is not its equity. The run still finishes and reconciles |
[L2 DATA] crossed_book_batches |
How many incremental batches left the book with its best bid at or above its best ask. top_of_book() already reports a crossed book as nothing and every consumer skips the view, so the crossing is handled correctly and invisibly: a run that ignored four thousand crossed snapshots read exactly like a run whose strategy chose not to quote. The count is what separates them, and it rides on the depth line rather than getting its own because the line already says how much depth arrived. Printed only when non-zero, so a run with none keeps its recorded bytes -- the window every L2 number here comes from has none, which is why the field appears in no recorded line. A live top-of-book stream is the case that produces it: a snapshot message carrying only the best level supersedes the whole book, so the book clears on a new snapshot timestamp, and a non-zero count then means a stream that is genuinely crossed rather than a level the previous snapshot left standing (measured 2026-11-25, where the older clearing rule reported 84 crossed batches of 94 on an intact feed) |
[INTERVAL] n, mean, median, ci95_lo, ci95_hi, spread, includes_zero, sign_consistent, thin_sample |
A tool's line rather than a run's: tools/study/pnl_interval.py resamples a series and prints the interval each mean carries. Four fields are the answer and two are the correction. mean with ci95_lo and ci95_hi says how well the mean is pinned; spread says how far apart the draws themselves are, and on a series of windows it is usually the larger and the more interesting of the two -- five months of one instrument give an interval 1.9 bps wide around a mean while the draws span 2.75. median is beside the mean because a trend or one outlier moves the mean and not the middle. thin_sample is the repository's thirty-fill rule applied instead of remembered, and sign_consistent says whether the draws agree on a direction at all |
[L2 DATA] stale_trades, dislocated_trades |
The prints scored against the book, split by which way they disagreed with it. stale_trades is a print with no midpoint to compare against -- before the book was two-sided, or more than a second after its last update -- so on a window whose depth input covers quarter of an hour and whose trade file covers the day it counts almost every print: it is a fact about the inputs' coverage, not about the prints. dislocated_trades is a print more than five basis points from a current midpoint, which is a fact about the print. Both are printed only when non-zero, and neither filters anything: dropping a print far from the midpoint would move the fills instead of describing them, which is why the L1 path's filter is not repeated here |
[RESUME] orders, live, gone, cancelled, cash_usd, legs, next_order_id, unknown |
What a session took over: the orders the snapshot listed, how many of them the venue still held (and that the resume then cancelled), how many it had forgotten, the cash and leg count of the account that came back, and the id the new process continues from. orders is live + gone + unknown by construction, so a count that does not add up is a bug rather than a policy. unknown counts the orders the venue did not answer about, is printed only when it is not zero, and stops the run before it quotes: a session that starts on top of an order nobody has looked at is managing a position it cannot see. Printed only by a run given --snapshot-in |
[SNAPSHOT] path, symbol, last_ts, orders, legs, cash_usd |
Where a session was written and what it holds: the clock it stops at, the orders that may still be alive at the venue, and the account's legs and cash. The cash here is the same number as [PORTFOLIO]'s, because the snapshot is taken after the final mark. Printed only by a run given --snapshot-out |
[EXECUTION] post_only_cancels |
Orders the venue refused because they would have traded on arrival and the run asked for add-liquidity-only. They are not the strategy changing its mind: they are the fills the venue declined, so this is what --post-only costs in missed trades -- and the taker fees it saves are the other half of the same number. A replay's matcher reports the refusal as a cancel with this reason; a live venue's cancel of an order the relay sent add-liquidity-only is reported the same way only when no cancel for that order left this client -- the cancel we sent is the witness, because the venue's channel carries no reason of its own -- while a venue that refuses the order at placement reports a rejected order, so a live run reads this counter and audited_rejected_orders together. Until 2026-11-30 the relay labelled every cancel of a post-only order post_only, which made a live window's 679 own cancels read as 682 refusals in 738 orders; the windows measured before that date are withdrawn from this counter. Printed only when the run named the flag |
[EXECUTION] inv_above_frac, seconds_above_inv_frac, pnl_bps_above_cap |
The reduction slice, printed only when the run named --inv-above-frac: the boundary as a fraction of --inventory-limit, the book seconds the position sat at or above it, and what the marked net did there in bps of maker notional. The sample is taken after each event's own effects on all three entry points, so it is the position that event left; the PnL is a difference of two marks and the slice marks the books itself, because advance_mid marks only for an armed kill switch and an unmarked portfolio values a carried position at its entry. A zero here is a measured zero -- the run asked and never crossed -- which is why the boundary itself is printed beside them. The number is what a flatten rule is judged on before it is built: the same band takes AAPL from -3,165 to -871 and SPY from +262 to +161 (measured 2026-11-30: -9.57 bps above 50 contracts and +14.05 above 100 on 2026-11-26) |
[PNL] symbol= |
A book's own result, in a run that carries several books into one account: the same eight fields as the account's [PNL], computed from that symbol's realized PnL, its fees and its position marked at its own last midpoint. The account's line cannot say which leg lost the money; two books' lines add up to it exactly, because the split is taken where the account's totals are written |
[WIND-DOWN] position, closed, residual, spread_bps, fees_bps, total_bps |
What closing the session's position cost, for a run given --close-out: the position carried into the end, how much of it the touch could take, and what is left. spread_bps is the distance from the midpoint to the limit the exit crossed -- the touch, or the --close-out-cross-bps band, which is a ceiling rather than the fill when a live venue's book is further away -- and fees_bps the taker fee, both against the notional actually closed, so total_bps is what the final mark was optimistic by. no_liquidity=1 means the order went out and the touch showed no size; refused=1 means a control or the venue stopped it, and the two are not printed apart -- but they are told apart from no_liquidity, which a refused exit used to read as; grew=<units> means the position grew while the exit was working, a working quote that filled the other way, which is a third fact again and is printed beside either of the others. Printed only when the flag asked for it, because a run that does not ask carries the position into the mark |
[GATE] fill_markout_buy_near_ticks, fill_markout_buy_far_ticks, fill_markout_sell_near_ticks, fill_markout_sell_far_ticks, fill_markout_samples |
What our own resolved fills at each distance from the midpoint were worth, quantity weighted, in ticks: the number the adverse-selection gate requires a quote to cover |
[GATE] market_markout_buy_near_ticks, market_markout_buy_far_ticks, market_markout_sell_near_ticks, market_markout_sell_far_ticks, market_markout_samples |
The same measurement taken from the venue's own prints, as if we had been the passive side. The first version of the gate used these, and they can disagree with our fills in sign, which is why the record keeps both |
[GATE] markout_gate_skips, flatten_orders, flatten_quantity |
Sides refused because the measured cost could not be covered, and the orders sent to reduce a position past the flatten band. Printed only when --markout-cover or --flatten-band asked for them |
Every field above is written somewhere under src/. tools/gate/check_written_fields.py fails the build if
one stops being written, because a field that is read but never written looks exactly like a field whose
value is zero.