Custom fitness functions for optimisation¶
cBot optimisation is the process of determining the best possible parameter values for a cBot before deploying it for live trading. In cTrader, you can optimise cBots against a wide range of built-in criteria such as maximising profits or minimising equity drawdown. However, you can also write your own fitness functions for optimisation which can be useful if you want to apply custom performance criteria to a cBot. In this article and its corresponding video, we discuss how custom fitness functions work.
Note that this article does not cover the process of cBot optimisation using built-in criteria. To learn more about this process, click here.
Define custom fitness functions¶
A custom fitness function is just an override of the GetFitness()
method.
1 2 3 4 |
|
The override must accept a single argument of the GetFitness
args type in order for it to count as a custom fitness function.
Write a custom fitness function¶
Since the GetFitness()
method must return a double
, the body of the method should contain a calculation that results in a value of this type.
For example, we can write a simple custom fitness function that squares the total number of winning trades made by a cBot and then divides this value by the total value of losing trades. This way, we allocate more weight to winning trades and, in a sense, allow the optimised cBot to make some risky plays.
Here is what our fitness function would look like.
1 2 3 4 |
|
At this point, we can add the override to any cBot we want, then save and build it.
Use a custom fitness function in optimisation¶
After adding our function, we need to add an instance of our chosen cBot and then switch to the Backtesting tab. To conduct optimisation using our custom function, all we have to do is open the Optimisation criteria section and select the Custom option. Afterwards, we can run optimisation as usual.
During optimisation, higher fitness scores will be allocated to the passes during which the cBot has maximised the ratio between the number of winning trades squared and the number of losing trades.
Modify the custom fitness function¶
We can also go back to our custom function and modify it as follows.
1 2 3 4 5 6 7 8 9 10 11 |
|
This function still follows the previous algorithm but it only applies the custom calculation if more than 20 trades have been placed by a cBot. In any other case, the function returns the lowest possible double
. In theory, this should improve results by reducing the possibility of statistical bias.
If we rebuild the bot and run optimisation again, we should see several passes get a very low fitness score due to the low number of total trades placed by the cBot.
Lastly, we want to minimise equity drawdown while still encouraging active trading.
1 2 3 4 5 6 7 8 |
|
If we run optimisation again, we should see the best possible passes given our trading strategy.
Custom fitness functions provide an excellent tool for determining the best possible parameter values suited to your unique approach to trading.