This document contains information regarding ways to use Visual Studio Code to work with the Lean engine, this includes using Lean’s Docker image in conjunction with local development as well as running Lean locally.
Before anything we need to ensure a few things have been done:
-
- Get the Extension Mono Debug for C# Debugging
- Get the Extension Python for Python Debugging
-
Get Docker:
- Follow the instructions for your Operating System
- New to Docker? Try docker getting-started
-
Install a compiler for the project (Only needed for C# Debugging or Running Locally)
- On Linux or Mac:
- Install mono-complete
- Test msbuild with command: msbuild -version
- On Windows:
- Visual Studio comes packed with msbuild or download without VS here
- Put msbuild on your system path and test with command: msbuild -version
- On Linux or Mac:
-
Pull Lean’s latest image from a terminal
- docker pull quantconnect/lean
-
Get Lean into VS Code
- Download the repo or clone it using: git clone https://github.com/QuantConnect/Lean
- Open the folder using VS Code
We have set up a relatively easy way to develop algorithms in your local IDE and push them into the container to be run and debugged.
Before we can use this method with Windows or Mac OS we need to share the Lean directory with Docker.
-
Windows:
- Guide to sharing
- Share the LEAN root directory with docker
-
Mac:
- Guide to sharing
- Share the LEAN root directory with docker
-
Linux:
- (No setup required)
Next we need to be sure that our Lean configuration at .\Launcher\config.json is properly set. Just like running lean locally the config must reflect what we want Lean to run.
You 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",
In order to use a custom C# algorithm, the C# file must be compiled before running in the docker, as it is compiled into the file "QuantConnect.Algorithm.CSharp.dll". Any new C# files will need to be added to the csproj compile list before it will compile, check Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj for all algorithms that are compiled. Once there is an entry for your algorithm the project can be compiled by using the “build” task under “Terminal” > “Run Build Task”.
Python does not have this requirement as the engine will compile it on the fly.
This section will cover how to actually launch Lean in the container with your desired configuration.
In VS Code click on the debug/run icon on the left toolbar, at the top you should see a drop down menu with launch options, be sure to select Debug in Container. This option will kick off a launch script that will start the docker. With this specific launch option the parameters are already configured in VS Codes tasks.json under the run-docker task args. These set arguements are:
"image=quantconnect/lean:latest",
"config_file=${workspaceFolder}/Launcher/config.json",
"data_dir=${workspaceFolder}/Data",
"results_dir=${workspaceFolder}/",
"debugging=Y",
"python_location=${workspaceFolder}/Algorithm.Python"
As defaults these are all great! Feel free to change them as needed for your setup.
From a terminal launch the run_docker.bat/.sh script; there are a few choices on how to launch this:
-
Launch with no parameters and answer the questions regarding configuration (Press enter for defaults)
* Enter docker image [default: quantconnect/lean:latest]: * Enter absolute path to Lean config file [default: _~currentDir_\Launcher\config.json]: * Enter absolute path to Data folder [default: ~_currentDir_\Data\]: * Enter absolute path to store results [default: ~_currentDir_\]: * Would you like to debug C#? (Requires mono debugger attachment) [default: N]:
-
Using the run_docker.cfg to store args for repeated use; any blank entries will resort to default values! example: ./run_docker.bat run_docker.cfg
image=quantconnect/lean:latest config_file= data_dir= results_dir= debugging= python_dir=
-
Inline arguments; anything you don't enter will use the default args! example: ./run_docker.bat debugging=y
- Accepted args for inline include all listed in the file in #2
Python algorithms require a little extra work in order to be able to debug them locally or in the container. Thankfully we were able to configure VS code tasks to take care of the work for you! 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": "PTVSD",
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 debug inside of the container we must first start the container, follow the steps described in the section “Running Lean in the Container”. Once the container is started you should see the messages in Figure 2.
If the message is displayed, use the same drop down for “Debug in Container” and select “Attach to Python (Container)”. Then press run, VS Code will now enter and debug any breakpoints you have set in your Python algorithm.
To debug locally we must run the program locally using the programmed task found under Terminal > Run Task > “Run Application”. Once Lean is started you should see the messages in Figure 2.
If the message is displayed, use the launch option “Attach to Python (Local)”. 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!
- Error messages about build in VSCode points to comments in JSON. Either select ignore or follow steps described here to remove the errors entirely.