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:

  1. Advance time (data source produces new MarketState).
  2. Build observations per agent (via ObservationFilter).
  3. Strategies emit OrderRequests.
  4. Exchange resolves matches and updates portfolios.
  5. 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 Simulatable process from market_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:
    1. Aggressive crossing (limit price crosses BBO).
    2. Deterministic mid-price sweep (mid passed through the limit).
    3. Probabilistic Poisson arrival at distance from mid.

Strategies

All strategy implementations for the engine:

StrategyDescription
AvellanedaStoikovStrategyAS optimal quotes, optional vol estimation and real-vol passthrough
AvellanedaStoikovHestonStrategyAS variant with Heston volatility dynamics
AvellanedaStoikovHawkesStrategyAS variant with Hawkes intensity dynamics
AvellanedaStoikovBilateralHawkesStrategyAS variant with bilateral Hawkes intensities
AvellanedaStoikovBilateralHawkesOrderFlowImbalanceStrategyAS variant with OFI signal
AvellanedaStoikovExactStrategyAS with exact (non-approximate) solution
ConstantSymmetricStrategyFixed symmetric spread around mid
ZeroIntelligenceStrategyRandom orders
RandomStrategyRandom bid/ask placement
ExternalStrategyLoad 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 VecEnvConfig with 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).