VALYNOXSTRATEGY RESEARCH TECHNOLOGY
AlphaPrimeAlphaPrime
ACADEMY / Indicators and signals

Mean reversion explained: the formula and a testable futures example

Mean reversion bets that a price far from its recent average moves back toward it. Measure the distance as a z-score, z = (price − average) ÷ standard deviation, for example buying below −2 and exiting near 0. Write exact entry and exit rules, then test them after costs, in trending periods, on nearby settings and on other markets.

By · Published · updated · Editorial policy

Contents
  1. What is mean reversion?
  2. What is the mean reversion formula?
  3. How do you turn mean reversion into rules a backtest can follow?
  4. Why does a mean reversion strategy fail in trending markets?
  5. How do trading costs change a mean reversion backtest?
  6. How do you check that the result is not a lucky setting?
  7. Frequently asked questions
  8. References

What is mean reversion?

Mean reversion is the idea that a price that has moved far from its recent average tends to move back toward it. You measure the stretch as a z-score: price minus its moving average, divided by the standard deviation over the same bars. A testable futures rule buys below −2, exits near 0, and is tested after costs.

Think of a rubber band tied to a post. Pull it far and it tends to snap back. A mean reversion strategy waits for the band to be pulled far, then bets on the snap. A trend-following strategy makes the opposite bet: that a move away from the average will keep going. Both ideas can be tested. Neither is true in every market or in every period. Entry signals for it usually come from technical indicators that measure the distance from an average.

In futures, traders apply the idea in two common ways. They apply it to one contract's price, for example an index future that stretches too far within a quiet session. They also apply it to a spread between two related contracts. This article uses the first case, because it is the easiest to turn into clear rules.

What is the mean reversion formula?

The standard way to measure mean reversion is the z-score, which says how many standard deviations the price is away from its average. A z-score is a number that tells you how unusual a value is compared with recent values. Standard deviation is a measure of how widely prices have spread around their average over the same bars. The standard deviation indicator page explains how it is calculated.

The formula is:

z = (P − M) / S

  • P is the current closing price.
  • M is the average of the last N closes, usually a simple moving average.
  • S is the standard deviation of the same N closes.
  • N is the lookback, the number of bars used for both M and S.

A z-score of 0 means the price sits on its average. A z-score of −2 means the price is two standard deviations below its average. If you draw lines at +2 and −2 around a 20-bar average, you get the familiar Bollinger Bands with their default settings. The z-score is the same information as one number, which makes it easy to put into a rule.

In a perfect bell-shaped distribution, about 95% of values fall within two standard deviations of the mean. Price data has fatter tails than that. Moves beyond ±2 happen more often than the bell curve suggests, and some of them keep going. So ±2 is a starting point to test, not a statistical promise of a bounce.

The same price shown two ways: on top, the price dips below the lower band around its moving average; below, the z-score crosses minus 2 where the rule buys and returns to 0 where the rule exits Buy at −2, exit at 0 Same price, two views Buy Exit Price z-score 0 −2 z turns the stretch into one number. So: write the rule on z, not on feel.
The same price shown two ways: bands around the average on the chart, and the z-score below it. The rule acts when z crosses −2 and exits when it returns to 0.

How do you turn mean reversion into rules a backtest can follow?

A mean reversion idea becomes testable only when every decision is written down, including when to give up. "Buy when it looks oversold" cannot be tested. A list of exact conditions can. Here is one complete set of rules for an index future on 30-minute bars:

  1. Calculate M and S over the last 20 closes, then z, at the close of each bar.
  2. Enter long when z closes below −2. Buy at the open of the next bar.
  3. Enter short when z closes above +2. Sell at the open of the next bar.
  4. Take the normal exit when z crosses back to 0, at the open of the next bar.
  5. Use a stop loss at a fixed distance from the entry price.
  6. Use a time stop: exit after 10 bars if neither exit has happened. A time stop is an exit that closes a trade after a set number of bars, whatever the price.
  7. Trade only in the session you chose, and be flat at the end of that session.

Two details decide whether this backtest is honest. First, the rule decides at the bar close and fills on the next bar. Filling at the same close that produced the signal uses a price you could not have traded. Second, the exit at z = 0 happens partly because the average moves toward the price, not only because the price moves toward the average. Your profit per trade is often smaller than the distance from entry to the starting average.

Hypothetical example

A 20-bar average is 4,800.0 and the standard deviation is 10 points. A bar closes at 4,778.0, so z = (4,778 − 4,800) / 10 = −2.2. The rule buys at the next open, 4,779.0. Six bars later the average has fallen to 4,794.0 and a bar closes at 4,795.0, so z is back above 0. The rule sells at the next open, 4,794.5. The gain is 15.5 points. The average dropped 6 points, so price did not have to climb all the way back to 4,800.

Mean reversion rules usually win often and lose rarely but in larger amounts. A high win rate alone tells you nothing. Use the win rate and expectancy calculator with your average win and average loss. It shows what each trade is worth on average.

Mean reversion fails when a market trends, because a stretched price keeps stretching instead of snapping back. A trending regime is a period in which prices move steadily in one direction, so each new bar tends to extend the move. In a strong sell-off, z can close below −2 for many bars in a row. The rule buys, gets stopped, buys again, and gets stopped again.

The same mean reversion rule in two regimes: in a sideways market its buys at the lows turn into small wins, in a falling market each buy keeps falling and is stopped out Same rule, two regimes Buys in a range vs a sell-off Sideways Small wins Falling Stop-outs Trends trigger stop after stop. So: test the rule in trending periods.
The same rule in two regimes: small gains in a sideways market, repeated stop-outs in a falling one.
Market behaviourWhat the z-score doesTypical result for the rule
Sideways rangeSwings across ±2 and back to 0Frequent small wins
Slow trendStays on one side of 0 longerFewer exits at 0, more time stops
Strong trend or news shockStays beyond ±2 for many barsRepeated stop losses in a row

You cannot remove this weakness, but you can measure it and limit it. Four steps help:

  1. Find the trending periods in your history, such as a sharp crash or a long rally, and read the trades the rule took there.
  2. Keep the stop loss and the time stop. Without them, the backtest can hide one losing trade that would have ended the account.
  3. Test a trend filter, for example: skip long entries when a 100-bar average is lower than it was 20 bars ago. Keep the filter only if it also helps on data it was not tuned on.
  4. Look at the worst losing streak, not only the total result. That streak is what you would have to sit through.

How do trading costs change a mean reversion backtest?

Costs hit mean reversion harder than most strategy types, because it takes many trades with small targets. The rule exits near the average, so the gain per trade is limited. Every round trip still pays commission, exchange fees and slippage. Slippage is the difference between the price your backtest assumed and the price you would actually get.

A second cost is hidden in the fill assumption. Many mean reversion traders buy a falling price with limit orders. A backtest that fills every limit order the moment price touches it is optimistic. In a fast drop, the orders that fill are often the ones that keep losing, and the ones that would have won are often missed. Next-bar market fills plus realistic slippage are the safer default. Because the backtesting with next-bar execution in AlphaPrime (Valynox) charges fees and slippage on every fill, a mean-reversion entry that looked fine at the signal price is read at the price it could actually reach.

Hypothetical example

Assume a tick value of $12.50, commission and fees of $5 per round trip, and one tick of slippage on each side. That gives a round-trip cost of $30. A 5-minute version of the rule averages 3 ticks gross per trade, which is $37.50. After costs it keeps $7.50. With slippage doubled to two ticks per side, the cost is $55 and the trade loses $17.50 on average. A 30-minute version averages 12 ticks gross, which is $150. It keeps $120 after normal costs and $95 after doubled slippage.

The faster version looks busier and smoother, but most of its edge goes to costs. The slower version survives the cost stress. For the full method of setting commission and slippage, see trading costs in backtests: commission, slippage and the average trade.

How do you check that the result is not a lucky setting?

A trustworthy mean reversion result still holds when you change the settings a little and when you move it to other markets. The lookback N and the entry threshold are choices. If you test many combinations and keep the best one, some of its result is luck. Research on technical trading rules showed that the best of many rules tested on the same data can look strong by chance alone (Sullivan, Timmermann & White, 1999). The more variants you try, the higher the best backtest tends to be, even when no variant has a real edge (Bailey, Borwein, López de Prado & Zhu, 2014).

Run a small grid of nearby settings and look at its shape, not only its best cell:

What you changeExample valuesA good signA bad sign
Lookback N10, 20, 30, 40 barsNeighbours give similar resultsOnly one value works
Entry threshold1.5, 2.0, 2.5Fewer trades but a similar result per tradeThe result flips sign between values
Bar size15, 30, 60 minutesSame direction of resultWorks on one bar size only
MarketOther liquid index futuresWeaker but still positive after costsFails everywhere else
PeriodEach separate yearMost years acceptableOne year carries everything
Two grids of results across nearby settings: on the left most cells are positive, a plateau that suggests a real effect; on the right only one cell is positive among negative neighbours, which suggests a lucky setting Trust a plateau, not a spike Results across nearby settings − + + + − + + + + + − + + + − − − − − − − − + − − − − − − − Neighbours agree One lucky cell A real edge survives nearby settings. So: pick from the plateau's middle.
A plateau of similar results (left) suggests a real effect. One bright cell among poor neighbours (right) suggests a lucky setting.

Then test the chosen setting on data it was not tuned on. Walk-forward analysis is a test that tunes the rule on one window of data, trades the next window unchanged, then moves both windows forward and repeats. Walk-forward analysis: why one backtest is not enough explains how to choose the windows. In AlphaPrime, you can describe the rule's entry and exit conditions and the parameter ranges to explore in strategy generation. You can then run walk-forward analysis and other checks with your own thresholds in strategy validation.

What this means if you use AlphaPrime: set realistic fees, slippage and your trading session before the first run (next-bar execution is a fixed rule), so mean reversion candidates are ranked after costs. Then judge them by the shape of their nearby settings and by their walk-forward results, not by the single best backtest.

Frequently asked questions

What is the mean reversion formula?

The common formula is the z-score: z = (price − average) ÷ standard deviation, with the average and the standard deviation taken over the same number of recent bars. A z-score of −2 means the price is two standard deviations below its recent average. Mean reversion rules usually enter when z passes a threshold such as ±2 and exit when z returns toward 0.

What does 均值回歸 mean?

均值回歸 is the Chinese term for mean reversion. It describes the tendency of a price, or a spread between two prices, to move back toward its average after moving far away from it. In trading it is the basis of strategies that buy stretched lows and sell stretched highs.

Does mean reversion work on futures?

It depends on the market, the bar size, the period and the costs, and the only way to find out is to test it. Mean reversion rules tend to do well in sideways periods and badly in strong trends. Test it after realistic costs, on nearby settings, on other markets and with walk-forward analysis before you rely on it.

What is the difference between mean reversion and trend following?

Mean reversion bets that a price far from its average will move back toward it. Trend following bets that a move away from the average will continue. The two often do well in opposite market conditions, which is why some traders test them together in one portfolio.

What lookback should a mean reversion strategy use?

There is no single correct lookback. A 20-bar window is a common starting point because it matches the default for Bollinger Bands. Test a range of nearby values, such as 10 to 40 bars, and prefer a setting whose neighbours give similar results after costs.

References

  • Sullivan, R., Timmermann, A., & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. The Journal of Finance, 54(5), 1647–1691.
  • Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.