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 innano-backtest/src/events.rs:11:
Event Structure
Fromnano-backtest/src/events.rs:79:
sequence field ensures deterministic ordering when multiple events occur at the exact same nanosecond:
Event Queue
Implemented as a binary min-heap innano-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 innano-backtest/src/latency.rs:54:
Jitter Models
Fromnano-backtest/src/latency.rs:26:
Latency Calculation
Colo Latency Models
Pre-configured for common colo facilities:Event Processing Loop
Fromnano-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
- Always use event queue - Don’t process events directly
- Include latency simulation - Realistic backtests require realistic latency
- Pre-allocate capacity - Avoid reallocations during backtest
- Use sequence numbers - Ensure deterministic ordering
- Timestamp everything - Critical for analysis and debugging
Debugging Events
Related Topics
- Order Books - Market data events update order books
- Strategies - Strategies respond to events
- Price Types - Timestamp type for event ordering