Download the code

Optimisation in Finance: Building the Optimal Trading Strategy

In a previous example, it was shown how MATLAB can be used to backtest a simple trading strategy in 8 lines of code, where the trading strategy was developed based on the Relative Strength (RS) index. In this article, I take Kawee’s work a step further and investigate if we can optimise the buy and sell RS index values to maximise our returns. The buy and sell signals are originally chosen at an RS Index of 40 and 70 respectively. Over a six-year period, this gave approximately 18% return-on-equity. Can this be improved?

This is done by comparing three different optimisation techniques – the pattern search, genetic algorithm and fmincon – to investigate which one yields the most optimal buy and sell signals based off of the RS Index. The best result obtained is 82% return-on-equity with the genetic algorithm!

This was achieved by leveraging MATLAB’s powerful ability to perform complex mathematical optimisations with relative ease and simplicity. We have provided a live script to get you started with these optimisations right away.

Step 1: Load the input data

Daily time series data of the Dow Jones Industrial Average from 2012 to 2018 is loaded here. The data has already been preprocessed by way of removing missing values. It is this price data upon which the trading strategy will be based.















Step 2: Set up the optimisation problem

To perform an optimisation, you have to set up the problem first. This means specifying an objective function, design variables and constraints. We will use the same objective function, design variables and constraints for all three of our optimisation algorithms.



Define objective function

The objective function specifies what we want to optimise. In this case, we want to maximise the difference positively between our initial price in 2012, and our final price in 2018. You can see this function at the bottom of the script.



Define design variables

The design variables are what you are trying to optimise for: the buy and sell indices. We also choose initial values. Let’s choose the buy signal such that it’s 20% more than the smallest RS index of the price data, and the sell signal such that it’s 20% less than the biggest RS index of the price data. The choice of using the RS index is arbitrary. There are several technical indicators built into MATLAB including stochastic oscillators, acceleration and momentum indicators and moving average convergence\divergence indicators, amongst others.




Define constraints

Constraints are important to control your optimisation. Firstly, find the smallest RS index of the price data, and let this be the smallest possible number that the buy and sell indicators can be. Similarly, let the biggest RS index of the price data be the largest possible number that the buy and sell indicators can be.




Here, we specify that the sell indictor is at least 10 units larger than the buy indicator




We have no linear equality constraints




Step 3: Perform the three optimisation techniques

Perform pattern search optimisation





Now, use the fmincon solver. We specify some additional options for the algorithm here.







Use the genetic algorithm optimisation





The incredible thing about MATLAB is that the calculation of these very complex optimisations took 1 line of code each!

Step 5: Test the performance of the trading strategies

Now, using the optimal buy and sell indicators, let’s do a backtest to compare our results with the original RS Index values.







View the equity curves for each solver





















We see that for all three optimisations that there has been a significant increase in equity over the six-year period. The genetic algorithm had the best results with approximately 82% increase in equity; followed by the pattern search with about 80% and then fmincon with 63%. In all three cases, the equity has improved substantially from the 18% yielded from the case with no optimisations.

Analyse Sharpe ratios and maximum drawdowns























The pattern search and genetic algorithms have a much higher Sharpe ratio than the case with no optimisation. Investors can thus expect more return for the volatility to which they are exposed. For both of these solvers, a Sharpe ratio higher than 1 means that the return on investment is greater than the risk taken. Furthermore, the pattern search and genetic algorithm have a much lower maximum drawdown than the other methods. This implies that they have a lower relative riskiness compared to the other techniques.

These potentially complex optimisation algorithms were written in minimal lines of easy-to-write code. In addition, we could easily back-test and evaluate the performance and risk metrics by using simple, supplementary calculations. By leveraging the power of MATLAB we have created an optimal trading strategy and increased our return-on-equity from 18% to 82%!

Adapted from:

Numpacharoen, K. Backtesting Trading Strategies in Just 8 Lines of Code. [Online]. Accessed 15/07/2019.

Supporting functions

Objective function for optimisations







Backtest generating function








What Can I Do Next?

Follow us