This document contains information regarding ways to use Visual Studio Code to work with the Lean engine, this includes a couple options that make lean easy to develop on any machine:
-
Using Lean CLI -> A great tool for working with your algorithms locally, while still being able to deploy to the cloud and have access to Lean data. It is also able to run algorithms locally through our official docker images **Recommended for algorithm development.
-
Using a Lean Dev container -> A docker environment with all dependencies pre-installed to allow seamless Lean development across platforms. Great for open source contributors.
-
Locally installing all dependencies to run Lean with Visual Studio Code on your OS.
To use Lean CLI follow the instructions for installation and tutorial for usage in our documentation
Before anything we need to ensure a few things have been done for either option:
-
- Get Remote Containers Extension
-
Get Docker:
- Follow the instructions for your Operating System
- New to Docker? Try docker getting-started
-
Pull Lean’s latest research image from a terminal
docker pull quantconnect/research:latest
-
Get Lean into VS Code
- Download the repo or clone it using:
git clone [https://github.com/QuantConnect/Lean](https://github.com/QuantConnect/Lean)
- Open the folder using VS Code
- Download the repo or clone it using:
-
Open Development Container
- In VS Code, either:
-
Select "Reopen in Container" from pop up box.
OR
-
Ctrl+Shift+P (Command Palette) and select "Remote-Containers: Rebuild and Reopen in Container"
-
- In VS Code, either:
You should now be in the development container, give VS Code a moment to prepare and you will be ready to go! If you would like to mount any additional local files to your container, checkout devcontainer.json "mounts" section for an example! Upon any mount changes you must rebuild the container using Command Palette as in step 5.
-
Install .NET 6 for the project
-
(Optional) Get Python 3.8.13 for running Python algorithms
- Follow Python instructions here for your platform
-
Get Lean into VS Code
- Download the repo or clone it using:
git clone [https://github.com/QuantConnect/Lean](https://github.com/QuantConnect/Lean)
- Open the folder using VS Code
- Download the repo or clone it using:
Your environment is prepared and ready to run Lean.
This section will cover configuring, building, launching and debugging lean. This is only applicable to option 2 from above. This does not apply to Lean CLI, please refer to CLI documentation
We need to be sure that our Lean configuration at .\Launcher\config.json is properly set.
Your configuration file should look something like this for the following languages:
"algorithm-type-name": "**AlgorithmName**",
"algorithm-language": "Python",
"algorithm-location": "../../../Algorithm.Python/**AlgorithmName**.py",
"algorithm-type-name": "**AlgorithmName**",
"algorithm-language": "CSharp",
"algorithm-location": "QuantConnect.Algorithm.CSharp.dll",
Before running Lean, we must build the project. Currently the VS Code task will automatically build before launching. But find more information below about how to trigger building manually.
In VS Code run build task (Ctrl+Shift+B or "Terminal" dropdown); there are a few options:
- Build - basic build task, just builds Lean once
- Rebuild - rebuild task, completely rebuilds the project. Use if having issues with debugging symbols being loaded for your algorithms.
- Clean - deletes out all project build files
Now that lean is configured and built we can launch Lean. Under "Run & Debug" use the launch option "Launch". This will start Lean with C# debugging. Any breakpoints in Lean C# will be triggered.
Python algorithms require a little extra work in order to be able to debug them. Follow the steps below to get Python debugging working.
First in order to debug a Python algorithm in VS Code we must make the following change to our configuration (Launcher\config.json) under the comment debugging configuration:
"debugging": true,
"debugging-method": "DebugPy",
In setting this we are telling Lean to expect a debugger connection using ‘Python Tools for Visual Studio Debugger’. Once this is set Lean will stop upon initialization and await a connection to the debugger via port 5678.
Now that Lean is configured for the python debugger we can make use of the programmed launch options to connect to Lean during runtime.
Start Lean using the "Launch" option covered above. Once Lean starts you should see the messages in figure 2 If the message is displayed, use the launch option “Attach to Python”. Then press run, VS Code will now enter and debug any breakpoints you have set in your python algorithm.
Figure 2: Python Debugger Messages
20200715 17:12:06.546 Trace:: PythonInitializer.Initialize(): ended
20200715 17:12:06.547 Trace:: DebuggerHelper.Initialize(): python initialization done
20200715 17:12:06.547 Trace:: DebuggerHelper.Initialize(): starting...
20200715 17:12:06.548 Trace:: DebuggerHelper.Initialize(): waiting for debugger to attach at localhost:5678...
Here we will cover some common issues with setting this up. This section will expand as we get user feedback!
- The "project file cannot be loaded" and "nuget packages not found" errors occurs when the project files are open by another process in the host. Closing all applications and/or restarting the computer solve the issue.
- Autocomplete and reference finding with omnisharp can sometimes bug, if this occurs use the command palette to restart omnisharp. (Ctrl+Shift+P "OmniSharp: Restart OmniSharp")
- Any error messages about building in VSCode that point to comments in JSON. Either select ignore or follow steps described here to remove the errors entirely.