Skip to content

Introduction

What's cTrader Automate?

cTrader Automate is an integral part of cTrader desktop application, it allows you to extend the capabilities of cTrader desktop by developing custom trading indicators and cBots.

What's a cBot?

A cBot is a trading robot, or think of it as a program that runs inside cTrader desktop and allows you to easily execute trading operations or manage them.

With a cBot you have full access to all cTrader charting and trading features.

What's a custom indicator?

A custom indicator is another type of extension that you can develop by using cTrader desktop, it allows you to run a program on a cTrader chart with access to price data, chart drawing, account data, and much more.

How to develop a cTrader custom indicator or a cBot?

To develop a cTrader custom indicator or a cBot you use C# programming language, and .NET.

You must have a basic understanding of C# and .NET, if you don't you can find lots of free tutorials and video courses on internet for C# and .NET.

Each indicator or cBot will be hosted by cTrader desktop on a separate child process under cTrader desktop main process, and they run on a fully isolated process from cTrader desktop.

Creating your first cTrader custom indicator

Let's create a simple moving average indicator, to create a new custom indicator on cTrader follow these steps:

1. Go to "Automate" section on your cTrader desktop:

Image title

cTrader Automate

2. Switch to "Indicators" tab:

Image title

Indicators

3. Click on "New" button, cTrader Automate will create a new custom indicator with some default code on it:

Image title

Creating New Indicator
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace NewIndicator
{
    [Indicator(AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate

            Print(Message);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = 
        }
    }
}

4. Now, let's rename our indicator to "Custom Moving Average", to do that right click on indicator and select "Rename" options:

Image title

Renaming Indicator

Then type the new name and press "Enter".

5. Now we have to write the indicator code, clear the default code from indicator and replace it with:

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

namespace CustomMovingAverage
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class CustomMovingAverage : Indicator
    {
        [Parameter("Periods", DefaultValue = 10)]
        public int Periods { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {           
        }

        public override void Calculate(int index)
        {
            double sum = 0;

            for (int i = 0; i < Periods; i++)
            {
                sum += Source[index - 0];
            }

            Result[index] = sum / Periods;
        }
    }
}

6. Build the indicator by click on indicator build button:

Image title

Building Indicator

7. Create an instance of indicator by attaching it on a chart:

Image title

Creating Indicator Instance

8. Here is the result:

Image title

Custom Moving Average Indicator

9. Now you might wondering on how to use this indicator on normal cTrader desktop charts, for that go to cTrader trade section, select a chart, then click on toolbar "Indicators" icon:

Image title

Adding Indicator to Chart

Type "Custom Moving Average" on Search text box and it will appear, click on it.

For now don't worry about the indicator code, we will explain it in more detail on Creating and Indicator.

Creating your first cTrader cBot

Let's create a simple cBot that:

  • Opens a buy position when there is an up or bullish bar/candle
  • Opens a sell position when there is a down or bearish bar/candle
  • Closes a buy position when there is a down or bearish bar/candle
  • Closes a sell position when there is an up or bullish bar/candle

Follow these steps:

1. Go to cTrader Automate:

Image title

cTrader Automate

2. Switch to "cBots" tab:

Image title

cBots

3. Click on "New" button:

Image title

Creating New cBot

It will create a new cBot with some default code on it:

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate

            Print(Message);
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

4. Now, let's rename our cBot to "Bar Buy Sell", to do that right click on cBot and select "Rename" options:

Image title

Renaming cBot

Then type the new name and press "Enter".

5. Now clear all the code from cBot and replace it with:

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

namespace BarBuySell
{
    [Robot(AccessRights = AccessRights.None)]
    public class BarBuySell : Robot
    {
        [Parameter(DefaultValue = "BarBuySell")]
        public string Label { get; set; }

        protected override void OnBar()
        {
            var lastBar = Bars.Last(1);
            var position = Positions.Find(Label, SymbolName);

            if (lastBar.Close > lastBar.Open)
            {
                if (position is not null && position.TradeType == TradeType.Sell) _ = ClosePosition(position);

                _ = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, Label);
            }
            else if (lastBar.Close < lastBar.Open)
            {
                if (position is not null && position.TradeType == TradeType.Buy) _ = ClosePosition(position);

                _ = ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.VolumeInUnitsMin, Label);
            }
        }
    }
}

6. Now build the cBot by clicking on "Build" button:

Image title

Building cBot

7. To use our cBot we have to create an instance of it, right click on it and select "Add Instance" option from context menu:

Image title

Adding Instance to cBot

8. Now we can either run it on our created symbol instance, backtest it, or optimize it:

Image title

Instance of cBot

9. You can access this cBot also from cTrader desktop Trade section, go to Trade section, there is a cBots icon:

Image title

Trade Chart cBots Icon

You can add it on a chart and run it.

For now don't worry about the cBot code, we will explain it in more detail on Creating and Running a cBot.


Last update: June 26, 2022

Comments