Overview
TheBacktestEngine 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.
config:BacktestConfig- Configuration including latency, fees, risk, and execution settings
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.
instrument:Instrument- The instrument to register
- Creates an order book for the instrument
- Initializes position tracking
- Required before processing market data for the instrument
Running the Backtest
run
Runs the complete backtest with a strategy.
strategy:&mut S- Mutable reference to a strategy implementing theStrategytrait
- Processes all events in the event queue
- Updates metrics and statistics continuously
- Stops when queue is empty or risk limits are breached
- Transitions to
CompletedorStoppedstate
run_n
Runs the backtest for a limited number of events.
strategy:&mut S- Mutable reference to a strategymax_events:usize- Maximum number of events to process
Event Processing
schedule_event
Schedules an event to be processed at a specific timestamp.
timestamp:Timestamp- When the event should occurevent_type:EventType- Type of event (market data, order submit, fill, etc.)
process_event
Processes a single event from the queue.
MarketData- Triggers strategy’son_market_datacallbackOrderSubmit- Submits order to simulated exchangeOrderAck- Notifies strategy of order acknowledgmentOrderFill- Applies fill to position and notifies strategyOrderCancel- Notifies strategy of cancellationOrderReject- Notifies strategy of rejectionEndOfData- 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.
- Total/realized/unrealized P&L
- Trade counts and win rate
- Drawdown statistics
- Volume and fill statistics
stats
Returns reference to detailed performance statistics.
- 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.
- Event queue
- Exchange simulator
- Positions and P&L
- Metrics and statistics
- Order books (cleared but not removed)
- State to
Ready
Complete Example
See Also
- BacktestConfig - Configuration options
- PerformanceMetrics - Performance tracking
- Strategy Trait - Strategy interface