Skip to content

Using Visual Studio and Other .NET IDEs

Creating new cBots/Indicators

As stated in the introduction to this documentation, you may choose to use external IDEs to create new cBots/indicators instead of doing so using the built-in code editor. Using external IDEs carries several advantages such as being able to use custom IDE extensions including linters and auto-formatters.

Although there exist a large number of suitable IDEs for working with C# and .NET, we recommend using either Microsoft Visual Studio or Visual Studio Code. Both of these IDEs are easy-to-use and offer a wide a array of customizable settings facilitating work with C# and .NET. Our documentation, therefore, only covers these IDEs.

Visual Studio

Installing the 'cTrader.Automate' Nuget Package

Before creating a cBot/indicator, please, install the cTrader.Automate Nuget package. You can do so by performing the following actions.

  • Open the 'Project' menu in Visual Studio and select 'Manage Nuget Packages'.
  • Select nuget.org as your package source and type in cTrader into the search bar.
  • Click on the cTrader.Automate package and select 'Install'.

Image title

Note

If you are working from an older version of Visual Studio (2015 or earlier), you will also need to install the NuGet Package Manager. For further information, please, consult this documentation.

Alternatively, open the 'Tools' menu, select 'NuGet Package Manager', and open the 'Package Manager Console'. In it, type Install-Package cTrader.Automate.

After installing the cTrader.Automate Nuget package, whenever you build a cBot/indicator project, Visual Studio will automatically create a .algo file for your project in its build directory. When working in Windows, the package will also create a corresponding .algo file in the ../Documents/cAlgo/Sources/Robots/Indicators folder.

Please, note that creating a cBot/indicator via the cTrader UI may be more convenient. cTrader Automate automatically creates a project folder inside the above directory and it also creates a cBot/indicator using the default code template which you can then modify.

Editing cBots/Indicators in Visual Studio

To edit an already existing cBot/indicator inside Visual Studio, right-click on it in the cTrader UI. In the below menu, press on 'Edit in Visual Studio'.

Image title

Creating New cBots/Indicators in Visual Studio

To create a new cBot/indicator, create a new project inside Visual Studio and choose 'Class Library'. Name your project however you would like your cBot/extension to be named.

Image title

In the 'Class1.cs' file, write the code for your cBot/indicator. Afterward, open the 'Build' menu and select 'Build Solution'. Alternatively, press Ctrl+Shift+B. After a successful build, you should see a .algo file in the directories specified above.

If you do not see your new cBot/indicator in the cTrader UI, relaunch the cTrader platform, and it should appear among other extensions.

For additional information about Visual Studio, please, consult this documentation.

Visual Studio Code

Compared to Visual Studio, VS Code is a much-more lightweight code editor, which justifies its soaring popularity among developers. To create cBots/indicators via VS Code, you have to meet the following prerequisites.

  • Install the .NET SDK.
  • Install a C# extension within VS Code.

To install a suitable extension, open the extensions marketplace, type in C# in the search bar and choose either the official Microsoft extension or any other suitable add-on.

Image title

Afterward, you will have to use .NET command-line interface (CLI) to create a solution and a class library project containing your cBot/indicator.

Creating an Indicator

To create an indicator via VS Code, open a new PowerShell terminal by choosing 'New Terminal' in the 'Termial' menu. Alternatively, press Ctrl+Shift+`.

1. In the terminal, open your Documents\cAlgo\Sources\Indicators directory.

cd Documents\cAlgo\Sources\Indicators\

2. Create a new directory for your indicator. It will contain your indicator solution and project. Switch to this directory.

mkdir MyIndicator
cd MyIndicator

3. Create a new .NET solution inside the newly created directory.

dotnet new sln

4. Create a class library .NET project.

dotnet new classlib --name MyIndicator

This project will be created inside the MyIndicator directory.

5. Add the project to your solution.

dotnet sln add .\MyIndicator\MyIndicator.csproj

6. Install the cTrader.Automate Nuget package.

dotnet add .\MyIndicator\MyIndicator.csproj package cTrader.Automate

7. Add an indicator class. To do so, rename the Class1.cs file to MyIndicator.cs and open it, then replace its contents with the following placeholder code. Edit the code inside VS Code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using cAlgo.API;

namespace cAlgo;

[Indicator(AccessRights = AccessRights.None)]
public class MyIndicator : Indicator
{
    [Parameter(DefaultValue = "Hello world!")]
    public string Message { get; set; }

    [Output("Main")]
    public IndicatorDataSeries Result { get; set; }

    protected override void Initialize()
    {
        // To learn more about cTrader Automate visit our Help Center:
        // https://help.ctrader.com/ctrader-automate

        Print(Message);
    }

    public override void Calculate(int index)
    {
        // Calculate value at specified index
        // Result[index] =
    }
}

8. Build the project.

dotnet build --configuration Release

You can also build your project with source code.

dotnet build --configuration Release -p:IncludeSource=True

You can change the build configuration to Debug if you want to debug your indicator.

After a successful build you will have the indicator ".algo" file inside it's project bin folder and also inside Documents\cAlgo\Sources\Indicators\ folder if you are using Windows.

Creating a cBot

Creating a cBot is similar to creating an indicator except you have to use the Robot class instead of the Indicator class and the Documents\cAlgo\Sources\Robots\ directory instead of Documents\cAlgo\Sources\Indicators\.

You can use the below sample code for your cBot class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using cAlgo.API;

namespace cAlgo.Robots;

[Robot(AccessRights = AccessRights.None)]
public class MyRobot : Robot
{
    [Parameter(DefaultValue = "Hello world!")]
    public string Message { get; set; }

    protected override void OnStart()
    {
        // To learn more about cTrader Automate visit our Help Center:
        // https://help.ctrader.com/ctrader-automate

        Print(Message);
    }

    protected override void OnTick()
    {
        // Handle price updates here
    }

    protected override void OnStop()
    {
        // Handle cBot stop here
    }
}

Note

You should use Documents\cAlgo\Sources only if you are using a Windows system, otherwise you can create and build your indicator/cBot projects in any directory.

Other .NET IDEs

If you so choose, you can use other IDEs to work with cBots and indicators. Follow these steps to create valid cTrader extensions.

  1. Create a .NET class library project.
  2. Install the cTrader.Automate Nuget package.
  3. Create a class file containing your cBot/indicator source code. Indicators inherit from the Indicator class while cBots must inherit from the Robot class.
  4. Build your extension. For more information on compiling/building, please, check the related section of this documentation.

Last update: December 8, 2022

Comments