Pine Script v5 · Strategy
Mean reversion
A Pine Script v5 strategy built on SMA (close, 200). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 2 settings; defaults include Move Limit 70 and MA Length 200.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Mean reversion repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 8 of 8 trades.
- How did Mean reversion do on BTCUSDT 1D with default settings?
- +0.14%
- From 26 Jan 2024 to 21 Oct 2026, over 17 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Mean reversion run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 17 before, 15 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Mean reversion use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Mean reversion run in Zeal Terminal?
- Yes
- It compiles in 11 ms and runs 1,000 bars in 30 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.14%
- $1,384.67
- Buy and hold
- +102.5%
- same period
- Closed trades
- 17
- 12 won · 5 lost
- Profitable
- 70.6%
- of closed trades
- Profit factor
- 1.06
- gross profit ÷ gross loss
- Max drawdown
- 2.10%
- −$21,213.30
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
2 settings have no label
In the settings dialog moveLimit (line 6), maLength (line 7) show an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 18, 19, 20, 21. The upgraded version below moves each call inside an if block.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Move Limit (no label in the script) | 70 | int | isBigMoveDown isBigMoveUp |
| MA Length (no label in the script) | 200 | int | ma |
What it calculates
- SMAclose, MA Length (200)
ma · line 8
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Entry Long long | isLongBuyisThreeDown and isBigMoveDown | On | 18 |
| Close Entry Long | isLongExitclose > high[1] | On | 19 |
| Open Entry Short short | close < ma and isShortBuy | On | 20 |
| Close Entry Short | isShortExitclose < low[1] | On | 21 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +0.1% | +102.5% | 17 | 71% | 1.06 | 2.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.1% | +5.5% | 18 | 72% | 0.84 | 0.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.0% | +9.8% | 17 | 59% | 0.90 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 9 | 67% | 0.84 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 24 | 46% | 0.82 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 20 | 60% | 1.20 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 18 | 67% | 1.84 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 19 | 53% | 1.17 | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 4 | 100% | $4,892.03 |
| Sideways market | 5 | 40% | −$9,318.90 |
| Falling market | 8 | 75% | $5,811.54 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
1.9%
The 95th percentile of the worst drawdown when the same 17 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.8% to 1.9%; the tick is what actually happened, 1.8%. Order luck alone moves it this much.
All 17 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 17 Mar 2024 65,300.60 | 25 Mar 2024 67,210.00 | 8 | $1,909.36 |
| long | 5 Jul 2024 57,050.00 | 7 Jul 2024 58,230.10 | 2 | $1,180.11 |
| long | 2 Sep 2024 57,301.80 | 3 Sep 2024 59,132.10 | 1 | $1,830.35 |
| long | 10 Mar 2025 80,734.50 | 13 Mar 2025 83,680.10 | 3 | $2,945.64 |
| short | 22 Apr 2025 87,516.20 | 28 Apr 2025 93,749.30 | 6 | −$6,233.07 |
| long | 2 Aug 2025 113,298.00 | 4 Aug 2025 114,209.00 | 2 | $910.89 |
| long | 26 Aug 2025 110,112.00 | 3 Sep 2025 111,240.00 | 8 | $1,128.03 |
| long | 15 Nov 2025 94,594.00 | 24 Nov 2025 86,830.00 | 9 | −$7,764.00 |
| long | 15 Dec 2025 88,172.20 | 22 Dec 2025 88,658.90 | 7 | $486.71 |
| short | 5 Jan 2026 91,529.70 | 19 Jan 2026 93,673.10 | 14 | −$2,143.40 |
| long | 19 Jan 2026 93,673.10 | 28 Jan 2026 89,250.00 | 9 | −$4,423.15 |
| long | 6 Feb 2026 62,909.90 | 14 Feb 2026 68,854.00 | 8 | $5,944.10 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $3,332.03 |
| short | 17 Mar 2026 74,884.70 | 19 Mar 2026 71,246.60 | 2 | $3,638.12 |
| long | 3 Jun 2026 66,760.80 | 8 Jun 2026 63,332.00 | 5 | −$3,428.83 |
| long | 11 Sep 2026 76,568.70 | 15 Sep 2026 78,189.20 | 4 | $1,620.47 |
| long | 8 Oct 2026 83,321.80 | 12 Oct 2026 83,773.10 | 4 | $451.31 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Mean reversion this page | — | v5 strategy | +0.1% | 17 | 2.1% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 4 strategy calls use the when = argument (line 18, 19, 20, 21). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 2 settings use the untyped input() (line 6, 7). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 17 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 18: when = isLongBuy → if block
- Line 19: when = isLongExit → if block
- Line 20: when = close < ma and isShortBuy → if block
- Line 21: when = isShortExit → if block
⋯strategy(title="Mean reversion", shorttitle="MeanRev", precision=16 , overlay=true)-moveLimit = input(70)+moveLimit = input.int(70)-maLength = input(200)+maLength = input.int(200)ma = ta.sma(close, maLength)⋯isShortExit = close < low[1]-strategy.entry("Entry Long", strategy.long, when=isLongBuy)+if isLongBuy+ strategy.entry("Entry Long", strategy.long)-strategy.close("Entry Long", when=isLongExit)+if isLongExit+ strategy.close("Entry Long")-strategy.entry("Entry Short", strategy.short, when=close < ma and isShortBuy)+if close < ma and isShortBuy+ strategy.entry("Entry Short", strategy.short)-strategy.close("Entry Short", when=isShortExit)+if isShortExit+ strategy.close("Entry Short")plot(ma, color=color.gray)Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 17 before, 15 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// Based on strategy by hackertrader (original idea by QuantpT)-//@version=5+//@version=6strategy(title="Mean reversion", shorttitle="MeanRev", precision=16 , overlay=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 11 ms | Yes 1,000 bars in 30 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © stormis// Based on strategy by hackertrader (original idea by QuantpT)//@version=5strategy(title="Mean reversion", shorttitle="MeanRev", precision=16 , overlay=true)moveLimit = input(70)maLength = input(200)ma = ta.sma(close, maLength)downBar = open > closeisThreeDown = downBar and downBar[1] and downBar[2]isThreeUp = not downBar and not downBar[1] and not downBar[2]isBigMoveDown = ((open - close) / (0.001 + high - low)) > moveLimit / 100.0isBigMoveUp = ((close - open) / (0.001 + high - low)) > moveLimit / 100.0isLongBuy = isThreeDown and isBigMoveDownisLongExit = close > high[1]isShortBuy = isThreeUp and isBigMoveUpisShortExit = close < low[1]strategy.entry("Entry Long", strategy.long, when=isLongBuy)strategy.close("Entry Long", when=isLongExit)strategy.entry("Entry Short", strategy.short, when=close < ma and isShortBuy)strategy.close("Entry Short", when=isShortExit)plot(ma, color=color.gray)