Genetic Algorithms in Trading: What is it and how do they work?

Among the types of algorithms that exist, genetic algorithms are the ones that have gained the most popularity in recent years within the world of trading.

More and more traders use them to create and optimize their systems within the industry. If this sounds Chinese to you, let’s bring it down to earth.

What do genetic algorithms have to do with optimizing my trading system?

More than you imagine. The number of indicators and rules that you can use to create your trading system from these indicators is so great that it is almost impossible to test them all.

Trying to test all the rules to find the one that gives you the best results requires a lot of time and, above all, a team with a great capacity for calculation that is not available to everyone.

This is where genetic algorithms come in to work their magic. They find in no time the best solution or the closest to itwithin a universe of combinations.

1. What is a genetic algorithm?

Let’s start at the base of everything: what is an algorithm.

A algorithm You can see it as a very well-structured set of rules or steps that describe exactly which route or process to follow to find the solution to a specific problem.

Now, let’s go to the genetic algorithms.

The genetic algorithmsdeveloped by John Holland in 1975, are based on or inspired by the theory of genetic evolution or natural selection of living beings to search for or find the most optimal solution within a set of possible solutions.

They are a type of algorithm that belongs to the family of evolutionary computation. This means that they evolve over time, and adapt the solution to the problem until they find the best one.

2. Characteristics of genetic algorithms

These are the main characteristics of genetic algorithms:

  • Are random. The search processes for the best solution are not tied to a particular rule, but do it randomly until they find it.
  • consider a set of possible solutions. There is more than one solution to the problem and by combining some of them a better solution can be found.
  • When these algorithms are implemented it is not known with certainty. because have succeeded or because they have failed.

The third point is important, we cannot find a causality in our search. Let’s keep going…

3. How trading systems with genetic algorithms work

To understand how genetic algorithms work, it is necessary to first understand some things that we are going to see with an example.

Imagine that a person is represented by a chromosome and this chromosome by a set of genes.

Think of genes as the development of a quality (skin color, eye color, height, etc.).

If two parents are short, there is a very high probability that their children will also be short. If two parents are white-skinned, it is most likely that their children will be white-skinned. This process of transferring their qualities and characteristics is the beginning of heredity.

But they can also happen random alterations or mutations in the transmission of genes from parents to children. This mutation modifies the genes of the child and allows the appearance of people with new qualities different from those of the parents. This is the principle of variability.

The process of natural selection allows a species to adapt to the environment. The fittest or strongest individuals have a high probability of having strong children in the next generation. Less fit or weak individuals are less likely to have children in the next generation.

We must be very clear about these principles of genetic theory that I have just mentioned, because our genetic algorithm must comply with them. So a genetic algorithm works like this:

  • initialization: we generate an initial random population of individuals constituted by the set of candidate solutions to solve the problem.
  • Evaluation: We classify these individuals according to the criteria to be optimized by applying a fitness function, known as the fitness function, to determine how good a solution is.
  • Selection: we choose the individuals that will be crossed according to the value of the fitness function. The most optimal individuals, or with better fitness, have a greater probability of mating than the less optimal ones.
  • recombination or crossover: we combine the selected individuals, from two individuals/parents two children are obtained that combine the characteristics of both parents. The objective of the crossing is to create new populations to arrive at new possible solutions.
  • Mutation: several genes of some individuals are randomly modified to explore new possible genes that produce possible better solutions than those found so far and that were not covered by the individuals of the original or current population.
  • Replacement: a part of the original individuals (or all) are replaced by the new ones and the best ones are selected to form the next generation.
  • End or stop condition: Each execution of all previous steps is what we define as a generation. The cycle stops when the maximum number of generations is reached or when there are no changes in the population.

4. How to create a trading system with genetic algorithms

The problem of creating and optimizing trading systems is similar to the problem of natural selection in evolutionary theory. Through natural selection within a species, the fittest individuals reproduce and pass their genes on to the next generation. After several generations the optimal parameters are reduced to adapt to the environment.

Imagine you want to design a trading system on a certain currency pair and on a 15 min bar chart to go long the market only when a bull market correction occurs.

Before continuing, I want to clarify that you should not confuse the trading algorithm that includes the rules for entering and exiting the market, with the genetic algorithm that is responsible for finding the most optimal or suboptimal.

The first thing you should do is define the trading rules. Let’s imagine that you use a simple moving average, for example, 200 periods and the RSI indicator (14 periods).

If the price closes above the MA of 200 and the RSI above 50, we can say that we are in an uptrend, without going into details to simplify the example.

Combining the two previous conditions we obtain the rules of our trading strategy:

  • Buy when the price closes above (higher) the 200 SMA and the RSI is below (less) 50.
  • Do not operate if the above rule is not met.

The input parameters will be formed by the set of parameters of the previous rules. These two parameters are going to be optimized using genetic algorithms to try to maximize the benefits of the strategy.

The algorithm will try to maximize, because even though we design our algorithm well, we are not guaranteed to generate any profit.

In this way, we can know what the ideal values ​​of the RSI will be, for example: instead of being less than 50, the genetic algorithm can determine that the best results are obtained when the RSI is less than 40. Or the same, use the MACD indicator in place of the RSI…

The two input parameters are encoded as one chromosome. This coding can be binary or floating point (real numbers) and consists of an array with as many elements as parameters to be optimized, in this example two. The set of values ​​of a solution is called a gene.

6. Conclusions

Genetic algorithms are a good tool to find optimal or suboptimal solutions with fewer resources. If we do it without using genetics, we will need more powerful equipment and surely more time, but we will be looking for the best possible solution to our problem.

In the end, it is one more tool. It makes sense to shorten timelines and resources, but it’s not the holy grail. What really matters is the what, the why and the for what, more than the how.

Have you already used this type of algorithms?

I read you in comments, thanks for getting here.

Leave a Comment