Skip to content
All library documents

Calculating Returns for Large Multi-Asset Portfolio Backtests

Article Quant Q&A · Author: Matteo

Summary

The document asks how to calculate historical portfolio returns when a strategy spans thousands of securities and supplies target weights for each trading day. It contrasts this task with event-driven backtesters that process instruments individually, which can become impractical at very large universe sizes. The central idea is to represent prices and portfolio weights as data matrices and compute the resulting return series using a portfolio-oriented workflow.

The responses offer two software approaches: a pandas-based backtester intended for broad universes, and a system that filters a large universe daily before applying intraday or end-of-day trading logic. A separate answer describes using an R portfolio package to calculate returns from a price matrix, weights, and rebalance dates. These are suggestions, not a comparative benchmark: the document reports no runtime, accuracy, or performance evidence. It also does not explain important backtest assumptions such as transaction costs, missing prices, corporate actions, or whether weights are formed before or after each day’s returns. One recommendation comes from an affiliated respondent, so it should be read as a software suggestion rather than an independent evaluation.

Key ideas

  • Large-universe portfolio returns can be computed from asset prices and time-varying target weights.
  • Matrix-based calculations can avoid iterating through each security with a small-universe workflow.
  • The responses identify pandas and R-based tools as possible approaches to portfolio backtesting.
  • A return calculation alone does not establish realistic trading performance without assumptions about costs, data, and rebalancing.

Tags

Full text
# Backtesting a portfolio strategy with several assets


# Backtesting a portfolio strategy with several assets












I am aware that there exist several libraries and programs that allow to baktest a portfolio strategy by iterating through the OHLC dataframe of the stocks of interest (Backtrader, Backtesting, ...). However, these methods are useful when we are working on one or few stocks: when it comes to backtest a strategy that includes several stocks (thousands), it becomes impossible to iterate though all these datasets.

In these cases, how can we determine the ex-post returns of a portfolio containing such quantity of instruments? For example, if we know the weights for each stock at each trading day to invest in the portfolio, how can we test such strategy?

## Answer by Brian from QuantRocket (score 2)

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

QuantRocket supports two backtesters, both of which are designed to support universe sizes in the thousands.

- Moonshot is pandas-based and has the scalability you would expect from a pandas-based library. It's a good choice if you already know and love pandas.

- Zipline has a design that allows you to dynamically filter thousands of securities each day and execute intraday trading logic on the filtered subset. It also supports end-of-day strategies.

There are examples of strategies targeting large universes in QuantRocket's Code Library.

You're right that many backtesters are geared toward a small number of securities, and trying to adapt them to support thousands of securities is generally an uphill battle.

Disclaimer: I'm affiliated with QuantRocket.

## Answer by Enrico Schumann (score 2)

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

If using R is an option: With package PMwR, which I maintain, you could compute a time-series of returns for such a portfolio in one line of code:

```
## P -- a matrix of prices: each column holds the prices of one asset
## w -- a matrix of target weights: each row holds a portfolio
## t -- a vector of times (i.e. row numbers) at which to rebalance

library("PMwR")
returns(P, weights = w, rebalance.when = t)
```

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.