Skip to content

Optimise a cBot in cTrader

This article explains how to optimise trading bots in cTrader Windows and Mac.

It can be difficult to specify an optimal set of initial parameters for a cBot. Luckily, cTrader offers a built-in cBot optimisation feature. Optimisation takes your code and runs it several times with each pass being based on various parameter values. It then presents you with a set of customisable results which you can use to define the optimal parameter configuration.

To use this feature, select a cBot instance and switch to the Optimisation tab.

Image title

Afterwards, define the backtesting period for optimisation by using the dropdown calendars or by dragging the slider.

Image title

Optimisation settings

As optimisation is essentially just a series of backtests, you can specify backtesting settings similarly to how it is done in the Backtesting tab.

Optimisation parameters

Click the Parameters button to the left of the calendar slider. You should see the following window.

Image title

In it, check the flags next to the cBot parameters that you would like to optimise. The Timeframe parameter is available for all cBots.

Optimisation criteria

Press the Criteria button located to the right of the Optimisation parameters button. cTrader will open the following tab.

Image title

Optimisation criteria define how the optimisation algorithm ranks your result after backtesting. You can choose between the following options:

  • Standard - a series of predefined criteria that you can aim to either minimise or maximise. To do this, choose a criterion from a dropdown menu to the right and select the optimisation direction in its related menu to the left. To add a new criterion, click Add criterion.
  • Custom - a custom criterion defined within your cBot code using the GetFitness() method as shown in the below examples.
1
2
3
4
5
protected override double GetFitness(GetFitnessArgs args)
{
    // Maximise the winning trades/losing trades ratio.
    return args.WinningTrades / args.LosingTrades;
}
1
2
3
4
5
6
protected override double GetFitness(GetFitnessArgs args)
{
    /* Maximise the winning trades/losing trades ratio
    while giving winning trades more weight. */
    return Math.Pow(args.WinningTrades, 2) / args.LosingTrades;
}

Multi-criteria calculations

If multiple criteria are used to calculate parameter effectiveness, cTrader will use all of them equally to calculate the fitness value for an optimisation pass.

Specifically, the platform multiplies the values of all maximising criteria and separately multiplies the values of all minimising criteria. Subsequently, it divides the absolute multiplication value for the maximising criteria by the same value for the minimising criteria.

The following pseudocode shows exactly how multi-criteria fitness values are calculated.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
numerator = 1.0
if (valuesToMaximize.Length > 0)
    numerator = Abs(Multiply(criteriaValuesToMaximize))

denominator = 1.0
if (valuesToMinimize.Length > 0)
    denominator += Abs(Multiply(criteriaValuesToMinimize))

/* The 'sign' variable can be either +` or -1 depending on whether
there are criteria for which their values are less than 0. */
sign = criteriaValuesToMinimize.Concat(criteriaValuesToMaximize).Any(v => v < 0) ? -1 : 1

fitness =  sign * numerator / denominator

Method

Click the Method button (represented either as GA or # depending on which option is chosen). In the below menu, choose which optimisation method is used by the platform.

Image title

The following options are available:

  • Genetic Algorithm - the algorithm emulates the biological evolution process. Please see a separate section for its detailed description.
  • Grid (#) - the algorithm sequentially backtests each possible set of parameters.

Note

In our estimation, the genetic algorithm is significantly faster for finding optimal parameter values.

Resources

Click the Resources button to select which percentage of your CPU load is allocated for backtesting. Move the slider to set the CPU usage percentage.

The more resources you allocate, the faster the optimisation process will be completed. However, you may see a drop in performance when using other applications.

Image title

Note that the CPU resources can also be adjusted during optimisation itself.

Start and manage the optimisation procedure

To proceed with optimisation, click the Play button to the right of the calendar slider. Depending on your machine resources, the optimisation settings, and cBot complexity, optimisation may take some time.

The UI bar immediately below the calendar slider will provide information about the number of backtesting passes completed, the elapsed time and the remaining time estimate.

Image title

The central screen in the Optimisation tab, meanwhile, will provide a real-time grid containing information about all of the backtesting passes that the algorithm has completed.

Image title

To apply a set of parameters tested during a certain pass, click the Apply button. It only becomes active if no optimisation procedures are currently running.

Alternatively, check the Autoselect the best pass flag in the upper-left corner for cTrader to automatically select the pass (and the parameters) that has achieved the best possible result according to your specified optimisation criteria.

Optimisation results

After optimisation is complete, the central table will display the final list of all backtesting passes and their results.

This table has the following columns. As the grid is fully configurable, these columns can be dragged-and-dropped or disabled altogether after right-clicking the table and deselecting them from the contextual menu.

Field Definition
Pass The pass number.
Fitness The value showcasing how well the pass fits the optimisation criteria.
Equity Total equity at the end of the pass.
Balance Total balance at the end of the pass.
Net profit The difference between the final balance and the starting balance.
Trades The total number of closed positions.
Winning trades The total number of winning trades achieved during the pass.
Losing trades The total number of losing trades achieved during the pass.
Profit factor The total profit/total loss ratio.
*Max equity drawdown (%) The maximum percentage of the equity drawdown.
Max balance drawdown (%) The maximum percentage of the balance drawdown.
Max equity drawdown The maximum equity drawdown specified in the account deposit currency.
Max balance drawdown The maximum balance drawdown specified in the account deposit currency.
Average trade The average profit for all trades made during the pass.
Pass parameters Click the Apply button in this column to apply the parameters from this pass to your cBot.

Select a pass to view detailed statistics about it in the display below the central grid.

The first seven tabs in this display provide the same information as the same tabs in the Backtesting window. The Pass parameters tab is unique to optimisation.

Pass parameters

The Pass parameters tab provides the following information.

Image title

cTrader will highlight all optimised parameters in green while any fixed parameters (not enabled in the Parameters menu) will remain unhighlighted.

Saving and loading optimisation results

cTrader also allows for saving and loading optimisation results to and from a locally stored .optres file.

Note

A .optres file is simply a collection of key-value pairs with keys representing various optimisation settings and metrics.

Tip

You can use .optres files to continue refining your cBots across multiple local machines without losing any progress. You can also feed data from this file to generative AI tools to try and detect any patterns in the passes that your cBot has completed.

To save optimisation results, wait until the process is concluded and click the Save icon. In the file explorer dialogue, type the file name and save the file.

To load optimisation results, click the Load icon and select the required file in the file explorer window.

Image title