Skip to content

Walk Forward Optimization #7031

Open
Open

Description

@jaredbroad

Desired Behavior

  • Create Walk Forward Optimization API in LEAN.
  • Harness existing parameters and optimization controller technology to implement scheduled optimizations.
  • Use a pluggable source for the optimization provider.
  • In backtesting, run local LEAN instances inside LEAN Foundation, as backtests can take a long time.
  • In live trading, run an APIOptimizationHandler to run the optimization lift in the cloud; to keep live loads light.
  • Use the Scheduled Event System to handle initializing the optimization.

QCAlgorithm.Optimize(DateRule, TimeRule, {Optimization Target | Optimization Target Func<BacktestResults[]>} )

  • Date/Time Rules - When to run WFO / Month Start, Week Start etc.
  • Optimization Target - Select the winning set pf parameters.
  • The result would then set the algorithm parameters set.

Checklist

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues

Activity

  1. added
    featureNew functionality or support for a new security; not a bug report.
    on Mar 1, 2023
  2. algo-dude commented on Jun 1, 2023

    @algo-dude

    A simple idea of how I would expect to use this:

    image
  3. jaredbroad commented on Aug 4, 2023

    @jaredbroad
    MemberAuthor

    Parent and Child algorithms to avoid nested optimizations

  4. KlausGlueckert commented on May 5, 2024

    @KlausGlueckert

    this would be interesting! Please allow to pass an objective function to "score" where one can use the runtime statistics of the backtest and not only point estimates of the score of the backtest.

  5. RooFernando commented on Jul 5, 2024

    @RooFernando

    Is there a way to run the backtest on the research.ipynb?

    I would like to optimize parameters but would need to extract values from the run backtest (objective function). Is it possible to call on the backtest strategy within research Jupyter notebook?

  6. Whatsonyourmind commented on Apr 2, 2026

    @Whatsonyourmind

    For walk-forward parameter optimization, the key insight is optimizing for out-of-sample Sharpe, not in-sample — which changes the algorithm choice significantly.

    Genetic Algorithms with NSGA-II selection give you the Pareto frontier of risk-adjusted return vs. parameter stability. This is more useful than a single 'best' parameter set because you can visualize the tradeoff: parameters that maximize return tend to be brittle, while robust parameters cluster in a different region of the search space.

    The practical implementation that works well:

    1. GA for global search — population of 50-100 parameter sets, tournament selection, 2-point crossover
    2. Simulated Annealing for local refinement — take the top 5 GA candidates and search their neighborhoods with geometric cooling
    3. Fitness = OOS Sharpe * stability_score — where stability_score penalizes parameters that are sensitive to small perturbations (compute by jittering each parameter ±5% and checking if performance drops >20%)

    This hybrid converges 3-5x faster than either algorithm alone because GA finds the right neighborhood and SA finds the exact optimum within it.

    One implementation detail that matters: the walk-forward window size should be chosen via cross-validation, not fixed. We've seen 6-month in-sample / 1-month OOS work well for daily strategies, but this varies by holding period.

  7. jaredbroad commented on Oct 28, 2026

    @jaredbroad
    MemberAuthor

    Work transferred from PR #9611

    Add scheduled walk-forward optimization support by @scarab-systems, branch scarab-systems:feature-7031-walk-forward-optimization, head 372b67d79.
    PR: #9611

    The PR is being closed to tidy up the queue; the proposed change is recorded here for review and can be reopened or re-submitted once the approach is agreed.

    Changed files (22 files, +1720/-2):

    • Algorithm/QCAlgorithm.cs (+115/-0)
    • AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs (+6/-0)
    • Api/ApiWalkForwardOptimizationProvider.cs (+121/-0)
    • Api/WalkForwardOptimizationBacktestSelector.cs (+118/-0)
    • Api/WalkForwardOptimizationCloudSettings.cs (+112/-0)
    • Common/AlgorithmImports.py (+3/-0)
    • Common/Interfaces/IAlgorithm.cs (+6/-0)
    • Common/Interfaces/IWalkForwardOptimizationProvider.cs (+34/-0)
    • Common/Optimizer/NullWalkForwardOptimizationProvider.cs (+45/-0)
    • Common/Optimizer/WalkForwardOptimizationRequest.cs (+102/-0)
    • Common/Optimizer/WalkForwardOptimizationResult.cs (+70/-0)
    • Engine/Server/LocalLeanManager.cs (+5/-0)
    • Launcher/config.json (+4/-1)
    • Optimizer.Launcher/LocalWalkForwardOptimizationProvider.cs (+133/-0)
    • Optimizer/LeanOptimizer.cs (+55/-0)
    • Optimizer/OptimizationResult.cs (+9/-1)
    • Tests/Algorithm/AlgorithmWalkForwardOptimizationTests.cs (+175/-0)
    • Tests/Api/ApiWalkForwardOptimizationProviderSelectionTests.cs (+121/-0)
    • Tests/Api/ApiWalkForwardOptimizationProviderTestBase.cs (+123/-0)
    • Tests/Api/ApiWalkForwardOptimizationProviderTests.cs (+104/-0)
    • Tests/Engine/WalkForwardOptimizationProviderRoutingTests.cs (+78/-0)
    • Tests/Optimizer/LocalWalkForwardOptimizationProviderTests.cs (+181/-0)
    PR description

    Description

    Adds scheduled walk-forward optimization support for algorithms:

    • QCAlgorithm.Optimize(...) overloads that schedule walk-forward optimization similarly to Train(...).
    • A provider abstraction for local and cloud-backed walk-forward optimization execution.
    • Local optimizer and QuantConnect API-backed providers.
    • Result plumbing for selected parameter sets and backtest summaries, with safeguards against nested child optimizations.

    Related Issue

    Addresses #7031

    Motivation and Context

    Issue #7031 requests walk-forward optimization support in LEAN. This PR adds a core API contract and provider routing so algorithms can trigger optimization over time and apply chosen parameters without hard-coding a single execution backend.

    Requires Documentation Change

    Yes. If accepted, documentation should describe the new Optimize(...) API and the walk-forward-optimization-* configuration keys for local and cloud execution.

    How Has This Been Tested?

    Local environment: macOS, .NET SDK in this checkout.

    • dotnet build Tests/QuantConnect.Tests.csproj /p:Configuration=Debug /v:quiet
    • Focused walk-forward optimization tests: 13 passed
    • Optimizer regression tests: 186 passed
    • git diff --check origin/master...HEAD

    Types of changes

    • New feature (non-breaking change which adds functionality)

    Disclosure: I used AI-assisted coding tools while preparing this PR. I reviewed the changes myself, tested them, and take responsibility for the implementation and any follow-up revisions needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or support for a new security; not a bug report.impact-highup-next

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions