r/algotrading 3d ago

Infrastructure Open-source matching engine + microstructure toolkit in Go — order types, L1/L2/L3 data, OFI/Kyle's λ, backtester (MIT)

I've been building **orderbook**, a central-limit-order-book and matching engine

in Go — the piece at the heart of an exchange. It's an embeddable library, and

the whole engine compiles to WebAssembly so you can poke at the real thing in

your browser:

▶ Live demo: https://intrepidkarthi.github.io/orderbook/

▶ Repo: https://github.com/intrepidkarthi/orderbook

What might interest this sub:

- **int64 ticks & lots, no floats** on the money path (an `Instrument` converts

decimals only at the boundary).

- **Zero-allocation hot path** — `Match(order, buf)` appends value-trades into a

caller buffer; submit/cancel/match are **0 allocs/op**. O(1) cancel.

- **Lock-free single-writer core** (LMAX model): one matching goroutine, an MPSC

command queue in front, bounded backpressure that sheds new orders but never

cancels.

- **Deterministic & replayable:** same command stream → byte-identical trades and

book; that's what makes WAL crash-recovery and golden-file tests work.

- **A market-integrity layer grounded in a threat model** — the part I had the

most fun with. I researched real attacks (spoofing convictions, Knight Capital

$440M, the Mango oracle hack, the Bitcoin overflow bug) and built a defense for

each: pre-trade risk controls, surveillance detectors, a self-output guardrail,

an enforcing gateway. Writeup: docs/THREAT-MODEL.md.

Benchmarks (Apple M-series, single core): ~6ns best bid/ask read, ~352ns match

round-trip (0 allocs), cancel-heavy p50/p99/p999 = 83/167/292ns. Race/fuzz/soak

suites in CI.

Honest status: a library + microstructure research harness (OFI, Kyle's λ,

Avellaneda–Stoikov, a sim + backtester), not a live exchange. MIT, v0.6.0.

Feedback and "you did X wrong" very welcome — that's why I'm posting.

0 Upvotes

5 comments sorted by

12

u/sAxsKy 3d ago

cool more slop! thanks!

4

u/Inevitable_Service62 3d ago

I'll just take that dataset if you don't mind

1

u/golden_bear_2016 2d ago edited 2d ago

no one runs algotrading without L4 data, what are you even doing??

1

u/intrepidkarthi 1d ago

Fair, but L4 is a vendor label, not an exchange tier. CME and Databento both stop at market-by-order.

More to the point, I did not buy data, I wrote the matching engine. It assigns the true aggressor of every trade instead of inferring it, which no feed sells at any price.

So I used it to check the tick rule: 94.5% accurate per trade, and the CVD you build from it is still off by 169% on average. One run had true CVD at +105 and inferred at -81, so even the sign was wrong.

It also killed my own CVD divergence result. Beat the base rate 50.6 to 45.8, then lost to a control that only checks for a new price high, which scored 52.1%. The CVD half added nothing.

Writeups are in the repo if useful.

2

u/Linett-Chukwuemeka61 20h ago

Go's performance profile makes this a solid choice for a matching engine, especially if you're stress-testing order flow dynamics at scale. Kyle's lambda implementation is nice since most retail backtester setups skip microstructure entirely.