Backtesting

Debugging local backtests

Introduction

Debugging is an important part of writing any algorithm. The CLI makes it easy to use the builtin debugger of the most popular editors to debug LEAN algorithms. This page explains how to start local debugging for Python and C# with all editors supported by the CLI.

Python and PyCharm

Local debugging for Python in PyCharm requires PyCharm's remote debugging functionality, which is only available in PyCharm Professional. After making sure you are running the Professional edition, follow these steps to start local debugging for Python in PyCharm:

  1. Follow the How to set up local autocomplete for Python in PyCharm tutorial.
  2. Open the directory containing the main.py file in a new PyCharm window. It is important that you open the project directory itself, not the directory you ran lean init in.
  3. Start debugging using the "Debug with Lean CLI" run configuration (this configuration is created when you create a new project with the CLI).
  4. Run the lean backtest command with the --debug pycharm option.
    $ lean backtest "My Project" --debug pycharm
    20210322 18:58:23.355 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
    20210322 18:58:23.360 TRACE:: Engine.Main(): Started 6:58 PM
  5. Terminate the debugger in PyCharm once LEAN has exited.

After finishing Python debugging with PyCharm you will see a message saying "Connection to Python debugger failed". You can safely ignore this message.

Python and VS Code

Follow these steps to start local debugging for Python in VS Code:

  1. Follow the How to set up local autocomplete for Python in VS Code tutorial.
  2. Open the directory containing the main.py file in a new VS Code window. It is important that you open the project directory itself, not the directory you ran lean init in.
  3. Run the lean backtest command with the --debug ptvsd option and wait until the CLI tells you to attach to the debugger.
    $ lean backtest "My Project" --debug ptvsd
    20210322 18:59:37.352 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
    20210322 18:59:37.359 TRACE:: Engine.Main(): Started 6:59 PM
    20210322 18:59:39.055 TRACE:: DebuggerHelper.Initialize(): waiting for PTVSD debugger to attach at localhost:5678...
  4. In VS Code, open the Run tab and run the configuration called "Debug with Lean CLI" (this configuration is created when you create a new project with the CLI).

C# and Visual Studio

Follow these steps to start local debugging for C# in Visual Studio:

  1. Follow the How to set up local autocomplete for C# in Visual Studio tutorial.
  2. Open the project containing the Main.cs file in a new Visual Studio window. It is important that you open the project directory itself, not the directory you ran lean init in.
  3. Run the lean backtest command with the --debug vsdbg option and wait until the CLI tells you to attach to the debugger.
    $ lean backtest "My Project" --debug vsdbg
    20210423 13:50:54.634 TRACE:: DebuggerHelper.Initialize(): waiting for debugger to attach...
  4. In Visual Studio, open the process selector using "Debug > Attach to Process...".
  5. Select "Docker (Linux Container)" as connection type.
  6. Select "lean_cli_vsdbg" as connection target.
  7. Double-click on the process named "dotnet".
  8. Tick the checkbox in front of "Managed (.NET Core for Unix)" and click OK to start debugging.

After finishing C# debugging with Visual Studio you will see a message saying "The debug adapter exited unexpectedly.". You can safely ignore this message.

C# and Rider

Follow these steps to start local debugging for C# in Rider:

  1. Follow the How to set up local autocomplete for C# in Rider tutorial.
  2. Open the project containing the Main.cs file in a new Rider window. It is important that you open the project directory itself, not the directory you ran lean init in.
  3. Run the lean backtest command with the --debug rider option and wait until the CLI tells you to attach to the debugger.
    $ lean backtest "My Project" --debug rider
    20210423 13:50:54.634 TRACE:: DebuggerHelper.Initialize(): waiting for debugger to attach...
  4. In Rider, select "Run > Attach To Remote Process...".
  5. In the pop-up that opens, select the target named "root@localhost:2222".
  6. Wait for Rider to connect and select the process named "dotnet QuantConnect.Lean.Launcher.dll" when a selector pops up to start debugging. You may have to select "Remote debugger tools are not loaded to the remote host. Click to load" first.

C# and VS Code

Follow these steps to start local debugging for C# in VS Code:

  1. Follow the How to set up local autocomplete for C# in VS Code tutorial.
  2. Open the directory containing the Main.cs file in a new VS Code window. It is important that you open the project directory itself, not the directory you ran lean init in.
  3. Run the lean backtest command with the --debug vsdbg option and wait until the CLI tells you to attach to the debugger.
    $ lean backtest "My Project" --debug vsdbg
    20210423 13:50:54.634 TRACE:: DebuggerHelper.Initialize(): waiting for debugger to attach...
  4. In VS Code, open the Run tab and run the configuration called "Debug with Lean CLI" (this configuration is created when you create a new project with the CLI).

After finishing C# debugging with VS Code you will see a message saying "The pipe program 'docker' exited unexpectedly with code 137.". You can safely ignore this message.

You can also see our Videos. You can also get in touch with us via Chat.

Did you find this page helpful?

Contribute to the documentation: