02 / 06 Systems Programming

Aquila: quantitative backtesting framework

An event-driven trading simulator that replays historical market data with nanosecond precision, removing OOP overhead to push single-thread performance to the hardware limit.

aquila.backtesting
Aquila dashboard

Exploring Low-Latency Systems

I built Aquila because I wanted to understand exactly how high-frequency, low-latency systems work under the hood. I wanted to see what it takes to strip away overhead, avoid the kernel, and push single-threaded C++ performance to the absolute hardware limit.

The result is a highly optimized pipeline designed to minimize CPU cache misses, kernel involvement, and memory allocation overhead during market data processing.

  • ✦SMA Crossover: Uses a sliding window with running sums to compute the Simple Moving Average without O(N) recalculations.
  • ✦Bollinger Bands: Implements Welford's online algorithm for numerically stable, O(1) rolling standard deviation variance updates.

HFT-Grade System Primitives

To achieve nanosecond precision, the engine completely bypasses standard heap allocations during the hot path by utilizing a custom std::pmr::unsynchronized_pool_resource (EventArena).

Instead of virtual functions which require vtable lookups and prevent inlining, the engine leverages Static Dispatch (CRTP) for strategy classes. Market data is explicitly packed using a Structure of Arrays (SoA) layout rather than AoS, perfectly optimizing it for SIMD vectorization (AVX2) and eliminating CPU cache misses.

  • ✦Lock-Free Event Bus: SPSC ring buffer with acquire/release fences to eliminate mutexes and false sharing.
  • ✦Zero-Copy File I/O: Uses OS-level memory mapping (mmap) to parse CSV data directly from the OS page cache.
  • ✦Hardware Timing: Utilizes __rdtsc with _mm_lfence for micro-benchmarking latency at the 3-nanosecond level.
  • ✦CPU Core Pinning: Prevents OS thread migration and L1/L2 cache invalidation.

Realistic Execution & Reporting

The execution simulator actively models real-world constraints such as slippage (in basis points) and commission rates. It computes advanced performance metrics including Sharpe Ratio, Sortino Ratio, Max Drawdown (MDD), and Profit Factor.

When a backtest finishes, Aquila instantly exports a report.json. This file can simply be dragged and dropped into the dedicated front-end application to visualize the backtest performance.

  • ✦Framework-less UI: Built with raw HTML/CSS/JS using a premium dark-mode aesthetic and glassmorphism.
  • ✦Custom Canvas Rendering: An HTML5 Canvas plots the equity curve with buy/sell points directly overlaid.
  • ✦Latency Profiling: Visualizes Time Stamp Counter (TSC) latency data (mean, p50, p95, p99).
C++23 CRTP std::pmr Lock-free queues Zero-copy Hardware TSC SPSC ring buffer