Skip to content

Creating and Running a cBot

Action Map

The following diagram showcases the action flow of creating and running a new cBot.

graph TB
  B([Add a New cBot]) ==> C([Edit the Sample Code]);
  C ==> D([Save and Build Your cBot]);
  D ==> E([Create and Customize a cBot Instance]);
  E ==> F([Run a cBot Instance]);

Add a New cBot

Open the 'New ...' menu to the right of the search bar and select 'New cBot'. If the 'New cBot' option is already chosen, simply click on it.

Image title

cTrader Automate will automatically create a new cBot containing sample code. You should see a 'New cBot' extension added to the list of cBots.

You can rename your new bot by selecting it and pressing F2. Alternatively, right-click on the new extension and choose 'Rename'.

Edit the Sample Code

Click on your new bot to open the code editor window containing the following 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
28
29
30
31
32
33
34
35
36
37
  using System;
  using System.Linq;
  using cAlgo.API;
  using cAlgo.API.Indicators;
  using cAlgo.API.Internals;
  using cAlgo.Indicators;

  namespace cAlgo.Robots
  {
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        /* This method performs an action of your choosing 
        when a cBot is launched. */
        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        /* This method performs an action of your choosing
        every tick. */
        protected override void OnTick()
        {
            // Put your core logic here
        }

        /* This method performs an action of your choosing
        when a cBot stops working. */
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
  }

As we have discussed previously, the cBot attribute (Robot) along with its optional properties such as TimeZone and AccessRights, precedes the cBot class (NewcBot) declaration.

The OnStart() method is called every time when an instance of your cBot is launched. In turn, the OnTick() method is called every tick. Last but not least, the OnStop() method is called every time an instance of your new cBot stops operating.

Please, note that, as the OnTick() method performs a certain action every tick, it is CPU-intensive. In many cases, there is also very little practical need for performing a trading operation every tick, particularly when trading stocks or indices.

Instead, you could replace the OnTick() method with the OnBar() method. As implied by its name, it is only called at the open of every bar, significantly reducing CPU loads.

These are far from the only methods you can use to set up the behavior of your cBot. However, they are the simplest to use if you are just starting out with C#. Please, refer to the 'References' section to learn more about the classes, methods, and properties you can use when coding cBots. In addition, you can always choose to write custom methods as shown in our introduction to C# and .NET.

Given what you have just learned and the examples provided in other parts of this documentation, edit the code of your cBot to suit your requirements.

Image title

To attain the same result, you can copy and paste the below code after the namespace declaration.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    [Robot()]
    public class Sample_cBot : Robot
    {
        protected override void OnStart()
        {
            var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, 10000);

            if (result.IsSuccessful)
            {
                var position = result.Position;
                Print("Position entry price is {0}", position.EntryPrice);
            }
        }
    }

Save and Build Your cBot

Save your code by clicking the 'Save' button located to the upper-left of the code editor window. Alternatively, press Ctrl+S. You can also right-click anywhere in the code editor and choose 'Save' from the newly opened menu.

Image title

Before you can use your algorithm to trade, you need to convert it into a working cBot. To build a cBot, click the 'Build' icon at the top of the code editor or in the cBot menu. The keyboard shortcut for this action is Ctrl+B.

If the build succeeded you will be notified with the respective message in the 'Build Result' viewer. If the build fails, this window will, instead, display a summary of all errors encountered during building. Clicking on an error description in the 'Build Result' viewer will show you the exact place in your code where this error occurs.

If there are changes to your code made since the last build, you should see a red '*' sign next to the 'Build' icon. In this case, build the bot again before running it.

Create and Customize a cBot Instance

If you have successfully completed all of the above actions, your cBot should be ready for trading. To start using it, create a new instance by performing one of the following actions.

  • Click on the '+' icon to the right of the extension name. In the menu that appears, select a symbol that you would like to trade.

Image title

  • Click on the 'three dots' icon to the right of the cBot name and select 'Add Instance'. cTrader will automatically create a new instance trading the EURUSD symbol using h1 as the timeframe.

You should see a new instance located directly below your cBot.

Image title

If you have specified any customizable parameters in your code, they will be listed in the 'Parameters' tab located to the left of the 'Trade Watch' display. Before running an instance, make sure that these parameters match your requirements.

Image title

You can save your current parameter values to a locally stored .cbotset file by clicking on the 'Save' icon in the upper-right corner of the 'Parameters' tab.

Alternatively, you can upload parameter values from an already existing .cbotset file by pressing the 'Folder' icon.

Run a cBot Instance

To run the newly created instance, press the blue 'Start cBot' button to the right of the instance name. If this if your first time using cBots or indicators, you will see a warning window. Check the "I understand how algorithmic trading works..." flag and press 'OK'. If prompted, provide your robot with the required access rights.

Image title

To stop the newly created instance, press the red 'Stop' button. Please, note that you can change the symbol and timeframe for the currently chosen instance but only if it is not running. It is impossible to change the parameters of any currently running instances.


Last update: December 8, 2022

Comments