Using Visual Studio and Other 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.
External IDEs
Here is the full list of external IDEs currently supported by cTrader.
- Visual Studio
- Visual Studio Code
- Rider
- Sublime Text
You can use any of them to edit any algo whose source code you have access to. Simply select such an algo in the list and open the 'Edit in...' dropdown at the top of the code editor window. In the menu, select the IDE that you want to open the algo in.
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 customisable settings facilitating work with C# and .NET. Our documentation only covers these IDEs.
Visual Studio¶
Installing the 'cTrader.Automate' Nuget Package¶
Before creating a cBot or an indicator, 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 incTrader
into the search bar. - Click on the
cTrader.Automate
package and select 'Install'.
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, 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 an .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.
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.
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, 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.
Afterward, you will have to use .NET command-line interface (CLI) to create a solution and a class library project containing your cBot/indicator.
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.
Creating an Indicator¶
To create an indicator via VS Code, open a new PowerShell terminal by choosing 'New Terminal' in the 'Terminal' 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 |
|
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 |
|
Other .NET IDEs¶
If you so choose, you can use other IDEs to work with cBots, plugins, and indicators. Follow these steps to create valid cTrader extensions.
- Create a .NET class library project.
- Install the
cTrader.Automate
Nuget package. - Create a class file containing your cBot/indicator source code. Indicators inherit from the
Indicator
class while cBots must inherit from theRobot
class. - Build your extension. For more information on compiling/building, check the related section of this documentation.