Overview
The metrics module provides comprehensive performance tracking for backtests, including P&L metrics, trade statistics, drawdown analysis, and risk-adjusted returns. Source:nano-backtest/src/metrics.rs
BacktestMetrics
Core performance metrics collected during backtest execution.Key Metrics
P&L Metrics
| Field | Description |
|---|---|
total_pnl | Total P&L (realized + unrealized) |
realized_pnl | P&L from closed positions |
unrealized_pnl | P&L from open positions |
total_fees | Total fees paid |
Trade Statistics
| Field | Description |
|---|---|
num_trades | Total number of round-trip trades |
winning_trades | Number of profitable trades |
losing_trades | Number of losing trades |
gross_profit | Sum of all winning trade P&Ls |
gross_loss | Sum of all losing trade P&Ls (absolute) |
Volume Statistics
| Field | Description |
|---|---|
total_volume | Total contracts traded |
buy_fills | Number of buy-side fills |
sell_fills | Number of sell-side fills |
maker_fills | Number of maker fills (liquidity providing) |
taker_fills | Number of taker fills (liquidity taking) |
Drawdown Metrics
| Field | Description |
|---|---|
max_drawdown_pct | Maximum drawdown as percentage of peak |
max_drawdown_abs | Maximum drawdown in absolute dollars |
peak_pnl | Highest P&L reached during backtest |
Constructor
new
Creates a new empty metrics instance.
Calculated Metrics
win_rate
Calculates the win rate as a percentage.
winning_trades / num_trades
Example:
profit_factor
Calculates the profit factor (gross profit / gross loss).
-
1.0: Profitable strategy
- < 1.0: Losing strategy
- 2.0+: Strong strategy
avg_trade_pnl
Calculates average P&L per trade.
avg_winning_trade
Calculates average winning trade P&L.
avg_losing_trade
Calculates average losing trade P&L.
maker_ratio
Calculates the ratio of maker fills to total fills.
Recording Methods
record_fill
Records a fill for metrics tracking.
record_trade
Records a completed round-trip trade.
update_pnl
Updates P&L tracking and drawdown calculation.
Time Methods
duration_secs
Returns the backtest duration in seconds.
PerformanceStats
Detailed performance statistics including risk-adjusted returns.Fields
| Field | Description |
|---|---|
daily_returns | Daily return series |
equity_curve | Cumulative P&L over time |
equity_timestamps | Timestamps for equity curve points |
trade_pnls | P&L for each individual trade |
sharpe_ratio | Annualized Sharpe ratio |
sortino_ratio | Annualized Sortino ratio (downside-only) |
calmar_ratio | Annual return / max drawdown |
max_consecutive_wins | Longest winning streak |
max_consecutive_losses | Longest losing streak |
recovery_factor | Total return / max drawdown |
Constructor
new
Creates a new empty statistics instance.
Data Recording
add_equity_point
Adds a point to the equity curve.
add_daily_return
Adds a daily return observation.
add_trade_pnl
Adds a trade P&L observation.
Calculation
calculate
Calculates all statistics from recorded data.
initial_capital: Starting capital for return calculationsmax_drawdown: Maximum drawdown percentage from metrics
- Sharpe ratio (annualized, assuming 252 trading days)
- Sortino ratio (using downside deviation only)
- Calmar ratio (annual return / max drawdown)
- Consecutive win/loss streaks
- Recovery factor
Risk-Adjusted Returns
Sharpe Ratio
Measures excess return per unit of total volatility. Formula:(mean_daily_return / std_daily_return) * sqrt(252)
Interpretation:
- < 1.0: Poor risk-adjusted returns
- 1.0-2.0: Good
- 2.0-3.0: Very good
-
3.0: Excellent (rare)
Sortino Ratio
Measures excess return per unit of downside volatility (only negative returns). Formula:(mean_daily_return / downside_std) * sqrt(252)
Interpretation: Similar to Sharpe but more relevant for asymmetric strategies
Calmar Ratio
Measures annualized return relative to maximum drawdown. Formula:annual_return / max_drawdown_pct
Interpretation:
-
1.0: Return exceeds max drawdown
-
3.0: Strong risk-adjusted performance
RollingStats
Efficient rolling window statistics calculator.Constructor
new
Creates a new rolling statistics calculator.
Methods
add
Adds a value to the rolling window.
mean
Returns the rolling mean.
variance
Returns the rolling variance.
std_dev
Returns the rolling standard deviation.
sharpe
Returns the rolling Sharpe ratio (not annualized).
Usage Example
Complete Example
See Also
- BacktestEngine - Running backtests
- BacktestConfig - Configuration options