Overview
HftBacktest supports backtesting strategies that trade multiple assets simultaneously. This enables:- Multi-asset market making
- Cross-exchange arbitrage
- Pairs trading and statistical arbitrage
- Portfolio strategies
- Spread trading (futures calendar spreads, etc.)
Multi-Asset Architecture
TheBacktest struct from backtest/mod.rs:604-1140 manages multiple assets:
EventSet
Maintains event timestamps for all assets and determines which event to process next across the entire portfolio.
Per-Asset Processors
Each asset has its own local and exchange processors with independent:
- Order books
- Order states
- Latency models
- Queue models
Unified Timeline
All assets share the same simulation clock (
cur_ts), ensuring proper chronological event ordering.Independent Configuration
Each asset can use different tick sizes, lot sizes, fee structures, and market depth implementations.
Creating Multi-Asset Backtests
Python API
Rust API
Accessing Assets
Access each asset by its index:Event Coordination
TheEventSet from backtest/evs.rs:24-106 coordinates events across assets:
Multi-Asset Strategies
Pairs Trading
Cross-Exchange Arbitrage
Multi-Asset Market Making
Portfolio State
Each asset maintains independent state:- Position
- Balance
- Orders
- Fees
Cross-Exchange Configuration
When backtesting across exchanges, configure exchange-specific parameters:Different Latencies
Different Latencies
Each exchange has different latency characteristics:
Different Fee Structures
Different Fee Structures
Exchanges have different maker/taker fees:
Different Tick/Lot Sizes
Different Tick/Lot Sizes
Price and quantity increments vary:
Different Queue Models
Different Queue Models
Each exchange has unique matching behavior:
Latency Offset for Cross-Exchange
When data is collected from one location but you’re deploying elsewhere:backtest/mod.rs:188-194:
Performance Considerations
- Event Processing Overhead
- Memory Usage
- Data Loading
More assets = more event streams to coordinate:
- 2 assets: ~1.5x slower than single asset
- 5 assets: ~3x slower
- 10 assets: ~5x slower
EventSet.next() scans all asset timestamps linearly.Validation
Cross-Asset Consistency Checks
Correlation Analysis
Verify asset price relationships:Examples
Official examples:Making Multiple Markets
Market making across multiple instruments simultaneously
High-Frequency Grid Trading Comparison
Comparing the same strategy across different exchanges
Market Making with Alpha - Basis
Using basis between spot and futures as alpha signal
Fusing Depth Data
Combining order books from multiple sources
Common Patterns
Iterate Over All Assets
Asset-Specific Order IDs
Avoid order ID collisions across assets:Clear All Inactive Orders
Related Topics
Backtesting
Event-driven architecture supports multi-asset
Latency
Different latencies per asset
Order Book
Fused order books for cross-exchange strategies