Skip to content

ResultsAnalyzer

QuantConnect.Lean.Engine.Results.Analysis.ResultsAnalyzer

ResultsAnalyzer(
    result: Result,
    algorithm: QCAlgorithm,
    language: Language,
    logs: Sequence[str],
    speed_tracker: AlgorithmSpeedTracker = None,
)
ResultsAnalyzer(
    algorithm: QCAlgorithm,
    language: Language,
    start_time: Union[datetime, date],
    performance_tracking_tool: PerformanceTrackingTool,
    progress_monitor: BacktestProgressMonitor,
)

Bases: Object

Runs the suite of backtest diagnostic tests against a single backtest, in one of two modes depending on how the instance is created: a final analysis instance is created with the completed result and logs, and runs the full analysis set once; an in-run analysis instance is created with the engine's speed counters, is kept alive for the duration of the backtest, and periodically runs the in-run capable analyses incrementally against the intermediate results and logs.

Signature descriptions:

  • Initializes a new instance of the ResultsAnalyzer class for the final analysis of a completed backtest. Use create_for_final_analysis or create_for_in_run_analysis to create instances.

  • Initializes a new instance of the ResultsAnalyzer class for in-run analysis of a backtest still in progress. Use create_for_in_run_analysis to create instances.

Parameters:

Name Type Description Default
result Optional[Result]

The backtest result to analyze.

None
algorithm QCAlgorithm

The algorithm instance used for history requests and settings.

required
language Language

The programming language the algorithm is written in.

required
logs Optional[Sequence[str]]

The full list of log lines produced by the backtest.

None
speed_tracker Optional[AlgorithmSpeedTracker]

The speed metrics tracked for the backtest, or null when speed is not tracked.

None
start_time Optional[Union[datetime, date]]

The UTC time the backtest started, for the speed samples' elapsed time.

None
performance_tracking_tool Optional[PerformanceTrackingTool]

The engine's data point counters, for the speed samples.

None
progress_monitor Optional[BacktestProgressMonitor]

The backtest day-progress monitor, for the speed samples.

None

is_in_run

is_in_run: bool

Whether this instance was created for in-run analysis of a backtest still in progress (see create_for_in_run_analysis), as opposed to the final analysis of a completed backtest.

This Property is protected.

analyses

analyses: Sequence[BaseResultsAnalysis]

The diagnostic analyses to run, in execution order: descending by weight, so changing an analysis weight automatically reorders execution. Created once and reused across runs, since the analyses are stateless and their weights are constant. In-run instances filter the set to the analyses that declare they can run while the backtest is in progress (see BaseResultsAnalysis.runs_in_run).

This Property is protected.

requires_equity_curves

requires_equity_curves: bool

Whether the equity and benchmark curves should be built before running the analyses. Building them requires a benchmark history request, so in-run instances skip it: none of the in-run analyses read the curves, and building them would issue the history request on every run.

This Property is protected.

speed_tracker

speed_tracker: AlgorithmSpeedTracker

The speed metrics tracked for the running backtest, made available to the analyses through ResultsAnalysisRunParameters.speed. An in-run instance owns its tracker and feeds it a sample on each run; the final analysis instance receives the same tracker so the speed analysis also runs against the full-run metrics. Null when speed is not tracked.

This Property is protected.

run

run(
    time_limit_seconds: int = 5,
    max_failed_analyses: int = 10,
) -> Sequence[Analysis]
run(
    result: BacktestResult,
    logs: Sequence[str],
    total_performance: AlgorithmPerformance,
    time_limit_seconds: int = 1,
    max_failed_analyses: int = 10,
) -> Sequence[Analysis]

Signature descriptions:

  • Runs all registered diagnostic checks against the backtest in weight order, stopping early when the time limit or maximum failure count is reached.

  • Runs the in-run analyses against the given intermediate backtest result and the log lines produced since the previous run. The returned findings are the merge of this run's findings into the ones accumulated by previous runs: findings from analyses scanning the order event and log streams are accumulated (first sample kept, counts totaled), while findings from state-based analyses are replaced on every run.

Parameters:

Name Type Description Default
time_limit_seconds int

Wall-clock seconds allowed for the full chain before early exit.

5
max_failed_analyses int

Maximum number of failing analyses to collect before stopping; also the max returned.

10
result Optional[BacktestResult]

The current intermediate backtest result. Its orders and order events

None
logs Optional[Sequence[str]]

The full list of log lines produced so far; the analyzer analyzes the

None
total_performance Optional[AlgorithmPerformance]

The current total algorithm performance, for analyses that read

None

Returns:

Type Description
Sequence[Analysis]

Depends on the signature used. Case 1: [Up to max_failed_analysesQuantConnect.Analysis entries with solutions, ranked by weight.]; Case 2: [The accumulated findings, ranked by analysis weight.]

complete_speed_tracking

complete_speed_tracking() -> AlgorithmSpeedTracker

Completes the speed metrics with one final sample so they cover the backtest through its end, and returns the tracker for the final analysis to reuse. The tracker is left untouched when no sample can be taken, like when the algorithm never left warm-up.

create_for_final_analysis

create_for_final_analysis(
    result: Result,
    algorithm: QCAlgorithm,
    language: Language,
    logs: Sequence[str],
    speed_tracker: AlgorithmSpeedTracker = None,
) -> ResultsAnalyzer

Creates an analyzer for the final analysis of a completed backtest, running the full analysis set once through run(int, int).

Parameters:

Name Type Description Default
result Result

The backtest result to analyze.

required
algorithm QCAlgorithm

The algorithm instance used for history requests and settings.

required
language Language

The programming language the algorithm is written in.

required
logs Sequence[str]

The full list of log lines produced by the backtest.

required
speed_tracker AlgorithmSpeedTracker

The speed metrics tracked for the backtest, typically completed by the in-run analyzer through complete_speed_tracking, or null when speed is not tracked.

None

Returns:

Type Description
ResultsAnalyzer

The final analysis instance.

create_for_in_run_analysis

create_for_in_run_analysis(
    algorithm: QCAlgorithm,
    language: Language,
    start_time: Union[datetime, date],
    performance_tracking_tool: PerformanceTrackingTool,
    progress_monitor: BacktestProgressMonitor,
) -> ResultsAnalyzer

Creates an analyzer for in-run analysis of a backtest still in progress. The instance is expected to be kept alive for the duration of the backtest, sampling the given engine speed counters on each run(BacktestResult, IReadOnlyList{string}, AlgorithmPerformance, int, int) call.

Parameters:

Name Type Description Default
algorithm QCAlgorithm

The algorithm instance used for history requests and settings.

required
language Language

The programming language the algorithm is written in.

required
start_time Union[datetime, date]

The UTC time the backtest started, for the speed samples' elapsed time.

required
performance_tracking_tool PerformanceTrackingTool

The engine's data point counters, for the speed samples.

required
progress_monitor BacktestProgressMonitor

The backtest day-progress monitor, for the speed samples.

required

Returns:

Type Description
ResultsAnalyzer

The in-run analysis instance.

get_analyses

get_analyses() -> Sequence[BaseResultsAnalysis]

Creates the full set of diagnostic analyses to run against the backtest. Each analysis declares through BaseResultsAnalysis.runs_in_run whether it can also run while the backtest is in progress, which in-run instances filter this set by.

This Class is protected.

set_analysis_data

set_analysis_data(
    result: Result, logs: Sequence[str]
) -> None

Sets the backtest data to analyze. Used by in-run instances, which are kept alive and run multiple times against fresh data.

This Class is protected.

Parameters:

Name Type Description Default
result Result

The backtest result to analyze.

required
logs Sequence[str]

The list of log lines to analyze.

required

take_speed_sample

take_speed_sample() -> Optional[AlgorithmSpeedSample]

Takes a sample of the engine speed counters for the algorithm speed analysis. Null while the algorithm warms up, since the warm-up pace would skew the speed metrics.

This Class is protected.