Using Genetic Algorithms for Trading Applications
If 1 Then Begin On the first bar
If EFlag 1 Then Begin If we are evolving..
We get a guess and place it in VR
CA-Save 12, And we save it!
End;
If EFlag = 0 Then Begin If we are running a saved solution..
CA-Load 12, Load it into VR
End; End;
Run the actual trading rules. We retrieve gene data. then returns a 1 or a 0 (False) based on the application of the rule defined by its gene-data inputs. The inputs, of course, are taken from a gene in the gene string. We are allowing 3 genes, hence rules, and requiring all rules be True in order to take a trade.
I
GetGeneElem 0, Get first element of first gene GetGeneElem 1, Get second element of first gene GetGeneElem 2, Get third element of first gene GetGeneElem 3, Get fourth element of first gene
= the rule
GetGeneElem 4, Get first element of second gene GetGeneElem 5, Get second element of second gene GetGeneElem 6. Get third element of second gene GetGeneElem (1, 7, Get fourth element of second gene
= run the rule
GetGeneElem 8, Get first element of third gene GetGeneElem 9, Get second element of third gene GetGeneElem 10, Get third element of third gene GetGeneElem 11, Get fourth element of third gene
Res3 = run the rule
If = 1 And 1 And Res3 1 Then Begin If = 0 Then Buy At Open; End; If 0 Then Begin If 4 Then At stop; End: I TABLE 20.4
Finally, we need to tell TSEvolve how “good” its guess was. That is, we need to report the “fitness” of the guess it provided. Using the information, TSEvolve will be able to provide better and better guesses as the population maintained internal to TSEvolve evolves. Of course, we must do this only on the last bar of each generation, when all data have been processed for that generation’s guess as to the best chromosome set rules). The Date
clause in the if.. statement below provides us with a crude way to detect the last bar of a run. Of course, we only need to do this if we are evolving,
so we also have the EFlag 1 clause in the if.. statement that follows.
1
EFlag = 1 And Date Then Begin
Fitness = We will maximize I
GA-Fit (Fitness); Tell TSEvolve goodness of current guess Write out some useful info to a text file
“C:\TEST.TXT”, “AT”); Generation For K 0 To 11 Begin
Gene data
SC-Fprintf Genes, 1 per
If K = 3 Or K = 7 Or K 11 Then
line SC-Fprintf “N”, New line
End;
N”, End;
We are done. Verify this code, set up built-in optimizer to step the single input from 1 to whatever number of generations you want to run. Use an increment of one. To run the system, set to whatever generation you liked best as determined from studying the data. To do another evolution run, change the solutions file name or delete the existing solutions file from your disk (otherwise you will be retrieving old solutions rather than evolving new ones). I
302 Advanced to Develop Trading Using Genetic Algorithms for Trading Applications 303
On the first bar of every run, we get a chromosome that was the result of the last mating operation. We then use the values on each gene to test our defined set of three rules which will be together. We will buy
when all three of these rules are true. We hold our position for at least four bars and exit it after that period on a stop at 50 percent of a day average true range below yesterday’s low.
When we are in an evolution mode, we evaluate the fitness of a given rule set based on the trading performance as reported in We use a simple fitness function in this evaluation: 2 x
This function will produce profitable combinations with a good drawdown-to-profit ratio. If instead of this measure we use a mea- sure such as profit factor, the genetic algorithm might have evolved a so- lution with five or fewer trades, all of which are winners and have a profit factor of 100. These systems have too few trades and most likely will not be profitable in the future because they are curve-fitted systems.
We then write to both a solution file used by to reload solutions into once we are done evolving, and an in- formation file which we to analyze our results and se- lect the generation we would want to trade.
The way this code is written, it will evolve a generation if the value passed to the system is greater than the maximum value stored in the so- lution file. the value passed is less than the maximum generation in this file, it will run that generation to produce its trading signal.
Let’s now discuss the code for This code first initializes the return value of this function to false and then calculates normaliza- tion factors based on average true ranges for Datal, Data2, and Data3. Next, we find the rule that is being requested. Let’s look at each of these rule templates.
Rule 1 wants to see whether a momentum is above a given threshold. Both the lookbacks and the threshold are found using the genetic algo- rithm. Rule 2 compares two exponential moving averages and is true when the first one is greater than the second. The genetic algorithm also sets the periods used in these moving averages.
Rule 3 is true when a is below a threshold, and Rule 4 is true when a is above a threshold. Rule 5 is the same as Rule 1 but uses Data2; so is Rule 6, but now for Data3. Rules 7 and 8 are the same as Rule 2 but use Data2 and Data3, respectively. Rule 9 is when the first
moving average is less than the second and is applied to Datal. Rules 10 and 11 are the same as Rule 9 but are applied to Data2 and Data3. Rule 12 is just like Rule 1, but the value must be below a given -1 threshold rule. Rules 13 and 14 are the same as Rule 12, but are used for Data2 and Data3.
We evolved these rules with a population size of a crossover rate of and a mutation rate of They were evolved for genera- tions. A generation is a single mating of the population. We found that, in general, we had a number of good solutions. We then tested them, from to Four of them performed very well in both the devel- opment set and the testing set. One of the nice features about
is that we are able to rerun any generation without coding the rules. The chromosomes for each generation are stored in a solution file and can be used by to reload a given set of rules without any coding.
Let’s now see how these four generations/solutions performed during both the development and combined testing/out-of-sample sets (allowing $50.00 for slippage and commissions). Our development set period was from to and our combined set period was from to Note that we used 210 for back, so that the first 10 months of data did not generate any trades on the development set. The results are shown in Table 20.5.
We then translated our trading rules into both and En- glish so that we could see whether they would make sense to a domain ex- pert. These restated rules are shown in Table 20.6.
Note that we can use genetic algorithms to induce rules that we can
translate into both and English, and then have them stud- ied by a domain expert. We analyzed these rules and found several ex- amples of some of the theories discussed in earlier chapters. For example, the rule from generation 1909 has, as one of its conditions, that the XAU has not gone up too much. This rule confirms the existence of the con- cept of intermarket inversion, which we discussed earlier. Briefly, the concept is that intermarket relationships will sometimes invert. Positive ones become negative and negative ones become positive for some mar- kets, when these markets move too quickly. The classic example of this effect is that, when interest rates drop too quickly or gold rises too fast, the inverse relationship between gold and T-Bonds will invert and they will become positively correlated. Another example of this occurs with
304 Using Advanced Technologies to Develop Trading Strategies
TABLE 20.5 RESULTS OF SELECTED GENERATION OF RULES