Skip to content

Commit

Permalink
Prevent QuantBook composer reset on notebook initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Feb 27, 2025
1 parent f9d84a6 commit b58929c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Research/QuantBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
using QuantConnect.Lean.Engine.Setup;
using QuantConnect.Indicators;
using QuantConnect.Scheduling;
using System.Collections;

namespace QuantConnect.Research
{
Expand All @@ -52,6 +51,12 @@ public class QuantBook : QCAlgorithm
private IDataProvider _dataProvider;
private static bool _isPythonNotebook;

/// <summary>
/// Indicates whether the Lean system and algorithm handlers have already been initialized,
/// preventing the QuantBook to reset the composer and handlers on re-creation.
/// </summary>
public static bool HandlersInitialized { get; set; }

static QuantBook()
{
//Determine if we are in a Python Notebook
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions Research/QuantConnect.csx
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ Config.Reset();
Initializer.Start();
Api api = (Api)Initializer.GetSystemHandlers().Api;
var algorithmHandlers = Initializer.GetAlgorithmHandlers(researchMode: true);
QuantBook.HandlersInitialized = true;
1 change: 1 addition & 0 deletions Research/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b58929c

Please sign in to comment.