What is algorithmic trading?

Trading algorismic

Wednesday 23 February 2022



The stock market was a market where, traditionally, meat and bone investors exchanged securities, setting prices based on supply and demand that existed at all times.

This view has changed dramatically in recent years in which most of the operations that cross in the different markets of the world are launched automatically by robots that execute algorithms.

Algorithmic trading is the process of using a computer program to generate buy or sell orders based on a set of instructions or prediction models.

The key to algorithmic trading is the lack of human intervention, as the impact of human emotions on decision making is ruled out.

Buy and sell orders can be placed on the market manually (paper trading) or automatically (this is called automated trading).


How are these trading algorithms?

The algorithms used try to monetize a certain pattern of behavior that we have detected based on historical information. Therefore, what is sought is to have a statistical advantage when making a decision to buy or sell.

For example, some algorithms focus on looking for the continuity of an upward or downward trend, others look for stocks that have moved far from their average valuation, some look for the classic arbitrage that occurs in transitional periods, while others look for divergences between pairs of correlated values.

Trading algorithms can be based on rules (such as when a buy / sell condition passes) and / or can also be programmed using Machine Learning algorithms.


Exemple d’algorisme basat en regles, en aquest cas amb una mitjana aritmètica curta de 15 sessions i una mitjana aritmètica de 150 sessions. Compraria quan la mitjana curta talles de baix cap a dalt la mitjana llarga i vendria al fer el creuament de dalt cap a baix..

Example of a rule-based algorithm, in this case with a short arithmetic mean of 15 sessions and an arithmetic mean of 150 sessions. I would buy when the short average cuts from the bottom up the long average and sell when doing the cross from top to bottom.


Designing these algorithms is a complex task that requires a proper combination of logical reasoning and creativity. In addition, development usually involves an extended phase in trial and error time to result in a winning strategy.

Many of these algorithms need to be adaptive and evolve over a lifetime, because when a strategy is very profitable, other agents appear trying to replicate and exploit it, and as a result, it can become a losing strategy.


Process of creating a trading algorithm using Machine Learning

The process of creating a trading algorithm using Machine Learning can be broadly divided into the following steps:

  1. Definition of the problem statement

  2. Read historical data for quotes for one or more assets

  3. Checks on the good condition of the data

  4. Target variable settings

  5. Feature engineering

  6. Dividing data into two sets: testing and validation and training of the model

  7. Backtesting of results and analysis of algorithm performance


For example, following the same numbering of the steps we would do:

  1. We want to know if we need to buy or sell shares of Company X.

  2. We would get historical data from Company X.

  3. We would check that there are no jumps in the dates of the data obtained, that there are no duplicates, that there are no meaningless maximums or minimums, ...

  4. As a "target variable", for example, we can define whether we want to know if the shares of company X will increase by 5% in the next 5 days. As we have the historical data, for each of the days, we can see that the next 5 days have passed and mark the target variable for that day with a "Buy" or "Do nothing"

  5. As features, for example, I can use technical trend indicators like ADX, an oscillator like RSI (Relative Strength Index), and one that tells me the volatility or correlation between two temporal averages. We make the corresponding calculations for each of the days in order to obtain the "features" that should be used to predict our "target variable"

  6. We will divide the dataset into two sets taking into account the temporality. For example, the data from January 1, 2015 to January 1, 2021, and we will save the data for the second period, from January 2, 2021 to January 31, 2022 to validate.

  7. Backtesting of results and analysis of algorithm performance

Once the model was obtained using different Machine Learning algorithms, we would do what is known as "Backtesting", that is, we would test the strategy obtained to make the model in the previous step with the data of the last year, this way we could check if the algorithm had won or lost and by what amount.


Result of the value of the portfolio over time using the strategy of the previous graph. The flat tran sections indicate that we are out of the market.


Getting a good trading algorithm for a certain value or set of values is no guarantee, but it does offer a "statistical" advantage when it comes to making decisions.

If you are satisfied with the performance of the “Backtesting” strategy, then the next step would be to start trading in manual mode (Paper trading). If not, you should adjust your strategy to an acceptable performance. And once the results in manual mode are satisfactory, you can start trading automatically through an online broker.


Figure showing the process of implementing an automated trading strategy, Taken from the book (1)


Can these trading algorithms be rented?

As a result of the explosion in the use of algorithmic programs to invest in the stock market, a whole industry has emerged. While large financial institutions have their own research departments to develop custom software, small investors can also benefit from these models thanks to rental robots.


Where can I learn more?

You can consult the sample code that generates the graphs in this article at the following Github repository

On the net you can find a lot of articles, Moocs and videos, I personally recommend these online courses that are now free:




Segueix-nos a

Els nostres articles del bloc d'inLab FIB


inLab FIB incorporates esCert


First LogoCSIRT Logo

inLab is member of

inLab és centre TECNIO