Skip to content

Add DeregisterAll and automatic indicator deregistration on security removal - #9673

Draft
jhonabreul wants to merge 2 commits into
QuantConnect:masterfrom
jhonabreul:feature-deregister-all-security-cleanup
Draft

Add DeregisterAll and automatic indicator deregistration on security removal#9673
jhonabreul wants to merge 2 commits into
QuantConnect:masterfrom
jhonabreul:feature-deregister-all-security-cleanup

Conversation

@jhonabreul

@jhonabreul jhonabreul commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Description

Universe churn leaks helper-created indicators and consolidators: when a security leaves the universe, consolidators created by the indicator helpers are never disposed and keep being re-scanned forever. Algorithms with per-symbol state and add/remove churn accumulate them until an OutOfMemoryException hours into a run.

The change:

  • QCAlgorithm.DeregisterAll(Symbol) — one-line cleanup of everything the helpers created for a symbol. RegisterConsolidator (the choke point all helpers funnel through) now tracks indicators/consolidators per symbol; DeregisterAll disposes them via the existing paths.
  • Multi-symbol indicators (e.g. B(target, reference)) are fully deregistered through any of their symbols.
  • Consolidators added directly via SubscriptionManager.AddConsolidator are intentionally not tracked, so they survive universe removals.
  • Settings.AutomaticIndicatorDeregistration (default false) — opt-in automatic DeregisterAll when a security is removed from the algorithm.
  • Fixes a leak in SubscriptionManager.RemoveConsolidator: the scan wrapper was only disposed while the symbol still had subscription configs, so post-universe-removal cleanup left it in the scan queue forever. It is now disposed unconditionally.

Also audited: a user-state slot on Security already exists (security["my_state"] = obj via DynamicObject) — documentation gap, no engine work. Split-safe re-warm of selection indicators is deferred: there is no engine-side seam to user filter functions, and feeding adjusted prices avoids the problem.

Related Issue

N/A

Motivation and Context

Per-symbol cleanup on universe removal takes a hand-rolled loop today, and getting it wrong is invisible until an OOM. A one-line (or automatic) cleanup makes the correct pattern the easy one.

Requires Documentation Change

Yes: DeregisterAll and Settings.AutomaticIndicatorDeregistration, next to DeregisterIndicator and the universe cleanup examples.

How Has This Been Tested?

  • New AlgorithmDeregisterAllTests (7 tests): target-symbol-only disposal, multi-symbol indicators, no-op after manual deregistration, raw AddConsolidator consolidators kept, automatic deregistration on removal iff the setting is on.
  • New SubscriptionManagerTests case reproducing the wrapper leak: red before the fix, green after.
  • New DeregisterAllRegressionAlgorithm (C# + Python) and AutomaticIndicatorDeregistrationRegressionAlgorithm (C#): add/remove churn, cleanup asserted, fresh indicators work after re-adding.
  • Related suites green: 472 passed, 0 failed; full Tests.Algorithm namespace with CI filters.

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>

…removal

- QCAlgorithm.DeregisterAll(symbol) disposes every indicator and consolidator
  created for the symbol through the algorithm helper methods in one call
- Settings.AutomaticIndicatorDeregistration (default false) runs the same
  cleanup automatically when a security is completely removed from the
  algorithm, e.g. when it leaves the universe
- Fix SubscriptionManager.RemoveConsolidator leaking the consolidator scan
  wrapper when the symbol's subscription configs were already removed: the
  wrapper stayed in the scan queue being re-enqueued forever
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.

1 participant