Skip to content

Key Types of cBots

cBots are versatile tools that, depending on their code and logics, may fulfil various roles and functions ranging from fully automated trading to providing support for the implementation of manual trading strategies. In this article and its corresponding video, we will define the key types of cBots, outline their key differences and list some best practices for creating and using cBots of various types.

Defining the Key Types of cBots

Broadly speaking, there exist four key types of cBots.

Note

Note that the below categories differ purely based on cBot behaviors. No categories are specified within the actual cBot code. Additionally, some developers may also create custom classifications of cBots.

  • Automated Trading Strategies. As implied by the name, cBots of this type autonomously execute trading strategies. For example, a 'Three Black Crows & Three White Soldiers' cBot places a new buy order after encountering three consecutive green candles, and a new sell order when facing three consecutive red candles.
  • Scripts. cBots belonging to this type perform an action on start and stop after execution is complete. As an illustration, a script cBot may establish a stop loss for all currently open positions regardless of their direction, volume, or instrument.
  • Trading Assistants. Trading assistants are designed to perform some sort of helpful action that is complementary to a manual or automatic trading strategy. For example, a cBot of this type can manage your trailing stop losses using custom rules that are different from the built-in trailing stop loss functionality offered by cTrader.
  • Trading Panels. cBots of this type do not trade autonomously; instead, they create custom controls that may be used for various purposes. For instance, a cBot may launch a WebView of a Forex news aggregator while integrating custom 'Buy' and 'Sell' buttons as well as a symbol selector inside the WebView, allowing the user to trade without leaving the news site.

In the below sub-sections, we discuss each of the above types in detail, outline their benefits and limitations, and suggest various use cases.

Automated Trading Strategies

cBots belonging to the automated trading strategy type are complex but powerful. They have to correctly implement a trading strategy including all its aspects such as risk management, position sizing, and technical analysis.

You can create a simple automated trading strategy in less than two minutes by simply replacing the default new cBot template with the below code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class BasicTrendBot : Robot
    {
        protected override void OnBar()
        {
            if (Bars.ClosePrices.Last(1) > Bars.ClosePrices.Last(2))
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "FollowTrend");
            }
            else if (Bars.ClosePrices.Last(1) < Bars.ClosePrices.Last(2))
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "FollowTrend");
            }
        }
    }
}

The cBot opens a new position on every bar. A long position is opened if the price has gone up one bar ago in comparison to the bar that had preceded it. Conversely, a short position is opened if the price has gone down.

Backtesting and Optimisation

Automated trading strategies typically include a lot of 'moving parts' and parameters. As a result, such cBots often need to be extensively backtested and optimised before being launched on live accounts. Fortunately, cTrader provides built-in tools that can handle backtesting and optimisation for you.

cTrader CLI

Automated trading strategies typically need to run for long periods of time so that they can react to various technical analysis signals. To save on RAM and CPU consumption in these cases, you can launch cBots via cTrader CLI without ever opening cTrader Desktop (note that it is possible to launch any cBot via cTrader CLI, not just cBots of this type).

Scripts

Scripts are typically needed to perform some sort of action that would be difficult or time-consuming to do manually. These actions are typically taken on cBot start so that you can launch a script, observe the results of it working, and then stop its instance on success to save on RAM and CPU consumption.

As an illustration, a script can close all positions whose gross profit exceeds 50 units of a trader’s account deposit currency. In effect, the script would act as a universal take profit for all open positions that you can trigger at any time. Here is how the code for this script would look like.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using cAlgo.API;
using cAlgo.API.Collections;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class BasicScriptBot : Robot
    {

        protected override void OnStart()
        {
            foreach (var position in Positions) 
            {
                if (position.GrossProfit > 50) 
                {
                    ClosePosition(position);
                }
            }
            Stop();
        }
    }
}

After launching the cBot, we should see several profitable positions being gradually closed.

Stopping Scripts

As scripts need to only execute a specific action once, you can instruct cTrader to immediately close a script after execution is completed successfully. As shown in the above example, you can do so by calling the Stop() method.

Trading Assistants

The purpose of trading assistants is to regularly perform helpful actions. While this makes them similar to scripts, scripts only perform an action on instance start. In contrast, trading assistance are designed to continuously react to market conditions or other factors, and execute operations in response to certain patterns.

For an illustration, consider a cBot that automatically hedges your positions as soon as you open them. The code for this cBot could look as follows.

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class BasicAssistantBot : Robot

    {
        private bool hasHedgedPosition = false;

        protected override void OnStart()
        {
            Positions.Opened += Positions_Opened;
        }

        private void Positions_Opened(PositionOpenedEventArgs args) 
        {
            if (!hasHedgedPosition) 
            {
                hasHedgedPosition = true;
                var position = args.Position;
                var oppositeTradeType = position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                ExecuteMarketOrder(oppositeTradeType, SymbolName, position.VolumeInUnits / 2);
            }
            else 
            {
                hasHedgedPosition = false;            
            }
        }
    }
}

Upon opening a position, the cBot immediately tries to open another one in the opposite direction; the volume of this new position is exactly half of the volume of the original position.

Note that we are using the hasHedgedPosition field to avoid an infinite loop so that the cBot only hedges a new position once.

Callback Functions

Trading assistance often use callback functions acting as handlers for various events. To find out which events you can handle for a certain class, simply open the built-in API documentation, search for the required class, and navigate to the 'Events' section in the table of contents.

Trading Panels

Trading panels display custom controls interacting with which triggers various actions. Usually, trading panels offer quality-of-life improvements to the default cTrader UI. As an illustration, a cBot may display a 'Buy' button, clicking on which results in cTrader executing a market order for a predefined volume.

To create such a cBot, simply use the 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
using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class BasicPanelBot : Robot
    {

        Button buttonBuyOrder;

        protected override void OnStart()
        {

            buttonBuyOrder = new Button
            {
                Text = "Buy",
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            buttonBuyOrder.Click += ButtonBuyOrder_Click;

            Chart.AddControl(buttonBuyOrder);
        }

        private void ButtonBuyOrder_Click(ButtonClickEventArgs args) 
        {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, 10000);
        }
    }
}

Upon clicking on the 'Buy' button, a new market order for 10,000 units is placed. The convenient placement of the button allows for quickly reacting to new market opportunities.

Combining Trading Panels With Other Types of cBots

The purpose of trading panels is to provide custom UI controls allowing users to perform various operations. As such, you can combine trading panels with other types of cBots. For example, you can use a trading panel to add a custom 'Hedge' button that, on click, hedges all currently opened positions similarly to the trading assistant example above.

Summary

All four categories of cBots have valid uses and can be combined depending on your preferences. For example, you can simultaneously launch an automated trading strategy and a trading assistant to achieve the best possible results. You can experiment with cBots of various types to make sure that your preferred approach to trading is executed fully and without fault.

Subscribe to our YouTube channel