Add Count property to derivative security filter universes - #9605
Merged
jhonabreul merged 1 commit intoJul 10, 2026
Merged
Conversation
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()
Martin-Molinero
approved these changes
Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds an
int Countproperty toIDerivativeSecurityFilterUniverse<T>and implements it in all implementations:ContractSecurityFilterUniverse<T, TData>(base ofOptionFilterUniverseandFutureFilterUniverse) returns the number of contracts currently in the universe, reflecting any filters already applied.EmptyContractFilter<T>'s internal empty universe returns0.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 publicCountproperty:Related Issue
N/A
Motivation and Context
Calling
len()on anOptionFilterUniverse/FutureFilterUniversefrom a Python algorithm's contract filter raisedTypeError: 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'sCount().Requires Documentation Change
No.
How Has This Been Tested?
OptionFilterUniverseTestsandFutureFilterTests:PythonLenReturnsTheNumberOfContractsInTheUniverse: assertslen()works from Python and reflects applied filters. These tests reproduce theTypeErrorand fail without this change.CountReturnsTheNumberOfContractsInTheUniverseandCountIsUpdatedAsFiltersReduceTheUniverse: assertCountreturns the universe size and is updated as chained filters (Strikes/Expiration/FrontMonth) reduce the universe.EmptyContractFilterUniverseCountIsZero: asserts theEmptyContractFilteruniverse reportsCount == 0.OptionFilterUniverseTests,OptionFilterTests,OptionStrategyFilterTestsandFutureFilterTestsfixtures pass.BasicTemplateOptionsAlgorithm.pywith alen(filtered)call in the option filter through the Lean launcher.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>