Trust in Backtests but Control with Fowardtests

Trust in Backtests but Control with Fowardtests

28 January 2024, 16:46
Eugen Funk
0
447

Optimizing trading strategies is the best feature of MT5. It provides an intelligent parameter tuning algorithm, which - when applied the smart way - can find nice trading strategies.

However, the core problem of optimized strategies is that they seem to perform awesome during the backtest and fail right after they are deployed.

Did you know that many optimized strategies fail after they are deployed?

This blog article deals with the steps on how we can avoid this issue.  The article initially introduces the optimized parameters and shows then how we can find a strategy which performs well in history and on future data.

Step 1: Designing an entry pattern

Everyone who starts with trading, looks at entry patterns first. How to recognize a situation on the market, when we can open a position which will be a win?

There are so many ideas on the market and from my experience 10x more effort is going into a recoverying strategy for cases, when the entry pattern leads us to a loosing situation. Anyway, for this article I would like to take a Price Action pattern with two consecutive higher-highs and two consecutive higher lows as an entry criterion.

As shown in the setup above, the bot is waiting for two highs where the second is higher than the first (higher-high). Same for the lows: we wait for two higher lows. As soon as the price breaks the level of the higher-high, we open a position. That's it. No woodoo. 


Next let's see what to do with this trade now.

Step 2: Trade Management and First Optimization

The question where to exit this trade is not so easy to be answered. 10 Million Gurus will tell something like "bro, just use the previous lower-low as the Stop-Loss. And some previous fractal for take-Profit". This is a good idea. So let' define the following:

Take-Profit is: entry price + dTP where dTP is the amount of points between the entry price and the envisioned Take profit level (e.g. previous Higher high).

Stop-Loss is: entry price - dSL, where dSL is the amount of points between the entry price and the envisioned Stop Loss level (e.g. last Lower-Low).

Next we tell the optimizer to try different values for dTP and dSL. Maybe 0.5*dTP is the best TP price or maybe it's 5*dTP?

In my strategies I usually not just scale the dTP and dSL ranges but do also set restrictions on the minimal and maximum required distances between two Highs or two Lows. The optimizer can handle all of these parameters. 


Step3: Fighting Overfitting

I guess you too have met an overfitted trading strategy, which was performing 100% per month in the backtest but blew up all accounts after 4 weeks of trading. We want to avoid that!


People who start learning machine learning, run in their first days into the so called overfitting problem. This means, that the learner finds a solution which works only for the training dataset.

Imagine following analogue situation:

Everytime you drive with your car past a traffic light, you note the time and if the light is green or red. Now you collect 20 records and 2 green records are on a Monday. Now, if you let some fancy AI learn to predict the traffic light from the time, it will tell you

that if you drive on Monday, the traffic light is green. Always!


In trading, beginners usualy think: Great! Let's drive with max speed. In AI we trust! 🌅


How to avoid it? We optimize the EA parameters on data before 2023 and test the performance on data after Jan 2023. The test after 2023 is called the forward-test.


So we expect the performance of the optimized EA to be similar in the backtest and in the forward-test. But is it always the case?

Let's see!


Data Mining as Solution 🏆️

MetaTrader runs usually more than 10,000 tests with different parameters. As mentioned above we optimize the parameters on data before 2023 and test them on data after Jan 2023. This means we make 20,000 runs in total. Keeping focused with that amount of data is not easy. For this reason I developed this charting technique for my bot creation process so you won't find it explained anywhere but here. It is worth to take a few seconds to take a loot at it. Is it important? YES! Because it shows that the success of a trading bot on future data is not a lucky draw!

The vertical axis is the CAGR (e.g. 1.5 means 50% per year profits per year on average) of the backtest and the horizontal axis is CAGR of the forward test. Each purple point represents a strategy with its unique parameters.

The red dashed line shows where the CARG of the backtest was similar to CAGR of the forward test. This means, that

all strategies along the red dashed line have same performance on backtest and on new validation data!

This gives us a quantitative reason to expect same profits in the future! 🔥

Note, that most of the super successful backtest strategies were failing in the forward test. Which one do we select for trading? Of course the one, which is on the red dashed line and has the highest CAGR.

How to create such a Chart?

First, I exported the Backtest and Forward test results to an Excel xml file like this:

After that I copied some columns from the backtest and some columns from the forward test xml file into the third Excel file, which computers the CAGR and plots the plot chart.

The third Excel file, which plots the mean yearly gain for backtest and forward test exports.

Next, I would summarize the outcome of the charting hustle with a few result examples.

Results Before:

When we simply select the best backtest, we end up with this:

Results After:

when we select the best combination backtest-to-forward test as described in this article, we end up with a strategy which dlivers very similar performance for backtest data and the future forward test data. This is exactly what we want.

Conclusion

I recommend to consider such evaluation in every EA-development process. In think it is so critical, that actually everyone who is building trading bots should apply it. Why doesn't anyone publish such EA-analysison MQL5 market when offering a trading bot there?


PS: After several months of digging into EA development, I will publish soon more articles and another great update for AI for Gold. The time has come to strive for the 400%++ profits again.



Share it with friends: