Hi all,
I hope that asking for help here will help others trying to get started using QuantConnect's Lean CLI with Visual Studio. I'm using VS 2019 Community Edition.
I'm tantilizingly close but have not yet managed to hit a breakpoint and start debugging an algorithm.
I started off following this tutorial:
https://www.quantconnect.com/docs/v2/lean-cli/getting-started/lean-cliThis was useful:
https://www.quantconnect.com/docs/v2/lean-cli/user-guides/troubleshootingAnd then this:
https://www.quantconnect.com/docs/v2/lean-cli/tutorials/backtesting/debugging-local-backtests#04-C-and-Visual-StudioIn the C# and Visual Studio section of that last article, the final step is:
5) In Visual Studio, attach to the debugger using "Extensions > Mono > Attach to mono debugger".
But it feels like there should be a step 6!
When I complete step 5, my application starts. That is clear because the green "play" button in Visual Studio debugging toolbar turns into a red "stop" button. After about a minute (and without me doing anything) it stops. No errors or anything. It just stops.
And during that minute, I never hit the breakpoint set early on in my code. The breakpoint is on line 9 of Main.cs (the default, unaltered template) in:
public override void Initialize()
{
SetStartDate(2020, 8, 2); // Breakpoint is right here, but it isn't being hit
Nothing is written to the Output window during that time.
What have I done wrong?
Many thanks
Benjamin
Jasper van Merle
Hi Benjamin,
It sounds like you did everything correctly so it's weird that it's not working. Can you try setting a breakpoint somewhere later in the algorithm (like uncomment the AddEquity("SPY") call in Initialize and set a breakpoint in OnData) and see if that works?
Benjamin Charlton
Thanks for the fast response, Jasper. I have set another breakpoints as below but to no avail:
public class MyQC : QCAlgorithm
{
public override void Initialize()
{
SetStartDate(2020, 8, 2); // Set Start Date
SetCash(100000); // Set Strategy Cash
AddEquity("SPY", Resolution.Minute); // Breakpoint here
}
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// Slice object keyed by symbol containing the stock data
public override void OnData(Slice data)
{
if (!Portfolio.Invested) // Breakpoint here
{
SetHoldings("SPY", 1);
Debug("Purchased Stock");
}
}
}
I don't have a Program.cs with an entry point to "start" the algorithm, which is new to me... you sure we don't need anything like this...?
public static void Main(string[] args)
I notice that the docs say "The commands which run the LEAN engine locally also depend on Docker being installed and running. See Installing Docker to learn how to install Docker on your operating system."
I have installed Docker, but is that all I have to do? Do I need to add any images or special configuration to Docker?
I have quantconnect/lean installed on Docker. Should I remove it?
Jasper van Merle
Hi Benjamin,
It is normal for LEAN algorithms not to have a "public static void Main" because the entrypoint of LEAN is not the algorithm but the LEAN launcher, which instantiates the algorithm and runs its methods.
The Docker thing is also normal, just installing Docker is all you need to do and the existence of quantconnect/lean is expected. If you remove it, the CLI will pull it again the next time you run a backtest.
Can you please try the following?
1. Add System.Diagnostics.Debugger.Break(); in between two statements (like in between SetStartDate and SetCash).
2. Add breakpoints in VS on the line before and after the statement added in step 1.
3. Run lean backtest "Project Name" --debug mono --update, connect to the debugger and see how many times the debugger breaks.
This is supposed to break 3 times, but I'm hoping it at least breaks on System.Diagnostics.Debugger.Break(); to confirm that the issue is VS not transmitting breakpoints to the Mono instance in the Docker container.
Benjamin Charlton
Thanks for helping me diagnose this issue, Jasper. Here's what I have done:
1) Opened my algorithm csproj in Visual Studio.
2) Removed all the breakpoints
3) Added breakpoints as below:
public override void Initialize() { SetStartDate(2020, 8, 2); // Breakpoint here System.Diagnostics.Debugger.Break(); SetCash(100000); // Breakpoint here AddEquity("SPY", Resolution.Minute); }
In a console, changed directory to the same directory where I configured Lean:
cd "C:\Users\Administrator\Software Development\Lean"
Then ran the command:
lean backtest "BenjaminQC" --debug mono --update
Then, quick as a flash, I click the following menus:
Extensions > Mono > Attach to mono debugger [mono debugger] (TCP 127.0.0.1)
At this point, if I take a peek into Docker Desktop, under the Containers/Apps tab I see something has been added. It looks like this:
gifted_cohen
quantconnect/lean:latest
running port:55556
Each time I try, it displays a different two-word title in place of "gifted_cohen", like "sleepy_payne".
Meanwhile, back in the console, it says:
Pulling quantconnect/lean:latest... latest: Pulling from quantconnect/lean Digest: sha256:520eef759fe51132d4968e068b548e893729a1d5879dfcafa034b1b409999dd9 Status: Image is up to date for quantconnect/lean:latest docker.io/quantconnect/lean:latest Compiling all C# files in 'C:\Users\Administrator\Software Development\Lean\BenjaminQC' Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET Copyright (C) Microsoft Corporation. All rights reserved. Determining projects to restore... Restored /LeanCLI/BenjaminQC.csproj (in 73 ms). /LeanCLI/Main.cs(28,29): warning CS0618: 'Symbol.implicit operator Symbol(string)' is obsolete: 'Symbol implicit operator from string is provided for algorithm use only.' [/LeanCLI/BenjaminQC.csproj] BenjaminQC -> /LeanCLI/bin/Debug/BenjaminQC.dll Docker container starting, attach to Mono debugger at localhost:55556 to begin
Then some time later, "gifted_cohen" (or whatever the name is for this particular run) no longer displays in Docker Desktop Back in the console in Visual Studio, three more lines are added:
debugger-agent: DWP handshake failed. Error: Something went wrong while running 'BenjaminQC' in the 'backtesting' environment, the output is stored in 'BenjaminQC\backtests\2021-04-14_17-13-05'
If I look in that folder, it's empty.
No breakpoints were hit that I noticed.
Sorry if that was too much detail, but, having never done this before, it's hard to know if I'm doing the right thing or what I should expect to see happening on the screen.
Jasper van Merle
Hi Benjamin,
There is no such thing at too much detail, at least now we've got an error to work with :)
"Then, quick as a flash, I click the following menus:" is what may cause problems. Can you try running the lean backtest "BenjaminQC" --debug mono --update command, wait until you see "Docker container starting, attach to Mono debugger at localhost:55556 to begin" and only then attach to the debugger?
Benjamin Charlton
Ok, I tried that just now... still a mysterious silence afterwards. No output. No breakpoints. The docker container seems to remain open for some arbitrary amount of time and then goes away without any notice.
Jasper van Merle
This issue was fixed through a screen sharing session, the fix was to go to "Extensions > Mono > Settings" in Visual Studio and to enter the following settings:
Remote Host IP: 127.0.0.1
Remote Host Port: 55556
Mono Debug Port: 55556
Another important thing to keep in mind is that when debugging with the CLI, you need to manually save all modified files before starting the lean backtest command (unlike in VS where it does so automatically once you run a project).
Benjamin Charlton
Thank you, Jasper, for your time away from the discussion forum helping to solve this problem. I'm glad to report that it's working now. Here are some learning experiences I gained from you that I'll share here for everyone:
- Windows PowerShell is an appropriate terminal/console for running the commands.
- Ensure that your Solution's configuration Build Mode is set to "Debug" not "Release"
- Your solution will be built automatically every time you run
- In Extensions > Mono > Settings, double check the following, because they had reverted back to defaults in my case:
- Remote Host Port: 55556
- Mono Debug Port: 55556
- Do not attach the debugger until after you see the following message displayed in Windows PowerShell: "Docker container starting, attach to Mono debugger at localhost:55556 to begin"
- The algorithm will not start running until after you attach the debugger.
- When everything is working fine, you should expect to hit any breakpoints in Visual Studio at this point. Apparently remote debugging runs a bit faster in JetBrains Rider than it does in Visual Studio but I'm finding the VS experience satisfactory for now.
- Output from Lean will be mixed with Debug("") statements in your code and will be shown in Windows PowerShell. The numbers on the left are the dates and times (in Universal Coordinated Time UTC) when the backtest was run.
- For a less crowded log (with just Debug statements and information about trades) look in your project directory in the subfolder Backtests/[Date]/ProjectName-log.txt
- After the backtest has run, the docker container gets automatically deleted.
Thanks for getting me started. I hope these tips help somebody else one day.lean backtest "Project Name" --debug mono
But (unlike the traditional "Run" command in Visual Studio) it will not automatically save changes to your code files, so make sure that to save changes to your .cs files before running the backtest.
Benjamin Charlton
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!