Skip to content

Using Visual Studio / Other .NET IDEs

Creating new cBots/Indicators

To create a new cBot/Indicator you just have to create a .NET class library project on your favorite .NET IDE or by using .NET CLI, then reference the cTrader.Automate Nuget package on your project.

You can install the Nuget package by using: Install-Package cTrader.Automate

After installing the cTrader.Automate Nuget package whenever you build the project it will create a ".algo" file for your project inside it's build (bin) directory and in case of Windows it will also create a ".algo" file in Documents -> cAlgo -> Sources -> Robots/Indicators directory.

The preferred way to created a new cBot/Indicator is to use cTrader -> Automate New button, as it will create the project folder inside Documents -> cAlgo -> Sources -> Robots/Indicators directory and it will generate the cBot/Indicator class files with default template, but it's only possible if you are using Windows, as cTrader desktop is only available on Windows.

For other operating systems you should use the conventional .NET way to create the cBot/Indicator project as explained before.

Visual Studio

Microsoft Visual Studio is a powerful IDE which can be used for development of cBots and Custom Indicators.

In order to edit cBots or Custom Indicators in Microsoft Visual Studio, you have to first Install Visual Studio on your system.

If you have a proper version of Microsoft Visual Studio installed, you can perform the “Edit in Visual Studio” command from the context menu of a cBot or a Custom Indicator:

Image title

You can use Visual Studio to create, build, and debug cTrader Automate cBots/Indicators.

You can visit Visual Studio Documentation to find out more information about Microsoft Visual Studio.

Visual Studio Code

Visual Studio Code is a very popular lightweight code editor, to use Visual Studio Code for cBot / Indicator development you have to:

  1. Install .NET SDK on your system
  2. Install a C# extension to Visual Studio Code like ms-dotnettools.csharp

Then you have to use dotnet CLI to create a solution and a class library project which will contain your cBot or indicator.

Creating an Indicator

1. Open Terminal on VS code (if you are in Windows go to your Documents\cAlgo\Sources\Indicators directory):

cd Documents\cAlgo\Sources\Indicators\

2. Create a new directory for your indicator, this directory will contain your indicator solution and project:

mkdir MyIndicator

3. Create a new .NET solution inside that directory:

dotnet new sln

4. Create a class library .NET project:

dotnet new classlib --name MyIndicator

This project will be created inside MyIndicator directory.

5. Add the project to your solution:

dotnet sln add .\MyIndicator\MyIndicator.csproj

6. Install the Automate Nuget package:

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

7. Now your Indicator project is ready, but you have to add an indicator class before building, rename the Class1.cs file to MyIndicator.cs and open it, then replace it's content with:

 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 it 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 build finished 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 Indicator class and Documents\cAlgo\Sources\Robots\ directory instead of Documents\cAlgo\Sources\Indicators\.

You can use this 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 project on any directory.

Other .NET IDEs

You can also use any other .NET IDE or code editor like Visual Studio Code or Jetbrains Rider, just follow this steps:

  1. Create a .NET class library project
  2. Install cTrader.Automate Nuget package
  3. Create a class file that will contain your indicator / cBot source code file, if it's a cBot then it should derive from Robot class and if it's an indicator it should derive from Indicator class
  4. Build it, for more detail regarding how to compile/build please check Compiling/Building page.

Last update: June 26, 2022

Comments