From b58929c2b34a5d8ff1ae4b144006b5cf2c7dd150 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Thu, 27 Feb 2025 11:03:46 -0400 Subject: [PATCH] Prevent QuantBook composer reset on notebook initialization --- Research/QuantBook.cs | 19 +++++++++++++++++-- Research/QuantConnect.csx | 1 + Research/start.py | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Research/QuantBook.cs b/Research/QuantBook.cs index f59e4e2cb219..51f11ad2978a 100644 --- a/Research/QuantBook.cs +++ b/Research/QuantBook.cs @@ -38,7 +38,6 @@ using QuantConnect.Lean.Engine.Setup; using QuantConnect.Indicators; using QuantConnect.Scheduling; -using System.Collections; namespace QuantConnect.Research { @@ -52,6 +51,12 @@ public class QuantBook : QCAlgorithm private IDataProvider _dataProvider; private static bool _isPythonNotebook; + /// + /// Indicates whether the Lean system and algorithm handlers have already been initialized, + /// preventing the QuantBook to reset the composer and handlers on re-creation. + /// + public static bool HandlersInitialized { get; set; } + static QuantBook() { //Determine if we are in a Python Notebook @@ -115,10 +120,20 @@ public QuantBook() : base() // Sets PandasConverter SetPandasConverter(); + Composer composer; + if (HandlersInitialized) + { + // Reset the flag so we reset the composer on QuantBook re-creation + HandlersInitialized = false; + composer = Composer.Instance; + } + else + { // Reset our composer; needed for re-creation of QuantBook Composer.Instance.Reset(); - var composer = Composer.Instance; + composer = Composer.Instance; Config.Reset(); + } // Create our handlers with our composer instance var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(composer); diff --git a/Research/QuantConnect.csx b/Research/QuantConnect.csx index 1ed924442b32..9c77a56c8103 100644 --- a/Research/QuantConnect.csx +++ b/Research/QuantConnect.csx @@ -86,3 +86,4 @@ Config.Reset(); Initializer.Start(); Api api = (Api)Initializer.GetSystemHandlers().Api; var algorithmHandlers = Initializer.GetAlgorithmHandlers(researchMode: true); +QuantBook.HandlersInitialized = true; diff --git a/Research/start.py b/Research/start.py index fbbcd4b7eaf6..1438fba11090 100644 --- a/Research/start.py +++ b/Research/start.py @@ -36,6 +36,7 @@ Initializer.Start() api = Initializer.GetSystemHandlers().Api algorithmHandlers = Initializer.GetAlgorithmHandlers(researchMode=True) +QuantBook.HandlersInitialized = True # Required to configure pythonpath with additional paths the user may have # set in the config, like a project library.