Skip to content

Python model interop: subclassable SlippageModel base, algorithm-level SetSlippageModel, None framework models, callable security seeder - #9678

Draft
jhonabreul wants to merge 2 commits into
QuantConnect:masterfrom
jhonabreul:feature-python-model-interop
Draft

Python model interop: subclassable SlippageModel base, algorithm-level SetSlippageModel, None framework models, callable security seeder#9678
jhonabreul wants to merge 2 commits into
QuantConnect:masterfrom
jhonabreul:feature-python-model-interop

Conversation

@jhonabreul

@jhonabreul jhonabreul commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Description

Python algorithms commonly subclass .NET models, pass callables as seeders, or pass None as framework models. Today these crash at initialization with cryptic interop errors, e.g.:

interface takes exactly one argument
IRiskManagementModel must be fully implemented. Please implement these missing methods on NoneType: ManageRisk

Changes:

  • New SlippageModel concrete base class (parallel to FeeModel) with a virtual GetSlippageApproximation returning zero. Existing slippage models derive from it, so Python can subclass it.
  • New QCAlgorithm.SetSlippageModel (C# and PyObject overloads) applies a slippage model to all present and future securities. Later per-security Security.SetSlippageModel calls still win, and the model survives a later SetBrokerageModel.
  • New BrokerageModelSecurityInitializer(IBrokerageModel, PyObject) constructor accepts an ISecuritySeeder, a plain callable (wrapped into FuncSecuritySeeder), or None. Anything else raises a prescriptive error naming the accepted inputs.
  • The framework PyObject setters (SetAlpha, SetExecution, SetPortfolioConstruction, SetRiskManagement, SetUniverseSelection) accept None as the corresponding null model.
  • PythonWrapper.ValidateImplementationOf reports missing members as snake-cased Python signatures, e.g. get_slippage_approximation(asset, order).

Deferred: clearer errors when instantiating a subclass of a .NET interface — needs a pythonnet-side change, cannot be intercepted from Lean.

Related Issue

N/A

Motivation and Context

These are the natural, type-checker-friendly ways to declare custom models from Python. They compile green and die at initialization with cryptic interop errors.

Requires Documentation Change

Docs can recommend subclassing SlippageModel and document the new algorithm-level set_slippage_model.

How Has This Been Tested?

  • New AlgorithmSlippageModelRegressionAlgorithm (C# + Python, identical statistics) covering the algorithm-level slippage model, per-security precedence, callable seeder, and None framework models.
  • Unit tests in AlgorithmModelsTests, SecurityCustomModelTests, BrokerageModelSecurityInitializerTests, and PythonWrapperTests cover the new surfaces and error cases.
  • All touched fixtures: 86 passed, 0 failed. Affected namespace suites with CI filters: 8671 passed, 0 failed. Slippage-related regression algorithms: 6 passed.

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>

…el SetSlippageModel, None framework models, callable security seeder
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