Skip to content

Support SetBenchmark(null) to disable the benchmark - #9856

Merged
Martin-Molinero merged 1 commit into
masterfrom
bug-9854-set-benchmark-null
Oct 1, 2026
Merged

Martin-Molinero merged 1 commit into
masterfrom
bug-9854-set-benchmark-null

Conversation

@Martin-Molinero

Copy link
Copy Markdown
Member

Description

QCAlgorithm.SetBenchmark(Symbol) now sets a constant zero benchmark (FuncBenchmark(_ => 0)) when the symbol is null, instead of creating a security benchmark. Since the benchmark is then set, the brokerage model default is not loaded in PostInitialize and no benchmark security is added.

Related Issue

Closes #9854

Motivation and Context

set_benchmark(None) binds to SetBenchmark(Symbol symbol), which passed the null symbol to SecurityBenchmark.CreateInstance and failed with Object reference not set to an instance of an object. This gives users a direct way to disable the benchmark instead of the set_benchmark(lambda x: 0) workaround. From C#, SetBenchmark((Symbol)null) behaves the same.

Requires Documentation Change

No

How Has This Been Tested?

Added AlgorithmBenchmarkTests.NullBenchmarkDisablesTheBenchmark for C# (SetBenchmark((Symbol)null)) and Python (algorithm.set_benchmark(None) called from a Python module). It asserts that after PostInitialize the benchmark is a FuncBenchmark evaluating to 0 and that no securities were added. Both cases 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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Martin-Molinero
Martin-Molinero merged commit 33e3945 into master Oct 1, 2026
15 of 16 checks passed
@Martin-Molinero
Martin-Molinero deleted the bug-9854-set-benchmark-null branch October 1, 2026 14:23
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.

Support SetBenchmark(null) to disable the benchmark

1 participant