Basics of Backtest and Cross-sectional Momentum

Haohan Wang
Dec 28, 2015 · 7 min read

I will dig deeper around the topic of building a more sophisticated trading and backtesting algorithm including more details. I will also provide and explain to you with a momentum and long/short equity strategy example to testify the theory.

Before you deciding on using any strategy, here are three questions that you should ask yourself:

  1. How much should you invest in this trade?
  2. How much can you afford to lose?
  3. How risky is it?

You can calculate that here.

By considering these questions, you need to not only make a prediction about whether the prices will rise or fall, but also decide whether they will go up a lot or a little depending on various factors especially their volatility. The strategies such as long/short equity strat. contain both long and short strategy, which means you cannot just make positive forecasts(you want to buy the asset that you expect its price will go up) but also negative forecasts. What’s more, a forecast shouldn’t be binary but should be scaled. I will illustrate the third question explicitly in my backtest.

In this article, I will focus on how to backtest on your strategy. Before moving into the code part, I need to introduce the statistical theory behind the Backtesting: Hypothesis Testing.

Since we are obviously going to use a finite sample(dataset) size in our backtest, we know that whatever statistical measures we compute are all subject to randomness. In other words, we may be just get lucky in applying our strategy and we may be profitable in a small data sample or within a certain timeframe. In order to resolve this issue, we develop ‘Hypothesis testing’ methodology and I will explain part of it below:

  1. The statistical measures that we calculated in the test are called test statistic.
  2. We generally assume that the true average daily returns are actually 0 based on an infinite data(this assumption is called the null hypothesis).
  3. We also assume that the probability distribution of daily returns has the mean equals to 0 based on null hypothesis.
  4. Like I used verbosely in my previous articles whenever I conducted a statistic test, the p-value will play an important role in that step. P-value(link) represents the probability that the average daily returns will be at least as large as the observed value in the backtest. If the p-value is very small(cutoff point is 0.05 in general), that means we can ‘reject the null hypothesis’, and summarize that the bacsktested average returns are statistically significant.
  5. I will also bring up another critical measure: Sharpe Ratio. If you are familiar with finance, you probably know that Sharpe Ratio(you can also check the concept by clicking the word at the beginning of the paragraph) is calculated by the formula below: Sharpe ratio = (Mean portfolio return − Risk-free rate)/Standard deviation of portfolio return. From the formula and the concept of Sharpe ratio, we can summarize it as a risk-adjusted approach of return which is often used to assess the performance of a portfolio. It measures the increments in excess returns (as measured by the portfolio return minus the risk-free rate) for each additional unit of risk (as measured by the standard deviation of the portfolio returns).
  6. Since we already assume that the daily returns follow a standard parametric probability distribution such as the Gaussian Distribution. As for the standard test statistic for Normal Distribution is using the average divided by the standard deviation and multiplied by the square root of the number of data points(n). Therefore, if the daily Sharpe Ratio multiplied by the square root of the number days
  7. in the backtest is greater or equal to the critical value 2.325, then we can identify that the p-value is actually smaller than or equal to .01). Thus, we consistently believe that high Sharpe-Raio strategies are more statistically significant.

Here I will use cross-sectional momentum backtest as an example to ‘backtest’ all the theory that I mentioned above.

In the previous article, I explained how to measure time-series momentum(A security’s own past return predicts its future return; from this, auto-correlation is used. The main advantage is that time series momentum can measure and analyze all asset classes because it relies only on its own past price. Therefore, you can consider buying assets with positive (excess) returns in the past and selling those with negative returns).

In terms of this test, I will use another momentum strategy: cross-sectional momentum(future outperformance of a stock is predicated by its outperformance relative to its peers.) combined with fundamental factors as well as long/short equity strategy. It seems like a portfolio of futures or stocks often exhibit cross-sectional momentum: a simple ranking algo based on returns would work for this strategy.

The typical cross-sectional strategy starts with ranking assets by their past returns and creates a zero-investment long-short portfolio, the long-leg equally weighting the top quantile (often the top decile for stocks; what I am using is’ >99%’) and the short-leg equally weighting the bottom quantile(here is < 1%). We can also refer to these as quantile cross-sectional strategies. A variation of this strategy, which has been considered in the theoretical analysis of cross-sectional momentum invests an amount proportional to each asset’s (past) deviation from the cross-sectional average return. Moreover, because the weights are linear in the assets’ past returns, we can also refer to this as an (unscaled) linear cross-sectional strategy.

The graph below shows the backtesting outcome from 12–23–2014 to 12–23–2015 of the strategy compared with the benchmark(S&P 500).

From the risk metrics on the top of the graph, we can tell this strategy works well within this timeframe(generates total returns of 74.9% in the end; β is relatively small which is -0.15; also with Sharpe ratio: 1.93; the only part that may not be very ideal is the Max Drawdown which is 16.6% over this period). But it does not perform very good at the beginning of last 12 months. From the analysis of Sharpe ratio, we can basically come to a conclusion that this model is statistically significant to some point.(we can check by doing this calculation: 1.93 * (365)^1/2 >2.325)

The code screenshots below are the code that I used for the backtest. Let’s begin with the first one, I set up the 200 days look-back period plus the 25 days returns window(calculate the returns within 25 days) as well as the monthly rebalance frequency in this step. Also, both long and short leverage are +/-0.5 in this case(try to keep them even in order to control the risk).

The prices that I used to calculate the returns and normalized returns is based on the 200 days look-back time period and 25 days returns period(there are total 25 trading days per month).

Then I choose the top the long-leg equally weighting the top quantile (often the top decile for stocks; top quantile here is’ >99%’) and the short-leg equally weighting the bottom quantile(here is ‘< 1%’).

I also set up the entry and exit signals for my momentum strategy since one way to use momentum is to have cutoffs and enter a position when momentum is significantly high and leave it when momentum is significantly low. Here we should enter the position if the ranks of the stocks in my universe in within the top 1% of all stocks in the universe; on the other hand, we should exit the position if the ranks of the stocks are lower than bottom 1% of all stocks in the universe.

Here I will continue to use fundamental factors to filter down the universe which is an essential component in this backtest. (Fundamentals would be one of the most common strategies that people use to evaluate the securities and believe to be rational. Most companies will generate financial reports (financial metrics )on a quarterly and annual basis. Fundamentals are those metrics and other derived metrics calculated via the filings and used to assess and analyze their health and prospects in the future.)

I have shown you the process about how to use fundamental factors to create a cross-sectional query of selecting equities and constructing your tradable universe. The fundamental method also has the built-in function to rank the securities based on certain criteria.

The total number of stocks that I am choosing from is 500. I use the filter function to filter down the universe and I intend to obtain the stocks with large market cap and positive EPS. As usual, the list ordered based on their market cap from highest to lowest.

In the end, I would like to share with you some benefits I find about the algo trading and hopefully you can find something useful for your strategy:

  • This provides a way can help you focus on a big picture and create new ideas;
  • You can make decision more rational because it is backed by data and execute without emotion;
  • In the end, you can always test before you invest.

As usual, please leave your comment on anything inefficient or inaccurate. I expect to hear the advice or questions from you. Hope you enjoy the article!


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store