Skip to content

Automatically deregister indicators and consolidators when a security is removed - #9673

Open
jhonabreul wants to merge 3 commits into
QuantConnect:masterfrom
jhonabreul:feature-deregister-all-security-cleanup
Open

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

Conversation

@jhonabreul

@jhonabreul jhonabreul commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When a security leaves the algorithm, the indicators and consolidators created for it through the helper methods (SMA, RSI, Consolidate, RegisterIndicator, ...) are never cleaned up. Their scan wrappers stay in the consolidator scan queue forever, so algorithms with per-symbol indicators and universe churn leak until an OutOfMemoryException.

The change:

  • QCAlgorithm.RegisterConsolidator, the choke point every helper funnels through, now tracks the indicators and consolidators it registers per symbol.
  • On Securities.CollectionChanged removals, the new private QCAlgorithm.DeregisterIndicatorsAndConsolidators(symbol) disposes everything tracked for that symbol through the existing paths.
  • Multi-symbol indicators, e.g. B(target, reference), are fully deregistered when any of their symbols is removed.
  • Consolidators added directly through SubscriptionManager.AddConsolidator are not tracked and are kept.
  • New IAlgorithmSettings.AutomaticIndicatorDeregistration, true by default, turns the cleanup off.
  • SubscriptionManager.RemoveConsolidator now disposes the scan wrapper even when the symbol has no subscription configs left.
  • It was only disposed inside the loop over the configs, so any cleanup after a universe removal left the wrapper in the scan queue.

Related Issue

N/A

Motivation and Context

Keeping the removed security's indicators buys nothing: they never update again, even when the security is added back. Cleaning them up by default removes a leak that is invisible until the algorithm runs out of memory.

Requires Documentation Change

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

How Has This Been Tested?

  • New AutomaticIndicatorDeregistrationTests (7 tests): removal disposes the symbol's helper indicators and consolidators, keeps raw ones, honors the setting.
  • New SubscriptionManagerTests.RemoveConsolidatorDisposesScanWrapperEvenWithoutSubscriptions: red before the wrapper fix, green after.
  • New AutomaticIndicatorDeregistrationRegressionAlgorithm (C# + Python): manual remove and re-add, indicators and consolidator stop updating on removal, fresh ones work after re-adding.
  • New AutomaticIndicatorDeregistrationOnUniverseSelectionRegressionAlgorithm: universe deselection and reselection, deregistered on removal, new ones get data after reselection.
  • Full Tests.Algorithm namespace with CI filters: 13232 passed, 0 failed.

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>

@jhonabreul jhonabreul closed this Aug 25, 2026
…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
…lection

- make the per-symbol cleanup private as DeregisterIndicatorsAndConsolidators,
  the setting is the only public surface
- default Settings.AutomaticIndicatorDeregistration to true: kept indicators never
  update again after their security is removed, even when it's added back
- add AutomaticIndicatorDeregistrationOnUniverseSelectionRegressionAlgorithm
  covering universe deselection and reselection
- fold the DeregisterAll algorithms and tests into the automatic ones
@jhonabreul jhonabreul changed the title Add DeregisterAll and automatic indicator deregistration on security removal Automatically deregister indicators and consolidators when a security is removed Oct 13, 2026
@jhonabreul jhonabreul reopened this Oct 13, 2026
@jhonabreul
jhonabreul force-pushed the feature-deregister-all-security-cleanup branch from 421b654 to 1b3e9cd Compare October 13, 2026 22:26
@jhonabreul
jhonabreul marked this pull request as ready for review October 13, 2026 22:27
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