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

Laguerre RSI Trend Following Strategy

The Laguerre RSI attempts to improve the responsiveness of the regular RSI, whilst keeping whipsaw trades to a minimum. Let’s see how well it detects short-term pullbacks for a trend following strategy!

read more

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.

read more

Build a Diversified Portfolio With QuantAnalyzer

The ability to efficiently trade a diversified portfolio of strategies is one of the biggest advantages of algorithmic trading. Here we will use QuantAnalyzer’s Portfolio Master to build a portfolio consisting of high performing, uncorrelated strategies.

read more

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!

read more

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

What’s the Best Time to Trade Forex?

What’s the Best Time to Trade Forex?

The forex markets are open 24/5, but not all hours are created equal. Here I dissect my broker data to determine the best time to trade forex.

Forex Weekend Gaps: Can You Exploit Them?

Forex Weekend Gaps: Can You Exploit Them?

Have you noticed that forex weekend gaps usually reverse within 3 days? Here I’ll program a mean reversion strategy to exploit gaps over the last 18 years!

Money Flow Index: An Improved RSI?

Money Flow Index: An Improved RSI?

The Money Flow Index is sometimes called the volume-weighted RSI. Can it outperform the RSI in this trend following strategy?

Automated Bollinger Bands Squeeze Forex Strategy

Automated Bollinger Bands Squeeze Forex Strategy

StrategyQuant’s BBWR indicator is the perfect tool to detect a Bollinger Bands squeeze. Here I explain how it’s calculated, and use it to program a breakout strategy for the AUDJPY!

Should You Use the Kelly Criterion for Forex Trading?

Should You Use the Kelly Criterion for Forex Trading?

The Kelly criterion is a famous mathematical formula that attempts to maximize your long-term capital growth. In this post, I’ll apply it to a EURUSD breakout strategy and explain some of its potential shortcomings when applied to forex trading.

Can a Trading Pause Improve Your Trend Following Results?

Can a Trading Pause Improve Your Trend Following Results?

A temporary trading pause can improve your win rate if you’re trend following a volatile market. Here I’ll program a trading pause into a simple breakout strategy, and test its effectiveness on the Widow Maker – the GBPJPY.

Laguerre RSI Trend Following Strategy

Laguerre RSI Trend Following Strategy

The Laguerre RSI attempts to improve the responsiveness of the regular RSI, whilst keeping whipsaw trades to a minimum. Let’s see how well it detects short-term pullbacks for a trend following strategy!

How to Use the Supertrend Indicator

How to Use the Supertrend Indicator

Despite its cool name, the Supertrend indicator often seems to slip under the radar. Here I explain how it’s calculated, and combine it with moving averages to produce a simple trend following strategy.

Strategy Development

Do You Know Your System Quality Number?

Do You Know Your System Quality Number?

The System Quality Number measures the profitability & consistency of your trading system. Here’s how to calculate your SQN and use it to improve your trading!

How to Get a Realistic Backtest Spread

How to Get a Realistic Backtest Spread

Your choice of backtest spread can certainly make or break a strategy. This post will show you how to study the intraday spread variations of your market, and suggest several ways to avoid paying ridiculous spreads.

Do You Know Your Strategy’s Optimization Profile?

Do You Know Your Strategy’s Optimization Profile?

Your strategy’s optimization profile often reveals its robustness, helping you select strategies that will remain profitable in live trading. Here I explain why an optimization profile is important, and how you can easily obtain one using StrategyQuant’s optimizer.

Which MT4 Backtest Report Metrics Should You Use?

Which MT4 Backtest Report Metrics Should You Use?

Understanding your backtest report is an essential part of being a successful strategy developer. Here I explain what the numbers mean, and how you can make use of each metric during strategy development.

Out-of-sample Testing Using Monte Carlo Simulations

Out-of-sample Testing Using Monte Carlo Simulations

Traders often use Monte Carlo simulations to estimate worst-case drawdowns, but did you know they can be used for out-of-sample testing too? This post demonstrates the use of StrategyQuant’s Monte Carlo simulator to randomize historical prices and strategy parameters, helping you select robust strategies for live trading.

How Many Trades Should Your Backtest Have?

How Many Trades Should Your Backtest Have?

We all want a large sample of trades in our backtests, but practical limitations such as data availability often get in the way. Here I’ll explain why 30 trades is insufficient, and how you can use standard error to quantify the uncertainty arising from a small sample size.

Build a Diversified Portfolio With QuantAnalyzer

Build a Diversified Portfolio With QuantAnalyzer

The ability to efficiently trade a diversified portfolio of strategies is one of the biggest advantages of algorithmic trading. Here we will use QuantAnalyzer’s Portfolio Master to build a portfolio consisting of high performing, uncorrelated strategies.

Strategy Optimization Using MT4

Strategy Optimization Using MT4

How do you improve your trading strategy in MT4? This post will show you how to optimize the entry and exit parameters for a moving average crossover strategy. Finally, an intraday time filter will be added to help avoid false breakouts.

Create Your Trading Algorithm in 15 Minutes (FREE)

Create Your Trading Algorithm in 15 Minutes (FREE)

Converting your trading idea into an algorithm is the first step towards reaping the benefits of automated trading. This guide will cover the creation of a simple moving average crossover algorithm, without any actual programming.

What Is Drawdown in Trading?

What Is Drawdown in Trading?

Are you getting a comprehensive assessment of your strategy’s downside? This post will discuss several methods to measure drawdowns, helping you build and select strategies that better suit your risk appetite.

How to Select the Best Trading Strategy Entry

How to Select the Best Trading Strategy Entry

With an abundance of technical indicators available, selecting your strategy’s entry conditions can be overwhelming. This post will illustrate a method to graphically compare the profitability of different entries.

Live Trading

What’s the Best Time to Trade Forex?

What’s the Best Time to Trade Forex?

The forex markets are open 24/5, but not all hours are created equal. Here I dissect my broker data to determine the best time to trade forex.

How to Find a Real Trading Guru

How to Find a Real Trading Guru

Every day I come across a trading guru offering educational content on the internet. Many of them speak of huge returns with minimal effort. Should these be trusted? Here’s some tips on how to separate the wheat from the chaff.

How to Enjoy Stress-Free Trading

How to Enjoy Stress-Free Trading

Trading is a great way to make some additional income, but not if you’re constantly pulling your hair out. Here I offer 7 tips to help make your trading profitable and stress-free.

How to Select the Best Forex VPS

How to Select the Best Forex VPS

A virtual private server (VPS) is a virtual computer that you can rent and access remotely. It provides a reliable platform on which to execute your forex strategies. This post will help you decide whether you need a VPS, and show you how to select an optimal VPS.

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