Robot Example 3 – Moving Average Crossover

0

Prompted by Boston Technologies moving average crossover EA here is a robot based around a basic moving average crossover system. Like the Boston Technologies version this robot stays in the market 100% of the time, and changes between long and short positions when a fast exponential moving average crosses over a slower EMA. Unlike the Boston Technologies version, it does not (yet!) include money management, but does come complete with the source code, so you can see what happens under the hood.

It demonstrates how you can use MetaTrader's built in indicators as part of a forex trading system. It also demonstrates a trading system that doesn't use any stop-loss orders!

Feel free to experiment with different values for the Fast Period and Slow Period input settings, and try it using different time frames. Does it work better with minute, hour or daily bars?

Here are the results from the MetaTrader strategy tester for the first quarter of 2008 using the input settings suggested by Boston Technologies (Hourly Bars, FastPeriod = 20, SlowPeriod = 33)

Graph

For comparison purposes here are the results using the same input settings but for the first quarter of 2009. Notice any difference?

Strategy Tester Report
GuruEx03
Symbol EURUSD (Euro vs US Dollar)
Period 1 Hour (H1) 2009.01.05 00:00 – 2009.03.27 22:00 (2009.01.05 – 2009.03.29)
Model Every tick (the most precise method based on all available least timeframes)
Parameters Magic=12347; Slippage=30; Lots=1; FastPeriod=20; SlowPeriod=33; Hysteresis=4;
Bars in test 2427 Ticks modelled 2203801 Modelling quality 90.00%
Mismatched charts errors 0
Initial deposit 10000.00
Total net profit -9950.60 Gross profit 8629.00 Gross loss -18579.60
Profit factor 0.46 Expected payoff -382.72
Absolute drawdown 9950.60 Maximal drawdown 14989.40 (99.67%) Relative drawdown 99.67% (14989.40)
Total trades 26 Short positions (won %) 13 (30.77%) Long positions (won %) 13 (7.69%)
Profit trades (% of total) 5 (19.23%) Loss trades (% of total) 21 (80.77%)
Largest profit trade 2535.20 loss trade -2402.80
Average profit trade 1725.80 loss trade -884.74
Maximum consecutive wins (profit in money) 2 (3763.80) consecutive losses (loss in money) 11 (-8341.40)
Maximal consecutive profit (count of wins) 3763.80 (2) consecutive loss (count of losses) -8341.40 (11)
Average consecutive wins 1 consecutive losses 4

Graph

# Time Type Order Size Price S / L T / P Profit Balance
1 2009.01.07 14:38 buy 1 1.00 1.37249 0.00000 0.00000
2 2009.01.09 16:31 close 1 1.00 1.34843 0.00000 0.00000 -2402.80 7597.20
3 2009.01.09 16:31 sell 2 1.00 1.34843 0.00000 0.00000
4 2009.01.16 07:00 close 2 1.00 1.32298 0.00000 0.00000 2535.20 10132.40
5 2009.01.16 07:00 buy 3 1.00 1.32298 0.00000 0.00000
6 2009.01.19 16:00 close 3 1.00 1.31310 0.00000 0.00000 -987.20 9145.20
7 2009.01.19 16:00 sell 4 1.00 1.31310 0.00000 0.00000
8 2009.01.22 04:00 close 4 1.00 1.30208 0.00000 0.00000 1095.00 10240.20
9 2009.01.22 04:00 buy 5 1.00 1.30208 0.00000 0.00000
10 2009.01.23 06:00 close 5 1.00 1.29420 0.00000 0.00000 -787.20 9453.00
11 2009.01.23 06:00 sell 6 1.00 1.29420 0.00000 0.00000
12 2009.01.26 13:00 close 6 1.00 1.29558 0.00000 0.00000 -139.40 9313.60
13 2009.01.26 13:00 buy 7 1.00 1.29558 0.00000 0.00000
14 2009.01.29 02:00 close 7 1.00 1.31180 0.00000 0.00000 1626.00 10939.60
15 2009.01.29 02:00 sell 8 1.00 1.31180 0.00000 0.00000
16 2009.02.03 05:00 close 8 1.00 1.29038 0.00000 0.00000 2137.80 13077.40
17 2009.02.03 05:00 buy 9 1.00 1.29038 0.00000 0.00000
18 2009.02.04 18:00 close 9 1.00 1.28540 0.00000 0.00000 -497.20 12580.20
19 2009.02.04 18:00 sell 10 1.00 1.28540 0.00000 0.00000
20 2009.02.06 19:00 close 10 1.00 1.28998 0.00000 0.00000 -463.60 12116.60
21 2009.02.06 19:00 buy 11 1.00 1.28998 0.00000 0.00000
22 2009.02.10 06:00 close 11 1.00 1.28210 0.00000 0.00000 -786.40 11330.20
23 2009.02.10 06:00 sell 12 1.00 1.28210 0.00000 0.00000
24 2009.02.10 13:22 close 12 1.00 1.29928 0.00000 0.00000 -1718.00 9612.20
25 2009.02.10 13:22 buy 13 1.00 1.29928 0.00000 0.00000
26 2009.02.10 22:00 close 13 1.00 1.28830 0.00000 0.00000 -1098.00 8514.20
27 2009.02.10 22:00 sell 14 1.00 1.28830 0.00000 0.00000
28 2009.02.11 13:00 close 14 1.00 1.29395 0.00000 0.00000 -566.40 7947.80
29 2009.02.11 13:00 buy 15 1.00 1.29395 0.00000 0.00000
30 2009.02.11 18:00 close 15 1.00 1.28920 0.00000 0.00000 -475.00 7472.80
31 2009.02.11 18:00 sell 16 1.00 1.28920 0.00000 0.00000
32 2009.02.13 07:00 close 16 1.00 1.29258 0.00000 0.00000 -343.60 7129.20
33 2009.02.13 07:00 buy 17 1.00 1.29258 0.00000 0.00000
34 2009.02.13 15:00 close 17 1.00 1.28370 0.00000 0.00000 -888.00 6241.20
35 2009.02.13 15:00 sell 18 1.00 1.28370 0.00000 0.00000
36 2009.02.13 19:01 close 18 1.00 1.28888 0.00000 0.00000 -518.00 5723.20
37 2009.02.13 19:01 buy 19 1.00 1.28888 0.00000 0.00000
38 2009.02.16 00:00 close 19 1.00 1.27900 0.00000 0.00000 -987.20 4736.00
39 2009.02.16 00:00 sell 20 1.00 1.27900 0.00000 0.00000
40 2009.02.19 12:00 close 20 1.00 1.26658 0.00000 0.00000 1235.00 5971.00
41 2009.02.19 12:00 buy 21 1.00 1.26658 0.00000 0.00000
42 2009.02.20 08:08 close 21 1.00 1.25700 0.00000 0.00000 -957.20 5013.80
43 2009.02.20 08:08 sell 22 1.00 1.25700 0.00000 0.00000
44 2009.02.20 18:00 close 22 1.00 1.26966 0.00000 0.00000 -1266.00 3747.80
45 2009.02.20 18:00 buy 23 1.00 1.26966 0.00000 0.00000
46 2009.02.24 01:00 close 23 1.00 1.26710 0.00000 0.00000 -254.40 3493.40
47 2009.02.24 01:00 sell 24 1.00 1.26710 0.00000 0.00000
48 2009.02.24 12:00 close 24 1.00 1.28208 0.00000 0.00000 -1498.00 1995.40
49 2009.02.24 12:00 buy 25 1.00 1.28208 0.00000 0.00000
50 2009.02.24 16:01 close 25 1.00 1.27130 0.00000 0.00000 -1078.00 917.40
51 2009.02.24 16:01 sell 26 1.00 1.27130 0.00000 0.00000
52 2009.02.24 18:52 close at stop 26 1.00 1.27998 0.00000 0.00000 -868.00 49.40

Use the following links to view the source code for the "robot" that produced another set of "astonishing" results:

In the MetaTrader MQL4 language

In the MetaTrader MQL5 language, using a traditional procedural style

In the MetaTrader MQL5 language, using an object oriented style

Leave a Comment

Fields marked by an asterisk (*) are required.