Skip to content

Security initialization on re-addition #8681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jhonabreul
Copy link
Collaborator

@jhonabreul jhonabreul commented Apr 7, 2025

Description

Ensure securities are initialized every time they are added or re-added, including scenarios like manual removal and re-addition and deselection and re-selection by universes, like options or custom universes.
Also make sure initialization happens only once for each security on addition.

Performance tests:

Since now the securities are being initialized every time they are re-added (either manually or by being selected by a universe), we expect to have minor performance impacts on algorithms that are doing selection.

Daily coarse selection: 50 top equities by dollar volume and their options:

public override void Initialize()
{
    SetStartDate(2014, 01, 01);
    SetEndDate(2014, 03, 01);
    SetCash(100000);

    UniverseSettings.Resolution = Resolution.Daily;

    var seeder = new FuncSecuritySeeder(security => GetLastKnownPrices(security));
    SetSecurityInitializer(security => seeder.SeedSecurity(security));

    var universe = AddUniverse(coarse => coarse.OrderByDescending(x => x.DollarVolume).Take(50).Select(x => x.Symbol));

    AddUniverseOptions(universe, u => u.Expiration(0, 7));
}

Master: ~136s on avg
PR: ~146s on avg (~7% slower)

Daily coarse selection: randomly selecting 50 out 200 top equities by dollar volume and their options:

var universe = AddUniverse(coarse => coarse.OrderByDescending(x => x.DollarVolume).Take(200).OrderBy(_ => random.Next()).Take(50).Select(x => x.Symbol));
AddUniverseOptions(universe, u => u.Expiration(0, 7));

Note: selecting 50 randomly so we force some equities are de-selected and re-selected each day, which forces more security initializations.

Master: ~166s on avg
PR: ~191s on avg (~7% slower)

Related Issue

Part of #7929

Motivation and Context

Requires Documentation Change

How Has This Been Tested?

Regression algorithms

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 merged commit e903096 into QuantConnect:master Apr 9, 2025
19 of 24 checks passed
@jhonabreul jhonabreul deleted the bug-7929-security-re-initialization branch April 9, 2025 20:26
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