How to Reference Third-Party Libraries in cTrader Algo
Third-party libraries enable users to expand and improve the functionality of their cBots, indicators and plugins. Since cTrader Algo is developed with .NET 6.0, users can add and use third-party libraries built with .NET 6.0 in their algo projects.
In this article and the accompanying video, you will learn how to reference third-party libraries in the cTrader Algo API. Using the Discord.Net library, we will demonstrate how to send messages to a Discord channel.
Add a Reference by Installing a NuGet Package¶
In our first example, we plan to add a reference in our cBot project by installing a package from the NuGet library. NuGet is the official package manager for the .NET platform used to create, consume and distribute packages or libraries.
Note
If you develop algorithms in Visual Studio, use the method of adding a reference described below. Otherwise, navigate to the next section of this article.
In cTrader Algo, create a cBot. Click the 'New' button, enter its name and then click the 'Create' button.
Next, go to the NuGet website, input 'Discord' in the text field and then select the Discord.Net library from the results.
Click the 'Package Manager' tab and then copy the command there.
Now, return to cTrader and click 'Edit in Visual Studio'.
Your cBot project should open in the Visual Studio application on your computer.
In Visual Studio, open the 'Tools' menu, select 'NuGet Package Manager', then 'Package Manager Console'.
Paste the command you copied earlier from the NuGet website and run it.
1 |
|
Having installed the required package, we can proceed to develop our cBot example.
Start by declaring important parameters, such as the Discord bot token and channel ID.
1 2 3 4 5 |
|
Then declare a Discord client object and a message channel object.
1 2 |
|
In our OnStart()
method, write a simple code to send a message to our channel when the cBot starts.
1 2 3 4 5 6 7 8 9 10 |
|
You can copy the full code below:
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Now, build the cBot.
Return to cTrader. Click 'Add instance', specify your preferred parameters in the window and then click the 'Add instance' button.
Enter your Discord bot token and channel ID.
Start the cBot instance. When the 'Access Request' window appears, click 'Allow' to continue.
The cBot should start running, and you should see a message arrive in the Discord channel.
Add a Reference through the cTrader UI¶
Some libraries are not available as NuGet packages. If you plan to use such libraries in your cBot project, you have to add their DLL files directly as references.
Here, we intend to demonstrate how you can reference the same Discord.Net library by adding its DLL files.
Let’s return to Visual Studio and uninstall the Discord.Net library we installed in the previous subsection. Open the NuGet Package Manager Console as you did before and run the following command:
1 |
|
The Discord packages will be removed as references in our Discord Message Example cBot. If the cBot is rebuilt now, the build will end with several error messages due to the missing libraries.
Go back to cTrader and add the required libraries using the 'Manage references' functionality at the top.
When the 'Reference Manager' window appears, navigate to 'Browse' and then click the 'Browse' button.
Select the DLL file and then perform the same action for all the DLL files you want to reference.
Click 'Apply' and build the cBot again.
Now, when you run a cBot instance, the build will be completed without errors, and you should see the message arrive in the Discord channel.
This article has demonstrated how to reference third-party libraries in cTrader Algo using two different methods. To learn more about the cTrader Algo API, see our documentation or post a question on our forum.