backtrader.reports.performance module

Performance metrics calculator.

Extracts and calculates all performance metrics from strategies and analyzers.

class backtrader.reports.performance.PerformanceCalculator[source]

Bases: object

Unified performance metrics calculator.

Extracts and calculates all performance metrics from strategies and analyzers, including: - PnL metrics: total return, annual return, cumulative return - Risk metrics: max drawdown, Sharpe ratio, SQN, Calmar ratio - Trade statistics: win rate, profit/loss ratio, average profit/loss

strategy

Strategy instance

Usage example:

calc = PerformanceCalculator(strategy) metrics = calc.get_all_metrics() print(f”Sharpe ratio: {metrics[‘sharpe_ratio’]}”) print(f”SQN rating: {metrics[‘sqn_human’]}”)

__init__(strategy)[source]

Initialize the performance calculator.

Parameters:

strategy – backtrader strategy instance (result from run())

get_all_metrics()[source]

Return dictionary of all performance metrics.

Returns:

Dictionary containing all performance metrics

Return type:

dict

get_pnl_metrics()[source]

Get profit and loss related metrics.

Returns:

PnL metrics dictionary

Return type:

dict

get_risk_metrics()[source]

Get risk-related metrics.

Returns:

Risk metrics dictionary

Return type:

dict

get_trade_metrics()[source]

Get trade statistics metrics.

Returns:

Trade statistics dictionary

Return type:

dict

get_kpi_metrics()[source]

Get key performance indicators.

Returns:

KPI metrics dictionary

Return type:

dict

get_equity_curve()[source]

Get equity curve data.

Returns:

(dates, values) Lists of dates and equity values

Return type:

tuple

get_buynhold_curve()[source]

Get buy-and-hold comparison curve.

Returns:

(dates, values) Lists of dates and buy-and-hold values

Return type:

tuple

static sqn_to_rating(sqn_score)[source]

Convert SQN score to human-readable rating.

Reference: http://www.vantharp.com/tharp-concepts/sqn.asp

Parameters:

sqn_score – SQN score

Returns:

Human-readable rating

Return type:

str

get_strategy_info()[source]

Get strategy information.

Returns:

Strategy information dictionary

Return type:

dict

get_data_info()[source]

Get data information.

Returns:

Data information dictionary

Return type:

dict