📜 ⬆️ ⬇️

How I did a tester optimizer for finding profitable strategies on the stock exchange

Introduction


image

In algorithmic trading when creating mechanical trading systems (MTS), the question of the lifetime of trading algorithms is very important. Yes, and in principle it is quite difficult to find them. In a constantly changing market, sooner or later there comes a moment when even the most perfect and profitable algorithm begins to cause losses. And it is necessary, as they say, “twist up” or optimize for the current market conditions. One of the most common are trading systems (TS), working with candlestick charts with their variety of indicators for technical analysis.

image
')
To build strategies based on indicators, various parameters are used, such as timeframes, periods, parameter weights, and so on. And if several different indicators are used in the trading strategy at once, then the number of input parameters for the same strategy increases by an order and it becomes quite difficult to choose the most optimal values ​​for the current market.
To solve these problems, there are various testers optimizers.

Strategy Optimization Techniques


The most obvious solution seems to take all the possible strategies, test them using the historical method and select the most profitable ones. But when the number of variable parameters becomes more than two, three, and the number of possible options for a strategy in the ranges of these parameters begins to amount to thousands and tens of thousands, the search method becomes at times simply impracticable due to the length of such testing.

There are ready-made strategy optimizers in other software products such as Wealth-Lab , AmiBroker . But they use their own scripting languages ​​and, as a rule, some other limitations arise, and the strategies cannot be fully tested. How to translate their strategies in them? Can this tester do all that we need? Will tests reflect reality? And many other questions arise when you begin to study this topic in more detail.

Besides, these are “black boxes” and nobody really knows how they make calculations. And when it comes to money, there should be no room for every chance and uncertainty. “On the word” to the creators of such software, I do not believe. How many times have I encountered the most serious products with all sorts of glitches and bugs, letters and calls to technical support. At the same time, we become dependent on people who are completely unnecessary to us. In general, I have no confidence in them. All these problems greatly slow down the implementation of algorithms, and accordingly take up our time and money.

And I wondered: “Why not write your own optimizer? Is it really so difficult? ”It turned out to be difficult, but quite real. In addition, there is confidence in the results and freedom in the settings and upgrades and modifications of the program. Actually, with these thoughts, I took up work.

Based on stochastic optimization. Stochastic optimization is a class of optimization algorithms that uses randomness in the search for optimum. The stochastic optimization algorithms are used if the objective function is complex, multi-extremal, with discontinuities, with interferences, etc. At the same time, they allow to explore only part of the strategy options area and, based on the data obtained, to form an idea of ​​the space as a whole.

He familiarized himself with the main stochastic optimization methods used - genetics, Monte Carlo, a swarm of particles, their varieties, and other methods. In general, there are many types of stochastic methods. For example, the method of "Swarm of particles" or so popular "Genetic algorithms . " There are also elegant solutions like the “Annealing Simulation” algorithm (a beautiful gif on the right, I advise you to look at it).

The latter method, for example, with a high degree guarantees finding a global extremum. As with this method, it periodically deviates from the path and additionally studies the neighboring areas. But the speed of research is not the highest. The essence of the methods is the same - we choose random values ​​and somehow analyze them. From the method to the method only two parameters change - the speed and accuracy of the study. And inversely proportional. The higher the testing speed, the worse the quality of the results and vice versa. When choosing a method, everyone decides for himself what he is willing to donate.

Extremum search


For example, the method of “simulated annealing” allows you to find a global extremum. However, if you think about it, then the global extremum itself will come to nothing for us if there is no convergence to it. That is, if neighboring extremums do not conditionally evenly decrease around the extremum, then it is very likely that this global extremum is random in nature and will be of little use to us because it is inadequate, and we will be spoiled by calculations. Therefore, it is important to study the parameters around the extremum. If there is convergence, then there is a system and this strategy can be studied further.

All stochastic optimization methods have one common drawback - they can rest on some kind of local extremum, and that very optimal one can be overlooked. To avoid this, you need to maximize the sample area and the number of iterations. But speed of calculations suffers from it. So you should always look for a middle ground.
Due to the complexity and non-evidence of the calculations, I set aside the methods of “Imitation annealing” and other “Swarms of particles”. As a result, I came to the conclusion that the most accessible and convenient way in my case is the Monte Carlo optimization.

The first version of the Monte Carlo tester optimizer


Classic Maximum Search

He decided to take the logic from the article “Nonlinear stochastic optimization by the Monte-Carlo method” from the collection of St. Petersburg State University as the basis of his first tester optimizer. Anyone interested in this area, I advise you to read their collections. Many interesting diverse articles about optimization in various fields. Where these stochastic methods just do not apply!

So here. The essence of the method is that we create a multidimensional matrix consisting of varieties of strategies with different parameters. We randomly select strategies from this matrix, test them and determine the most profitable strategy. For the criterion of profitability until he took the expectation. And so it is possible to make a complex parameter. We accept the point from this strategy in the matrix for the epicenter and cut the edges of the matrix as far as possible from the epicenter to the depth we set. Thereby we reduce the sample area and in a new way we test random strategies from the resulting reduced area, repeat the iteration. So we continue until we get to the extremum.

There are lots of ways to determine the magnitude of the decrease in the sample area. Statistical, where they study the change in the gradient of the objective function or empirical, where they look at how quickly the extremum itself changes from iteration to iteration. And on the basis of these data, it is decided whether to continue the study further or stop the iterations, and accept that we have already found the maximum with a given error. The so-called stop criterion.

But as I noted above, it is important to study the area around the extremum, and therefore, I decided to converge to the end, and at the last iteration check all the adjacent strategies completely. I did not tinker with the gradients and made the convergence static as a percentage of the initial sample. That is, how much to cut the multidimensional matrix after each iteration by 1% or 20% we decide at the very beginning. Also, we immediately, given our time capabilities, decide how many strategies we will take from the matrix at each iteration for testing. Thus, the size of the matrix is ​​not important to us at all, we know exactly how many iterations and how much we will spend! This is the whole charm of stochastic methods.
Based on the above, I wrote a program to search for the best parameters of strategies.
Baseline data for optimization:

Console testers (I have several of them, completely load the processor) receive strategy parameters as inputs, test them and save the results at the end to binary files. This is done to intermediately save test data in case of errors, as well as protection against memory leaks and other glitches. In general, the diversification of the risk of failure of something. The program itself transfers all data, divides the load so that the testers work simultaneously at full capacity and at the end of one the other is immediately started. Long suffered how to synchronize everything, but everything turned out autonomously, quickly and conveniently!
In this case, all parameters and results are displayed on the main window of the Monte Carlo program. So, it is clear and clear all that is happening there. There is a logging window and a window with test results. After each iteration, the program opens the serialized files, counts the statistics on them, sorts and displays them on the screen.

Interface of Monte-Carlo tester optimizer:

image

In work:

image

End testing. The best result was with a expectation of 88%. Moreover, out of 6060 variants, only 778 were tested, of which 116 repeated.

image

Before testing strategies are checked whether they have not been tested before, because by the extremum the density increases and at the end the area around the maximum is completely covered. And we will not test the same again. All test results are processed without problems by the Analyzer strategy visualization program. It is always possible to manually correct the GO (guaranteed security), commission or change the initial deposit:

image

The test results window displays a large statistical table for all test and optimization results. Any parameter can be sorted by column. When you double-click on any line, all the parameters go to the visualization window, so you do not need to score anything in the cells (I’m not glad myself)!

Test results window:

image

Together:

image

However, after the implementation of the first Monte Carlo tester-optimizer and the study of his work, he came to the conclusion that he performs his task, but not in the capacity I wanted. In the classical optimization methods, in each new iteration, the best value is sought and further research is conducted around it. In my case, regarding him, I cut off the matrix of options for strategies.

Conventional scheme of the stochastic Monte-Carlo maximum search algorithm:
image

Advanced Algorithm


After the first iteration, when we carried out the first space exploration, we must somehow reduce the study area for the next sample. But we still know almost nothing about this space and, it seems to me, rather rashly to cut off unknown areas. In general, the algorithm is designed to search for a global maximum, and we are interested in all local and global maxima.

We need to know all the parameters where the strategy can be profitable. Perhaps, the strategy with some parameters brings a good profit, but it is more risky, while with other parameters it makes a profit a little less, but it turns out to be more stable and less risky, and if we follow our algorithm, we can lose sight of it. At the same time, we cannot explore the space in great detail, and it will be quite expensive for us to do an exhaustive search.

How to be in this case? I decided to move away from the classical scheme and act as in trading: "You can not control your profits, you can only control your risks." Therefore, I decided not to take risks and take measures in order not to unintentionally remove a good strategy from the research.

How then to cut the matrix? We will cut only those areas that have explored! That is, we will remove the micro areas around the worst studied strategies. The essence of the algorithm is that we do not explore good areas of strategies, we do not explore bad ones. And we can additionally explore the best strategies at the end of optimization.

Here is shown the operation of such an algorithm:

image

In fact, the matrix is ​​multidimensional (in my case, a maximum of 9 measurements), but to explain the principle of operation we will use all of our favorite three dimensions:

Points in this space are already tested strategies with different values ​​of the “long” and “short” sliding. The lighter the point, the better its expectation.

In principle, it could be depicted in two coordinates:

image

But in three coordinates I like more - more clearly.
So, black dots in space are the worst strategies for testing. Their lines connecting the path of the algorithm from point to point. Gray dots in the plane are strategies that we remove from the study area. The lines between them are the path of the algorithm for removing strategies from the matrix. The lines between black dots and gray ones are a projection of the worst strategy onto the plane. The single gray points on the plane are the projections of already tested strategies onto the plane.

Here you can see how the algorithm goes from one worst strategy to another, starting with the worst:

image

Advantages of the algorithm:


We deliberately remove the worst strategies from the research space. Thus, at the next iterations, we explore areas with more profitable strategies and do not waste precious testing time on studying areas that we do not need. In the end, our field of study converges to all the maxima of space.

As a result, we get something like this:

image

I couldn’t screw in the interpolation, so there is no surface, I am content with points.
In a multidimensional matrix, you can see the section by measurements:

"Long - expectation"

image

"Short - expectation"

image

Appearance of the tester optimizer "Researcher":

image

All applications were written entirely in C #. Before starting the optimization, we configure the following parameters:

You can run instead of optimizing the "random search". Here strategies are tested not on a grid, but in a random order. That is, we can stop the study at any time and evaluate the result. Of course, the more strategies we test, the clearer we get an idea of ​​space.

image

Suppose we explored the space and roughly represent how many maxima are there. And what does this give us? Almost nothing for now ...
We need to investigate these maxima, they are random or systemic. To do this, in the tester-optimizer provided an opportunity to choose the best strategies and additionally, explore in more detail the areas around them. Explore the strategies that we missed when optimizing. Now we know almost everything about the extremes of space! The obtained data can be further investigated for clustering, re-optimization and so on. But that's another story!

PS Not much about yourself. I got acquainted with trading about a year ago, at first I was trading manually, then I realized that it was not mine. I came to the conclusion that it is better to trade according to clear rules and automated. The first algorithm was written in Quik's script language, but this language (qpile) turned out to be incredibly poor. Then he began to get acquainted with C # and write his first trading robots on it. At the moment I am creating a multifunctional platform for algorithmic trading.

Good luck to all! Regards, Alexey.

Source: https://habr.com/ru/post/209198/


All Articles