Skip to content

Add Count property to derivative security filter universes - #9605

Merged
jhonabreul merged 1 commit into
QuantConnect:masterfrom
jhonabreul:feature-derivative-filter-universe-count
Jul 10, 2026
Merged

Add Count property to derivative security filter universes#9605
jhonabreul merged 1 commit into
QuantConnect:masterfrom
jhonabreul:feature-derivative-filter-universe-count

Conversation

@jhonabreul

Copy link
Copy Markdown
Collaborator

Description

Adds an int Count property to IDerivativeSecurityFilterUniverse<T> and implements it in all implementations:

  • ContractSecurityFilterUniverse<T, TData> (base of OptionFilterUniverse and FutureFilterUniverse) returns the number of contracts currently in the universe, reflecting any filters already applied.
  • EmptyContractFilter<T>'s internal empty universe returns 0.

Besides making the contract count directly accessible from C#, this enables len() on filter universes in Python algorithms, since Python.NET assigns __len__ to enumerable CLR types that expose a public Count property:

def initialize(self):
    option = self.add_option("GOOG")
    option.set_filter(self.filter)

def filter(self, universe):
    filtered = universe.strikes(-2, +2).expiration(0, 180)
    self.log(f"selected={len(filtered)}")  # previously: TypeError: object of type 'OptionFilterUniverse' has no len()
    return filtered

Related Issue

N/A

Motivation and Context

Calling len() on an OptionFilterUniverse/FutureFilterUniverse from a Python algorithm's contract filter raised TypeError: object of type 'OptionFilterUniverse' has no len(), forcing users to enumerate the universe just to count contracts. There was also no direct way to get the contract count from C# without LINQ's Count().

Requires Documentation Change

No.

How Has This Been Tested?

  • New unit tests in OptionFilterUniverseTests and FutureFilterTests:
    • PythonLenReturnsTheNumberOfContractsInTheUniverse: asserts len() works from Python and reflects applied filters. These tests reproduce the TypeError and fail without this change.
    • CountReturnsTheNumberOfContractsInTheUniverse and CountIsUpdatedAsFiltersReduceTheUniverse: assert Count returns the universe size and is updated as chained filters (Strikes/Expiration/FrontMonth) reduce the universe.
    • EmptyContractFilterUniverseCountIsZero: asserts the EmptyContractFilter universe reports Count == 0.
  • Full OptionFilterUniverseTests, OptionFilterTests, OptionStrategyFilterTests and FutureFilterTests fixtures pass.
  • Manually verified end-to-end by running BasicTemplateOptionsAlgorithm.py with a len(filtered) call in the option filter through the Lean launcher.

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>

Exposes the number of contracts in IDerivativeSecurityFilterUniverse
implementations. This also enables len() on OptionFilterUniverse and
FutureFilterUniverse instances in Python algorithms, which previously
raised TypeError: object of type 'OptionFilterUniverse' has no len()
@jhonabreul
jhonabreul merged commit 73ae785 into QuantConnect:master Jul 10, 2026
7 of 8 checks passed
@jhonabreul
jhonabreul deleted the feature-derivative-filter-universe-count branch July 10, 2026 20:20
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.

2 participants