SPY daily data, 2016 to 2026: the 20-day MA, 3% envelope rule, long and short, lost 34.4% after costs over 86 trades, winning 46.5% of them, against +277.8% for holding SPY. Long only it made 16.7%. The best of 5 runs was 20-day MA, 3% envelope on SPY, long only: +16.7%, held-out survived. Every run, every trade and the script are on this page.
Answers here: Does mean reversion work on the index? · 3% or 2% envelope? · What is the win rate? · Long only, or both ways?
SPY daily data, 4 January 2016 to 23 October 2026, long and short: 86 trades, lost 34.4% after costs, 46.5% of trades won, profit factor 0.68, worst drawdown 40.4%, 20% of the time in the market. Holding SPY returned +277.8% over the same period.
Held out on its own, the last third (16 March 2023 to 23 October 2026) lost 2.6% on 22 trades: it did not survive its held-out period.
Long only, the same rule made 16.7% on 44 trades (59.1% won, profit factor 1.41, held-out survived): the short side cost 51.0 points of return in a period when SPY rose 277.8%.
Across all 5 runs on this page, 2 made money after costs, 2 survived the held-out third, and 0 beat holding the symbol.
The best run was 20-day MA, 3% envelope on SPY, long only: +16.7% net on 44 trades, profit factor 1.41, worst drawdown 20.5%, held-out survived. The tester's verdict on it reads "made money, but could be luck".
MA Envelope Reversion Strategy, from the strategy library.
A moving-average envelope: goes long when the close crosses back above a band 3% below the 20-day average and goes short when it crosses back below a band 3% above it; the trade closes when the close crosses the average itself. A stop sits 2 ATR away. A tighter 2% band was run on SPY, and long-only versions on SPY and QQQ. The RSI and Bollinger pages test the other two common mean-reversion rules.
The indicator itself, with its Pine code drawn on real market data: Mean Reversion.
Net and holding the symbol are after costs over the same period. Held-out is the verdict on the last third of the history judged alone: survived, decayed, did not survive, or too few trades to judge.
| Symbol | Timeframe | Settings | Direction | Trades | Win rate | Net | Holding | Profit factor | Max drawdown | Held-out |
|---|---|---|---|---|---|---|---|---|---|---|
| SPY | daily | 20-day MA, 3% envelope | long and short | 86 | 46.5% | -34.4% | +277.8% | 0.68 | 40.4% | did not survive |
| SPY | daily | 20-day MA, 2% envelope | long and short | 149 | 51.7% | -32.0% | +277.8% | 0.78 | 44.8% | did not survive |
| QQQ | daily | 20-day MA, 3% envelope | long and short | 147 | 51.0% | -57.8% | +542.3% | 0.67 | 61.3% | did not survive |
| SPY | daily | 20-day MA, 3% envelope | long only | 44 | 59.1% | +16.7% | +277.8% | 1.41 | 20.5% | survived |
| QQQ | daily | 20-day MA, 3% envelope | long only | 60 | 65.0% | +2.9% | +542.3% | 1.08 | 33.3% | survived |
20-day MA, 3% envelope on SPY, long and short, the same rule long only, and SPY itself, each calendar year after costs. A partial first or last year counts the sessions it has.
| Year | 20-day MA, 3% envelope, long and short | 20-day MA, 3% envelope, long only | Holding SPY |
|---|---|---|---|
| 2016 | -2.0% | +2.1% | +11.2% |
| 2017 | 0.0% | 0.0% | +18.5% |
| 2018 | +5.3% | +2.8% | -6.9% |
| 2019 | -8.2% | -0.2% | +28.6% |
| 2020 | -8.5% | +13.4% | +15.1% |
| 2021 | -2.9% | +1.7% | +28.8% |
| 2022 | -19.5% | -17.1% | -19.9% |
| 2023 | -1.8% | +9.0% | +24.8% |
| 2024 | +6.1% | +3.5% | +24.0% |
| 2025 | -1.3% | +4.5% | +16.6% |
| 2026 | -6.9% | -2.3% | +11.2% |
The 86 trades of 20-day MA, 3% envelope on SPY, long and short, oldest first: side, entry and exit days, fill prices after slippage, and the return after both commissions. The other runs' trades open below.
| Side | In | Out | Entry | Exit | Return |
|---|---|---|---|---|---|
| short | 2016-03-09 | 2016-04-08 | 199.30 | 205.40 | -3.2% |
| long | 2016-06-29 | 2016-07-01 | 204.88 | 209.30 | +2.1% |
| short | 2016-07-14 | 2016-08-26 | 216.34 | 217.98 | -0.9% |
| short | 2018-01-19 | 2018-01-23 | 279.72 | 282.83 | -1.2% |
| short | 2018-01-26 | 2018-02-05 | 284.19 | 273.55 | +3.6% |
| long | 2018-02-15 | 2018-02-26 | 271.67 | 275.85 | +1.4% |
| long | 2018-03-27 | 2018-04-13 | 266.25 | 267.33 | +0.3% |
| long | 2018-10-17 | 2018-10-23 | 280.52 | 270.86 | -3.5% |
| long | 2018-11-01 | 2018-11-07 | 271.70 | 277.48 | +2.0% |
| long | 2018-11-27 | 2018-11-29 | 266.42 | 273.63 | +2.6% |
| long | 2019-01-02 | 2019-01-08 | 246.05 | 256.75 | +4.2% |
| short | 2019-01-25 | 2019-02-05 | 265.53 | 272.71 | -2.8% |
| short | 2019-02-08 | 2019-02-19 | 268.67 | 277.65 | -3.4% |
| long | 2019-05-15 | 2019-05-31 | 281.67 | 276.12 | -2.1% |
| long | 2019-06-05 | 2019-06-06 | 282.41 | 283.21 | +0.2% |
| short | 2019-06-24 | 2019-07-12 | 294.15 | 300.02 | -2.1% |
| long | 2019-08-09 | 2019-08-14 | 292.66 | 285.80 | -2.4% |
| long | 2019-08-19 | 2019-08-22 | 292.26 | 293.15 | +0.2% |
| long | 2019-10-04 | 2019-10-14 | 291.22 | 295.84 | +1.5% |
| long | 2020-03-27 | 2020-04-07 | 253.36 | 274.13 | +8.1% |
| short | 2020-04-22 | 2020-04-29 | 278.27 | 294.35 | -5.9% |
| short | 2020-05-04 | 2020-05-14 | 280.66 | 279.02 | +0.5% |
| short | 2020-05-20 | 2020-06-03 | 295.74 | 310.32 | -5.0% |
| short | 2020-06-12 | 2020-06-16 | 308.15 | 315.56 | -2.5% |
| long | 2020-06-30 | 2020-07-02 | 304.07 | 314.14 | +3.2% |
| short | 2020-07-17 | 2020-08-06 | 321.76 | 333.47 | -3.7% |
| short | 2020-08-10 | 2020-08-26 | 334.97 | 345.26 | -3.2% |
| short | 2020-09-01 | 2020-09-02 | 350.11 | 356.06 | -1.8% |
| short | 2020-09-04 | 2020-09-09 | 346.03 | 337.64 | +2.3% |
| long | 2020-09-28 | 2020-10-02 | 333.31 | 331.61 | -0.6% |
| short | 2020-10-19 | 2020-10-27 | 348.52 | 339.84 | +2.4% |
| long | 2020-11-04 | 2020-11-05 | 340.95 | 349.14 | +2.3% |
| short | 2020-11-23 | 2020-12-17 | 357.18 | 372.26 | -4.3% |
| short | 2021-04-07 | 2021-05-12 | 405.83 | 411.34 | -1.5% |
| long | 2021-09-23 | 2021-10-01 | 439.96 | 428.53 | -2.7% |
| long | 2021-10-04 | 2021-10-15 | 433.12 | 444.64 | +2.6% |
| short | 2021-10-28 | 2021-11-05 | 455.34 | 469.41 | -3.2% |
| short | 2021-11-10 | 2021-11-29 | 465.45 | 464.20 | +0.2% |
| long | 2021-12-03 | 2021-12-08 | 459.29 | 468.57 | +1.9% |
| long | 2022-02-01 | 2022-02-03 | 450.84 | 450.83 | -0.1% |
| long | 2022-02-28 | 2022-03-08 | 432.14 | 418.47 | -3.3% |
| long | 2022-03-10 | 2022-03-17 | 422.64 | 433.47 | +2.5% |
| short | 2022-03-24 | 2022-03-29 | 445.81 | 461.44 | -3.6% |
| short | 2022-04-01 | 2022-04-12 | 453.20 | 443.20 | +2.1% |
| long | 2022-04-29 | 2022-05-02 | 423.71 | 410.72 | -3.2% |
| long | 2022-05-05 | 2022-05-05 | 424.64 | 410.01 | -3.5% |
| long | 2022-05-18 | 2022-05-19 | 403.61 | 387.82 | -4.0% |
| long | 2022-05-24 | 2022-05-27 | 392.67 | 407.82 | +3.8% |
| short | 2022-06-01 | 2022-06-10 | 415.04 | 394.99 | +4.7% |
| long | 2022-06-27 | 2022-07-08 | 391.16 | 387.16 | -1.1% |
| short | 2022-07-25 | 2022-07-29 | 395.64 | 411.09 | -4.0% |
| short | 2022-08-10 | 2022-08-11 | 418.68 | 424.63 | -1.5% |
| short | 2022-08-22 | 2022-08-23 | 416.94 | 413.01 | +0.8% |
| long | 2022-09-09 | 2022-09-13 | 402.84 | 401.71 | -0.4% |
| long | 2022-09-15 | 2022-09-22 | 393.07 | 373.55 | -5.1% |
| long | 2022-10-05 | 2022-10-10 | 373.49 | 360.52 | -3.6% |
| long | 2022-10-14 | 2022-10-19 | 368.63 | 368.88 | 0.0% |
| short | 2022-10-28 | 2022-11-04 | 379.77 | 377.10 | +0.6% |
| short | 2022-11-18 | 2022-12-01 | 397.62 | 409.35 | -3.0% |
| short | 2022-12-06 | 2022-12-07 | 399.31 | 393.01 | +1.5% |
| long | 2022-12-22 | 2023-01-09 | 383.16 | 390.26 | +1.8% |
| short | 2023-01-19 | 2023-02-01 | 389.24 | 412.81 | -6.2% |
| short | 2023-02-07 | 2023-02-21 | 408.76 | 403.17 | +1.3% |
| long | 2023-03-15 | 2023-03-22 | 385.98 | 398.62 | +3.2% |
| short | 2023-04-06 | 2023-04-26 | 406.66 | 406.83 | -0.1% |
| short | 2023-06-21 | 2023-07-12 | 436.07 | 446.51 | -2.5% |
| long | 2023-09-26 | 2023-09-27 | 429.21 | 423.28 | -1.5% |
| long | 2023-09-29 | 2023-10-11 | 431.81 | 435.53 | +0.8% |
| long | 2023-10-31 | 2023-11-03 | 416.29 | 432.99 | +3.9% |
| short | 2023-11-29 | 2023-12-12 | 457.03 | 462.98 | -1.4% |
| short | 2023-12-18 | 2023-12-26 | 470.86 | 476.57 | -1.3% |
| long | 2024-04-23 | 2024-04-30 | 501.93 | 508.42 | +1.2% |
| short | 2024-05-21 | 2024-05-31 | 529.14 | 523.72 | +0.9% |
| long | 2024-08-09 | 2024-08-14 | 529.95 | 542.72 | +2.3% |
| short | 2024-08-23 | 2024-09-06 | 559.38 | 550.08 | +1.6% |
| long | 2025-03-06 | 2025-03-07 | 575.63 | 565.51 | -1.9% |
| long | 2025-03-18 | 2025-03-25 | 564.95 | 575.15 | +1.7% |
| long | 2025-04-10 | 2025-04-25 | 532.38 | 546.50 | +2.6% |
| short | 2025-05-22 | 2025-06-10 | 582.50 | 603.24 | -3.7% |
| short | 2025-07-08 | 2025-07-23 | 621.18 | 633.35 | -2.1% |
| long | 2025-11-24 | 2025-11-26 | 662.87 | 677.42 | +2.1% |
| long | 2026-03-24 | 2026-03-27 | 651.49 | 635.38 | -2.6% |
| long | 2026-04-01 | 2026-04-07 | 654.05 | 656.47 | +0.3% |
| short | 2026-04-30 | 2026-05-06 | 714.44 | 733.79 | -2.8% |
| short | 2026-05-13 | 2026-05-28 | 738.26 | 754.73 | -2.3% |
| short | 2026-08-06 | 2026-08-25 | 770.01 | 766.37 | +0.4% |
Pine v6, exactly as it ran for 20-day MA, 3% envelope on SPY, long and short. The variants change only the inputs listed in the table.
//@version=6
strategy("MA Envelope Reversion Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05, slippage=1, margin_long=0, margin_short=0)
src = input.source(close, "Source", group="Setup")
len = input.int(20, "MA length", minval=1, group="Setup")
pct = input.float(3, "Envelope %", minval=0.1, step=0.1, group="Setup")
useLong = input.bool(true, "Trade longs", group="Direction")
useShort = input.bool(true, "Trade shorts", group="Direction")
atrLen = input.int(14, "ATR length", minval=1, group="Risk")
stopMult = input.float(2, "Stop (ATR multiples, 0 = off)", minval=0, step=0.5, group="Risk")
basis = ta.sma(src, len)
upper = basis * (1 + pct / 100)
lower = basis * (1 - pct / 100)
atr = ta.atr(atrLen)
longSig = ta.crossover(close, lower) and barstate.isconfirmed
shortSig = ta.crossunder(close, upper) and barstate.isconfirmed
exitLong = ta.crossover(close, basis) and barstate.isconfirmed
exitShort = ta.crossunder(close, basis) and barstate.isconfirmed
pU = plot(upper, "Upper envelope", color=color.new(#E5484D, 20), linewidth=1)
pL = plot(lower, "Lower envelope", color=color.new(#16A56A, 20), linewidth=1)
plot(basis, "Basis", color=color.new(#A3A39E, 0), linewidth=1)
fill(pU, pL, color=color.new(#A3A39E, 94), title="Envelope fill")
if longSig and useLong
strategy.entry("Long", strategy.long)
strategy.exit("Long stop", "Long", stop=stopMult > 0 ? close - stopMult * atr : na)
if shortSig and useShort
strategy.entry("Short", strategy.short)
strategy.exit("Short stop", "Short", stop=stopMult > 0 ? close + stopMult * atr : na)
if exitLong
strategy.close("Long")
if exitShort
strategy.close("Short")
Alpaca daily data since 2016, 0.05% commission and 0.025% slippage on every fill, the last third of the history held out, textbook settings and nothing tuned. The engine, fills and what it does not model are written out once, in data and methodology.
Describe the rule in your own words or paste the script. The Free plan writes the Pine v6 script with the AI, runs it on any US stock or ETF with the same costs and the same held-out check, and shows every trade on the chart. No card.
Seventeen popular rules through the same engine, the same costs and the same held-out check, with every trade listed. Next: Bollinger Bands strategy, RSI strategy, Stochastic strategy.
SPY, daily data, 2016 to 2026, long and short: 20-day MA, 3% envelope lost 34.4% after costs over 86 trades (46.5% won, profit factor 0.68) against +277.8% for holding SPY, and the held-out third did not survive. Long only it made 16.7%. Across all 5 runs on the page, 2 made money after costs, 2 survived their held-out third and 0 beat holding the stock.
From 46.5% to 65.0% across the 5 runs. The classic setting, 20-day MA, 3% envelope on SPY long and short, won 46.5% of 86 trades. The win rate alone does not decide the result: that run's profit factor was 0.68, so its losers outweighed its winners, and the net return was -34.4%.
Of the 2 settings tested, 20-day MA, 3% envelope on SPY (long only) did best: +16.7% net, profit factor 1.41, held-out survived. That is the best of 5 tries on 10.8 years of daily data of one symbol, which is already a fitted number. A setting that works on one symbol or one period only is a coincidence, not a setting; run it on your own symbol and timeframe before trusting it.
0 of the 5 runs did. SPY itself returned +277.8% between 4 January 2016 and 23 October 2026, so a rule that is out of the market part of the time has a lot to make up; what a timing rule usually gets you is a smaller drawdown, not a bigger return. The classic run's worst drawdown was 40.4% with 20% of the time in the market.
The last third of every run's history (16 March 2023 to 23 October 2026 for the lead run) was judged on its own, with no fitting on it: 2 of 5 runs survived, 0 decayed, 3 did not survive and 0 had too few trades there to judge. Survived means a positive Sharpe ratio on the held-out part at least half the fitted period's; it is a check on one later stretch, not a forecast.
With the free Alpha Charts tester's own engine: the Pine v6 scripts on this page, run on Alpaca daily data since 2016, split-adjusted and not dividend-adjusted, one position at a time with the whole account, fills at the next candle's open, a commission of 0.05% and slippage of 0.025% on every fill, the last third of the history held out. The settings are the textbook ones and every variant is stated; nothing was tuned to make the page look better.
Every number on this page is the output of the Alpha Charts backtest engine on 25 October 2026, on Alpaca market data; the scripts are the strategy library's templates, shown as they ran. Symbols are US-listed ETFs and stocks; results are price returns after the costs stated. Past results are not a forecast and nothing here is investment advice. TradingView and Pine Script belong to TradingView, Inc.; Alpha Charts is not affiliated with it.