Skip to content

Allow setting summary statistics during Initialize - #9855

Merged
Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Martin-Molinero:bug-9853-summary-statistic-in-initialize
Oct 1, 2026
Merged

Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Martin-Molinero:bug-9853-summary-statistic-in-initialize

Conversation

@Martin-Molinero

Copy link
Copy Markdown
Member

Description

QCAlgorithm.SetSummaryStatistic now keeps statistics set before the statistics service is attached, and SetStatisticsService applies them once the result handler provides the service. Previously the call dereferenced a null _statisticsService.

Related Issue

Closes #9853

Motivation and Context

The statistics service is attached in the result handler's SetAlgorithm, which Engine.Run calls after Setup.Setup(...) has already executed Initialize(), so calling SetSummaryStatistic / set_summary_statistic from Initialize failed with Object reference not set to an instance of an object.

Requires Documentation Change

No

How Has This Been Tested?

Extended the StatisticsResultsAlgorithm regression (C# and Python) to set a summary statistic in Initialize and assert it is present in Statistics.Summary at OnEndOfAlgorithm. Both variants fail with the reported null reference without the fix and pass with it.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

🤖 Generated with Claude Code

SetSummaryStatistic threw a null reference when called from Initialize
because the statistics service is attached after setup runs. Keep the
statistics set before the service is available and apply them once it's
attached.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Martin-Molinero
Martin-Molinero merged commit 0a9e3a8 into QuantConnect:master Oct 1, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SetSummaryStatistic throws a null reference when called from Initialize

1 participant