Installation
Install HftBacktest using pip:Quick Start
Here’s a minimal example of a market-making strategy:Core Concepts
Backtesting Workflow
- Configure Assets: Set up one or more trading assets with market data and latency models
- Create Backtester: Initialize a backtester instance (HashMap or ROIVector)
- Implement Strategy: Write your strategy as a Numba JIT-compiled function
- Run Backtest: Execute the strategy through the backtester
- Analyze Results: Use the statistics module to evaluate performance
Market Depth Models
HftBacktest provides two market depth implementations:- HashMapMarketDepth: Uses a hashmap for efficient sparse order book storage
- ROIVectorMarketDepth: Uses a vector for dense order book storage within a range of interest
Numba Integration
Strategies run in Numba JIT-compiled functions for maximum performance. All core API methods are compatible with Numba’s@njit decorator.
API Organization
The Python API is organized into the following modules:- Main Classes: Core backtesting classes and market depth interfaces
- Types: Type definitions, constants, and data structures
- Data: Data loading, validation, and preparation utilities
- Stats: Performance metrics and statistical analysis
Time Units
By default, HftBacktest uses nanoseconds for all timestamps and durations. Ensure your data uses the same time unit, or adjust accordingly.Order Management
Order IDs
Each order must have a unique ID. There should not be any existing order with the same ID on both local and exchange sides.Order Status
Orders progress through states:NEW → FILLED / CANCELED / EXPIRED
Time-In-Force Options
GTC: Good ‘till cancelGTX: Post-only (maker-only)FOK: Fill or killIOC: Immediate or cancel
Asset Types
When configuring assets, specify the contract type:- Linear assets: Use
.linear_asset(contract_size)for standard futures/spot - Inverse assets: Use
.inverse_asset(contract_size)for inverse perpetuals
Next Steps
- Explore the HftBacktest class documentation
- Learn about data types and constants
- Review data preparation utilities
- Understand performance metrics