How Genetic Algorithms Can Improve a Trading System

By: Murray Ruggiero

The following is an excerpt from Murray Ruggiero's Cybernetic Trading Strategies

The process of developing a good trading strategy requires an iterative process of development and testing that could require weeks or months for even a market wizard.  A technology called “genetic algorithms” can speed up this process by a factor of 1,000 to 10,000 or more, and allow the trader to incorporate his or her expertise into this solution. 

Genetic algorithms were invented by John Holland during the mid-1970s to solve hard optimization problems.  This method uses the natural selection, so-called “survival of the fittest”, to solve optimization problems using computer software.

There are three main components to a genetic algorithm: first, a way of describing the problem in terms of a genetic code, like a DNA chromosome; second, a way to simulate evolution by creating offspring of the chromosomes, each being slightly different from its parents; and third, a way to evaluate the goodness of each of the offspring.

Genetic algorithms have many uses in developing trading applications.  Some of these are: 1) evolving a neural network, 2) evolving trading rules, 3) combining or selecting multiple trading strategies or 4) using money management applications.

Neural Usage: There are two different types of applications in using genetic algorithms to build neural nets.  The first is to evolve inputs and/or the configuration of your network.

Genetic algorithms can find near-optimal solutions for NP Complete type problems.  These are problems in which the number of combinations makes it impossible to try all of them in a lifetime.  Genetic algorithms can intelligently search subsets of these solutions to find a near-optimal solution for these problems quickly, even on a desktop computer.  This makes it practical to search for the best possible input and configurations for our neural network architecture.

To select the configuration, we’ll use a neural network that had 49 inputs and 10 hidden nodes for our example.  Ideally, we would like to eliminate inputs from our model that can be removed without degrading the trading performance.  Even for a network of this size, trying all of the combinations becomes an NP complete type problem.  However, we could use genetic algorithms to select a near optimal architecture for this neural network.

The first step in developing this solution is to design our chromosome.  We’ll use two genes.  The first will contain 49 binary elements, one for each input.  The second gene will contain information used in the internals of the neural network.  These are: the number of hidden nodes, the initial weight range and learning rate.

Next, we need to develop a fitness function.  We’ll base our fitness function on actual trading performance of the neural network tested using a simple system and not the accuracy of the neural network or its correlation with the target.  One example of a simple fitness function we could use is:

Fitness = (NetProfit-2*Drawdown)*(.20*(Training Cases/Weights))

This fitness function will give high scores to networks that produce a good net-profit-to-drawdown ratio.  These scores will be weighted so that simpler architectures will score higher.  This type of fitness function will produce networks with good trading characteristics while trying to produce the smallest networks possible.  This is important because the higher the ratio of training cases to weights, the more robust the neural network.

The second application is actually to evolve the connection weights for a neural network.  Often times this method will develop better trading models than standard neural network algorithms because we are not restricted by the standard error functions used in many neural network algorithms.

For example, many back propagation-like algorithms use the root mean squared error function.  This error function will produce a model with smaller errors on the more common small moves but allow larger errors on the rarer large moves.  This will result in large losing trades that often will make a neural network un-tradable.  If we use a genetic algorithm, we can design the best fitness function for our application.

To design such a solution for the neural network, we start by using the same preprocessing and target output.  This gives us 49 inputs and one output.  We also will use 10 hidden nodes, which means we’ll have 510 weights.  Our chromosomes have 510 real number elements that represent each weight.

For a fitness function, we could use NetProfit2*Drawdown and evaluate our neural network using simple trading rules such as, if the neural network output crosses above zero buy, when it crosses below zero sell.  Each member of the population would represent a set of neural network weights.  Using the genetic evolutionary process, we’ll evolve the best set of weights for trading using our preprocessing and desired output.

Evolving Trading Rules – One of the most popular methods for evolving trading rules is to have the genetic algorithm combine rule templates and optimize their parameters.  In this type of application, we’ll normally combine a fixed number of entry rule templates “anded” together that are both selected and optimized by the genetic algorithm.  We can use any exit rule, for example, simply holding the position for five days.

First, we need to know how many rules we are “anding” together and which rule templates our genetic algorithm can choose from and optimize.  In our example, we will “and” together three templates than can have up to three optimizable parameters each.

In this case we can design a chromosome with three genes, one for each rule we are “anding” together.  For example, we could use the following structure for these genes: Element 1 Template number integer.  Element 2-4 are real number elements that are mapped into a valid range for each rule template.  In our example, this design will create a chromosome with 12 elements.

Developing a fitness function requires building an objective way to evaluate the tradability of a given solution.  One simple example of this is the simple function fitness = Net Profit – 2*Drawdown.  This measure will work fairly well but might produce many solutions with too few trades.  We could modify this function so that solutions with 40 more trades would score higher, for example the function fitness = Maximum(1,Trades/40)*(NetProfit-2*Drawdown).

Using this new function over many generations will evolve children with more than 40 trades while also having a good net-profit-to-drawdown ratio.  The higher the ratio of trades to conditions, the more likely a given solution will continue to be reliable.

Combining Trading Methods – Genetic algorithms can also be used to either combine forecasts from multiple models or select the best model to trade at any given time.  In this application, we first need to select which of many trading models or strategies we will use in our analysis.  Then we need templates to evaluate their performance over given time periods for different trading strategies.  Finally, we develop templates that evaluate current market conditions.  We now will take a closer look at an example of this concept.

First we will develop rules that tell us if we should be long or short on a given day by combining various trading strategies.  Our goal is to develop trading rules using this structure:

If Method 1 = Long and Method 2 = Long and
Method1Performance = True and
Method2Performance = True and MarketCondition1 = True and
MarketCondition2 = True then buy market position = long;

In these rules Method1, Method2 are trading strategy templates.  The Method1Performance and Method2Performance are templates that evaluate different measures of the performance of a given trading strategy.  We could “and” together templates for either different strategies or the same strategy over different time periods.  The MarketCondition1 and MarketCondition2 are templates that evaluate the current fundamental or technical market conditions.  In this type of analysis, we could use CPI data, Commitment of Traders data or even technical indicators like ADX and develop rule templates to evaluate current market conditions.  This application is similar to evolving system trading rules except that we are using multiple types of templates and not just combining simple trading primitives.

To design a chromosome, which would use four different types of genes, for this application, we would have one type of gene for each type of template and a simple one element gene, which would represent our position: long, short or flat.  However, because we are “anding” together two of each type of template, we will have seven genes in our chromosome.  This application will use the same type of fitness functions used in evolving standard trading rules.

Money Management Applications – The final application is using a genetic algorithm as part of a money management strategy.  Calculating optimal f on a large portfolio can become an NP complete type problem.  We can use genetic algorithms to intelligently search these combinations and quickly find a near-optimal value for optimal f for each commodity in the portfolio.  In this application, we would set up our optimal f equations and simply use the genetic algorithm to plug in numbers that will be used to find these f values for the portfolio.  Normally, when calculating optimal f, we try to maximize net profit.  Using genetic algorithms we can use more complex measures of fitness for our solutions.  One example is to maximize the function fitness = NetProfit/Standard deviation of returns.  This function finds a near-optimal solution for returns on a risk-adjusted basis.

Genetic algorithms can be used in many different trading applications while also incorporating the expertise of a human trader.