# Genetic algorithm development - chromosome stucture based on buys/sells

Creating a GA algorithm for intraday trading (e.g., futures ES, NQ) is more difficult than textbook examples for GA function minimization/maximization. Initially, I assumed the parameters for buys and sells could be placed within each chromosome, but am now thinking that each chromosome can only represent a buy or a sell, where a single buy/sell parameter (gene) $$x$$ on the chromosome would result in a buy if $$x>0.5$$ and sell if $$x \leq 0.5$$ given parameter range [0,1].

Another challenge I am facing is that many of my rules are discrete (true/false) rather than continuously-scaled levels of indicators such as RSI level, ADX level, etc. For example, I have dozens of binary (0-no,1-yes) cross-over and cross-below type rules which are true/false. For these rules (genes), I am assuming boolean logic like buy=true if $$x>0.5$$.

The last challenge is that with many cross-over and cross-below rules, the odds that most of them are true for a given bar is unlikely, so the problem becomes one of finding the best combination of rules that e.g. maximizes the Sharpe(Sortino) ratio. In light of the above, would it be necessary to perhaps initialize each chromosome such that only one gene is set to $$x=0.75$$ and all other rules set to $$x=0.25$$, so that initially the fitness value (Sharpe) will be based on buys/sells if a single rule is true?

Certainly, there has to be a "trick" when using GAs when a lot of rules are considered for each chromosome, and the chances of e.g. 10 rules being true is rare, since only a few bars per 10,000 bars would have 10 rules being true.

One thing is certain with any GA: the value of every gene on a chromosome must be reflected in fitness, in other words, you can't have genes that trigger actions that don't affect fitness. So if fitness is like the height of a person (objective function), you can't have genes that encode eye or hair color, since those won't help minimize/maximize height.

Are there any classic papers (chapters) which describe chromosome setup for lots of binary (yes/no) trading rules?

Based on your terminology, it sounds like you're using GEP (gene expression programming), a subtype of GA. Any reason why you wouldn't use pure GA for this? I don't have a ton of experience with GEP, but it generally seems more complicated without adding much more in this context.

Otherwise, last question(s) first:

The last challenge is that with many cross-over and cross-below rules, the odds that most of them are true for a given bar is unlikely, so the problem becomes one of finding the best combination of rules that e.g. maximizes the Sharpe(Sortino) ratio. In light of the above, would it be necessary to perhaps initialize each chromosome such that only one gene is set to x=0.75 and all other rules set to x=0.25, so that initially the fitness value (Sharpe) will be based on buys/sell if a single rule is true?

This is a strategy question more than a GA question. To simplify, assuming you have 10 criteria you're considering; you decide, initially, to only trade if all 10 criteria are satisfied. Your resulting population would likely be quite small depending on asset, trading rules and time period. This would potentially be something you could tweak to get a reasonable population for testing (ie, set r=number of criteria satisfied equal to n such that your population > 100, etc). You could also potentially allow your GA to make the decision for you (ie, would stand to reason the GA will 'pick' a strategy with highest Sharpe absent additional restrictions).

One thing is certain with any GA: the value of every gene on a chromosome must be reflected in fitness, in other words, you can't have genes that trigger actions that don't affect fitness. So if fitness is like the height of a person (objective function), you can't have genes that encode eye or hair color, since those won't help minimize/maximize height.

Yes, but how do you know a priori which genes affect fitness? For instance, given the signals you referenced, how do you know whether RSI will impact portfolio Sharpe more or less an ADX? That's also sort of the point of GA...to let the algorithm tell you which have an impact.

Generally though, there's no 'trick'. Part of using a GA in the way you've referenced is simply setting up your strategy in such a way that it lends itself to GA. Beyond that, organizing your in- and outputs in a way the GA can use them and you can make sense of them.

Also, one guy's opinion (though a guy who applied GA to technical trading in FX markets as part of my master's thesis), using GA to pick a set of rules to trade on moving forward is really just elaborate datamining/overfitting. Your fitness criterion reflects the set of rules that happened to do the best on the asset(s) in question over the historical period. You have no real idea what that means nor whether it should or will be applicable moving forward. If you're doing this as a learning exercise, by all means; if you're hoping to extract truth and make money, I'd probably proceed with caution.

• I eventually plan on performing out-of-bag testing on windowed bar segments not included in training, at least to attempt to reduce over-fitting. – user6430 Feb 6 '20 at 1:37

The approach required when using a GA for determining the optimal combination of indicator signals for a trading rule is to intersperse randomly placed AND or OR logical statements between the boolean results (true,false) for indicators, which are randomly initialized using e.g.

$$\text{if } (x>0.5) \rightarrow \text{ AND}$$

$$\text{if } (x \leq 0.5) \rightarrow \text{ OR}$$

where the range of gene $$x$$ is [0,1]. In this fashion, during training the combination of AND and OR can change to the better. By using the same criterion for genes with range [0,1] for all the logical AND or OR sections of a chromosome, the GA will modify the real values of any $$x$$ and preferentially reward the best choice of (AND, OR) that maximizes fitness. When done, you will learn that e.g. only 2 out of 5 indicators are needed for trade entries (see below) and 3 are needed for trade exits.

But first, a little terminology. Each chromosome is called a "strategy" and consists of two segments of genes, a trade entry segment (multiple genes) and a trade exit segment (multiple genes). A chromosome therefore can either represent going long and exiting the long, or going short and exiting the short. Either way, the genes on a single chromosome encode how to enter and exit the market, and the entire chromosome represents either a long or short trade with its exit.

Optimization of the combination of indicators used for a trade entry(exit) is accomplished by use of randomly placed AND and OR logical statements between the indicator boolean results (true,false). So for example, if the indicators used for long entry are SMACrossAbove, RSI, LinRegSlope, the left side of a chromosome would be (with randomly placed AND and OR):

SMACrossAbove | AND | RSI<30 | OR | LinRegSlope>0.1

and the right side could be

SMACrossBelow | OR | RSI>70 | AND | LinRegSlope<-0.2

so when done the entire chromosome is

SMACrossAbove | AND | RSI<30 | OR | LinRegSlope>0.1 | SMACrossBelow | OR | RSI>70 | AND | LinRegSlope<0.2

One thing is certain, if the long entry logical is true you must go long, and if the exit logical is true you must exit long. You cannot mix enter long with .e.g. exit short (cover), or mix enter long with enter short.

A entire chromosome setup for shorting could be the exact opposite like

SMACrossBelow | AND | RSI>70 | OR | LinRegSlope<-0.02 | SMACrossAbove | OR | RSI<30 | AND | LinRegSlope>0.1

You can also have (add) genes on the chromsome which encode for indicator values like short and long period lengths for the SMA crossover, the RSI level, and the LinReg slope values. (the intent here is to optimize indicator parameters the same way as is usually done via historical exhaustive search, particle swarm optimization, neural network etc.)

There are papers that suggest performing crossover and mutation separately for long entry chromosomes, and short entry chromosomes, but I think it's not necessary.

For a given bar, once you determine if the indicator signals are true or false, if you generate a logical string for the entry segment and a string for the exit segment, you can rapidly test (C#) if the e.g. entry string is true by use of a DataTable(), as in:

string mylogic = "true and false or true or false and true and true";
System.Data.DataTable table = new System.Data.DataTable();
bool result = (bool)table.Compute(mylogic, "");


where "not" and parentheses can be used.

For fitness, the square of the Sterling ratio can be used in addition to the Sharpe or Sortino ratio. Percent winning trades can also be used for fitness, defined as fitness = #winning_trades / #trades. Although, I do like to use average profit sometimes, which would be sum(return-per-trade)/#trades.