Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (2)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,001,380$2,025,273Equity from $1,000,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
+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.

SettingDefaultType and rangeFeeds
Move Limit (no label in the script)70intisBigMoveDown isBigMoveUp
MA Length (no label in the script)200intma

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.

closecloseprice datamaLengthMA Lengthsetting · 200highhighprice datalowlowprice dataopenopenprice datamoveLimitMove Limitsetting · 70maSMA→ maisLongBuyisLongBuyconditionisLongExitisLongExitconditionisShortExitisShortExitconditiondownBar, isThreeDown, isThreeUpdownBar · isThreeDown +13 conditionsisBigMoveDown, isBigMoveUpisBigMoveDown · isBigMo…2 conditionsisShortBuyisShortBuyconditionentry:Entry LongOpen Entry Longstrategy.entryclose:Entry LongClose Entry Longstrategy.closeentry:Entry ShortOpen Entry Shortstrategy.entryclose:Entry ShortClose Entry Shortstrategy.closeplot:22plotplot

Order rules

OrderFires whenBy defaultLine
Open Entry Long longisLongBuyisThreeDown and isBigMoveDownOn18
Close Entry LongisLongExitclose > high[1]On19
Open Entry Short shortclose < ma and isShortBuyOn20
Close Entry ShortisShortExitclose < low[1]On21

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.

8/8trades 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+0.1%+102.5%1771%1.062.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.1%+5.5%1872%0.840.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.0%+9.8%1759%0.900.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.0%+8.4%967%0.840.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.0%+16.7%2446%0.820.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%2060%1.200.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.0%+148.3%1867%1.840.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%1953%1.170.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 barsTradesWonP&L
Rising market4100%$4,892.03
Sideways market540%−$9,318.90
Falling market875%$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.

0%2%
All 17 trades
SideEntryExitBarsP&L
long17 Mar 2024 65,300.6025 Mar 2024 67,210.008$1,909.36
long5 Jul 2024 57,050.007 Jul 2024 58,230.102$1,180.11
long2 Sep 2024 57,301.803 Sep 2024 59,132.101$1,830.35
long10 Mar 2025 80,734.5013 Mar 2025 83,680.103$2,945.64
short22 Apr 2025 87,516.2028 Apr 2025 93,749.306−$6,233.07
long2 Aug 2025 113,298.004 Aug 2025 114,209.002$910.89
long26 Aug 2025 110,112.003 Sep 2025 111,240.008$1,128.03
long15 Nov 2025 94,594.0024 Nov 2025 86,830.009−$7,764.00
long15 Dec 2025 88,172.2022 Dec 2025 88,658.907$486.71
short5 Jan 2026 91,529.7019 Jan 2026 93,673.1014−$2,143.40
long19 Jan 2026 93,673.1028 Jan 2026 89,250.009−$4,423.15
long6 Feb 2026 62,909.9014 Feb 2026 68,854.008$5,944.10
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$3,332.03
short17 Mar 2026 74,884.7019 Mar 2026 71,246.602$3,638.12
long3 Jun 2026 66,760.808 Jun 2026 63,332.005−$3,428.83
long11 Sep 2026 76,568.7015 Sep 2026 78,189.204$1,620.47
long8 Oct 2026 83,321.8012 Oct 2026 83,773.104$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.

ScriptSharesTypeNetTradesMax drawdown
Mean reversion this page—v5 strategy+0.1%172.1%
Follow the Crypto Shorts by xtradernetsmav4 strategyNo trades on this market
buy down and sell high trend 200 sma and ma cross by willi130503smav4 strategyNo trades on this market
Mayer Multiple Strategy by xtradernetsmav4 strategyNo trades on this market
Moving Average Cross Strategy early Closing by Geduldtradersmav4 strategy+73.8%20151.5%
MFI Simple Strategy by OztheWozsmav5 strategy−0.2%283.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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 11 msYes 1,000 bars in 30 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/// © 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)