Pine Script v5 · Strategy
Mean Reversion and Trendfollowing
A Pine Script v5 strategy built on SMA (close, 200) and RSI (close, 2). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits use a profit target and no stop loss. Each trade commits 100000 in cash and pays no commission. 5 settings; defaults include Trend Position Factor 1 and Mean Reversion Position Factor 0.5.
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 and Trendfollowing repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 15 of 15 trades.
- How did Mean Reversion and Trendfollowing do on BTCUSDT 1D with default settings?
- +14.24%
- From 26 Jan 2024 to 21 Oct 2026, over 26 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Mean Reversion and Trendfollowing run in Pine Script v6?
- Yes
- Same 26 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Mean Reversion and Trendfollowing use a stop loss?
- No
- Its exits set a profit target but no stop.
- Does Mean Reversion and Trendfollowing run in Zeal Terminal?
- Yes
- It compiles in 17 ms and runs 1,000 bars in 36 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +14.24%
- $14,235.20
- Buy and hold
- +102.5%
- same period
- Closed trades
- 26
- 17 won · 9 lost
- Profitable
- 65.4%
- of closed trades
- Profit factor
- 1.26
- gross profit ÷ gross loss
- Max drawdown
- 17.97%
- −$22,373.50
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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
strategy.exit is used without a stop, loss or trailing stop.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
4 settings have no label
In the settings dialog enableMeanReversion (line 7), enableTrendfollowing (line 8), trendPositionFactor (line 9), meanReversionPositionFactor (line 10) 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.
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 |
|---|---|---|---|
| Starting Date | timestamp("1 Feb 2000 12:00") | time | start_ts |
| Enable Mean Reversion (no label in the script) | true | bool | isTrendfollowingMode isRsiBuy isRsiClose |
| Enable Trendfollowing (no label in the script) | true | bool | isMeanReversionMode isSmaBuy isSmaClose |
| Trend Position Factor (no label in the script) | 1 | float | positionSizeFactor |
| Mean Reversion Position Factor (no label in the script) | 0.5 | float | positionSizeFactor |
What it calculates
- SMAclose, 200
sma200 · line 16 - RSIclose, 2
rsi2 · line 17
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 My Long Entry Id long | (isBuy and strategy.opentrades == 0) | On | 30 |
| Exit Exit limit | (isClose) | On | 33 |
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 | +14.2% | +102.5% | 26 | 65% | 1.26 | 18.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +2.6% | +5.5% | 8 | 75% | 1.90 | 3.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +37.6% | +9.8% | 40 | 75% | 1.45 | 28.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +51.6% | +8.4% | 13 | 77% | 13.60 | 4.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −16.4% | +16.7% | 36 | 58% | 0.76 | 31.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +43.2% | +21.5% | 11 | 64% | 9.85 | 7.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +11.5% | +148.3% | 30 | 67% | 1.23 | 19.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +28.0% | +17.1% | 14 | 71% | 8.27 | 5.7% | 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 | 1 | 0% | −$11,974.70 |
| Sideways market | 12 | 67% | $23,284.70 |
| Falling market | 13 | 69% | $2,925.20 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
30.2%
The 95th percentile of the worst drawdown when the same 26 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 14.2% to 30.2%; the tick is what actually happened, 13.3%. Order luck alone moves it this much.
All 26 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 16 Aug 2024 57,541.10 | 22 Aug 2024 61,156.00 | 6 | $3,141.22 |
| long | 24 Aug 2024 64,037.20 | 28 Aug 2024 59,415.00 | 4 | −$7,444.78 |
| long | 29 Aug 2024 59,034.90 | 10 Sep 2024 57,042.00 | 12 | −$1,615.25 |
| long | 25 Sep 2024 64,262.70 | 11 Mar 2025 78,595.90 | 167 | $20,983.90 |
| long | 13 Mar 2025 83,680.10 | 7 Apr 2025 78,430.00 | 25 | −$7,219.22 |
| long | 9 Apr 2025 76,322.40 | 13 Apr 2025 85,276.90 | 4 | $6,326.48 |
| long | 23 Apr 2025 93,443.00 | 5 Nov 2025 101,497.00 | 196 | $9,840.99 |
| long | 13 Nov 2025 101,654.00 | 27 Nov 2025 90,484.00 | 14 | −$6,813.64 |
| long | 2 Dec 2025 86,286.00 | 4 Dec 2025 93,429.90 | 2 | $4,851.70 |
| long | 13 Dec 2025 90,268.40 | 22 Dec 2025 88,658.90 | 9 | −$1,088.14 |
| long | 24 Dec 2025 87,486.00 | 29 Dec 2025 87,952.70 | 5 | $322.65 |
| long | 10 Jan 2026 90,641.30 | 14 Jan 2026 95,414.00 | 4 | $3,193.16 |
| long | 19 Jan 2026 93,673.10 | 15 Feb 2026 69,822.90 | 27 | −$15,846.90 |
| long | 18 Feb 2026 67,503.50 | 5 Mar 2026 72,666.80 | 15 | $4,154.56 |
| long | 8 Mar 2026 67,262.90 | 11 Mar 2026 69,948.60 | 3 | $2,251.72 |
| long | 19 Mar 2026 71,246.60 | 1 Apr 2026 68,284.50 | 13 | −$2,391.35 |
| long | 13 Apr 2026 70,741.60 | 17 Apr 2026 75,154.30 | 4 | $3,513.39 |
| long | 20 Apr 2026 73,801.80 | 23 Apr 2026 78,178.20 | 3 | $3,444.15 |
| long | 29 Apr 2026 76,342.80 | 2 May 2026 78,231.10 | 3 | $1,479.22 |
| long | 14 May 2026 79,313.60 | 12 Jun 2026 63,626.00 | 29 | −$11,974.70 |
| long | 19 Jun 2026 62,958.00 | 3 Jul 2026 61,560.00 | 14 | −$1,211.41 |
| long | 9 Jul 2026 62,290.00 | 11 Jul 2026 64,161.70 | 2 | $1,621.07 |
| long | 14 Jul 2026 62,334.50 | 21 Jul 2026 65,255.50 | 7 | $2,566.02 |
| long | 25 Jul 2026 64,140.00 | 31 Jul 2026 64,780.00 | 6 | $559.23 |
| long | 1 Aug 2026 62,887.90 | 5 Aug 2026 64,106.60 | 4 | $1,091.44 |
| long | 11 Aug 2026 63,970.00 | 18 Aug 2026 64,532.10 | 7 | $499.69 |
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 and Trendfollowing this page | — | v5 strategy | +14.2% | 26 | 18.0% |
| SRJ RSI Outperformer Strategy by Sapt_Jash | rsi sma | v5 strategy | +0.6% | 8 | 2.4% |
| RSI SMA Crossover Strategy by reees | rsi sma | v5 strategy | +39.8% | 42 | 30.8% |
| Short In Downtrend Below MA100 (Coinrule) by Coinrule | sma rsi | v4 strategy | −34.5% | 99 | 45.4% |
| Simple RSI and SMA Long and Short (by Coinrule) by Coinrule | rsi sma | v5 strategy | −3.0% | 2 | 17.7% |
| Maximized Moving Average Crossing (by Coinrule) by Coinrule | sma rsi | v4 strategy | −6.6% | 9 | 13.3% |
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.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
Yes
Same 26 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © I11L-//@version=5+//@version=6strategy("Mean Reversion and Trendfollowing", overlay=true, pyramiding=1, default_qty_value=100000, initial_capital=100000, default_qty_type=strategy.cash, process_orders_on_close=false, calc_on_every_tick=false)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 17 ms | Yes 1,000 bars in 36 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/// © I11L//@version=5strategy("Mean Reversion and Trendfollowing", overlay=true, pyramiding=1, default_qty_value=100000, initial_capital=100000, default_qty_type=strategy.cash, process_orders_on_close=false, calc_on_every_tick=false)// Input for the starting datestart_date = input.time(timestamp("1 Feb 2000 12:00"), title="Starting Date")enableMeanReversion = input.bool(true)enableTrendfollowing = input.bool(true)trendPositionFactor = input.float(1)meanReversionPositionFactor = input.float(0.5)// Convert the input string to a timestampstart_ts = timestamp(year(start_date), month(start_date), dayofmonth(start_date), 0, 0)// Check if the current bar's time is greater than or equal to the start timestampstart_condition = time >= start_tsvar tradeOrigin = ""sma200 = ta.sma(close,200)rsi2 = ta.rsi(close,2)isMeanReversionMode = close < sma200 or not(enableTrendfollowing)isTrendfollowingMode = close > sma200 or not(enableMeanReversion)isRsiBuy = rsi2 < 20 and enableMeanReversionisRsiClose = rsi2 > 80 and enableMeanReversionisSmaBuy = close > sma200 and enableTrendfollowingisSmaClose = close < sma200 * 0.95 and enableTrendfollowingisBuy = (isMeanReversionMode and isRsiBuy) or (isTrendfollowingMode and isSmaBuy)positionSizeFactor = isSmaBuy ? trendPositionFactor : meanReversionPositionFactor// Only execute the strategy after the starting dateif (start_condition) if (isBuy and strategy.opentrades == 0) tradeOrigin := isSmaBuy ? "SMA" : "RSI" strategy.entry("My Long Entry Id", strategy.long, qty=(strategy.equity / close) * positionSizeFactor, comment=str.tostring(positionSizeFactor)) isClose = tradeOrigin == "SMA" ? isSmaClose : isRsiClose if (isClose) strategy.exit("Exit", limit = close)plot(sma200)plot(sma200 * 0.95, color=color.orange)