← Home

QD / MADE BY AI

I built a Python cryptocurrency arbitrage proof of concept to verify that the opportunity was real and could be profitable, then migrated the engine to Rust and optimized latency.

I also built a real-time market data service in Rust. It ingests the top 20 L2 order-book levels (the API limit) from two exchanges across 11 pairs, publishes to ZeroMQ, and persists data in TimescaleDB.

  1. 2 exchanges11 pairs · L2 top 20
  2. Rust market dataDecode · normalize · publish
  3. ZeroMQ Pub/SubArbitrage engine

Rust market data → TimescaleDB persistence

Market data / tick-to-publish

Frame bytes to subscriber receipt. Excludes WebSocket read and TLS decryption.

End-to-end latency · µs

p50
74.43
p99
99.33
p99.9
119.94
Linear scale, 0-125 µs.
Stage · µsp50p99p99.9
JSON decode4.886.717.21
Parse to OrderBookData9.9613.5417.76
Build payload14.1318.7225.30
Serialise payload2.833.834.17
ZMQ socket.send0.170.460.96
CPU subtotal31.9743.2655.40
Handoff34.3954.6188.77
Tick-to-publish74.4399.33119.94

Arbitrage engine / tick-to-decision

Depth 5 optimization: p99.9 fell from 3,000 ns to 792 ns (-74%); allocations fell from 36 to 0.

Python and Rust comparison

ZeroMQ message bytes in, arbitrage verdict out. Pre-socket only; excludes network and exchange time. Depth 5; 500,000 iterations after 50,000 warmup.

Python, JSONNaive Rust, JSONOptimized Rust, JSON
02,5005,0007,50010,0002,6671,250458p503,7501,750625p999,6661,958709p99.9
Nanoseconds; linear scale. Exact values below.
Implementation · nsp50p99p99.9
Python, JSON2,6673,7509,666
Naive Rust, JSON1,2501,7501,958
Optimized Rust, JSON458625709

After the internship / binary wire experiment

JSON deserialization is 99.6% of the decision path; arithmetic takes 7 ns. I own the ZeroMQ publisher, so I replaced JSON with a 48-byte fixed frame.

p50
2 ns
p99
3 ns
p99.9
5 ns

Measured after the internship. Excluded from the internship benchmark results above.