I will show you how you can debug your cBots & indicators using cTrader Desktop & Microsoft Visual Studio, to get started it is recommended that you read the post below on how to convert your existing projects, if you have a new project created using cTrader you can skip the upgrade link below.
Starting From cTrader Automate
The 1st step is to make sure your project builds ok using cTrader Automate and that the target framework on the top of the screen shows .NET 6, once this is complete you can right-click your mouse on the cBot or indicator and select Edit with Visual studio
Using Visual Studio 2022
Once you have VS 2022 open you need to add a single line of code to your cBot OnStart() or Indicator Initialise methods, this will start the debugging process when you start the cBot or attach the indicator to a chart.
The line of code you need to add is shown below to copy.
System.Diagnostics.Debugger.Launch();
You will need to comment out this line of code when you are finished debugging or everytime you start your cBot or indicator it will start the debugging process.
Allow Full Access
You will also need to give your project full access for the debugging to work, you can set this to None when you are finished and ready to publish as a release build.
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
Add Your Breakpoints
To start the debugging process you need to first attach a breakpoint using Visual Studio on the line of code you want the processing to stop so you can step through the code.
Make sure your project is set to Debug mode and not Release mode.
When you have your breakpoint you can continue to the next step, remember you can have multiple breakpoints, these are very useful as it allows you to analyse what is happening in your code during runtime and can help identify bugs and problems in your algorithms.
How to Start The Debug Process
After you have set your breakpoints you now need to go back to cTrader Automate and start the cBot or attach the indicator to a chart, if the indicator is already on the chart you can refresh the chart.
This will open a window allowing you to choose which version of Visual Studio you wish to use for debugging, if you already have Visual Studio open with your cTrader project loaded then it will show as shown as highlighted, if you have not already opened a copy of Visual Studio then your options will be a New Instance.
We recommend that you Edit with Visual Studio from cTrader Automate to open Visual Studio and then add your breakpoints instead of opening a new instance of the IDE.
Now You Are Ready To Debug
Now you can get started and debug your project, when you start your cBot or attach your indicator to a chart Visual Studio will start debugging, your breakpoint will not be hit until the code is executed, so make sure you place it in the correct line.
How to make sure the debugging is working?
You can easily test if it is working by placing the breakpoint in the OnStart method of a cBot or the Initialise method of an indicator, if you do not see the code stop then something is wrong.
Example Debug Breakpoint Hit
Once a breakpoint is hit you will see a solid yellow background as shown below, you can hover your mouse over any object to see the contents, in this case, we can see a pending order was cancelled by the user.
Watch a Video Explainer
I have also created a video explainer showing all of the above and how it works in practice.