Skip to content

How to Create a cBot for cTrader in 5 Minutes

In this article and its corresponding video, we are going to describe a quick way to create a cBot in cTrader Algo. The cBot that we will create will implement a simple trading strategy called 'Three White Soldiers and Three Black Crows'. You can learn more about this strategy in our knowledge base.

Before following our instructions, make sure you have downloaded and installed the cTrader Desktop application from the official website. Launch it, and navigate to the cTrader Algo section. You can find it in the left panel. Click on the 'Algo' tab, and the cTrader Algo section should appear.

Add a New cBot

To create a new cBot, simply click on the 'New' button. Then, select 'Blank' and confirm your choice.

You will see a new item added to the list of cBots with 'New cBot' as its name. Select this bot, and the code editor window to the right will contain a code template to help you get started.

The next step is to rename the new cBot. To do so, right-click on it and select 'Rename' or press F2. We will use 'Three White Soldiers and Three Black Crows' as the new name of our cBot. Once done, press Enter. Your new cBot has been successfully created and is now ready for you to start coding your strategy.

Add the cBot Settings

Before we start implementing a trading strategy, we will take a look at the basic cBots parameters and methods. You can also consult our documentation to familiarise yourself with what different parameters and methods do.

  • cBot parameters allow for defining configurable attributes such as volume to be traded, or stop loss/take profit distances.
  • cBot methods define how your cBot behaves when specific events occur. There are four main events you need to handle when developing a cTrader cBot; all four of them are handled by four different methods provided by cTrader. These methods are OnStart(), OnTick(), OnBar(), and OnStop(). Note that the OnBar() method is missing in the default code template.

The following table defines these four methods in detail.

Method Name Definition
OnStart() This method is triggered when a cBot instance starts operating. It is used to initialise any variables you plan to use in your cBot such as indicators, counters, event handlers, or timers.
OnTick() This method is triggered on each incoming tick on the trading chart on which a cBot instance is running. Inside the OnTick() method, you can program custom entry and exit conditions as well as any other auxiliary function you need to run when a new tick arrives.
OnBar() This method is similar to OnTick(); however, it is triggered only when a new bar is drawn on the chart on which your cBot is running. Analogously to OnTick(), you can use it to program custom entry and exit conditions or any other functions that need to run on the formation of each new bar.
OnStop() This method is triggered when a cBot instance stops. It is used to perform final operations such as closing positions.

Add the Trading Logic

We will now add the code for our trading strategy. Our cBot should open a 'Buy' position when three green bars are formed on the chart and a 'Sell' position when there are three consecutive red bars.

First, we need to define the parameters of our cBot. These parameters will be fully customisable from the cTrader UI. We will define three parameters in our cBot.

  1. The volume of each trade.
  2. The stop loss in pips.
  3. The take profit in pips.

Paste the following code into your code editor just below the curly bracers after the class declaration (public class NewcBot : Robot)

[Parameter(DefaultValue = 1000)]
public double Volume { get; set; }
[Parameter(DefaultValue = 10)]
public double TakeProfit { get; set; }
[Parameter(DefaultValue = 10)]
public double StopLoss { get; set; }

We now need to implement the logic behind our trading strategy. Paste the following code in your OnBar() method. Remember that this method is called every time a new bar is formed.

//Three White Soldiers
if(Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1) 
&& Bars.ClosePrices.Last(2) > Bars.OpenPrices.Last(2)  
&& Bars.ClosePrices.Last(3) > Bars.OpenPrices.Last(3))
    ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "", StopLoss, TakeProfit);
//Three Black Crows
if(Bars.ClosePrices.Last(1) < Bars.OpenPrices.Last(1) 
&& Bars.ClosePrices.Last(2) < Bars.OpenPrices.Last(2)  
&& Bars.ClosePrices.Last(3) < Bars.OpenPrices.Last(3))
    ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "", StopLoss, TakeProfit);

Now we will examine the code more closely. cTrader gives you access to the chart historical bars, so we can check whether the last three bars are all green or all red, respectively. In the OnBar() method above, we first check whether the last three bars were all green. We then evaluate whether these bars were all red.

If one of the two conditions is true, we place a market order via the ExecuteMarketOrder() method. It takes several inputs (arguments), with the most important of them being the direction of the trade, the symbol, the traded volume, the stop loss, and the take profit.

In our case, the order direction (TradeType.Buy and TradeType.Sell) is defined by the trading strategy conditions. The symbol name is taken directly from the chart that our cBot is running on (SymbolName). The trading volume (Volume), stop loss (Stop Loss), and take profit (TakeProfit) are all taken from the cBot parameters we have defined.

Build and Run the cBot

We now need to check whether our cBot builds successfully. By default, the build result window is located directly beneath the code editor. If you do not see it, click on the 'Layout' button in the top-most bar of the cTrader UI and select 'Build Result'. The keyboard shortcut for this action is Ctrl + W

Afterward, press the 'Build' button located in the topmost bar of the cTrader UI. Alternatively, right-click on your cBot and click on 'Build' in the newly appeared menu, or simply press Ctrl + B.

Image title

If the build is successful, you will see a green message in the 'Build Result' area at the bottom of the code editor.

Image title

Alternatively, if there are issues with your code, you will see a red message and a detailed summary of all build errors.

Image title

After your cBot has been built successfully, you can start using it. All you need to do is add an instance on a trading chart. To do so, simply click on the 'plus' icon to the right of the name of your cBot. You will see a list of all symbols on which you can run your cBot.

Image title

We are going to select EURUSD. After you choose a symbol, a new cBot instance will be added. You will see a new item in the list of cBots with the symbol name and the timeframe you would like to trade.

You should also see the parameters you have coded in the 'Parameters' tab directly beneath the trading chart.

Image title

You can change these parameters however you want or simply leave them at their default values.

Additionally, you will see a 'Play' button at the top of the EURUSD chart.

Image title

To launch your instance, press this button. Alternatively, a copy of this button is also located to the right of your instance in the list of cBots to the left. Either one of these buttons will start the cBot.

After clicking on either button, you should see it turn orange, meaning that an instance of our cBot is now up and running. the 'Log' tab, you should also see a new line stating that the cBot has now started.

Backtest the cBot

Our cBot may take a long time to open a position on a real trading chart. We are going to run a quick backtest to see it in action on historical data. To do this, open the 'Backtest' tab.

Image title

Use the calendar slider closer to the top of the screen to select the date range that you would like to use for backtesting. Enable the 'Visual Mode' flag to track the results of backtesting as they occur.

We are going to move the slider back one month. Click on the 'Play' button now to launch the cBot on the chosen historical data.

Afterward, you will be able to see new positions being opened and/or closed in the tab immediately below the trading chart and to the right of the 'Parameters' section. Specifically, after detecting either of the two patterns we have coded, the cBot will place a 'Buy' order or a 'Sell' order.

Running a backtest is a useful way to analyse whether your cBot is working correctly.


We hope that this article has been helpful in demonstrating how you can create an automated trading robot using cTrader. To learn more, consult our extensive documentation or post a question on our forums.

You can also subscribe to your YouTube channel to be alerted when we post a new video.

Subscribe to our YouTube channel