Projects / HFT Engine

HFT Engine

activeRust0 · external dependencies

Low-latency trading engine in Rust with register-resident signal paths for Apple Silicon (inline ARM64 NEON) and x86_64 Linux (AVX2), a seqlocked tick ring and an SPSC order queue, and per-arch Linux scheduling through raw syscalls. Includes a live Kraken spot + futures feed, a perpetual-futures cost model, an anchored walk-forward backtester with an untouched holdout, and a small learned policy. Zero external dependencies.

STATUSactive
LANGUAGERust
METRIC0 · external dependencies
STACKRust · ARM64 NEON · x86_64 AVX2 · Lock-Free · Systems Programming · WebSocket · Backtesting · HFT · Low-Latency
LATENCY MEASUREMENT

In-process simulation; the clock is probed and reported with every run.

  • Two metrics: signal latency (tick in to trigger) and order handoff, a one-way cross-thread push from the strategy to the in-process exchange. Nothing is confirmed back, so it is not a round trip.
  • Earlier headline figures ("41 ns" and the per-platform tables) are withdrawn: on Apple Silicon every value was a multiple of the 24 MHz counter's 41.67 ns tick, the "round-trip" column was the one-way handoff, and ~400 samples made p99.9 just the max.
  • Histograms keep exact count/sum/min/max over the whole run; p99 prints only at 100+ samples and p99.9 at 1,000+. HFT_SIM_BURSTS / HFT_SIM_BURST_SIZE scale the sample, and HFT_CLOCK=tsc uses a calibrated invariant TSC on Linux x86_64.
  • No new numbers are published until they are measured on Linux with a proper clock and a large sample.
ARCHITECTURE
  • In-process exchange: order submission crosses zero kernel boundaries via an SPSC order queue with a real consumer cursor; a full queue drops and counts the order instead of overwriting.
  • Tick delivery through a newest-value ring with a per-slot seqlock (all atomics): the strategy copies the latest tick and retries torn reads, and skipped ticks and retries are counted, so the ingestor never blocks.
  • ARM64 NEON inline assembly: the 8-price window stays in two NEON registers across ticks (compiler-chosen via inout(vreg)); 9 instructions per tick (FMAX/FMAXV breakout, EXT shift, FCMGT), with zero window memory accesses between ticks.
  • x86_64 AVX2 equivalent: the window lives in a single __m256 (ymm) register, shifted with vextractf128/vpalignr/vinsertf128, reduced with vmaxps/vpermilps/vmaxss and compared branchlessly via vucomiss + seta; 15 instructions per tick.
  • Linux scheduling: SCHED_FIFO priority 50 and a sched_setaffinity pin through raw syscalls with separate x86_64 (syscall) and aarch64 (svc #0) paths; return codes are checked and a missing CAP_SYS_NICE is reported. On Apple Silicon the kernel rejects the affinity hint, so macOS relies on QOS_USER_INTERACTIVE only.
  • Cache-line-aligned structs (#[repr(C, align(64))]) against false sharing; every unsafe block carries a SAFETY comment, enforced by clippy in CI. Page pre-touch, SIMD warmup and warmup packets keep page faults and branch-predictor noise out of measurements.
LIVE MARKET DATA
  • kraken-feed adapter streams real Kraken trades into the engine over hand-rolled WebSocket: handshake (own SHA-1 + base64), RFC6455 framing with client masking, ping/pong. TLS is terminated by a local stunnel, which keeps the crate dependency-free.
  • Measures network transit (RTT/2 from ping/pong) alongside the engine's signal latency, showing milliseconds in flight against sub-microsecond engine reaction.
  • Kraken Futures mode streams the public perpetual ticker (e.g. PF_XBTUSD) with real bid/ask, mark price, and funding rate, packed into a 49-byte v4 packet that stays backward-compatible with the spot format.
  • Record, deterministic replay, synthetic captures, and a REST historical-trade collector for multi-hour backtests.
TRADING MODEL & BACKTESTING
  • Perpetual cost stack: spread-crossing fills (buy the ask, sell the bid), optional slippage, per-hour funding accrual, mark-price liquidation, and volatility-targeted sizing bounded by margin capacity. Each cost is broken out in the scorecard.
  • Scorecard reports net P&L, profit factor, max drawdown, Sharpe, Sortino, Calmar, time-in-drawdown, turnover, and a long/short split.
  • One AlphaModel shared by live and backtest paths. --backtest ranks a parameter grid on an anchored walk-forward over the first 80% of a capture, then scores only the chosen config, once, on an untouched final 20%; the overfit flag is walk-forward efficiency below 0.5.
  • Learned policy: a 6 → 8 → 1 MLP (65 weights, 260 bytes, L1-resident, branchless inference) trained by the gradient-free cross-entropy method, still zero dependencies, on a 70/30 time-split holdout (its overfit warning is a sign check).
  • Honest result (synthetic backtest, not live trading): on the trending synth, micro mean-reversion has negative edge even before fees (gross −4.2 bps/trade, 38% hit rate), deepening to −9.4 bps net with a ~5 bps round-trip fee. Trend-following starts slightly positive gross (+0.2 bps/trade), but realistic fees sink both.
  • Walk-forward verdict on the default synthetic capture: no demonstrated edge. With taker fees the chosen config loses 0.35% on the holdout; with a maker rebate and z-scored signals the best configs are slightly positive in walk-forward but the chosen one is still -0.02% on the holdout.