This post will illustrate the use of MT4 to:
Download free historical data
Verify your strategy’s logic
Backtest your strategy
The complete trading strategy can be downloaded here.
Before diving into strategy development, it pays to verify your algorithm’s logic. Unfortunately, with the numerous trading situations that may play out in real-time, debugging your algorithm can be challenging. Fortunately, Metatrader 4 (MT4) comes with a visual backtester that shows how your algorithm interacts with historical prices.
This is Part 2 of a 3-part guide on MT4 strategy development. Here we will illustrate the use of MT4’s Strategy Tester to verify the logic of our moving average crossover algorithm developed in Part 1 for the M30 USDJPY. Once verified, we will evaluate the strategy’s historical performance.
In Part 3, we will see whether optimizing some key parameters can improve performance.
To kick-start the process, let’s first import the algorithm into MT4. You can download the complete algorithm (in .mq4 format) here.
Importing Your Algorithm Into MT4
Trading algorithms are known as expert advisors (EAs) in the Metatrader world. If you programmed your EA using an external software, you will first need to import the .mq4 file to MT4’s data folder. By default, this can be accessed through File > Open Data Folder.
Navigate to MQL4 > Experts and paste the .mq4 file there. If you are using a portable version of MT4, the MQL4 folder can be accessed directly through your installation directory. With a portable MT4, all program files (including EAs, indicators etc.) are conveniently packed into a single folder. This makes it convenient when transferring your MT4 installation across computers, for example when you’re setting up a virtual private server. If you wish to make your MT4 portable, check out this guide.
For AlgoWizard users, you will also need to important its custom indicators to MT4. The steps are similar to those above, except that you place them in MQL4 > Indicators. Detailed instructions can be found here.
Compiling Your Expert Advisor
The .mq4 file you just imported contains code in MT4’s proprietary language, MetaQuotes Language 4. Before you can execute the code, it needs to be compiled into an .ex4 file. Skip this step if your strategy is already in the .ex4 format.
Head over to the Navigator window > Expert Advisors, right click and select Refresh. If your Navigator window is not visible, toggle it using Ctrl + N.
After refreshing, your EA should appear in the list. Your data folder will also contain an additional .ex4 file.
If you previously imported custom indicators, check that they now appear in the Indicators folder.
If your EA/indicator does not appear, there may be compilation errors. Press F4 to open the MetaEditor and load your .mq4 file. After clicking Compile, check for any errors at the bottom of the screen.
The last step before running backtests is to feed the EA with quality historical data.
Downloading Free Historical Data
You can download historical data from your broker through MT4’s History Center. I am using Alpari as my broker here; they provide quality 1-minute data up till 1999 for the forex majors. Before downloading the data, head over to Tools > Options > Charts and increase the Max bars in history & Max bars in chart to a large number. This makes sure whatever history you download will show up in your charts.
After that, press F2 to open the History Center shown below.
- Double click on the pair of your choice (USDJPY for our case), and once more on the 1 Minute timeframe.
- The most recent 2048 records will be shown. Click Download to get the full history.
You only need to download data for the 1-minute timeframe. MT4 will automatically compute the data for the higher timeframes using this 1-minute data. After the download is complete, clicking on the M30 timeframe should show data all the way till 1999.
Verifying Your Expert Advisor’s Logic
Setting Up The Strategy Tester
Open the Strategy Tester using Ctrl + R.
- Select the moving average crossover EA created earlier. The Expert properties button lets you configure the EA parameters. We will configure these later when optimizing the EA in Part 3. For now, the default parameters entered when building the EA will suffice.
- Select the Period and Symbol, M30 USDJPY in this case.
- Input a realistic Spread. The value here is in points, so 20 means 2 pips.
- For preliminary tests, the Open prices only test model should suffice. For a detailed explanation of each test model, check out the official MetaQuotes article.
- Input your backtest Dates. Here we will test the EA over the most recent 5 years.
- Toggle the Visual mode since we wish to verify the EA logic. The slider lets you set the speed at which prices move across the screen. 31 works best for me.
After clicking Start, you will see your EA interacting with incoming prices. Use your keyboard’s Pause key to start/stop the movement. The default MT4 chart template is not very useful. Let’s customize our chart’s appearance to suit the EA.
- If you’re like me, you find candlesticks far easier to read than bars, so let’s select that.
- Click on the Indicators list button and add a Moving Average. Add the fast moving average as per the settings below. Repeat for the slow moving average. Also add the Average True Range since the EA uses it to compute stop losses & profit targets.
If you wish, you can customize the chart appearance. Open the chart properties using F8.
I recommend saving your template for future use. Right-click anywhere on the chart to do so. Templates can be accessed via your data folder.
Lastly, I recommend toggling the Data window using Ctrl + D. This displays the OHLC and any indicator values for that candle.
Logic Verification
Try to verify your EA’s logic for every possible trading scenario. It pays (literally) to trade with simple algorithms. They are easier to debug are less likely to be curve fit.
When programming our moving average crossover algorithm in Part 1, we had the following logic:
- If the fast moving average crosses over the slow moving average, place a stop entry order at the high of the previous candle. Order is valid for 3 bars.
- Stop loss is 3*ATR from the entry price.
- Profit target is 5*ATR from the entry price.
- Regardless of trade performance, close the trade after 150 bars have elapsed.
Here we will attempt to verify the following scenario:
- A buy stop is placed after the fast moving average crosses above the slow moving average
- Stop price is the high of the most recently completed candle
- Order is triggered within 3 bars
- Trade is closed when the 3*ATR stop loss is hit
Let the prices run until the above scenario occurs. One instance is shown below:
- At the close of the 2016.01.05, 03:00 candle, the fast moving average has crossed over the slow moving average. A buy stop order is placed at the high of this candle, which is triggered during the following candle.
- The stop loss is triggered and the long trade is exited.
To verify that the price levels are correct, open the Journal tab at the bottom of the Strategy Tester.
Using the OHLC and ATR values in the Data window, we can verify that the entry price and stop loss/take profit levels are indeed correct.
Backtesting Your Expert Advisor
Now that we know our EA executes trades correctly, let’s see how profitable it is. We will simply uncheck the Visual mode in the Strategy Tester and rerun the backtest.
The Graph tab will show the equity curve, while the Report tab shows your backtest statistics.
The equity curve and 1.15 profit factor are decent, although there is a long stagnation midway through the backtest.
Backtest Accuracy
Can we get a more accurate backtest using a different test model? The Open prices only (OPO) model used here only executes trading actions at the opening of each bar, using OHLC information from the closed bars. There are two main sources of inaccuracies:
- Since we are entering on a stop order, the actual trade entry will likely occur midway through a bar. The OPO model thus cannot accurately identify the entry price.
- A bar’s range may be large enough to encompass more than one key price level (e.g. both the entry price and stop loss price). Since the ‘Open prices only’ model does not capture intrabar price action, the backtest engine will not know which order got triggered first. If in reality, your trade was entered and stopped out on the same bar, the OPO model would not have detected this losing trade.
Nonetheless, I believe the price discrepancy in point 1 is minor, while point 2 rarely occurs. If you want maximum precision, a tick backtest is what you need. For our purposes here, considering that an OPO backtest is much faster than a tick backtest, I think the slight loss in accuracy is justified.
For more information on tick backtests, such as the types of strategies requiring tick backtests, and sources of quality tick data, refer to my article on tick backtesting.
Develop or Discard?
So, returning to the original question in Part 1 of this 3-part series – does this strategy have any merit?
This will depend on your expectations and how this strategy stacks up against your existing portfolio. Personally, considering how basic the strategy currently is, I believe it is worthy of further development, perhaps in the form of parameter optimizations and the addition of trading filters. In fact, that is exactly what we will do in the third and final part of this article.
Wrapping Up
You should now be able to use MT4’s Strategy Tester to verify your strategy’s logic and determine its historical performance. It is not easy to find a trading edge these days; don’t be discouraged if it takes numerous attempts to find a viable solution. Sometimes the best ideas come when you least expect them, like when you’re mowing the lawn or taking a shower.
Our simple moving average crossover strategy deserves further development. Let’s try to optimize and improve it in Part 3 of this article.
0 Comments