Quant Memo
Foundational

Vectorized Backtesting and Its Tradeoffs

A vectorized backtest computes an entire strategy's history in a few array operations instead of a day-by-day loop — fast enough to sweep thousands of parameter combinations, but only honest if the strategy has no path-dependent logic hiding inside it.

A researcher backtests a moving-average crossover on 500 stocks over 15 years using a vectorized approach — computing signal and P&L as whole-array operations rather than a day-by-day loop — and sweeps 10,000 parameter combinations in about four minutes. The equivalent explicit loop over the same sweep would take most of a day. That speed is the entire reason vectorized backtesting exists, and it comes at a specific cost: a vectorized backtest only cheaply expresses strategies where each period's decision doesn't depend on what the strategy itself has done before, and a surprising number of real strategies quietly violate that.

What vectorization actually buys and costs

A vectorized backtest computes signal, position, and P&L with array-wide operations — a rolling mean here, a comparison there — rather than iterating day by day. It's fast because an array library batches the arithmetic instead of executing logic thousands of times per stock per parameter combination.

The cost is structural: anything depending on the strategy's own accumulated state — position size, holding duration, whether a stop-loss already triggered — resists a clean array formulation, because each period's answer depends on the previous period's answer to the same strategy-specific question, not just raw market data. Forcing this into vectorized form usually means approximating it away or writing a disguised loop that's just as slow as an honest one.

Worked example: where vectorization silently gets it wrong

A momentum strategy vectorized as "rank on 12-month return, go long the top decile, short the bottom, multiply by next month's return" is honest vectorization — position depends only on trailing data — and the fast version matches a slow, explicit loop to the last basis point.

Add a rule: "hold a position at least 3 months once entered, even if it drops out of the top decile." Now month tt's position depends on whether the stock was already held in t1t-1 and t2t-2 — path-dependent. A naive vectorized version that just re-ranks every month independently silently drops the minimum-hold rule entirely, with no error — it simply reports a different, wrong number. Running both on the same universe: the correctly-loop-based version shows annual turnover of 340% and net Sharpe 0.72; the naive vectorized version shows turnover of 890% and net Sharpe 0.31, because the extra re-entering and re-exiting eats the edge in costs. Whoever trusted only the fast version would conclude the hold rule doesn't help, when the code never implemented it.

stateless signal → array ops → done + minimum hold → needs state per stock a disguised loop pretending to be vectorized silently drops the rule
The moment a rule depends on the strategy's own prior decisions, honest vectorization stops being a shortcut and starts being a bug risk.

Vectorized backtesting is fast and correct exactly when each period's position depends only on external data, not the strategy's own trading history. The moment a rule references "have I already entered" or "how long have I held," vectorize what you can and loop honestly over the rest.

What this means in practice

Use vectorized backtesting for the initial, fast sweep across parameters on strategies without path dependence. The moment a rule depends on accumulated state, confirm the vectorized implementation actually encodes it — a hand-computed single-stock example over a few months matching the code's output exactly is a quick check — or switch that portion to an explicit loop. Cross-check any vectorized backtest against a slower Event-Driven Backtest Architecture version on a small sample before trusting a full sweep built on it.

Related concepts

Practice in interviews

Further reading

  • Chan, Algorithmic Trading: Winning Strategies and Their Rationale
ShareTwitterLinkedIn