Fig. 1. Optimization of the multidimensional space of trading strategies algorithms.Optimization of trading strategiesIn the process of algorithmic trading, it is constantly necessary to adjust the parameters of trading strategies algorithms. The combination of all possible parameters turns into a large multidimensional space of options strategies. To get the most profitable and stable strategies you need to explore this space and find the optimal parameters for trading.
The best way to research any set is to look through all its elements
Brute Force . However, given the enormous amounts of data that one has to face during optimization, as a rule, it turns out to be simply impossible to conduct such a study by exhaustive search. It is necessary to apply various analytical algorithms that can reduce the actual amount of research in the process of searching for extrema.
')
Most of these algorithms are well known:
the Monte Carlo method ,
the gradient descent method ,
the simulated annealing method ,
evolutionary algorithms , etc. There are various modifications of these optimization algorithms. In algorithmic trading, as a rule, there are implementations of genetic algorithms and Monte Carlo. Anyway, all these algorithms use "random number magic" or scientifically speaking nonlinear stochastic optimization.
The classic problem of stochastic optimization algorithms is that, for small volumes of actual research and small samples, they are not representative. For example, Monte Carlo is not effective in multi-extremal space, it emphasizes the study on global extremum, losing sight of local, but no less interesting extremes. The algorithm does not set itself such tasks, it just needs to find the most profitable strategy. The genetic algorithm can also go unsuccessful branch of mutations and
stop at some local extremum , etc.
This is because these optimization algorithms in the initial stages have to make decisions on a limited amount of data in a space that has not yet been studied, and important areas can easily fall out of the study. To avoid this, you need to increase the data sample and the time of the study, and in our case, time is worth its weight in gold. It is necessary at the minimum time spent exploring the extremes of space in as much detail as possible. At the same time, in the rapidly changing conditions of stock trading, it is important to pay attention not only to profitable, but also to stable parameters of trading strategies. Stable means parameters forming clusters with similar results. Profitable strategies that are out of clusters may not be stable and lead to serious losses. In turn, the strategy of the cluster is less susceptible to changes in the market.
Stochastic cluster optimization methodTaking into account the peculiarities of optimization of stock exchange strategies, a hybrid algorithm was developed (see the
last article ) which had one pleasant side effect - it successfully selected and investigated clusters. I gave the name of the resulting algorithm - “The method of stochastic cluster optimization”.
The research process therefore the algorithm takes place in two stages:
- Exploring strategy space with the removal of unprofitable and at risk areas
- Detailed study of extrema and clusters of space
Stage 1. Investigation of strategy space with the removal of unprofitable and at-risk areas.To get rid of uncertainty due to lack of data at the initial stages of research, the algorithm does not set the task of finding profitable strategies, but, on the contrary, searches for the most unprofitable and removes them from space along with the areas bordering on them with potentially high risks of losses.
The work is carried out in the following order:
- A multidimensional space is formed from all possible parameters of a trading strategy.
- Strategies are randomly selected from space and are tested on historical data with the specified parameters.
- According to the results of testing, border microregions are removed around the most unprofitable strategies. This reduces the research space and focuses on more profitable and stable areas in further iterations.
- Testing iterations are held until the strategy space is studied to the necessary extent.
In Fig. 2 shows how the study shifts towards extremes, while the risk of missing small clusters with possibly good and stable parameters is minimal.
Fig. 2. The first stage of the Stochastic Cluster Optimization algorithm is the study of the strategy space.
Stage 2. Detailed study of clusters and extremes.After the first stage of the study are well the types of extremes. However, due to the peculiarities of the algorithm (a lot of microregions are cut out), the space turns out to be “ragged” and some extremes can be investigated not in great detail. In order to fully explore all interesting clusters, the optimization algorithm begins the research process exactly the opposite. To do this, select all the best strategies and micro-regions are additionally distinguished around them. If strategies that have not yet been studied are found in these areas, then they are additionally tested (see Fig. 3).
Fig. 3. The second stage of the Stochastic Cluster Optimization algorithm is a detailed study of extrema.As a result, after the algorithm works, all areas of space that are interesting to us are investigated and clusters with profitable strategies are tested in detail. In this case, the actual amount of research, as a rule, is no more than 25-50% of the total volume of the strategy options space (see. Fig. 4).
Fig. 4. The speed of research of the Stochastic Cluster Optimization algorithm (left) is 2-4 times higher than the speed of the Brute Force algorithm (right).Walk forward optimizationSeemingly optimized parameters and you can start trading. However, this is not the end of the research process. The optimization process is at risk of “fitting” or re-optimizing the parameters for the historical data used in the process, so you need to additionally check the results. To do this, use the
Walk Forward method. The essence of the method lies in the fact that the strategy parameters are tested on historical data different from those used in the optimization process.
To do this, the entire range of historical data is divided into samples consisting of sets:
- IS ("In Sample") - the sample used for optimization
- OOS ("Out Of Sample") - a sample used to test optimization results
Moreover, the sampling ranges are formed in such a way that the OOS data follow one after another (see Fig. 5).
Fig. 5. Scheme Walk Forward optimization.To reduce the amount of research at the stages of testing the results, after optimization you can immediately filter out strategies with poor performance, thereby reducing the overall testing time. As a result of this check, we will obtain objective parameters of trading strategies that are protected from reoptimization (see Fig. 6 and Fig. 7).
Fig. 6. Optimization results on the data "In Sample".Fig. 7. Check the results of optimization on the data "Out Of Sample".Results analysisAs a rule, after a Walk Forward check, most of the trading strategies do not look as attractive as after the optimization. Ideally, strategies should confirm their statistics, while extremes and clusters retain their shape and position in space.
For a comfortable analysis of the results obtained, I visualized a multidimensional strategy space for each parameter in the format of a heat map (see
Fig. 8 ). The map visually evaluates the shape and size of the clusters, the position of the extremes, checks the effect of the parameters on the effectiveness of the strategy, evaluates the changes after checking for re-optimization, etc.
Fig. 8. An example of the cross section of the space on the optimized parameters and the objective function.For a comprehensive assessment of the results of Walk Forward optimization, a matrix is ​​constructed with all the steps and parameters that have passed filtering. The steps in which the parameters confirmed their indicators are highlighted in green and red, respectively, if not confirmed. Parameters that showed themselves well in a large number of steps can be considered more suitable for trading (see Fig. 9).
Fig. 9. Walk Forward matrix with all the results of the check for OOS data.If necessary, the results can be exported to third-party analysis systems for more detailed research. For example, in R, Excel, or Mathlab (see Figure 10).
Fig. 10. Export optimization results to Excel.In order to finally make sure that the chosen parameters are correct, detailed tests of strategies are carried out, the smoothness of the yield curve is assessed, bids are displayed on the schedule and the trade deals log is studied (see Fig. 11).
Fig. 11. Detailed analysis of the parameters of a trading strategy.ConclusionAfter optimization and all checks, we will have strategies that are potentially suitable for real trading on the Exchange.
Finally, we all rechecked, probably, you can already start trading? In fact, we are only halfway through, it is too early to send trading algorithms to battle. Next to be:
- Check strategies for live data from the Exchange to confirm the indicators obtained during testing.
- Build a portfolio of trading strategies for risk diversification. By the way, it also needs to be optimized.
- In the process of real trading, periodically reduce the results obtained with the results of tests to adjust the settings of the tester-optimizer.
But this is probably another time.
All successful trading!