Download MetaTrader 5

Genetic Algorithms vs. Simple Search in the MetaTrader 4 Optimizer

20 April 2007, 15:22
Andrey Khatimlianskii
2
4 301


1. What Are Genetic Algorithms?

The MetaTrader 4 platform now offers genetic algorithms of optimization the Expert Advisors' inputs. They reduce optimization time significantly without any significant invalidation of testing. Their operation principle is described in article named Genetic Algorithms: Mathematics in details.

This article is devoted to EAs' inputs optimization using genetic algorithms compared to the results obtained using direct, complete search of parameter values.

2. The Expert Advisor

For my experiments, I slightly completed the EA named CrossMACD that you may have known from the article named Orders Management - It's Simple:

  • Added StopLoss and TakeProfit to the placed positions.
  • Added Trailing Stop.
  • Used parameter OpenLuft to filter signals: Now signal will come if the zero line is crossed at a certain amount of points (with the accuracy to one decimal place).
  • Added parameter CloseLuft for the similar filtering of close signals.
  • Put in expernal variables the periods of the slow and the fast moving averages used for MACD calculations.

Now it is a practically completed Expert Advisor. It will be convenient to optimize it and use in trading. You can download EA CrossMACD_DeLuxe.mq4 to your PC and test it independently.

3. Optimization

Now we can start to optimize the EA. Three tests will be conducted with different amounts of optimizing searches. This will help to compare profits obtained using genetic algorithms in various situations.

After each test, I will manually remove the tester cache for the subsequent tests not to use combinations already found. This is necessary only for the experiment to be more precise - normally, automated chaching of results just enhances the repeated optimization.

To compare the results, optimization using genetic algorithms will be made twice: first time - in order to find the maximal profit (Profit), second time – to find the highest profit factor (Profit Factor). After that, the best three results for both optimization methods will be given in the summary report table sorted by the given columns.

Optimization is purely experimental. This article is not aimed at finding inputs that would really make greatest profits.

Test 1

  • chart symbol – EURUSD;
  • chart timeframe – Н1;
  • testing period – 2 years;
  • modelling – "Open prices only";
  • inputs searched in:

Variable Name

Starting Value

Step

Final Value

StopLoss

0

10

100

TakeProfit

0

10

150

TrailingStop

0

10

100

OpenLuft

0

5

50

CloseLuft

0

5

50

Number of searches

234256

It must be noted that, when using genetic algorithms, the expected time of optimization is approximately the same as that of optimization using direct inputs search. The difference is that a genetic algorithm continuously screens out certainly unsuccessful combinations and, in this way, reduces the amount of necessary tests several times (perhaps several tens, hundreds, thousands of times). This is why you should not be geared to the expected optimization time when using genetic algorithms. The real optimization time will always be shorter:

Direct search

Genetic algorithm

As you see, optimization using genetic algorithms took less than four minutes instead of the expected five and a half hours.



Optimization graph with genetic algorithms also differs from that with direct search. Since bad combinations have already been screened out, the subsequent tests are conducted with combinations of inputs that are more profitable by default. This is why the balance graph goes up:




Let us consider the results of both optimization methods in all details.

Results Table:


Direct search

Genetic algorithm

Total optimization time

4 h 13 min 28 sec

3 min 50 sec


SL

TP

TS

Open Luft

Close Luft

Profit

SL

TP

TS

Open Luft

Close Luft

Profit

1

70

140

0

20

30

1248.08

70

140

0

20

30

1248.08

2

70

140

0

20

35

1220.06

70

140

0

20

35

1220.06

3

70

150

0

20

30

1176.54

70

150

0

20

30

1176.54


SL

TP

TS

Open Luft

Close Luft

Profit Factor

SL

TP

TS

Open Luft

Close Luft

Profit Factor

1

100

50

40

50

5

4.72

0

50

40

50

5

4.72

2

90

50

40

50

5

4.72

90

50

40

50

5

4.72

3

80

50

40

50

5

4.72

80

50

40

50

0

4.72

As you can see from the table, optimization using genetic algorithms is some tens of times faster! The results are practically the same. There are several results with maximal profit of 4.72, this is why different combinations of inputs are reported, but it is not very important.

Now let's try to decrease the amount of searches, but increase the testing time. We will use the "All ticks" model for this.

Test 2

  • chart symbol – EURUSD;
  • chart timeframe – Н1;
  • testing period – 2 years;
  • modelling – "All ticks";
  • inputs searched in:

Variable Name

Start Value

Step

End Value

StopLoss

0

10

100

TakeProfit

0

10

150

TrailingStop

0

10

100

OpenLuft

0

10

50

Number of searches

11 616

Results table:


Direct search

Genetic algorithm

Total optimization time

32 h 32 min 37 sec

1 h 18 min 51 sec


SL

TP

TS

Open Luft

Profit

SL

TP

TS

Open Luft

Profit

1

50

0

0

20

1137.89

50

0

0

20

1137.89

2

70

0

0

20

1097.87

70

0

0

20

1097.87

3

60

0

0

20

1019.95

60

0

0

20

1019.95


SL

TP

TS

Open Luft

Profit Factor

SL

TP

TS

Open Luft

Profit Factor

1

50

90

60

50

4.65

50

90

60

50

4.65

2

50

140

60

50

4.59

50

140

60

50

4.59

3

100

90

60

50

4.46

70

90

60

50

4.46

For such an amount of searches, the optimization rate differs 25 times which is not bad either. The results conincide by practically 100%, the only difference is in the StopLoss value on the third pass. The profit factor remains maximal.

Now let's try to increase the amount of searches and descrease the testing time. Let us use the "Control points" model for this.

Test 3

  • chart symbol – EURUSD;
  • chart timeframe – Н1;
  • testing period – 2 years;
  • modelling – "Control points";
  • inputs searched in:

Variable Name

Start Value

Step

Final Value

StopLoss

0

10

100

OpenLuft

0

5

50

CloseLuft

0

5

50

Number of searches

1 331

Results table:


Direct search

Genetic algorithm

Total optimization time

33 min 25 sec

31 min 55 sec


SL

Open Luft

Close Luft

Profit

SL

Open Luft

Close Luft

Profit

1

0

0

45

1078.03

0

0

45

1078.03

2

70

20

15

1063.94

70

20

15

1063.94

3

70

20

25

1020.19

70

20

25

1020.19


SL

Open Luft

Close Luft

Profit Factor

SL

Open Luft

Close Luft

Profit Factor

1

80

50

15

2.73

80

50

15

2.73

2

70

50

15

2.73

70

50

15

2.73

3

90

50

15

2.65

90

50

15

2.65

The situation has changed. The optimization periods coincide (an insignificant error is admissible), and the results are identical. This can be explained through that optimization consisted of only 1331 searches and this amount of passes is just not enough for using genetic algorithms. they have no time to "pick up speed" - the optimization is faster due to screening out certainly losing inputs combinations, but having such amount of combinations as above, genetic algorithms cannot define what "parents" (inputs combinations) generate bad "off-spring". So, there is no sense to use them.


4. Conclusions

Genetic algorithms are a nice addition to the МТ 4 strategies optimizer. Optimization is dramatically enhanced if the amount of searches is large, the results coincide with those obtained by regular optimization.



Now there is no sense to use the full search in inputs. Genetic algorithms will find the best result faster and no less effectively.



5. Afterword

After having written the article, I satisfied my curiosity and launched optimization of CrossMACD_DeLuxe on all inputs. The amount of combinations made over one hundred million (103 306 896). The optimization using genetic algorithms took only 17 hours, while optimization using search in all inputs would take approximately 35 years (301 223 hours).



Conclusions are up to you.

Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/1409

Attached files |
Last comments | Go to discussion (2)
MQL4 Comments
MQL4 Comments | 8 Sep 2007 at 22:25

I have been looking for ways to optimize optimization, and this article intrigues me. How exactly would I go about enabling "genetic optimization?"

Thanks

Nick

MQL4 Comments
MQL4 Comments | 27 Sep 2007 at 16:29
Thanks for the great explanation and thorough testing Andrey! Very helpful.

Nick, there is a checkbox in the Strategy Tester when you click "Expert properties" -- it is on the first tab "Testing"
Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.

How to create an indicator of non-standard charts for MetaTrader Market How to create an indicator of non-standard charts for MetaTrader Market

Through offline charts, programming in MQL4, and reasonable willingness, you can get a variety of chart types: "Point & Figure", "Renko", "Kagi", "Range bars", equivolume charts, etc. In this article, we will show how this can be achieved without using DLL, and therefore such "two-for-one" indicators can be published and purchased from the Market.