Optimization of trading algorithm: Introduction
Hey there, Protraders!
Each аlgotrader faces the problem of selecting the optimal parameters of the trading system when constructing trading robots. The main system parameters are often determined by considerations of logic of the traded market situation. But “thin” system setup most often is produced by automatic methods. The process of selecting the best system parameters is called optimization. In the general case, bringing the system to the optimal (best) condition by searching the system parameters is called optimization. Optimization is realized by finding the extremum of the target function. For optimization conducting we need to have a mathematical model of the process (in our case it’s a trading system that works on a certain asset), a target function (some statistical system indicator), and search algorithm of the target function extremum. Optimization processes by algorithms types can be divided into structural and parametric. Structural algorithms are designed to improve the structure of the investigated system and parametric algorithms work only with the parameters of the system. For algotrading is used parametric optimization in most cases.
Using Protrader terminal and specialized tool “AlgoStudio” аlgotrader gets access to the two most powerful parametric algorithms of the optimization:
- Genetic algorithm;
- Particle swarm optimization.
In order to understand the scope, as well as the strengths and weaknesses of these optimization algorithms, let's analyze their fundamentals.
This algorithm was created on the theory of evolution. I.e. the basic idea of the algorithm is the crossover and mutation of the parameters of some initial "population". And the selection of those parameter combinations which most satisfy the target function. Consider the workflow of genetic algorithm in more detail. At the first stage some number of optimized parameters combinations is randomly created, let's call it the "initial population". Then, supplying alternately these combinations to our mathematical model (trading system), and testing the system at some time interval, we obtain the statistical parameters of the trading system (profit, loss, the number of trades, expectation value etc.) using each of the specified combinations of parameters of our "initial population". Let's compare the obtained results. Some from the system parameters sets will give us the best values of the target function. In the next step we'll select the best sets of system parameters and apply to them the so-called genetic operators which include “Crossover” and “Mutation”. When using the “Crossover”, each of the selected parameter sets is crossed with others in their population creating the new parameter sets that will inherit the signs of “parental sets”. When using the “Mutation” some part of the initial population changes some of its parameters, i.e. “mutates”. Received new parameter sets called the "new generation". In the next step, we substitute the obtained "new generation" parameter sets (after the mutation and crossover) in our trading system and test it. Let’s produce a selection of the best parameter sets in accordance with our target function by test results. Then repeat the above manipulations before the execution of the optimization termination condition, any parameter may be the condition, for example, the number of optimization “generations”, or achieving some predetermined value of the target function. Thus, after the completion of the genetic algorithm, we'll get the parameter sets of the investigated system which maximize or minimize the target function, for example, the profitability of the trading system.
To use Genetic optimization algorithm in the Protrader 3 terminal you need to open “AlgoStudio”.
Choose the “Optimization” tab and select “Genetic algorithm” in the menu “Properties – > Optimization setup – > Type”.
Settings of the genetic algorithm will appear in the menu “Properties – > Optimization setup”, let's decipher their meaning:
Type – allows selecting the optimization type. Two types are available here: genetic algorithm and particle swarm optimization.
Cycle limit – is the number of iterations for finding the optimal solution. Basic genetic algorithm and particle swarm algorithm does not use this limit – user cannot determine for how long the optimization will pass (more time = better results). Genetic algorithm which is implemented in the Protrader 3 produces the number of iterations equal to the value of this parameter.
Optimize – search of the maximum or minimum of the target function. This parameter determines which type of the target function extremum will be searched.
Use gradient – allows genetic algorithm to use gradient optimization in parallel with the classical genetic algorithm.
Use elitism – allows genetic algorithm to store the best population value (individual). This works well with average and large target areas and with large chromosome amount. Recommended to use this setting.
Chromosome count – is the number of items which are used by genetic algorithm. This parameter depends on the task, so it is not possible to determine the optimal value. Typically, a value is in the range [10 ... 50], but sometimes can be higher. Generally, a large target area requires a large number of chromosomes.
Use sharing – allows genetic algorithm to use the procedure of sharing which periodically checks the chromosomes (that are placed close to each other) and uses mutation of one of them. Also supports the proper arrangement of chromosomes.
Crossover/Mutation probability – these are very important parameters of the genetic algorithm that determine the amount (%) of mutation and crossover. The execution of this condition is desirable: Crossover probability + Mutation probability = 100 %. The large crossover value means that genetic algorithm will search for the decision in local target area. The large mutation value means that genetic algorithm will search well in global target area.
After setting all the settings, you need to select the optimized parameters in the “Strategy variables” window. Specify the minimum and maximum possible values for each parameter and also determine the step change of the parameter in the “Prec.” column. The final step in the optimization process setup will be the definition of target function “Targets” and setting of the “weight” of each selected target function in the optimization process. Explanation of the target function types will be given in a future article.
Particle swarm optimization
Let’s move to the second of optimization algorithms available to users of the Protrader 3 terminal. Like the genetic algorithm, particle swarm algorithm was established on the basis of observation of nature, namely, on the principle of moving the flock of birds. Each bird in the flock is watching for its comrades, and when one bird finds food, the whole flock will know about it. Thus, a flock of birds is extremely efficient executes the function of searching the food. Particle swarm optimization, using a similar principle, will search for an extremum of target function in the solution space of a specified trading system. Let’s imagine the trading system which Profit factor depends on two parameters n and k. I.e. our goal is to optimize the two parameters of the trading system. Accept the Profit factor of the trading system as a target function. Then the space of possible solutions to assigned task will be two-dimensional, and somewhere in the space of solutions a point will exist which corresponds to a system parameters combination in which the trading system shows the maximum profitability. For simplicity, specify the initial number of particles equal to two (particle x and particle y). They will be randomly located on the solutions plane.
According to the picture, profitability of the system at point y is higher than at a point x, according to the rules of the algorithm, particle x moves to a new point along calculated vector at design speed in the direction of the particle which is closer to the extremum of the target function (Profit factor). Also whole totality of the particles has global displacement vector in the solution space in the direction of the target function extremum. Thus, if particles are randomly “scattered” over the entire space of solutions then at the end of the algorithm particles will gather in the vicinity of the target function extremum, thereby defining a zone of optimal system parameters. Iterations will continue until the exit condition of the optimization won’t be reached, for example, set value of the target function. Also this algorithm is corrected by the speed of particles movement in the solution space, such amendment is known as “Inertia”. These amendments provide the balancing between breadth of research and the search speed of the target function extremum. To use Particle swarm optimization in Protrader 3 terminal, you need to open “AlgoStudio” and select “Particle swarm optimization” in the window “Optimization – > Properties – > Optimization setup – > Type”.
Settings of the Particle swarm optimization will appear in the menu “Properties – > Optimization setup”, let's decipher their meaning:
Particles count – the number of particles that is used in the current calculation.
Local/Global gravity factors – these parameters define the balance between finding a local extremum and complete analysis of the solution space, by analogy with Crossover and Mutation in genetic algorithm.
Inertia – determines how long the particle will move to the reversal.
Maximal velocity – defines the maximal speed. It should be increased for large solution spaces. Smaller values give the best results for small solution spaces.
Part to move – is the percentage of particles that will move. It should be between 0 and 1. The default value is 1 (all particles will move).
To start the optimization, tap the “Run” button.
We considered the general principles of optimization in this article and also described the optimization algorithms that are implemented in the Protrader 3 terminal. In the next article we will consider the types of the target function in detail, and also will configure the optimizer.
Have not tried PTMC yet? There is no better way to boost knowledge than to use it! Start trading with PTMC now!