Skip to content
All library documents

Building an Equity Strategy Backtest with Matrix Operations

Article Quant Q&A · Author: Maxime

Summary

The document outlines a basic workflow for backtesting an equity selection strategy in MATLAB. It forms a portfolio by applying a characteristic-based selection rule to a matrix of asset returns, then computes portfolio returns by averaging the returns of selected assets for each period. The example assumes monthly data, a one-month holding period, and an unchanged stock universe.

It then describes comparing the portfolio returns with normal returns estimated from an asset-pricing regression, such as a Fama–French model, to assess abnormal returns. The main practical lesson is that portfolio formation and return calculation require careful matrix manipulation, while the regression step is comparatively straightforward. The example is deliberately simple: it does not explain transaction costs, changing universes, overlapping holdings, or other realistic backtest complications. It recommends first implementing the simplified case and becoming comfortable with MATLAB before expanding the design.

Key ideas

  • A basic equity backtest first computes portfolio returns from a selection rule and then evaluates them against an asset-pricing benchmark.
  • A characteristic matrix can define which assets enter each period’s portfolio.
  • Matrix operations are central to implementing portfolio formation and return aggregation in MATLAB.
  • The example assumes monthly returns, a one-month holding period, and an unchanged asset universe.
  • The simplified workflow omits practical complications such as trading costs and changing holdings.

Tags

Full text
# Learn backtesting using MATLAB


# 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.

## Answer by Constantin (score 4, accepted)

https://quant.stackexchange.com/a/16025

## The general idea

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

- Computation of the portfolio return resulting from your portfolio formation rule (or trading strategy)

- 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.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.