How to Backtest Trading Strategy

How to Backtest Trading Strategy

Dear traders, in this lecture, I will show you how to backtest a trading strategy on MetaTrader. Moreover, I will compare some results that we have from EA Studio with the trading strategy backtest in MetaTrader. You will see that it’s the same thing.

Now, I have run here the Reactor for EURUSD on the H1 chart. Already, I have 23 strategies in the collection. And let’s have a look at the first one where I have a very simple trading strategy for backtesting – entry rule, Stochastic signal, exit rule, Envelopes. We have a Stop Loss of 95 pips and a Take Profit of 70 pips. A very simple strategy with 1 entry and 1 exit condition.

The strategy

But you can see a great balance chart, and this strategy did 542 trades for the last 5 years and a net profit of $4,114 which is for 0.1 lot. If I increase the lot to 1 and I click on Accept, it will be 41,143. It depends on the lots that you are using, but I will go back to 0.1.

What Strategy properties we use when backtest trading strategy
Strategy properties

And especially if you are trading with a smaller account, you need to use a smaller lot.

Now, for the strategy, we can see a lot from the report.

The statistics info

This is where we have a lot of statistics in EA Studio, and we have as well the report in MetaTrader. In addition, we have the journal that we can compare with the trading strategy backtest in MetaTrader.

The journal

Every Trading Strategy Backtest Has Losing Trades at Some Point

As well, we have the balance chart,

backtest trading strategy with the balance chart
The balance chart

and we have the indicator chart, which is really great.

The indicator chart in EA Studio
The indicator chart

In addition, you can see exactly where the trades were opened and closed. Let’s see one more time, what the entry rule was. The stochastic line crosses the signal line upwards. Going to the report, indicator chart, and here is the cross.

The cross
The cross

The Take Profit was hit. The green line shows where the Take Profit is. And if I look previously, here is a short trade, here we have the cross, Take Profit is hit.

Every strategy has negative trades, keep that in mind.

Profit factor

Of course, we are looking for strategies that have more profits than losses or what I said, the Profit factor – the net profits divided by the net losses. So when I set the Acceptance criteria in the reactor with a Profit factor above 1.2, I have really great strategies and you can see that I have more profits than losses.

After that, what I want to show you in this lecture is how to backtest a trading strategy using the Expert Advisor. I will go back to the editor and I will export this strategy for Meta Trader 4, you can export it as well for Meta Trader 5, depends which platform you use.

Exporting the strategy

Now, I will open the MetaTrader and I will paste the Expert Advisor here. I will go to File, Open Data Folder,

Go to File, then Open Data Folder

and then I click on MQL4,

I click on MQL4

and I click on Experts.

I click on Experts

Then, I will paste the Expert Advisor from my Downloads folder, and here it is.

I paste the Expert Advisor

One more time, for MetaTrader 5, you need to open the Expert Advisor with the MetaEditor and you need to compile it.

MetaEditor
The MetaEditor

For MetaTrader 4, you can go to Expert Advisors, right-click and hit on Refresh.

MetaTrader 4 backtest trading strategy
Right-click on Expert Advisors and refresh for MT4

This will compile the Expert Advisor.

The Magic number

And if you want to trade with this Expert Advisor, make sure you enable the AutoTrading, it must be green. However, if you want to just backtest a trading strategy, you don’t need to enable AutoTrading. So you will avoid opening trades. And in order to put it over the chart, you either drag it or double-click on it.

In addition, from Inputs, you can see the Stop Loss and Take Profit. The entry lot, you can change it from here if you want to increase it or decrease it. And we have the parameters for all the indicators. We also have the Magic number which is very important, especially when we trade with many Expert Advisors, which I will show in this course.

The Magic number allows us to follow the performance of each strategy and it’s very useful when we are doing portfolio trading with Expert Advisors.

The input menu in MetaTrader
The input menu

I click on OK, and the Expert Advisor is attached to the chart. There is a sad face because I disabled the AutoTrading.

I don’t want it to open trades with this strategy right now. I just want to show you how to backtest a trading strategy. So, I right-click and I go to Expert Advisors and I click on Strategy Tester.

MetaTrader Strategy Tester
I go to Expert Advisors, then Strategy Tester

A new window will open below and we can set the Expert Advisor. Usually, it will be the one that we have on the chart, the symbol.

The graph display is the same on both Meta Trader and EA Studio

As a model, you need to use Open prices only.

Use Open prices only as Model

This is because the Expert Advisors from EA Studio open trades on the opening of the bar. When there is a new bar coming, it checks for the entry rule. If it’s true, it will open the trade. So I click on Open prices only, and as Period, I have H1, and I have a spread of 10, which is OK. It is rounded to the higher number.

As we said, this is better for the backtest. The current spread with EURUSD is 2-3 points, but I will backtest the trading strategy with 10, which is a more pessimistic backtest and more realistic. I don’t want to test it with 2-3 points and because we have a floating spread with most of the brokers, there will be times when the spread will be higher.

This is how we do it more pessimistically and it’s the better thing you can do. If I click on Start, the backtest is really quick. This is because the code is great, no mistakes, no errors. If I go to Journal, you will see there are no errors.

using the journal to backtest trading strategy
The journal

And if I go to the graph, what you will notice is the very same graph as what we have in EA Studio.

backtest trading strategy with the graph
The graph

I will show you in detail that it’s the same thing.

MetaTrader & EA Studio Have a Difference in Their Trading Strategy Backtests

Let’s see how much profit this trading strategy backtest made. In the end, we have about 13,529. On the other hand, what we have in EA Studio is 14,114. Where does the difference come from? From the historical data. Let’s go to the editor one more time.

What we had was data starting from the 15th of July, 2015. This is what I have set in the Data Horizon.

Use Data Horizon to backtest trading strategy
The Data Horizon

Let’s find out what data range we have in Meta Trader. How can I do that? Firstly, I will press the Home key on my keyboard and I will go to the beginning.

It is on the 11th of December, 2015. A couple of months are missing. I will need to make it the same as much as I can. So 10th of December, I will go back to the Data Horizon in EA Studio, and I will enter 10th of December, 2015.

And I go back to the strategy. So what we have, the 10th of December. And I have 13,546. Very close to what we have in Meta Trader. Now, there might be a very small difference because the MetaTrader trading strategy backtest is not as good as the EA Studio backtest is.

EA Studio Works on a More Pessimistic Trading Strategy Backtest Outcome

If we have a very long candle and we have a trade open, and then if the Stop Loss or the Take Profit is within this candle, the Meta Trader will take the positive outcome because it doesn’t know what happened inside the bar.

The price went up and down. It will take a positive outcome, which is sometimes misleading. And this could happen with some strategies quite many times in the backtest. And EA Studio takes the negative result.

So again, it works on a more pessimistic trading strategy backtest outcome. Let me compare some of the results at the end of the backtest just from the last couple of days in EA Studio. I will click on Report. And here are all the trades that happened. I will go to the last page and have a look at some of the trades that we have.

Actually, the data that we have from Dukascopy that we have downloaded from the Free Historical Data App is until midnight yesterday. So from there, we can have a very small difference in the final result with the backtest.

EA Studio and MetaTrader data comparison

Now, let me try to make them both visual. For example, what I have on the 13th of July, I have a sale, and let’s see the other platform, on the 13th of July, yes, open short. 

example of data comparison in backtest trading strategy
The data comparison

It is at 10:00 p.m, it is 10:00 p.m. on Meta Trader as well. And the price is 1.1352, 1.1352. Stop Loss, 1.1448, 1.1448. Take Profit 1.1283, 1.1283. I’m not looking at the last digit, which is the point, because it depends on the spread. It depends on whether it’s floating or not. And there might be a little bit of a difference with the final profit.

Let’s see one more trade. For example, the one that opened on the 10th of July. What is it? It’s a buy at 1.1270 and it is at 9:00 o’clock, 9:00 o’clock, the 10th of July, we have 1.1270 long trade. So for the very beginners, the backtest matches from EA Studio and Meta Trader.

This shows a very simple thing, the Expert Advisors and the strategies that we build with EA Studio are real, they have a real backtest. Not just in EA Studio where they were created, but as well on MetaTrader. And, one more time, the backtest trading strategy in EA Studio works much better, much faster than the backtest on MetaTrader.

What we will learn next

And we have many more statistics which are very useful to improve our trading strategies. I will leave now the reactor working, but before I show you the results, I will record one more video where I will show you the 4 different ways to test Expert Advisors and I will show you which is the method that I like the most because it’s the fastest, it’s the most reliable way, and it works really nice for me.

Thanks for reading. We’ll see you in the next lecture.

Shopping Cart