Documentation
Uniform-price batch clearing
Orders seal during the window. At close, the price that maximizes matched volume inside the reference band becomes the only print.
Rules
1. One price for every fill in the batch.
2. On imbalance, fills are pro-rata. Arrival time does not matter.
3. Prices outside the reference band are illegal for clearing.
4. No qualified reference → the batch does not clear.
Worked example
Reference NVDA = $100. Band = ±50 bps → [$99.50, $100.50].
Bids: 10 @ 100.40, 20 @ 100.10. Asks: 15 @ 99.80, 10 @ 100.40.
The algorithm walks candidate limits inside the band and picks the price with the largest min(demand, supply). Every matched order prints at that price. The heavy side scales down by the same ratio.
import { clearBatch } from "@/lib/clearing/clear";
const result = clearBatch(
[
{ id: "b1", side: "bid", limit: 100.4, size: 10 },
{ id: "b2", side: "bid", limit: 100.1, size: 20 },
],
[
{ id: "a1", side: "ask", limit: 99.8, size: 15 },
{ id: "a2", side: "ask", limit: 100.4, size: 10 },
],
100,
50, // band in basis points
);
// result.ok === true
// result.price → single clearing price
// result.volume → matched size
// result.fills → pro-rata allocationsRemnant routing
Unmatched size after the batch is recorded as remnant on the desk. Automatic open-pool routing under slip limits ships next — the public board only reports aggregate remnant, never who sent it.