Skip to main content

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

Trade Statistics

Volume Statistics

Drawdown Metrics

Constructor

new

Creates a new empty metrics instance.

Calculated Metrics

win_rate

Calculates the win rate as a percentage.
Returns: Win rate between 0.0 and 1.0 Formula: winning_trades / num_trades Example:

profit_factor

Calculates the profit factor (gross profit / gross loss).
Returns: Profit factor (infinity if no losing trades) Interpretation:
  • 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.
Returns: Maker ratio between 0.0 and 1.0 Interpretation: Higher values indicate better liquidity provision (lower fees)

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

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.
Parameters:
  • initial_capital: Starting capital for return calculations
  • max_drawdown: Maximum drawdown percentage from metrics
Calculates:
  • 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