# Genetic Algorithm

## What Is the Genetic Algorithm¶

When discussing how cBots could be optimized, we have mentioned that cBots can be refined using the 'generic algorithm' method. In this guide, we explain exactly what it is and how it works.

In brief, the generic algorithm method is based on the natural selection theory. This theory states that only the fittest individuals survive for subsequent reproduction.

In cBot optimization, each optimization pass is considered an 'individual'. To evaluate individual passes, the genetic algorithm calculates a certain fitness score for each pass and compares this score against all other passes.

For it to work, the generic algorithms needs an initial population. To generate it, the optimizer will run several different passes with randomized parameters. After creating its initial population, the algorithm will start the process of finding the fittest optimization pass. This process continues until the fitness scores of 'child' passes start stagnating and converging. At this point, the algorithm is stopped.

## Genetic Algorithm Stages¶

Any genetic algorithm has the following stages.

``````graph TB
B([Selection]) ==> C([Crossover]);
C ==> D([Mutation]);
D ==> B;``````

The stages are explained as follows.

### Selection¶

During this stage, the algorithm finds two fittest optimization passes using their respective fitness scores.

### Crossover¶

After finding the two fittest optimization passes, the algorithm uses them to create new 'child' (or 'offspring') passes by using combinations of the parameter values of both of the 'parent' passes.

Example

If your cBot has four different parameters for optimization, the algorithm will get the values of the first and second parameters of 'offspring' passes from one 'parent' and the third and fourth parameters from the second 'parent'.

### Mutation¶

At this stage, the algorithm 'mutates' the 'offspring' passes by randomly modifying one or more parameter values.

After the mutation stage, the optimizer will run a new 'offspring' optimization pass. Subsequently, it will repeat all stages but only if the fitness score of an 'offspring' pass was higher than the last fittest optimization pass. Such an outcome would mean that there is room for improvement, indicating that optimization should continue.

Otherwise, the algorithm will increase its stagnation counter. If the stagnation counter reaches the value of the algorithm stagnation period parameter, the optimization process will be automatically stopped.

## Genetic Algorithm Parameters¶

The genetic algorithm has several parameters that it uses during its lifetime. Presently, it is impossible to change the values of these parameters. However, we are planning to add this option in a future release of cTrader desktop.

The parameters and their definitions are as follows.

Parameter Definition
Population Size The maximum size of a population or the maximum number of passes made during each optimization iteration.
Max Iteration Count The maximum number of optimization iterations performed by the algorithm.
Stagnation Period The maximum value of the stagnation counter. If this value is reached, the algorithm is stopped.
Elite Percentage This value is used to select an X% of individuals with the highest fitness scores from the current algorithm iteration. These passes will 'survive' to the next iteration.
Tournament Size Percentage This value is used to select an X% of individuals from an iteration for finding 'parent' passes.
Migrans Percentage This value is used to add an X% of randomly created passes during each new iteration or population generation.
Mutation Percentage The percentage of the parameters of 'offspring' passes to be mutated.
Mutation Probability Percentage The percentage of 'offspring' passes that to be mutated. The passes not included in this percentage will not go through the mutation stage.

Last update: September 29, 2023