Overview
HftBacktest implements a complete tick-by-tick backtesting approach that processes every market event in chronological order. Unlike traditional backtesting frameworks that operate on OHLCV bars, HftBacktest replays the market exactly as it occurred, event by event.Event-Driven Architecture
The backtesting engine operates on an event-driven model where all market activity is represented as discrete events:Event Timeline
The backtester maintains separate event streams for each asset and coordinates them using anEventSet that tracks timestamps:
Event Collection
All events (feed data, order responses, executions) are collected with their timestamps
Timestamp Ordering
The
EventSet identifies the next event with the earliest timestamp across all assets and processorsLocal vs Exchange Processors
HftBacktest separates the simulation into two processors to accurately model the distributed nature of trading:Local Processor
Represents your trading system:- Sees market feed data at
local_ts(after feed latency) - Submits orders at the current simulation time
- Receives order responses at
local_ts(after round-trip latency) - Maintains your view of the order book
Exchange Processor
Represents the exchange:- Processes events at
exch_ts(exchange timestamp) - Receives orders at
exch_ts + entry_latency - Simulates order fills based on queue position
- Sends responses back at
exch_ts + entry_latency + response_latency
hftbacktest/src/backtest/mod.rs:673-863 ensures accurate latency modeling.
Time Control
You control simulation time through several methods:elapse()
Advance time by a fixed duration
wait_next_feed()
Wait for the next market feed event
wait_order_response()
Wait for a specific order response
goto_end()
Process all remaining events
Tick-by-Tick Accuracy
The simulation processes every event in the feed data:- Depth Updates
- Trade Events
- Order Events
Every Level-2 order book update is applied:
Performance Optimization
Despite processing every tick, HftBacktest maintains high performance:Numba JIT Compilation
Numba JIT Compilation
Python strategies are compiled to machine code via Numba, achieving near-native performance:
Rust Implementation
Rust Implementation
The core backtesting engine is written in Rust for maximum performance. Event processing in
mod.rs:755-863 uses unsafe optimizations for zero-cost abstractions.Parallel Data Loading
Parallel Data Loading
Data files are loaded in parallel while backtesting runs:
Efficient Data Structures
Efficient Data Structures
HashMapfor fast order lookupVecDequefor FIFO queue management- Cache-aligned arrays for event processing
Why Tick-by-Tick Matters
For high-frequency strategies, bar-based backtesting is insufficient:Tick-by-tick advantages:
- Accurate queue position simulation
- Real order book reconstruction
- Proper latency accounting
- Realistic fill simulation
- Validates actual live trading results
Example: Event Processing Flow
Here’s how a typical market making strategy processes events:Related Topics
Latency Modeling
How feed and order latencies are simulated
Order Book
Level-2 and Level-3 order book reconstruction
Queue Position
Order fill simulation using queue models
Multi-Asset
Backtesting across multiple assets and exchanges