Contest of Expert Advisors inside an Expert Advisor

Evgeniy Trofimov | 15 September, 2010

Introduction

Dear developers and exchange analysts!

Often, many trading systems (TS) are stable for a specific time period, but further, the balance curve starts moving downwards. Trader disappoints in it and starts inventing new grails, optimizing parameters, etc.

I bring to your attention a tool for performing virtual trades - VirtualTrend.mqh. Using the function I suggest, you can open, close and trail virtual trades.

It has the following useful features:

  1. The possibility of creating adaptive Expert Advisors that can enable and disable virtual trading depending on the results of former trades.
  2. Rating several trading systems (in this example, 5 trading systems are presented) in the percentage terms depending on their profitability. The competition results in a decision of which trading system is the most preferable for further trading at the real market.
  3. The possibility of implementing trading strategies, which use several open trades by an instrument, into the MetaTrader 5 platform where cumulative positions are used (this feature is not described in this article, but suggested as a variant of using).

Let's take a look into the first two statements.

Let's take the Competition_v1.0.mq4 code as an example of adaptive Expert Advisor; there, on the competitive basis, a trade system to be used is determined. The Expert Advisor uses 5 (you can add more if you want) trading systems, which aren't chosen randomly, but on the basis of a more or less stable working on the daily timeframe of EURUSD.

Important! You should use just the stable trading systems, which make profit or constantly lose at a certain period of time.

For example, a trading system, which sequence of wins is "PPPPPPPLLLLLLLLPPLPPPPPPPP", fits the conditions to be used in this Expert Advisor; but if the sequence of wins is "LLPPLPLLPPPLLPLPPLPLPLLLPPLPL", it doesn't fit ("L" – losing trades, "P" – profitable trade).

A trading system where losing trades are more frequent that profitable ones, but the profit trades cover the former loss, cannot be used as well, because the Expert Advisor just doesn't manage to adapt to the profitability.

The stability of a trading system is determined "by sight" on the results of tests conducted for the maximum period (all history) in the strategy tester. The obtained diagram of change of balance allows determining the average weighted number of deals that cause a change of tendency. For example, a trading system performs the following sequence of deals: PPPPPPPPLLLLLLPPPPPPPPPLPLLLLLLLLLLLPPPPPPPLLLLLLLLLLLLLPPPPPPLLLLLLLLLLPLPPPLLPPPPPP. The tendency of balance changes after about 6-8 trades. When calculating the rating, it is recommended to set the period of averaging of the balance equal to a half of those trades. That is 3 or 4.

Enabling and disabling the rating system, which helps the Expert Advisor to adapt to the changes of profitability dynamics, is managed via the RatingON parameter. Using it, you can easily find a period of averaging of balance and set it for the Tх.PeriodRating parameter.

Each trading system can be enabled or disabled at your will using the Tх.Enabled parameter, where х is the number of TS.

All the tests of trading systems represented below are conducted at EURUSD (D1 timeframe) during the period from 1999.01.01 to 2010.06.01.

Trading System №1


Figure 1. The test of the trading system №1 without adaptation (RatingON = false)

The result of testing allows drawing a conclusion that after each 18-20 trades this strategy changes its dynamics of profitability.

That is why the T1.PeriodRating parameter is set to 20.

The test of the trading system №1 with adaptation enabled (RatingON = true)

Figure 2. The test of the trading system №1 with adaptation enabled (RatingON = true)

Trading System №2

During the test for the same period we have found out that the stability is equal to about 10 trades.


Figure 3. The test of the trading system №2 without adaptation


Рисунок 4. The test of the trading system №2 with the adaptation enabled

Trading System №3

The logic of entering and exiting is the same as the one of TS №1, however the periods of averaging of moving averages are different.

A little digression: you can rewrite the entire Competition_v1.0.mq4 and combine it from the strategies that consist of only moving averages with different periods.

I haven't conducted this interesting investigation yet, but if I make it, I'll inform you.


Figure 5. The test of the trading system №3 without adaptation

This TS performs few trades during a long period (almost 11 years). I recommend not using this strategy in real trading since the number of trades in the strategy tester is too small.

I've added this strategy for more obviousness of showing my development of the adaptation. The T3.PeriodRating parameter is set to 2 for this strategy.


Figure 6. The test of the trading system №3 with the adaptation enabled

Trading System №4

I've seen this strategy many times in literature.

It seems attractive even without the adaptation, but using it in trading doesn't require an automated trading, since it's a long-term one.


Figure 7. The test of the trading system №4 without adaptation

This trading strategy has a constant stability, so the period of processing the data for calculation of the rating should be no less than 20 trades. I set T4.PeriodRating=20.


Figure 8. The test of the trading system №4 with the adaptation enabled

Trading System №5

This TS is developed by me and I want to share it with you in such a sophisticated way.


Figure 9. The test of the trading system №5 without adaptation

Such chart can be left without the adaptation, but anyway, set T5.PeriodRating=10.


Figure 10. The test of the trading system №5 with the adaptation enabled

Multisystem Expert Advisor

The working of 5 Expert Advisors with and without the adaptation is demonstrated above.

Now we're going to consider an example of collaborative work of those Expert Advisors:

Symbol

EURUSD (Euro vs US Dollar)

Period

Day (D1) 1999.05.24 00:00 - 2010.07.05 00:00 (1999.01.01 - 2010.07.05)

Model

Control points (a very crude method, the results must not be considered)

Parameters

RatingON=false; FastTest=true; file="virtual.csv"; MinRating=50; _tmp1_="---- Trading system 1 ----"; T1.Enabled=1; T1.Magic=101; T1.lot=0.1; T1.Fast=10; T1.Slow=100; T1.TS=7000; T1.PeriodRating=20; _tmp2_="---- Trading system 2 ----"; T2.Enabled=1; T2.Magic=102; T2.lot=0.1; T2.PeriodCCI=30; T2.LevelCCI=200; T2.SL=500; T2.PeriodRating=10; _tmp3_="---- Trading system 3 ----"; T3.Enabled=1; T3.Magic=103; T3.lot=0.1; T3.Fast=30; T3.Slow=200; T3.TS=5000; T3.PeriodRating=2; _tmp4_="---- Trading system 4 ----"; T4.Enabled=1; T4.Magic=104; T4.lot=0.1; T4.SL=5000; T4.TS=5000; T4.LimitMACD=0.002; T4.PeriodRating=60; _tmp5_="---- Trading system 5 ----"; T5.Enabled=1; T5.Magic=105; T5.lot=0.1; T5.PeriodCCI=90; T5.LevelCCI=100; T5.TralingCCI=100; T5.SL=5000; T5.TS1=5000; T5.PeriodRating=10;






Bars in test

2994

Ticks modelled

219840

Modelling quality

n/a

Initial deposit

500000.00





Total net profit

617173.70

Gross profit

1342671.82

Gross loss

-725498.13

Profit factor

1.85

Expected payoff

2373.74



Absolute drawdown

76798.13

Maximal drawdown

172676.05 (28.98%)

Relative drawdown

28.98% (172676.05)


Total trades

260

Short positions (won %)

126 (29.37%)

Long positions (won %)

134 (33.58%)


Profit trades (% of total)

82 (31.54%)

Loss trades (% of total)

178 (68.46%)

Largest

profit trade

78151.67

loss trade

-18831.39

Average

profit trade

16374.05

loss trade

-4075.83

Maximum

consecutive wins (profit in money)

6 (89681.19)

consecutive losses (loss in money)

21 (-100325.23)

Maximal

consecutive profit (count of wins)

95057.65 (3)

consecutive loss (count of losses)

-100325.23 (21)

Average

consecutive wins

2

consecutive losses

4



Figure 11. The test of the multisystem Expert Advisor without adaptation

The result of combining the trading systems is the increase of profit, drawdown and number of trades.

Now let's look into the same test but with the RatingON parameter enabled.

Period

Day (D1) 1999.05.24 00:00 - 2010.07.05 00:00 (1999.01.01 - 2010.07.05)

Model

Control points (a very crude method, the results must not be considered)

Parameters

RatingON=true; FastTest=true; file="virtual.csv"; MinRating=1; _tmp1_="---- Trading system 1 ----"; T1.Enabled=1; T1.Magic=101; T1.lot=0.1; T1.Fast=10; T1.Slow=100; T1.TS=7000; T1.PeriodRating=20; _tmp2_="---- Trading system 2 ----"; T2.Enabled=1; T2.Magic=102; T2.lot=0.1; T2.PeriodCCI=30; T2.LevelCCI=200; T2.SL=500; T2.PeriodRating=10; _tmp3_="---- Trading system 3 ----"; T3.Enabled=1; T3.Magic=103; T3.lot=0.1; T3.Fast=30; T3.Slow=200; T3.TS=5000; T3.PeriodRating=2; _tmp4_="---- Trading system 4 ----"; T4.Enabled=1; T4.Magic=104; T4.lot=0.1; T4.SL=5000; T4.TS=5000; T4.LimitMACD=0.002; T4.PeriodRating=60; _tmp5_="---- Trading system 5 ----"; T5.Enabled=1; T5.Magic=105; T5.lot=0.1; T5.PeriodCCI=90; T5.LevelCCI=100; T5.TralingCCI=100; T5.SL=5000; T5.TS1=5000; T5.PeriodRating=10;






Bars in test

2994

Ticks modelled

219840

Modelling quality

n/a

Initial deposit

500000.00





Total net profit

227123.75

Gross profit

388438.79

Gross loss

-161315.05

Profit factor

2.41

Expected payoff

2341.48



Absolute drawdown

10921.17

Maximal drawdown

76482.03 (12.48%)

Relative drawdown

12.48% (76482.03)


Total trades

97

Short positions (won %)

50 (40.00%)

Long positions (won %)

47 (46.81%)


Profit trades (% of total)

42 (43.30%)

Loss trades (% of total)

55 (56.70%)

Largest

profit trade

71192.28

loss trade

-12680.47

Average

profit trade

9248.54

loss trade

-2933.00

Maximum

consecutive wins (profit in money)

5 (80463.85)

consecutive losses (loss in money)

13 (-50753.48)

Maximal

consecutive profit (count of wins)

80463.85 (5)

consecutive loss (count of losses)

-50753.48 (13)

Average

consecutive wins

2

consecutive losses

3


Рис. 12. The test of multisystem Expert Advisor with the adaptation enabled

The balance line flattened, the number of dramatic drawdowns decreased, the profit decreased 2.71 times, drawdown decreased 2.32 times, the number of trades decreased 2.68 times, the profit factor increased 1.3 times.

In the fig. 12, unlike in the previous tests, the diagram of volumes has appeared. It occurred as the result of the active rating system.

It acts in the following manner - at first, the profits Tх.PeriodRating of all closed virtual trades are summed up and the obtained value is divided by the number of days those trades were performed for. The obtained value is added by the cumulative profit of all open positions and divided by the number of days of their being active.

If the obtained value is negative, then it's set equal to zero. This operation is performed for each TS.

The rating system tells one trading system from another using magic numbers assigned to active trading strategies in the Tх.Magic initial parameters. It searches for the most profitable TS by the maximum value of profit and set the 100% rating for this system.

All the other TS are assigned with a rating relatively to the rating of leading TS. In the end, a table with three columns is created.

Example:

Magic
Profit
Rating, %
1
9
40.9
2
0
0.0
3
3
13.6
4
10
45.5
5
0
0.0
6
17
77.3
7
0
0
8
12
54.5
9
0
0.0
10
22
100.0

During the real trading, the obtained rating value is multiplied by the bet amount Tх.lot and is divided by 100. In this case, all the trading systems with the rating greater than zero will take part in the real trading.

To make only the leading trading system take part in the real trading, set the MinRating parameter equal to 100 in the Competition_v1.0 Exper Advisor.

Conclusion

The represented methodology doesn't give a 100% guarantee of getting a profit; however, I can tell you that smoothing of the balance curve is assured. Namely - decrease of profit, drawdown and number trades performed are guaranteed! Increase of profit factor - probably.

It's up to you to decide if it's good or not.

The description of the parameters of the Competition_v1.0.mq4 Expert Advisor:

Links to similar subjects: http://forum.mql4.com/ru/23455