engine
The backtesting layer. Runs strategies inside a simulated exchange with configurable matching, observation filtering, and multi-agent support.
What is currently available
Core engine
The Engine orchestrates the simulation loop:
- Advance time (data source produces new
MarketState). - Build observations per agent (via
ObservationFilter). - Strategies emit
OrderRequests. - Exchange resolves matches and updates portfolios.
- Data source receives aggregate impact.
Supports single-agent and multi-agent configurations with per-agent cash, transaction costs, and observation filters.
Data sources
Simulated (SimulatedDataSource):
- Wraps any
Simulatableprocess frommarket_model. - Generates BBO quotes from the process state.
- Supports pre-calculation for zero-impact scenarios (fast path).
- On-the-fly calculation with market impact feedback.
Historical (ParquetDataSource):
- Replays recorded market data from Parquet files.
- Used to backtest strategies on real exchange data.
Exchange (Exchange)
- L1 (BBO-only) exchange with infinite liquidity at top-of-book.
- Per-agent portfolio isolation via
HashMap<AgentId, Portfolio>. - Per-agent
CancelAll(cancels only that agent's resting orders). - Per-agent fill history and last-step fill snapshots.
- Transaction cost deduction per fill.
Matchers
Simple (SimpleMatcher):
- Deterministic immediate fill at BBO.
Stochastic (StochasticMatcher):
- Three-pronged fill evaluation per the Avellaneda-Stoikov model:
- Aggressive crossing (limit price crosses BBO).
- Deterministic mid-price sweep (mid passed through the limit).
- Probabilistic Poisson arrival at distance from mid.
Strategies
All strategy implementations for the engine:
| Strategy | Description |
|---|---|
AvellanedaStoikovStrategy | AS optimal quotes, optional vol estimation and real-vol passthrough |
AvellanedaStoikovHestonStrategy | AS variant with Heston volatility dynamics |
AvellanedaStoikovHawkesStrategy | AS variant with Hawkes intensity dynamics |
AvellanedaStoikovBilateralHawkesStrategy | AS variant with bilateral Hawkes intensities |
AvellanedaStoikovBilateralHawkesOrderFlowImbalanceStrategy | AS variant with OFI signal |
AvellanedaStoikovExactStrategy | AS with exact (non-approximate) solution |
ConstantSymmetricStrategy | Fixed symmetric spread around mid |
ZeroIntelligenceStrategy | Random orders |
RandomStrategy | Random bid/ask placement |
ExternalStrategy | Load strategy parameters from external config |
Backtesting
run_backtest produces per-path metrics:
- Total return, annualized return, volatility.
- Sharpe and Sortino ratios.
- Max drawdown, final equity.
- Mean, max, and min inventory.
- Adverse selection (bps) and realized edge (bps).
- PnL decomposition (spread component, directional component).
- Fill counts, mean hold time.
- Terminal liquidation cost.
BacktestSummary aggregates statistics across multiple Monte Carlo runs.
Vectorized environment (VecEnv)
For RL training:
- Runs hundreds of independent Heston + AS engine instances in parallel.
- Configurable reward: PnL or differential Sharpe ratio.
- Returns states, rewards, dones, equities, and positions.
- Fixed configuration via
VecEnvConfigwith defaults.
Python bindings (optional)
When compiled with --features python, the engine exposes strategies and
backtesting via PyO3. See engine/pyproject.toml.
What is not yet available
- L2 or L3 exchange (aggregated price levels, FIFO order-by-order matching).
- Queue priority mechanics.
- Latency profiles per agent (all agents see state simultaneously).
- Transaction fees / maker-taker fee structure.
- Multi-asset / cross-asset data sources.
- Hawkes-process data source in engine (only GBM, Heston, Bates).
- Exogenous signal feeds for agents.
- Endogenous market impact from aggregate fills.
- HJB-solver-driven strategies (optimal policies not yet wired as
Strategy).