Skip to main content

Overview

The BacktestEngine is the main component for running strategy backtests. It processes market data events, manages order execution, tracks positions, enforces risk limits, and calculates performance metrics. Source: nano-backtest/src/engine.rs

Constructor

new

Creates a new backtest engine with the specified configuration.
Parameters:
  • config: BacktestConfig - Configuration including latency, fees, risk, and execution settings
Returns: A new BacktestEngine instance in the Ready state Example:

State Management

Engine States

The engine progresses through the following states:

state

Returns the current engine state.

Instrument Registration

register_instrument

Registers an instrument for trading in the backtest.
Parameters:
  • instrument: Instrument - The instrument to register
Details:
  • Creates an order book for the instrument
  • Initializes position tracking
  • Required before processing market data for the instrument
Example:

Running the Backtest

run

Runs the complete backtest with a strategy.
Parameters:
  • strategy: &mut S - Mutable reference to a strategy implementing the Strategy trait
Details:
  • Processes all events in the event queue
  • Updates metrics and statistics continuously
  • Stops when queue is empty or risk limits are breached
  • Transitions to Completed or Stopped state
Example:

run_n

Runs the backtest for a limited number of events.
Parameters:
  • strategy: &mut S - Mutable reference to a strategy
  • max_events: usize - Maximum number of events to process
Returns: Number of events actually processed Use case: Step-through debugging or incremental processing

Event Processing

schedule_event

Schedules an event to be processed at a specific timestamp.
Parameters:
  • timestamp: Timestamp - When the event should occur
  • event_type: EventType - Type of event (market data, order submit, fill, etc.)

process_event

Processes a single event from the queue.
Event types handled:
  • MarketData - Triggers strategy’s on_market_data callback
  • OrderSubmit - Submits order to simulated exchange
  • OrderAck - Notifies strategy of order acknowledgment
  • OrderFill - Applies fill to position and notifies strategy
  • OrderCancel - Notifies strategy of cancellation
  • OrderReject - Notifies strategy of rejection
  • EndOfData - Marks backtest completion

Order Book Access

get_book

Gets an immutable reference to an instrument’s order book.

get_book_mut

Gets a mutable reference to an instrument’s order book.

Metrics and Statistics

metrics

Returns reference to performance metrics.
Includes:
  • Total/realized/unrealized P&L
  • Trade counts and win rate
  • Drawdown statistics
  • Volume and fill statistics
See BacktestMetrics for details.

stats

Returns reference to detailed performance statistics.
Includes:
  • Sharpe and Sortino ratios
  • Equity curve
  • Daily returns
  • Consecutive win/loss tracking

positions

Returns reference to the position tracker.

risk

Returns reference to the risk manager.

Status Information

current_time

Returns the current simulation timestamp.

events_processed

Returns the number of events processed.

pending_events

Returns the number of events remaining in the queue.

Reset

reset

Resets the engine to initial state for a new backtest run.
Resets:
  • Event queue
  • Exchange simulator
  • Positions and P&L
  • Metrics and statistics
  • Order books (cleared but not removed)
  • State to Ready

Complete Example

See Also