Optimizing a SuperTrend Trading Strategy Expert Advisor

Last Updated on November 28, 2022 by Mark Ursell

This article shows you how to optimize a MT4 Expert Advisor (EA) and turn a good trading strategy into a great one.

The article follows on directly from How to Create an Expert Advisor for a SuperTrend Trading Strategy. In the previous article, I showed how to build the EA using the MQL4 programming language. You can download a free copy of the EA by signing up to my mailing list via the Subscribe page.

Trading Strategy

The trading strategy that I am using in this article uses the SuperTrend technical indicator. The rules of the trading strategy are set out in the previous article about how to create the expert advisor.

Initial Results

I first backtested this trading strategy in my article Backtesting a SuperTrend Trading Strategy Using Excel.  In this section, I am going to compare my previous results with the results from the MT4 backtest.

Following on from the previous article we now have a complete EA.  Before we can do anything else we need to make sure that the file is saved in the correct directory (programs\MetaTrader\experts) and then click Compile in MetaEditor.  If you have written the file correctly there should be no error messages.

We can now switch to the MT4 terminal and open the Strategy Tester.  Select our EA from the drop-down list.  Select the EUR/USD pair and the 1 hour timeframe.  Then we are going to select Open Prices Only and click Start.  I am using the date range 2004.03.15 – 2013.11.29 so that I can compare the results to my previous backtest using Excel.  My initial inputs are:

PercentagePerTrade – 1 SLMultiplier – 1 TPMultiplier – 5 STMultiplier – 2 CloseEMA – 25 SMALength – 200 StartTime – 0 FinishTime – 23

[table caption=”” width=”500″ colwidth=”20|100|50″ colalign=”left|left|center|left|right”]

Criteria,Excel Backtest,MT4 Backtest
Initial Capital,” $100,000 “,”$100,000”
Final Capital,” $1,386,579 “,”$466,168”
Profit,” $1,286,579 “,”$366,168”
Wining trades,” $6,858,641 “,”$4,623,868”
Losing trades,” $-6,091,474 “,”$-4,257,700”
Profit Factor,1.15,1.09
Number of Wining Trades,639,558
Number of losing Trades,”1,600″,”1685″
% Winning Trades,29%,25%
Max Drawdown,38%,48%

Optimizing the Expert Advisor

Now that you have your initial results, you can start to improve them by optimizing the EA.

Comparing the results of the two backtests, we can see that MT4 showed worse results than Excel.  Despite the fact that they were both testing the same strategy this is not surprising. The MT4 analysis takes into account the cost of the spread, and this will reduce the profitability of any short term strategy.  I now want to look at whether we can improve the profitability of the strategy.

When we built the EA we included several variables that you can alter to change the results of the trading strategy.  Variables that we can backtest using MT4 are entered in our EA as external inputs.  The variables that I will test in this article are the trailing stop, the SuperTrend multiplier and the start and finish time.

Start and Finish Time

Forex is a global market and the time of day makes a big difference to inter-day traders.  Different trading strategies will work better at different times of the day and it is a good idea to test trade entries at different times of the day.  

The strategy that we are testing is a trend following type and will not be profitable when the market is choppy and lacking direction.  Therefore we want to use the backtest models to see whether certain times of day are better than others for trading this system.  I am testing this strategy using data based on European time.

I will click on Expert Properties and then select Optimized Parameter as Balance. Then under Inputs I will leave all the parameters the same except for StartTime and FinishTime. I will tick the box next to these to and run the StartTime from 0-11 and the FinishTime from 13-23.  Then I will exit this window, tick the Optimization box and then Start.  The graph below shows the StartTime along the x axis and FinishTime along the Y axis.

Optimize a MT4 SuperTrend Trading Strategy

The results shown in the graph above are quite encouraging.  I always want to see a chart that has strong dark areas, not just a scattered dark boxes because it suggests that a number of different parameters will have a similar level of profitability.

Running the backtest again, this time using the Optimization Parameter Profit Factor gives the following graph:

Optimize a MT4 EA - Profit Factor Backtest Optimisation Graph

The above graph has less variation than the previous but it is possible to see that strategies with a later start time (the right side of the graph) have a higher profit factor.

The results show that the profitability of our trading strategy are improved by limiting the time that it operates.

For the purposes of this backtest, I am going to use the times of 10-21 which includes most of the European and North American sessions.

SuperTrend Factor

The SuperTrend factor controls how far the SuperTrend line is from the midpoint of the candle.  A smaller factor means that the indicator is more sensitive to changes in the trend and a large factor means that the indicator will be better at following trends for longer.

Backtesting the parameters of technical indicators can be useful but it is important to beware of curve fitting.  In this case I am only going to test one variable to see if the results are consistent and to enable us to decide whether to have a shorter or longer SuperTrend factor.

Running the optimization for the STFactor of 1 – 5 and selecting Balance gives us the following graph:

Optimize a MT4 EA  - SuperTrend Factor Optimization Graph

Running the optimization again and selecting Profit Factor gives us the following graph:

Optimize a MT4 EA - SuperTrend Trading Strategy Profit Factor Optimization

Both of the above graphs suggest that this strategy works better with a higher SuperTrend factor.  In this backtest, I am going to use a SuperTrend factor of 2.5.

Take Profit Length

The final variable that I want to look at is the length of our profit target.  Our trading strategy is a trend following system and we want to make as much profit as possible from our winning positions.  A long target will allow the trading strategy to close most trades based on either the stop-loss or the normal closing rules.

Running an optimization for a Take Profit of 2-25 with a step of 1 gives the following results:

MT4 SuperTrend Optimisation Graph Take Profit Length

The above graph shows that this strategy is more profitable with a long take profit.  Therefore I am going to set a take profit length of 20.

Final Results

Following the above process to optimize the EA my final inputs are:

PercentagePerTrade – 1
SLMultiplier – 1
TPMultiplier – 20
STMultiplier – 2.5
CloseEMA – 25
SMALength – 200
StartTime – 10
FinishTime – 21

[table caption=”” width=”500″ colwidth=”20|100|50″ colalign=”left|left|center|left|right”]

Criteria,Initial MT4 Backtest,Final MT4 Backtest
Initial Capital,” $100,000 “,”$100,000”
Final Capital,”$466,168″,”$1,018,332″
Wining trades,”$4,623,868″,”$5,519,110″
Losing trades,”$-4,257,700″,”$-4,600,778″
Profit Factor,1.09,1.20
Number of Wining Trades,558,334
Number of losing Trades,”1,685″,944
% Winning Trades,25%,26%
Max Drawdown,48%,34%

The above results show how some minor tweaks to our original trading strategy can make a big difference to the profitability.  All the changes to the strategy have been for logical reasons and should help to make it more profitable over the long term.


If you would like to improve your trading and are familiar with using Microsoft Excel you can take advantage of my Tradinformed Backtest Models. These models are easy to use and program and will help you trade better so that you can master the financial markets.