Is Optimization Good In Trading? (How To Optimize A Trading Strategy)

Last Updated on December 4, 2021 by Oddmund Groette

Optimization in trading is good if done correctly and below you can read how to optimize a trading strategy. Optimization has negative associations for many traders, just like curve fitting, but optimization done correctly can give you valuable inputs and a better understanding of your strategy. If you know what you are doing, you should run an optimization test of all your backtests. We give you two simple (and naive) examples of how to optimize.

When you start live trading with a backtested strategy, you frequently get disappointed: the results are far away from the indicated results in the backtest. How could this happen after spending days backtesting your strategy? For new traders, this might come as a surprise, but for seasoned traders, this hardly comes as a surprise. The reason might be optimization and curve fitting of your backtest.

What is optimization in trading?

First, let’s define what optimization in trading really is:

Merriam-Webster defines optimization like this:

….as an act, process, or methodology of making something (such as a design, system, or decision) as fully perfect, functional, or effective as possible specifically : the mathematical procedures (such as finding the maximum of a function) involved in this.

Optimization is no different when it comes to trading. You look for the best variables or parameters and then search and optimize for the best settings for those variables. With today’s computing power, a software program can easily be programmed to find the best optimization for a certain strategy. It’s literally done in seconds and the software needed is cheap and good. In this article, we use Amibroker to optimize:

Is optimization good in trading?

Yes, optimization is good when it’s done correctly and you know what you are doing. It’s good because you get a better understanding of what is making the strategy perform well, and you get to understand how the variables of the strategy influence the end result.

Optimizing lets you easily understand if your backtest is based on luck or randomness. For example, if you have a trading strategy that goes long when the price breaks above the 25-day moving average, you can optimize it by changing the number of days in the moving average. If you find out that the results are much worse close to 25 days, you get a pretty strong indication that the original strategy most likely is down to chance and randomness. Hence it’s unlikely to be very successful in the future.

Instead of being ignorant to optimization, you should optimize every backtest you do. An optimization is, in reality, a test of how robust your strategy is. When you examine the variables, you get a much better understanding of your strategy: is it due to chance/luck or might it be something worth trading or testing out of sample?

An example of optimization in trading (the gold price)

If you are new to trading and backtesting, we provide you with a simple and naive example of optimization:

For example, you might want to find the best moving average crossover system for the gold price. The system works like this:

  • When the close crosses above the moving average, you go long at the open the next day.
  • When the close crosses below the moving average, you sell at the open the next day (close the long position).

As you can imagine, this is a very simple trend-following system. Despite its simplicity, a long crossover system like this has worked pretty well for the gold price over decades:

However, we can try to optimize such a crossover system by testing the number of days in the moving average.

For example, we can start with a minimum number of 5 days and continue up to 500 days. If we test every single one of the days it means 496 tests. To minimize the number of tests we can use intervals of 10 days thus reducing the number of tests by 90% to 50.

If  we perform this test on GLD, the ETF that tracks the gold price, we get the following result from inception to September 2021:

Each row shows the result for each input in the variable (the number of days in the moving average). The second column shows the number of days used in the system. Not all tests are included in the table above – about 40% didn’t fit.

However, we clearly see that such a crossover system works best with a high number of days in the moving average. The shorter the moving average, the worse the results. But because of the small number of trades, as shown in column 3, the results vary quite much.

An RSI optimization in the S&P 500

The RSI indicator is a widely popular indicator and has performed well as a mean reversion strategy on the S&P 500 over the last 30 years. But what works best – a short or long RSI? And which thresholds are best – low or high?

Let’s test to find out. For such a test we need three optimization parameters:

  1. For the number of days in the RSI calculation (from 2 to 5 days with intervals of 1)
  2. For the threshold of when to buy (from 10 to 35 with intervals of 5)
  3. For the threshold of when to sell (from 35 to 90 with intervals of 5)

We test on SPY, the ETF that tracks the S&P 500, from inception in 1993 until October 2021.

The optimization involves 144 tests (4 x 6 x 6). This is a lot of tests and it might be difficult to find out where the sweet spot is. We can set some minimum variables, for example, that we should have at least 250 trades over the period in order to have a significant number of trades/observations. This reduces the number of tests down to 53. We then sort on the profit factor.

We copied the results from Amibroker into OpenOffice:

Clearly, an RSI based on two or three days seems to give the best result. The balance of where to set the buy and sell thresholds might be less obvious from the results, however. To judge that you might want to have a look at the columns for CAR/CAGR, exposure (time spent in the market), and max. system drawdown.

Amibroker code for optimization:

If you are unsure of how to use the optimization function in Amibroker, we offer you the code plus all the other code for the trading strategies we have offered for free.

Relevant recommended reading:

Conclusion about optimization in trading:

Optimization might have a bad “reputation” in trading:

Optimization of a trading strategy done incorrectly results in a system that has performed well in the past but is unlikely to perform well on unknown future data. The predictive value is poor. Because of this, many traders shy away from doing any optimization.

However, as we have argued in this article, optimization done correctly can give you valuable information about the properties of your strategy. We believe you should ALWAYS look at optimizations of your backtests to better understand how robust or not robust your parameters are.

If you understand how to optimize a trading strategy properly you increase your chances of having a successful out-of-sample test.