# Details of the Genetic Programming Algorithm—The Heart of the System

The core of the Genetic System Search for Technical Analysis software is a genetic programming engine that uses the Darwinian mechanism of evolution to arrive at a superior technical analysis trading system.

## Initial Population Creation

Genetic System Search for Technical Analysis starts by creating a population of candidate investment systems. The initial and, in fact, all new population members generated during the course of the search are created at random. The creation function first builds indicator composites. (For instance, the 30-bar simple moving average of the opening prices.) Then those are coupled with logical terms, AND and OR to form a trading signal or rule. The depth of the rule (essentially the number of terms) is also random with a upper limit. A set of rules is combined to form a trading system of buy and sell signals.

## The Broker Simulator

Each of these trading systems (a set of buy and sell rules) is evaluated using the price and volume data of the security under study. As each population member is created or modified, it is passed to the internal broker function. The broker evaluates the trading rules at each bar of the supplied price and volume data for the stock under study. Trades are opened and closed according to the signals specified by the rules. For each trade the profit or loss and brokerage fees are calculated. At each bar of data the drawdown and accrued interest are noted.

## Fitness Evaluation

All investment systems are assigned a numerical value based on their profit, brokerage expenses, maximum drawdown and other factors. The members of the population are ranked by this "fitness" value.

The fitness function is a formula producing a single number for each population member. The program assigns a number to each member and ranks the population from least to most fit. The least fit members die off and the most fit members live on to reproduce. The program calculates the total profit of each population member and then subtracts other factors such as the drawdown. A population member with a very high profit may also have a large drawdown. Another candidate may have less profit but a lower drawdown and be rated higher in fitness than the first member.

Investors seldom rigorously and methodically inspect the ancillary factors of a trading system. The key power of the fitness function is that it takes all pertinent elements into account.

## Generational Evolution

Through a series of evolutionary steps called generations, the population of trading systems is winnowed in a manner akin to the Darwinian process. At each generation the rules of a few population members undergo analogs of natural selection.

- Crossover (also called Recombination)—The enter long rules of two good systems may be combined.
- Mutation—A simple moving average term may be exchanged for an exponential moving average.
- Reproduction—Good systems reproduce so that they are more likely to be chosen for crossover in future generations.
- Numerical Modification—The random adjustment of the term variables. A 40-day weighted moving average may be changed to a 50-day weighted moving average.

## Survival of the Fittest

New population members are created while the least fit members die off. Often GSSTA will take the basic building blocks and construct the more complex indicators on its own. The computer quickly discards elements that don't increase the fitness of the population members. It's survival of the fittest—just like biological evolution.

Population members with good fitness scores continue to live. Lesser members drop off the map—just like natural evolution. Woolly mammoths are gone but elephants live on. It has worked well in the natural world and over the last few decades, with the advent of modern computer techniques, it has been applied to mechanical and mathematical creations.

## Fitness Function Factors

Each set of trading rules is evaluated and assigned a fitness value. The most fit members of the population are carried forward into future generations while the least fit members are discarded. The fitness value comprises several factors.

### Profit Factor

The primary factor of the fitness function is the return provided by the trades specified by the genetic program. The total account return or gain is the sum of the cumulative trade profits and the amount earned on funds not invested in a security.

### Standard Deviation Factor

For each population member the program calculates the standard deviation of the trade profits. To combat overfitting, investment formulas that exhibit a wide variance in profits and losses are penalized in relation to other population members.

### Parsimony Factor

The fitness of a genetic program is adjusted by a parsimony factor, also called a complexity penalizing factor. The result is increased comprehensibility of the buy and sell rules and a reduced tendency to overfit the data.

### Consecutive Loss Factor

The fitness scores of trading systems that contain a series of consecutive losses are lowered. The rules with the greatest number of losses without intervening profitable trades are subject the largest rating reduction.

### Drawdown Factor

There is a drawdown factor in the fitness function that penalizes a genetic program with large drawdowns during with active trades.

### Signal Conflict Factor

The fitness value is reduced if any enter and exit signals conflict. For instance, a trading system that has coincidental enter long and enter short signals will be given a lower score.

See genetic programming in operation. Download the free trial version of the program.

## Examples of Evolutionary Changes to Investment Formulas

The genetic programming engine inside the Genetic System Search for Technical Analysis program uses several operations to arrive at a set of technical analysis trading rules.

### Crossover

In one area of the program it selects two population members with good fitness and therefore, the potential to be even better. As an example, the following two signals might be selected to participate in a crossover:

- The MACD is greater than 0 AND the 120-period Simple Moving Average of the low is greater than the low
- The 9-period Relative Strength Index is greater than 10 AND the 9-period ADXR is greater than 25

In a crossover operation one term in the first rule is exchanged for a term in the second rule. The resulting pair might then be:

- The MACD is greater than 0 AND the 9-period ADXR is greater than 25
- The 9-period Relative Strength Index is greater than 10 AND the 120-period Simple Moving Average of the low is greater than the low

### Mutation

The genetic mutation operator modifies just one rule at a time. The program selects a rule and randomly makes a change to one of its elements. Let's use the first unmodified rule above as an example:

The MACD is greater than 0 AND the 120-period Simple Moving Average of the low is greater than the low

After a mutation that rule might be changed to:

The MACD is greater than 0 AND the 120-period Simple Moving Average of the low is greater than the close.

The change of the comparison term from the low to the closing price is subtle but may be enough to push the rule set to a higher fitness number.

### Numerical Modification

A variation of mutation is the adjustment of numerical constants. Use the revised rule from the mutation operation as an example:

The MACD is greater than 0 AND the 120-period Simple Moving Average of the low is greater than the close

The program may change it to:

The MACD is greater than 0 AND the 130-period Simple Moving Average of the low is greater than the close

### Cascading Changes

There is no limit to the number of revisions each rule may undergo. During its life a rule appearing in a trading system may be transformed once, many times or never.

#### Try it for Yourself

Try genetic programming algorithms on your own computer. Download the free trial version of Genetic System Search for Technical Analysis (GSSTA).

#### Additional Information in the Blog

Read more in the Genetic Programming category of The GSSTA Blog.

## Overoptimization

The Genetic Search System for Technical Analysis software incorporates mechanisms to guard against overfitting of the results. Such bad curve fitting or overoptimization results in poor system performance in out-of-sample trading. A fragile set of investment formulas might show profitable trades upon construction but they fail when applied to a security during later use. The Standard Deviation and Consecutive Loss factors in the fitness formula penalize solutions that may fall apart when presented with new data.