Skip to main content

Overview

NanoARB uses an event-driven architecture for both backtesting and live trading. All market data updates, order lifecycle events, and timer callbacks flow through a priority queue ordered by timestamp, ensuring deterministic replay and accurate latency simulation.

Event Types

Defined in nano-backtest/src/events.rs:11:

Event Structure

From nano-backtest/src/events.rs:79:
The sequence field ensures deterministic ordering when multiple events occur at the exact same nanosecond:

Event Queue

Implemented as a binary min-heap in nano-backtest/src/events.rs:187:

Queue Complexity

  • Push: O(log n)
  • Pop: O(log n)
  • Peek: O(1)
  • Memory: O(n) where n = pending events

Event Flow

Scheduling Events

The event queue provides helper methods:

Latency Simulation

Implemented in nano-backtest/src/latency.rs:54:

Jitter Models

From nano-backtest/src/latency.rs:26:

Latency Calculation

Colo Latency Models

Pre-configured for common colo facilities:

Event Processing Loop

From nano-backtest/src/engine.rs:118:

Timing Considerations

Market Data Timestamps

Market data has multiple timestamps:

Order Timestamps

Fill Timestamps

Event Ordering Example

Same Timestamp Ordering

When events occur at the same nanosecond, sequence numbers determine order:

Performance Characteristics

Event Processing Rate

  • Typical: 1-5 million events/second
  • With complex strategies: 500K-1M events/second
  • Bottleneck: Strategy logic, not event queue

Memory Usage

  • Each Event: ~64 bytes
  • 1M pending events: ~64 MB
  • Queue pre-allocates capacity to avoid reallocations

Latency Distribution

Typical HFT latencies:

Best Practices

  1. Always use event queue - Don’t process events directly
  2. Include latency simulation - Realistic backtests require realistic latency
  3. Pre-allocate capacity - Avoid reallocations during backtest
  4. Use sequence numbers - Ensure deterministic ordering
  5. Timestamp everything - Critical for analysis and debugging

Debugging Events