Home Learning Metatrader 4 Expert Advisor Strategy Design: How to Optimize an EA Setup

Expert Advisor Strategy Design: How to Optimize an EA Setup

Expert Advisor Strategy Design: How to Optimize an EA Setup
Expert Advisor Strategy Design: How to Optimize an EA Setup

In this article you can learn how to design an Expert Advisor strategy and optimize the EA setup determining the best lengths, modes and methods to use.

In the previous article, we purposefully delimited all outside variables and most internal parameters to create a 20-200 Dual Simple Moving Average (SMA) Crossover EA that was shown to perform reliably well over 41 years (Jan-1971 to Jan-2012), achieving 15000 pips with less than 2500 pip draw down. I believe that the EA did well because it was pure (i.e. unencumbered with extra indicators, exits and filters) and it had borrowed the most popular length and method. In this article we will explore if the original EA can benefit from a different length or method.

Why did we choose the initial lengths of 20 SMA for our Fast Length and 200 SMA for our slow length? Because we had read in multiple places that when the 20-day SMA crosses over the 200-day SMA it is called the Golden Cross, when it crossed under it was called the Death Cross. These terms were more often used in the world of stocks and futures. The names coined for them (Golden and Death) suggest their awesome significance: they are watched and headed by all professional traders because of the ability to capture the long term trend. We simply changed the timeframe from daily bars to H4 bars in order to generate more crossover instances (and thus more trades) of medium duration. Moving averages calculated in daily bars with such long lengths generates too few trades to make a reliable statistical evaluation.

It was useful to choose some form of standardized value for the MACross, and the higher values of the Golden Cross do seem to work.  However, it would be interesting if we could test all the fast lengths between slow lengths from 50 to 300 to see if we can discover a more favorable length. I am not a big fan in optimizing the fast length. One length is plenty to optimize upon, and optimizing on more lengths or parameters risks over-optimization and curve-fitting. The slow one is the most important length anyway. Instead I will keep the fast length a fixed fraction of the slow length. The initial fraction chosen will be 1/10th. If the fast of 20 worked with the slow of 200, then it is suggestive that the fast length can work decent enough if it is 1/10th the length of the slow length.  It will be interesting to test the agility and accuracy of the MACross in a wider variety of maneuvers, from the relatively quick  5-50 Dual MA Crossover all the way to relatively slow (smoother)  30-300 Dual MA Crossover. It will be hard to beat the performance of the 20-200 Golden Cross itself, but we will try.

How does one make the fast moving average a fixed fraction (1/10th) of the slow moving average? One just deletes the Fast Moving Average as an extern variable and presents it instead as fraction:

//Place in extern variables section

extern int MAFraction = 10;//Place after Start() function
double MAFastLen = MASlowLen / MAFraction;

A word about Optimization. It can present a more objective and perhaps more profitable reason for trading with certain values over another, so long as it is done properly – knowing beforehand the dangers of over-optimization and taking steps to avoid them.  Please read article on Steps to Avoid Overoptimization.

The purpose of this optimization experiment is to determine the best lengths and methods to use for our Dual MA Crossover EA.

We will be asking two questions:

  1. What is the best length of the slow moving average?
  2. What are the best modes (SMA or EMA)?

Our optimization range for the lengths of the SlowMA will be from 50-300 (in steps of 10), to give us 25 potential combinations. We will conduct this optimization using the simple moving average, and then follow with same optimization on the exponential moving average.

We will be optimizing the lengths on a 10 year sample period between Jan-1999 and Jan-2009, we will choose the best lengths that period, and then walk forward with that length on a back test over more recent 3 years (Jan-2009 to Jan-2012), to see how our chosen length works if we had traded it after our 10-year optimization.

The testing account size will be $10,000, with a lot size of 0.1, and since the stop loss is 150 pips, the maximal losing trade will be $150, or 1.5% of the account size.

Round #1 (SMA Mode)

With the simple moving average (SMA), all prices of the time period in question are
equal in value. The SMA simply takes the sum of all of the past closing prices over the time period and divides the result by the number of prices used in the calculation.

N — is the number of calculation periods.


Optimization #1: Optimize the Slow Length from 50 to 300,  Keeping the Fast Length at 20. Slow length is best for optimization.

Here are the 26 optimization results for the MASlowLen of the Dual SMA Crossover for the period Feb 1973 to Feb 2013 (40 years) using “Control points” modeling:

Notice how results improve with each successive longer length. It seems that the H4 moving average crossover performs best with the smoothing factor of longer lengths. The absolute top result  is the slow length of 280, resulting an excellent $18925 / 18925 pips, and very healthy profit factor of 2.28. Notice how it earned 7000 more pips than the 200 slow length with 1200 pips less of a max drawdown. The max drawdown of the 20-200 combination was 3853 pips whereas the 20-280 was 2650 pips. This is all very promising.

The EMA version, which we will explore below, will face some stiff competition.

Round #2 (EMA Mode)

Exponentially smoothed moving average is calculated by adding the moving average of a certain share of the current closing price to the previous value. With exponentially smoothed moving averages, the latest prices are of more value. The benefit is that it picks up trends and trend reversals faster than the simple moving average.

EMA = (CLOSE(i)*P)+(EMA(i-1)*(1-P))

CLOSE(i) — the price of the current period closure;
EMA(i-1) — Exponentially Moving Average of the previous period closure;
P — the percentage of using the price value.

The downside of the EMA is that its speed sacrifices smoothness, getting into more false trades that happen in volatile sideways markets. Since we already noticed that the shorter lengths of the slow SMA produced poorer results than the longer lengths, I can already foresee that the EMA will not be as successful as the SMA because it will run into the same problem of speed. It seems that in the contest between speed and smoothness, speed kills.

Here are the top 26 optimization results for EMASlowLen of the Dual EMA Crossover
for the period Feb-1973 to Feb-2013 (40 years):

This EMA mode has a similar, but slightly more profitable, result than SMA mode. The best optimization, 260-EMACross weighs in at $19847 / 19847 pips, which is a 1000 pips more return that the 280-SMACross. However, drawdown is 800 pips less with the SMA cross. Notice how the 260 length doubled the performance of lengths between 100-130, which further brings home the point that increasing the length can increase the reliability.



A good optimization is a discovery / elimination process to reveal and compare the results of different parameters, choosing the ones that show promise and eliminating the weak performers or the ones that violate over-optimization rules.

In optimizing the dual moving average crossover strategy along different lengths for each method, we discovered that the longer lengths generated superior results.  It seems that the longer lengths creates a smoothing effect that is more advantageous than the speed of the shorter lengths. The longer the slower MA the bigger the trend it tries to identify and support, and the longer the fast MA the more reluctant it is to jump in and out of the dominate trend in anticipation of a reversal. The result is less noise and whipsaw. The shorter length may be more responsive to recent movements, but it is choppier as well.

In comparing the different methods, with the real choice between simple and exponential, it was clear that though the exponential had powerful results, the simple results were somewhat more superior because of the lower draw down. It turned out the 20-280 SMACross worked out the best, because of its 800 pip lower draw down, but the 20-260 EMACross is a powerful contender, turning out 1000 pips more profit.  Just as the smoothness of longer lengths trumps the speed of shorter lengths, so too it seems that the smoothness factor of the simple method trumps the speed factor of the exponential method. The exponential tries to solve the slowness factor of the simple by putting more weight on recent price points, and in doing so it can get into trends and reversals faster, but at the same time it can jump into more fake out trade as a result of choppy, sideways markets. These fake outs can increase the losing trade count and wither down the overall performance, particularly in the case of sideways markets.



Please enter your comment!
Please enter your name here

Sign-up in 5 seconds and receive 5 FREE e-books!
This is default text for notification bar