Русский 中文 Español Deutsch 日本語 Português
Genetic Algorithms vs. Simple Search in the MetaTrader 4 Optimizer

Genetic Algorithms vs. Simple Search in the MetaTrader 4 Optimizer

MetaTrader 4Tester | 20 April 2007, 15:22
16 098 2
Andrey Khatimlianskii
Andrey Khatimlianskii


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 Ltd.
Original article: https://www.mql5.com/ru/articles/1409

Attached files |
Last comments | Go to discussion (2)
[Deleted] | 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

[Deleted] | 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"
A Method of Drawing the Support/Resistance Levels A Method of Drawing the Support/Resistance Levels
This article describes the process of creating a simple script for detecting the support/resistance levels. It is written for beginners, so you can find the detailed explanation of every stage of the process. However, though the script is very simple, the article will be also useful for advanced traders and the users of the MetaTrader 4 platform. It contains the examples of the data export into the tabular format, the import of the table to Microsoft Excel and plotting the charts for the further detailed analysis.
Pivot Points Helping to Define Market Trends Pivot Points Helping to Define Market Trends
Pivot point is a line in the price chart that shows the further trend of a currency pair. If the price is above this line, it tends to grow. If the price is below this line, accordingly, it tends to fall.
Displaying of Support/Resistance Levels Displaying of Support/Resistance Levels
The article deals with detecting and indicating Support/Resistance Levels in the MetaTrader 4 program. The convenient and universal indicator is based on a simple algorithm. The article also tackles such a useful topic as creation of a simple indicator that can display results from different timeframes in one workspace.
Expert System 'Commentator'. Practical Use of Embedded Indicators in an MQL4 Program Expert System 'Commentator'. Practical Use of Embedded Indicators in an MQL4 Program
The article describes the use of technical indicators in programming on MQL4.