Pine Script v4 · Strategy
REVERSALS
A Pine Script v4 strategy built on SMA (close, 200). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 2 settings; defaults include Stop Loss .95 and How Many Bars 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 REVERSALS repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did REVERSALS do on BTCUSDT 1D with default settings?
- +3.27%
- From 26 Jan 2024 to 21 Oct 2026, over 29 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can REVERSALS be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does REVERSALS use a stop loss?
- Yes
- strategy.exit sets a stop on line 23.
- Does REVERSALS run in Zeal Terminal?
- Yes
- It compiles in 6 ms and runs 1,000 bars in 38 ms, in the browser.
BTCUSDT · 1D · default settings
- MarketFilter
- StopLossLine
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +3.27%
- $3,273.31
- Buy and hold
- +102.5%
- same period
- Closed trades
- 29
- 16 won · 13 lost
- Profitable
- 55.2%
- of closed trades
- Profit factor
- 1.06
- gross profit ÷ gross loss
- Max drawdown
- 18.34%
- −$21,583.30
One run with the author's default settings, $100,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.
-
Likely bug
sma(close, 200) ignores the settings line 15
Line 15 hard-codes 200 where line 12 uses “How Many Bars”. Changing those settings moves one band but not this one.
sma(close, 200)sma(close, HowManyBars) -
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 StopLoss (line 9), HowManyBars (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.
-
Note
Uses an argument v6 removed
when = on line 19. The upgraded version below moves each call inside an if block.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames sma → ta.sma.
- 1 strategy call uses the when = argument (line 19). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 29 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 12: sma → ta.sma
- Line 15: sma → ta.sma
- Line 16: sma → ta.sma
- Line 19: strategy.entry(…, true) → strategy.entry(…, strategy.long)
-// @version=4+// @version=5// © HermanBrummer⋯/// EXITS-if close > sma(high,HowManyBars)[1]+if close > ta.sma(high,HowManyBars)[1] strategy.close_all()/// ENTRIES-MarketFilter = sma(close, 200)+MarketFilter = ta.sma(close, 200)-F1 = close < sma(low,HowManyBars)[1]+F1 = close < ta.sma(low,HowManyBars)[1]F2 = close > MarketFilterplot(MarketFilter, "MarketFilter", color.yellow)-strategy.entry("Long", true, 1, when=F1 and F2)+strategy.entry("Long", strategy.long, 1, when=F1 and F2)/// STOP LOSS⋯Same 29 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 9: input() → input.float()
- Line 10: input() → input.int()
- Line 19: when = F1 and F2 → if block
⋯strategy("REVERSALS", overlay=true)-StopLoss = input(.95, step=0.01)+StopLoss = input.float(.95, step=0.01)-HowManyBars = input( 5 )+HowManyBars = input.int( 5 )/// EXITS⋯plot(MarketFilter, "MarketFilter", color.yellow)-strategy.entry("Long", strategy.long, 1, when=F1 and F2)+if F1 and F2+ strategy.entry("Long", strategy.long, 1)/// STOP LOSS⋯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: 29 before, 28 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.
-// @version=5+// @version=6// © HermanBrummer⋯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 |
|---|---|---|---|
| Stop Loss (no label in the script) | .95 | float · step 0.01 | StopLossLine |
| How Many Bars (no label in the script) | 5 | int | F1 close_all line 12 |
What it calculates
- SMA (
sma,ta.smain v5)close, 200MarketFilter · line 15
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 |
|---|---|---|---|
| Close | close > sma(high,HowManyBars)[1] | On | 13 |
| Open Long long | F1 and F2 | On | 19 |
| Exit Exit stop | every bar | On | 23 |
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 | +3.3% | +102.5% | 29 | 55% | 1.06 | 18.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +9.7% | +5.5% | 33 | 61% | 2.20 | 3.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.5% | +9.8% | 27 | 44% | 0.83 | 1.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.3% | +8.4% | 32 | 69% | 1.83 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 37 | 32% | 0.64 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 30 | 57% | 0.98 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.2% | +148.3% | 35 | 49% | 1.30 | 0.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 26 | 62% | 1.86 | 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 | 18 | 67% | $15,979.70 |
| Sideways market | 11 | 36% | −$12,706.40 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
26.9%
The 95th percentile of the worst drawdown when the same 29 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 12.4% to 26.9%; the tick is what actually happened, 17.3%. Order luck alone moves it this much.
All 29 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Oct 2024 66,668.60 | 29 Oct 2024 69,962.20 | 5 | $3,293.56 |
| long | 3 Nov 2024 69,374.70 | 7 Nov 2024 75,572.00 | 4 | $6,197.25 |
| long | 26 Nov 2024 93,010.00 | 30 Nov 2024 97,460.00 | 4 | $4,449.99 |
| long | 19 Dec 2024 100,204.00 | 20 Dec 2024 95,193.80 | 1 | −$5,010.21 |
| long | 21 Dec 2024 97,805.40 | 23 Dec 2024 92,915.20 | 2 | −$4,890.28 |
| long | 24 Dec 2024 94,881.50 | 26 Dec 2024 99,429.60 | 2 | $4,548.14 |
| long | 28 Dec 2024 94,299.00 | 3 Jan 2025 96,984.80 | 6 | $2,685.76 |
| long | 9 Jan 2025 95,060.60 | 13 Jan 2025 90,307.60 | 4 | −$4,753.04 |
| long | 28 Jan 2025 102,083.00 | 31 Jan 2025 104,723.00 | 3 | $2,640.11 |
| long | 2 Feb 2025 100,636.00 | 3 Feb 2025 95,603.90 | 1 | −$5,031.79 |
| long | 5 Feb 2025 97,763.10 | 21 Feb 2025 98,305.00 | 16 | $541.87 |
| long | 25 Feb 2025 91,552.90 | 26 Feb 2025 86,975.20 | 1 | −$4,577.65 |
| long | 27 Feb 2025 84,250.10 | 28 Feb 2025 80,037.60 | 1 | −$4,212.51 |
| long | 1 Mar 2025 84,349.90 | 3 Mar 2025 94,270.00 | 2 | $9,920.04 |
| long | 5 May 2025 94,277.60 | 7 May 2025 96,834.00 | 2 | $2,556.41 |
| long | 30 May 2025 105,590.00 | 10 Jun 2025 110,263.00 | 11 | $4,673.27 |
| long | 13 Jun 2025 105,672.00 | 22 Jun 2025 100,388.00 | 9 | −$5,283.59 |
| long | 23 Jun 2025 100,964.00 | 24 Jun 2025 105,334.00 | 1 | $4,370.07 |
| long | 2 Jul 2025 105,681.00 | 3 Jul 2025 108,850.00 | 1 | $3,168.46 |
| long | 1 Aug 2025 115,764.00 | 8 Aug 2025 117,472.00 | 7 | $1,707.95 |
| long | 16 Aug 2025 117,342.00 | 23 Aug 2025 116,936.00 | 7 | −$406.04 |
| long | 26 Aug 2025 110,112.00 | 3 Sep 2025 111,240.00 | 8 | $1,128.03 |
| long | 23 Sep 2025 112,651.00 | 29 Sep 2025 112,164.00 | 6 | −$487.03 |
| long | 10 Oct 2025 121,662.00 | 11 Oct 2025 112,774.00 | 1 | −$8,887.92 |
| long | 12 Oct 2025 110,644.00 | 17 Oct 2025 105,112.00 | 5 | −$5,532.22 |
| long | 30 Oct 2025 110,021.00 | 4 Nov 2025 104,520.00 | 5 | −$5,501.07 |
| long | 2 Sep 2026 77,439.00 | 4 Sep 2026 81,270.40 | 2 | $3,831.36 |
| long | 9 Sep 2026 78,455.80 | 19 Sep 2026 80,883.90 | 10 | $2,428.06 |
| long | 8 Oct 2026 83,321.80 | 21 Oct 2026 83,028.10 | 13 | −$293.67 |
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 |
|---|---|---|---|---|---|
| REVERSALS this page | — | v4 strategy | +3.3% | 29 | 18.3% |
| 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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 6 ms | With a workaround 1,000 bars in 38 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 19) so the engine trades the way the script says.
“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. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// @version=4// © HermanBrummer// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// BUYS WHEN THE CLOSE IS SMALLER THAN THE LOW OF 5 DAYS AGO// SELLS WHEN THE CLOSE IS HIGHER THEN THE HIGH OF 5 DAYS AGO// USES A 200 MOVING AVERGE AS A FILTER, AND DOESN'T TAKE TRADES IF THE MARKET IS BELOW IT'S 200 MA// USES A 5% STOP LOSS FROM ENTRIESstrategy("REVERSALS", overlay=true)StopLoss = input(.95, step=0.01)HowManyBars = input( 5 )/// EXITSif close > sma(high,HowManyBars)[1] strategy.close_all()/// ENTRIESMarketFilter = sma(close, 200)F1 = close < sma(low,HowManyBars)[1]F2 = close > MarketFilterplot(MarketFilter, "MarketFilter", color.yellow)strategy.entry("Long", true, 1, when=F1 and F2)/// STOP LOSSStopLossLine = strategy.position_avg_price * StopLossplot(StopLossLine, "StopLossLine", #FF0000)strategy.exit("Exit", stop=StopLossLine)