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.

Want to develop a portfolio of automated trading strategies?

Supercharge your strategy development with StrategyQuant

Access 14-day FREE trial here!

Get up to USD 300 discount!

Strategies need improvement?

Use QuantAnalyzer’s powerful analysis tools

Try the FREE version here!

Get 20% Discount here!

FXVM Forex VPS
Follow Us

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.

Hey there, Wayne here! I’m on a mission to develop robust algorithmic trading strategies for the forex markets. Trading Tact is where I share my trading methods and insights.

Follow Us
Have a Question?

Comments

0 Comments

Submit a Comment

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

Want to develop a portfolio of automated trading strategies?

Supercharge your strategy development with StrategyQuant

Access 14-day FREE trial here!

Get up to USD 300 discount!

Strategies need improvement?

Use QuantAnalyzer’s powerful analysis tools

Try the FREE version here!

Get 20% Discount here!

FXVM Forex VPS

Recent Posts

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.

read more
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.

read more
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.

read more
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!

read more
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.

read more
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.

read more
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.

read more
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.

read more

Hey there, Wayne here! I’m on a mission to develop robust algorithmic trading strategies for the forex markets. Trading Tact is where I share my trading methods and insights.

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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