Skip to content

Debugging

What Is Debugging

Debugging is a process during which you can detect and fix the errors present in your code.

Any .NET program can run in two different modes.

  • Release mode. This is the mode typically used in production environments. When using it, .NET enables various compiler and just-in-time optimisations that improve the speed with which your code executes and reduce its memory footprint.
  • Debug mode. This mode is primarily used in development/testing environments. All optimisations are disabled, and you can attach .NET debuggers to your code.

When working with .NET libraries and applications, debugging entails the following.

  • Running yoir code in debug mode.
  • Attaching a .NET debugger to your code.
  • Setting code breakpoints to indicate whether the execution flow should pause.
  • Following the code execution flow with planned pauses at breakpoints

Defining .NET Debuggers

.NET debuggers are programs that use runtime APIs. These APIs, in turn, allow for controlling the execution flow of .NET processes. In other words, they can pause execution at given breakpoints and store various states (including the values that individual variables hold during these states).

Building Algos in Debug Mode

As we have discussed above, debugging a cBot or an indicator entails building this extension in debug mode. By default, when you click on the 'Build' button in the cTrader UI, your extension is run in release mode to enable code optimisations.

To build your cBot/indicator in debug mode, you need to use either the .NET CLI or external IDEs such as Visual Studio. In this documentation, we show the debug process as it occurs in Visual Studio. However, this process can also be replicated in other IDEs with minor changes.

Install the latest version of Visual Studio on your machine and perform the following actions.

  • Open your indicator/cBot in Visual Studio by selecting the 'Edit in Visual Studio' option in the 'Edit in...' dropdown at the top of the code editor window.

  • In the menu above the Visual Studio code editor, select 'Debug'.

Image title

  • Build your cBot/indicator in debug mode.

Debugging a cBot/Indicator

Now that you know how to build a cBot/indicator/ in debug mode, we will focus on how to actually debug it.

  • Change your indicator/cBot access rights to FullAccess.

  • Attach a debugger to your indicator/cBot running process. You can do so by calling the System.Diagnostics.Debugger.Launch() method.

1
2
3
4
5
6
7
8
9
protected override void OnStart()
{
    var result = System.Diagnostics.Debugger.Launch();

    if (result is false)
    {
        Print("Debugger launch failed");
    }    
}
1
2
3
4
5
6
7
8
9
protected override void Initialize()
{
    var result = System.Diagnostics.Debugger.Launch();

    if (result is false)
    {
        Print("Debugger launch failed");
    }    
}

Note

The System.Diagnostics.Debugger.Launch() method has to be called in the OnStart() method (for a cBot) or the Initialize() method (for an indicator).

  • Run a new instance of your cBot or indicator. If an instance is already running, select it in the UI. You should be redirected to Visual Studio with a prompt to select a debugger. Do so and click on 'OK'.

Image title

Once debugging starts, you can set breakpoints, 'step into' lines of code, and see the call stack and variable values.

For a complete guide on how to debug in Visual Studio, check the Visual Studio debugging documentation.

Using the 'DEBUG' Preprocessor Directive

You can also use the DEBUG preprocessor directive in your cBot/indicator code to control the execution flow.

The #if DEBUG directive is useful when you want to run only a block or a line of code in debug mode.

1
2
3
4
5
6
7
8
9
protected override void OnStart()
{
#if DEBUG
    System.Diagnostics.Debugger.Launch();
    Print("Debugging");
#else
    Print("Not Debugging");
#endif
}

In the above example, the "Not Debugging" code line will not be compiled if you build your cBot in debug mode. Additional information about C# processor directives is available in this documentation.