Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (5)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$139,558$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
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.

SettingDefaultType and rangeFeeds
Starting Datetimestamp("1 Feb 2000 12:00")timestart_ts
Enable Mean Reversion (no label in the script)trueboolisTrendfollowingMode isRsiBuy isRsiClose
Enable Trendfollowing (no label in the script)trueboolisMeanReversionMode isSmaBuy isSmaClose
Trend Position Factor (no label in the script)1floatpositionSizeFactor
Mean Reversion Position Factor (no label in the script)0.5floatpositionSizeFactor

What it calculates

  • SMAclose, 200sma200 · line 16
  • RSIclose, 2rsi2 · 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.

closecloseprice datastart_dateStarting Datesetting · timestamp("1 Feb 20…trendPositionFactorTrend Position Factorsetting · 1meanReversionPositionFactorMean Reversion Position…setting · 0.5timetimeprice datasma200SMA→ sma200rsi2RSI→ rsi2start_tsstart_tsvaluetradeOrigintradeOriginvaluepositionSizeFactorpositionSizeFactorvalueisCloseisClosevalueisBuyisBuyconditionstart_conditionstart_conditionconditionisMeanReversionMode, isTrendfollowingMode, isSmaBuy, isSmaCloseisMeanReversionMode · i…4 conditionsisRsiBuy, isRsiCloseisRsiBuy · isRsiClose2 conditionsentry:My Long Entry IdOpen My Long Entry Idstrategy.entryexit:Exitexit Exitstrategy.exitplot:34plotplotplot:35plotplot

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(isBuy and strategy.opentrades == 0)On30
Exit Exit limit(isClose)On33

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.

15/15trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+14.2%+102.5%2665%1.2618.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+2.6%+5.5%875%1.903.1%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+37.6%+9.8%4075%1.4528.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+51.6%+8.4%1377%13.604.2%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−16.4%+16.7%3658%0.7631.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+43.2%+21.5%1164%9.857.5%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+11.5%+148.3%3067%1.2319.9%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+28.0%+17.1%1471%8.275.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 barsTradesWonP&L
Rising market10%−$11,974.70
Sideways market1267%$23,284.70
Falling market1369%$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.

0%35%
All 26 trades
SideEntryExitBarsP&L
long16 Aug 2024 57,541.1022 Aug 2024 61,156.006$3,141.22
long24 Aug 2024 64,037.2028 Aug 2024 59,415.004−$7,444.78
long29 Aug 2024 59,034.9010 Sep 2024 57,042.0012−$1,615.25
long25 Sep 2024 64,262.7011 Mar 2025 78,595.90167$20,983.90
long13 Mar 2025 83,680.107 Apr 2025 78,430.0025−$7,219.22
long9 Apr 2025 76,322.4013 Apr 2025 85,276.904$6,326.48
long23 Apr 2025 93,443.005 Nov 2025 101,497.00196$9,840.99
long13 Nov 2025 101,654.0027 Nov 2025 90,484.0014−$6,813.64
long2 Dec 2025 86,286.004 Dec 2025 93,429.902$4,851.70
long13 Dec 2025 90,268.4022 Dec 2025 88,658.909−$1,088.14
long24 Dec 2025 87,486.0029 Dec 2025 87,952.705$322.65
long10 Jan 2026 90,641.3014 Jan 2026 95,414.004$3,193.16
long19 Jan 2026 93,673.1015 Feb 2026 69,822.9027−$15,846.90
long18 Feb 2026 67,503.505 Mar 2026 72,666.8015$4,154.56
long8 Mar 2026 67,262.9011 Mar 2026 69,948.603$2,251.72
long19 Mar 2026 71,246.601 Apr 2026 68,284.5013−$2,391.35
long13 Apr 2026 70,741.6017 Apr 2026 75,154.304$3,513.39
long20 Apr 2026 73,801.8023 Apr 2026 78,178.203$3,444.15
long29 Apr 2026 76,342.802 May 2026 78,231.103$1,479.22
long14 May 2026 79,313.6012 Jun 2026 63,626.0029−$11,974.70
long19 Jun 2026 62,958.003 Jul 2026 61,560.0014−$1,211.41
long9 Jul 2026 62,290.0011 Jul 2026 64,161.702$1,621.07
long14 Jul 2026 62,334.5021 Jul 2026 65,255.507$2,566.02
long25 Jul 2026 64,140.0031 Jul 2026 64,780.006$559.23
long1 Aug 2026 62,887.905 Aug 2026 64,106.604$1,091.44
long11 Aug 2026 63,970.0018 Aug 2026 64,532.107$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.

ScriptSharesTypeNetTradesMax drawdown
Mean Reversion and Trendfollowing this page—v5 strategy+14.2%2618.0%
SRJ RSI Outperformer Strategy by Sapt_Jashrsi smav5 strategy+0.6%82.4%
RSI SMA Crossover Strategy by reeesrsi smav5 strategy+39.8%4230.8%
Short In Downtrend Below MA100 (Coinrule) by Coinrulesma rsiv4 strategy−34.5%9945.4%
Simple RSI and SMA Long and Short (by Coinrule) by Coinrulersi smav5 strategy−3.0%217.7%
Maximized Moving Average Crossing (by Coinrule) by Coinrulesma rsiv4 strategy−6.6%913.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

Verified

Nothing 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.

Download the v6 script

⋯// © 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 17 msYes 1,000 bars in 36 msNot measured
PineTSNot testedNot testedNot 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)