Back to Community
Evolutionary Strategy

Genetic algorithms are algorithms that mimic natural selection. This is a simple evolving algorithm that trades top stocks. Essentially, momentum strategies are randomly generated. Based on how those strategies would perform over a period of time (30 days), the best performers, or parents, are selected. Using attributes these parents have, new algorithms are generated that have similar attributes to the parents. This process is then repeated. Trades are made using the overall best performing algorithm.

Here's a cool example of a genetic algorithm:

Although the strategies being evolved are basic and don't perform great, this is just meant to be an example. I think there are a lot of ways one could extend this, like moving away from momentum or importing relevant data from a CSV file. There are also some variables that can easily be adjusted that may lead to better results, and the code is commented. Clone this, play around with it, and let me know what you think!

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 53a62fa7ffdc8c482aaba622
There was a runtime error.

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

8 responses

thanks your sharing!

some interesting reference:

Application of Genetic Algorithms to Quantitative Asset Allocation Models

genetic programming libraries in Python:
Pyvolution: Very modular and very extensible evolutionary algorithms framework, with complete documentation, Apache License 2.0
deap: Distributed Evolutionary Algorithms in Python, GNU Lesser GPL
pySTEP: Python Strongly Typed gEnetic Programming, MIT License
PyRobot: Evolutionary Algorithms (GA + GP) Modules, Open Source
PonyGEa small, one source file implementation of GE, with an interactive graphics demo application GNU GPL v3
inspyred: Biologically inspired computation encompasses a broad range of algorithms including evolutionary computation, swarm intelligence, and neural networks, GNU GPL v3
DRP: Directed Ruby Programming, Genetic Programming & Grammatical Evolution Library, GNU GPL

Jonathan Kinlay has been posting recently about the practices and pitfalls of genetic programming for algorithmic trading. I attempted a system based on it about ten years ago, but never really got anywhere.

I do know that for nonlinear global optimization problems, we had good results (in derivatives model fitting) with particle swarm differential evolution. Perhaps that could be adapted to trading system search problems.

Ty for sharing that is actually my area of research, using a few years of historical data I used to need over 1 day to compute a model I wonder how would quantopian and python deal with big models.

We have tried genetic programming and the results when adjusted for data snooping are terrible. For starters who are not familiar with data mining bias see this blog:

@SImon: Dr. Kinlay makes a few good points but the system he posts at the end of his blog has 240 trades in nearly 25 years. The small number of trades is one indication of an extreme fit. The point is that if his system was a top performer of a genetic programming algorithm, then it is probably random even if the out of sample performance looks nice because his selection ignores all those systems with bad out of sample performance.

I agree 100%, I think it's mostly a dead end, but perhaps differential evolution of deep neural nets might fit real relationships? Super tricky to avoid mining bias though, so many degrees of freedom. Marco de Prado's technique for backtest overfitting might help...

GP is just an optimization algorithm, it is as good as its fitness function and the model you are trying to optimize. If the model is over fitting then your fitness function is not doing its job properly if the model is not evolving then the strategy being optimized is not good. I see GP more as a tool-box.

Very nice Gus. Plenty of food for thought here. Been looking at Genotick recently - written in Java and so a bit of a slog for me. But if you are adept at Java you might like to take a look.Genotick

I am trying to do something similar using genetic programming, but trying to create a more turn-key web app approach.

Would love some feedback, as I would like to expand the data series and techniques that are available to the framework.
also trying to learn Python to do some Quantopian stuff (I am a Java guy).