Robot Example 1 – Random Entries
This is the first example of a MetaTrader expert advisor from our course on building your own forex trading system.
It is designed to demonstrate as simply as possible the basic structure of a forex "robot". The system stays in the market all the time. It enters long on an uptick, and short on a downtick, using a fixed position size. It uses a day trading style exit strategy, with a fixed profit target and a fixed stop-loss. These numbers can be changed to whatever value you want when you start the expert advisor – they are "input parameters" or "input properties" in the jargon. Feel free to experiment by backtesting the "robot" using the MetaTrader strategy tester, and trying lots of different combinations for the ProfitTarget and StopLoss settings, with a variety of different start and end dates. Notice in particular that not all combinations are as profitable as the default values!
Here are the results from the MetaTrader 4 strategy tester:
Symbol | EURUSD (Euro vs US Dollar) | ||||
Period | 1 Minute (M1) 2009.01.05 00:01 – 2009.04.10 22:59 (2009.01.05 – 2009.04.11) | ||||
Model | Every tick (the most precise method based on all available least timeframes) | ||||
Parameters | Magic=12345; Slippage=30; ProfitTarget=1000; StopLoss=10000; Lots=0.01; | ||||
Bars in test | 97958 | Ticks modelled | 2494305 | Modelling quality | 25.00% |
Mismatched charts errors | 0 | ||||
Initial deposit | 100.00 | ||||
Total net profit | 112.24 | Gross profit | 150.75 | Gross loss | -38.51 |
Profit factor | 3.91 | Expected payoff | 7.01 | ||
Absolute drawdown | 8.18 | Maximal drawdown | 98.24 (49.57%) | Relative drawdown | 49.57% (98.24) |
Total trades | 16 | Short positions (won %) | 9 (100.00%) | Long positions (won %) | 7 (85.71%) |
Profit trades (% of total) | 15 (93.75%) | Loss trades (% of total) | 1 (6.25%) | ||
Largest | profit trade | 10.72 | loss trade | -38.51 | |
Average | profit trade | 10.05 | loss trade | -38.51 | |
Maximum | consecutive wins (profit in money) | 15 (150.75) | consecutive losses (loss in money) | 1 (-38.51) | |
Maximal | consecutive profit (count of wins) | 150.75 (15) | consecutive loss (count of losses) | -38.51 (1) | |
Average | consecutive wins | 15 | consecutive losses | 1 |
# | Time | Type | Order | Size | Price | S / L | T / P | Profit | Balance |
1 | 2009.01.05 00:01 | sell | 1 | 0.01 | 1.38820 | 1.48838 | 1.37820 | ||
2 | 2009.01.05 09:22 | t/p | 1 | 0.01 | 1.37820 | 1.48838 | 1.37820 | 10.00 | 110.00 |
3 | 2009.01.05 09:22 | sell | 2 | 0.01 | 1.37725 | 1.47743 | 1.36725 | ||
4 | 2009.01.05 09:39 | t/p | 2 | 0.01 | 1.36725 | 1.47743 | 1.36725 | 10.00 | 120.00 |
5 | 2009.01.05 09:39 | sell | 3 | 0.01 | 1.36689 | 1.46707 | 1.35689 | ||
6 | 2009.01.05 13:50 | t/p | 3 | 0.01 | 1.35689 | 1.46707 | 1.35689 | 10.00 | 130.00 |
7 | 2009.01.05 13:50 | buy | 4 | 0.01 | 1.35683 | 1.25665 | 1.36683 | ||
8 | 2009.01.07 14:28 | t/p | 4 | 0.01 | 1.36683 | 1.25665 | 1.36683 | 10.02 | 140.02 |
9 | 2009.01.07 14:28 | buy | 5 | 0.01 | 1.36733 | 1.26715 | 1.37733 | ||
10 | 2009.01.08 15:07 | t/p | 5 | 0.01 | 1.37733 | 1.26715 | 1.37733 | 10.03 | 150.05 |
11 | 2009.01.08 15:07 | sell | 6 | 0.01 | 1.37739 | 1.47757 | 1.36739 | ||
12 | 2009.01.08 19:43 | t/p | 6 | 0.01 | 1.36739 | 1.47757 | 1.36739 | 10.00 | 160.05 |
13 | 2009.01.08 19:43 | sell | 7 | 0.01 | 1.36709 | 1.46727 | 1.35709 | ||
14 | 2009.01.09 15:02 | t/p | 7 | 0.01 | 1.35709 | 1.46727 | 1.35709 | 9.99 | 170.04 |
15 | 2009.01.09 15:02 | sell | 8 | 0.01 | 1.35670 | 1.45688 | 1.34670 | ||
16 | 2009.01.09 18:29 | t/p | 8 | 0.01 | 1.34670 | 1.45688 | 1.34670 | 10.00 | 180.04 |
17 | 2009.01.09 18:29 | sell | 9 | 0.01 | 1.34630 | 1.44648 | 1.33630 | ||
18 | 2009.01.12 09:09 | t/p | 9 | 0.01 | 1.33630 | 1.44648 | 1.33630 | 9.99 | 190.03 |
19 | 2009.01.12 09:09 | buy | 10 | 0.01 | 1.33623 | 1.23605 | 1.34623 | ||
20 | 2009.03.18 20:07 | t/p | 10 | 0.01 | 1.34623 | 1.23605 | 1.34623 | 10.72 | 200.74 |
21 | 2009.03.18 20:07 | buy | 11 | 0.01 | 1.34655 | 1.24637 | 1.35655 | ||
22 | 2009.03.19 12:44 | t/p | 11 | 0.01 | 1.35655 | 1.24637 | 1.35655 | 10.03 | 210.77 |
23 | 2009.03.19 12:44 | sell | 12 | 0.01 | 1.35673 | 1.45691 | 1.34673 | ||
24 | 2009.03.24 20:32 | t/p | 12 | 0.01 | 1.34673 | 1.45691 | 1.34673 | 9.96 | 220.73 |
25 | 2009.03.24 20:32 | sell | 13 | 0.01 | 1.34638 | 1.44656 | 1.33638 | ||
26 | 2009.03.27 12:41 | t/p | 13 | 0.01 | 1.33638 | 1.44656 | 1.33638 | 9.93 | 230.66 |
27 | 2009.03.27 12:41 | buy | 14 | 0.01 | 1.33637 | 1.23619 | 1.34637 | ||
28 | 2009.04.02 14:44 | t/p | 14 | 0.01 | 1.34637 | 1.23619 | 1.34637 | 10.07 | 240.73 |
29 | 2009.04.02 14:44 | buy | 15 | 0.01 | 1.34703 | 1.24685 | 1.35703 | ||
30 | 2009.04.06 03:24 | t/p | 15 | 0.01 | 1.35703 | 1.24685 | 1.35703 | 10.02 | 250.75 |
31 | 2009.04.06 03:24 | buy | 16 | 0.01 | 1.35724 | 1.25706 | 1.36724 | ||
32 | 2009.04.10 22:59 | close at stop | 16 | 0.01 | 1.31866 | 1.25706 | 1.36724 | -38.51 | 212.24 |
As you can see every trade was a winner, apart from the last one, which hadn't hit either the take profit or stop loss levels when the backtest finished. This trade was running an open loss at the time, hence the sad looking droop at the end of the equity curve. Time will tell whether this trade becomes yet another winner.
Use the following links to view the source code for the "robot" that produced those "astonishing" results:
In the MetaTrader MQL4 language
In the MetaTrader MQL5 language, using a traditional procedural style
In GFT's CTL language
In Java for Dukascopy's JForex platform
Leave a Comment