# Learn backtesting using MATLAB

What are some good ressources (books, articles, ...) to learn backtesting of investment strategies using MATLAB ?

It can be strategies related to fixed-income, equities, derivatives, ... whatever. The process of backtesting is more important than the actual strategy.

Thank you.

Maxime.

• While I like where this question is going, I would suggest to make it a little more concrete. What parts of the backtesting process would you like to learn? This can range anywhere from only estimating a normal return, where the portfolio returns from your strategy are already given; to implementing a full portfolio formation rule algorithmically. Dec 30, 2014 at 21:06
• To be honest I don't know much about backtesting. I was told that I will have to backtest new strategies or improve current one during my internship. So I would like to know a bit more about the subject before starting. What are the different parts of it ? Dec 30, 2014 at 21:31
• How comfortable are you in Matlab? Dec 30, 2014 at 21:32
• Just started matlab a few weeks ago. Already read some doc about it but mostly regarding the pricing of derivatives. Dec 30, 2014 at 21:41

# The general idea

For equity securities, a simple backtest will typically consist of two steps:

1. Computation of the portfolio return resulting from your portfolio formation rule (or trading strategy)
2. Risk-adjustment of portfolio returns using an asset pricing model

Step 2 is simply a regression and computationally very simple in Matlab. What's trickier is the implementation of step 1, which will require you to be very comfortable in Matlab, and there are different ways to do this.

If you know how to do an OLS regression in Matlab, what you should focus on is all kinds of matrix manipulations.

# Implementation in Matlab

## Portfolio formation and returns computation

To give you an example of how a primitive trading strategy could be implemented in Matlab, let's assume monthly return data and a uniform holding period of one month on $n$ assets over $k$ periods, where $i \in \{1,...,n\}$ and $k \in \{1,...,t\}$.

Assuming no changes in the composition of your stock universe, your returns matrix $X$ is of dimensions $k \times n$.

$$X = \begin{matrix} x_{11} & \dots & x_{1i} & \dots & x_{1n} \\ \vdots & \ddots & \vdots & \ddots & \vdots \\ x_{t1} & \dots & x_{ti} & \dots & x_{tn} \\ \vdots & \ddots & \vdots & \ddots & \vdots\\ x_{k1} & \dots & x_{ki} & \dots & x_{kn} \\ \end{matrix}$$

Where returns are computed as $x_{it} = \frac{p_{t+1,i}}{p_{ti}} -1$.

Assuming that your selection criterion is some kind of stock characteristic which is available at monthly frequency, you will also have a characteristics matrix $C$.

You then could write an algorithm which identifies those entries in $C$ which fulfill your selection criterion (e.g. exceed a certain threshold) and replace the corresponding entries (where $i$ and $t$ are the same) of an indicator matrix $I$ (which has been initialized as a zero matrix using the zeros function) with ones.

You can then multiply the entries of $I$ by those of the returns matrix $X$ to obtain a matrix $R$ which indicates the returns resulting from your holdings. You can then compute the mean of the non-zero entries for each row of $R$ to obtain your vector of portfolio returns.

## Risk-adjustment and identification of abnormal returns

In step 2 you compare this vector to the normal returns obtained from regression estimation of an asset pricing model such as the Fama-French model. By subtracting the normal return vector from your portfolio returns vector, you determine whether your trading strategy has resulted in a positive abnormal return, which is what you're aiming for.

# Recommendations

If you are new to Matlab, I personally suggest you familiarize yourself with it sufficiently to implement this simplistic strategy before relaxing some of the simplifying assumptions (such as uniform holding period and periodicity) and proceeding to more sophisticated implementations.

Again, what I would like to stress is that this requires you to be very comfortable with Matlab and especially the different ways to manipulate matrices, which can take some time. If you are not required to use Matlab for your internship and would like to get results fast, you could do step 1 in Excel instead, which is tedious, but doesn't require the (worthwhile) initial investment you need to make for Matlab.

To become familiar with Matlab, I am sure you have already discovered the extremely good documentation that comes with it. That, to me, is the single most valuable resource and likely more useful than any more finance-specific resources (with which I would wait until you are familiar with Matlab itself). All that's required to determine the normal return is an OLS regression and a rudimentary understanding of asset pricing models.