Optimize a Trading Strategy: Genetic Search

Photo by Markus Winkler on Unsplash

Welcome to the second article of our new series about optimization of a trading strategy! In the previous article, we provided an overview on this subject and showcased an example. We hope that the sub-sampling method that we disseminated has started to help you to select better trading parameter values from a rigorous statistics standpoint of view. Near the end of the article we left an interesting open question: “Grid search is exhaustive but very time-consuming. What other search algorithms can be used to accelerate the process of finding the best candidate?” Professional high frequency trading firms normally use 2 years of historical data for backtesting. Once you are very serious about finding the optimal trading parameter values using a rigorous statistical approach, the immediate problem is that the brute-force grid search will take forever to finish. Our backtest engine is lightning fast: it takes about 10 seconds to perform backtesting on one day’s of historical market data for the most heavily traded currency pair (BTW in an upcoming release that number will become 4 seconds). Nevertheless, that’s still not fast enough when it comes to exhaustive grid search. In this article, we will explore one type of accelerated search algorithms called genetic algorithm. The methodology presented here is not tied to our trading framework. In fact, it is not even tied to algorithmic trading. Genetic algorithm is generally applicable to a very wide range of mathematical problems.

In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.

For mathematical details you can refer to this wikipedia article. Long story short we can use the following genetic search procedure to find the best trading parameter candidate for any given trading algorithm:

[1] Creation: Create a list of objects each of which holding the trading parameters as their properties. The initial values for these properties can be randomly picked. We call these objects “individuals”.

[2] Evaluation: Evaluate the fitness of the individuals using some sort of criterion (e.g. expected final account balance). The criterion is directly related to how you envision that one set of trading parameter values is considered to be better than another. Please refer to our previous article to avoid a common pitfall. Choose the top few individuals with high fitness scores. We call these individuals “parents”.

[3] Reproduction: Mate the parents by somehow generating new sets of trading parameter values from the parents’ trading parameter values (e.g. taking weighted averages). We call these newly generated sets of trading parameter values “children”. If needed, give the children a small chance to mutate by slightly changing their trading parameter values.

[4] Elimination: Evaluate the fitness scores of the children. Examine the fitness scores of all the parents and children and remove the bottom few individuals with low fitness scores so that the entire population is brought back to its original number. At this point, we can proceed to the next round of reproduction-evaluation-elimination cycle.

[5] Termination: After enough rounds of reproduction-evaluation-elimination cycles, we’d find that the highest fitness score in the population can no longer be improved from one cycle to the next. This means the solution has converged, the best candidate has been found, and the procedure can be terminated.

If it hasn’t sounded too complicated so far, here is a concrete implementation in ~150 lines of code in the context of simplified Avellaneda-Stoikov market making: https://gist.github.com/cryptochassis/198b4d34837ec88676febd146140cdd3. Admittedly sometimes mathematics can be big headaches. However, we’d like to kindly remind our readers that algorithmic traders (i.e. us) make a living not based on sheer good luck but rather based on rigorous math in solid theoretical foundation plus flawless implementation in code. This is the edge that we have.

If you need help in math or coding, feel free to let us know by joining us on Discord https://discord.gg/b5EKcp9s8T. 🎉 We specialize in market data collection, high speed trading system, infrastructure optimization, and proprietary market making.

Disclaimer: This is an educational article rather than investment/financial advice.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store