Debugging & Backtesting Using MT4

Dec 22, 2020

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.

  1. Double click on the pair of your choice (USDJPY for our case), and once more on the 1 Minute timeframe.
  2. 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.

  1. 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.
  2. Select the Period and Symbol, M30 USDJPY in this case.
  3. Input a realistic Spread. The value here is in points, so 20 means 2 pips.
  4. 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.
  5. Input your backtest Dates. Here we will test the EA over the most recent 5 years.
  6. 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.

  1. If you’re like me, you find candlesticks far easier to read than bars, so let’s select that.
  2. 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:

  1. 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.
  2. Stop loss is 3*ATR from the entry price.
  3. Profit target is 5*ATR from the entry price.
  4. 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:

  1. 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.
  2. 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:

  1. 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.
  2. 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.

Powered By

Development Platform

Forex VPS

FXVM Forex VPS

Popular Posts

No Results Found

The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.

Make your money work for you!

Get promotions, trading ideas and strategy development tips delivered to your inbox!

Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Trading Strategies

Ichimoku Trend Following Strategy

Ichimoku Trend Following Strategy

Like candlesticks, the Ichimoku indicator is a fine Japanese creation. Here I’ll explain how the Ichimoku is plotted, and use it to build a trend following strategy for the USDJPY.

Forex Trading With Volume – Does It Work?

Forex Trading With Volume – Does It Work?

Volume is seldom discussed in forex trading. But as a stock trader would tell you, volume provides insights into the underlying market sentiment. Here I’ll demonstrate the use of two simple volume filters to improve entry reliability.

What Is the Kaufman Adaptive Moving Average?

What Is the Kaufman Adaptive Moving Average?

The Kaufman Adaptive Moving Average is a unique indicator that automatically adapts to the market’s noise. Here I explain its inner workings and show you how to build a trend following strategy around it.

Awesome Oscillator Forex Trend Strategy

Awesome Oscillator Forex Trend Strategy

The Awesome Oscillator is one of Bill Williams’ fine creations. It is a simple yet effective momentum indicator that can be a valuable addition to your strategy. Here we’ll use it together with a moving average to fine-tune our trend following entries.

Dual CCI Indicator Forex Trend Strategy

Dual CCI Indicator Forex Trend Strategy

The Commodity Channel Index (CCI) is an underrated momentum indicator that is often overshadowed by the RSI and MACD. I’ll show you how to build a simple trend following strategy using a pair of CCI indicators!

What is Fixed Ratio Money Management?

What is Fixed Ratio Money Management?

Have you heard of fixed ratio money management? How does it compare to the popular fixed fractional approach? Here I’ll explain how fixed ratio works, and see how it stacks up against fixed fractional money management.

What Is the Kaufman Efficiency Ratio?

What Is the Kaufman Efficiency Ratio?

Avoiding false breakouts is a common goal among trend traders. The Kaufman Efficiency Ratio provides a simple method of quantifying a market’s noise, helping traders focus on the smoothest trends. Let’s add the efficiency ratio to a simple trend following strategy and see whether its performance improves.

What Is the QQE Indicator?

What Is the QQE Indicator?

The QQE is a mysterious indicator that sometimes pops up in trading forums. Does it deserve a place alongside the more traditional momentum indicators like the RSI and CCI? Let’s add it to a trend following strategy to find out!

Do Bollinger Bands + Candlestick Patterns Work?

Do Bollinger Bands + Candlestick Patterns Work?

Bollinger Bands are great at detecting overbought and oversold conditions. Let’s use them to develop a countertrend strategy, and then refine our entries using limit entries and candlestick patterns.

How Good Are The Bollinger Bands’ Trailing Stops?

How Good Are The Bollinger Bands’ Trailing Stops?

Trailing stop losses are a popular feature in many trend following systems. Bollinger Bands, the ever-popular technical indicator among retail traders, actually contain two inbuilt trailing stops. Are these any good? Let’s find out!

Bollinger Bands vs. Keltner Channels

Bollinger Bands vs. Keltner Channels

Bollinger Bands are ubiquitous in the trading world, but there’s less discussion about their lesser-known cousin, the Keltner Channels. Do these perform as well as the Bollinger Bands? Let’s compare them over a broad range of input parameters.

Strategy Development

No Results Found

The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.

Live Trading

No Results Found

The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.

Make your money work for you!

Make your money work for you!

 

Get trading ideas and strategy development tips delivered to your inbox!

Thanks for subscribing!

Pin It on Pinterest

Share This