Pine Script v5 · Strategy
Gap Reversion Strategy
A Pine Script v5 strategy built on RSI (close, 2) and SMA (vix, 350). It has 5 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss. Each trade commits 100% of equity and pays 1 commission. 15 settings; defaults include RSI-length 2, RSI Source close and Sustain Volatility Filter For X 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 Gap Reversion Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Gap Reversion Strategy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Does Gap Reversion Strategy run in Pine Script v6?
- Not tested
- Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download.
- Does Gap Reversion Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 39.
- Does Gap Reversion Strategy run in Zeal Terminal?
- Yes
- It compiles in 17 ms and runs 1,000 bars in 39 ms, in the browser.
BTCUSDT · 1D · default settings
- SMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs CBOE:VIX 60 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Values that depend on it are empty on this chart.
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
Uses an argument v6 removed
when = on line 38, 40, 41. 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 |
|---|---|---|---|
| Backtest Date Range | |||
| Start Time | timestamp('01 Jan 2010 06:00 +0000') | time | InDateRange |
| End Time | timestamp('01 Jan 2099 00:00 +0000') | time | InDateRange |
| RSI Settings | |||
| RSI-length | 2 | int | RSI |
| RSI Source | close | source | RSI |
| Volatility Filter Settings | |||
| Sustain Volatility Filter For X Bars | 5 | int | VolatilityWarning |
| Variance Threshold | 500 | int | VolatilityWarning |
| Use VIX Variance Filter?This will prevent new signals from entering trades when the VIX is unusually high | false | bool | ReversionFilter |
| Trade Entry/Exit Settings | |||
| RSI Long QualifierThe selected RSI / reversion indicator value must be below this input after a gap down to place a trade | 20 | float · step 5 | LongEntryLimit entry Long |
| RSI Indicator Exit ValueThe strategy will exit the long position when the Reversion Indicator crosses over this value | 70 | float · step 5 | close Long |
| Stop % | 99 | float · step 0.25 | PercStop |
| Limit Entry Price Settings | |||
| Minimum Gap % of Price | .25 | float · min 0 · step .1 | GapDown PercLimit ATRLimit |
| Price % Entry Limit (close-gap-%)When the strategy issues a long buy signal, a limit order is generated at this % below the prior day's close price minus Gap % input | 1 | float · min 0 · step .1 | Limit |
| ATR Long Entry Limit (close-gap-%ofATR)When the strategy issues a long buy signal, a limit order is generated at this % of ATR below the prior day's close price minus Gap % input | 20 | float · min 0 · step 5 | Limit |
| Use % of ATR entry limit instead of % of price entry limit? | false | bool | Limit |
| ATR Length | 5 | int | Limit |
What it calculates
- RSIRSI Source (close), RSI-length (2)
RSI · line 12 - SMAvix, 350
mean · 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 |
|---|---|---|---|
| Open Long long | ReversionRule==true and ReversionFilter | On | 38 |
| Exit Exit stop | (InDateRange) | On | 39 |
| cancel_all | GapDown==false | On | 40 |
| Close Long | LongExit | On | 41 |
| Close | (not InDateRange) | On | 43 |
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.
- ClearHigher-timeframe data1 request.security call(s), none with lookahead on.
- 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
Not run on other markets: the script requests CBOE:VIX 60, and that data is not available here (a lower timeframe than the chart; the engine does not evaluate intrabar data). Its results would rest on empty values.
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 |
|---|---|---|---|---|---|
| Gap Reversion Strategy this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| Bull Trend Filtered StochRSI (BTFS) by SnarkyPuppy | highest rsi sma | v5 strategy | +36.9% | 21 | 21.5% |
| 2nd Grade Strategy by cyrule | sma rsi highest | v4 strategy | No trades on this market | ||
| Vix FIX - StochRSI Strategy by timj | sma rsi highest | v5 strategy | +1.0% | 21 | 10.7% |
| Stochastic RSI by Zeal Terminal | rsi highest sma | v5 indicator | indicator | ||
| SRJ RSI Outperformer Strategy by Sapt_Jash | rsi sma | v5 strategy | +0.6% | 8 | 2.4% |
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.
- 3 strategy calls use the when = argument (line 38, 40, 41). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 38: when = ReversionRule==true and ReversionFilter → if block
- Line 40: when = GapDown==false → if block
- Line 41: when = LongExit → if block
⋯if (InDateRange)- strategy.entry("Long", strategy.long, limit=LongEntryLimit, when = ReversionRule==true and ReversionFilter, alert_message="Buy")+ if ReversionRule==true and ReversionFilter+ strategy.entry("Long", strategy.long, limit=LongEntryLimit, alert_message="Buy") strategy.exit("Exit", "Long", stop=PercStop, alert_message="Sell")- strategy.cancel_all(when = GapDown==false) + if GapDown==false+ strategy.cancel_all() - strategy.close("Long", when = LongExit, alert_message="Sell")+ if LongExit+ strategy.close("Long", alert_message="Sell")if (not InDateRange)⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-// @version=5+// @version=6// Author = TradeAutomation⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 17 ms | Yes 1,000 bars in 39 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/// @version=5// Author = TradeAutomationstrategy(title="Gap Reversion Strategy", shorttitle="Gap Reversion Strategy", process_orders_on_close=false, overlay=true, pyramiding=0, commission_type=strategy.commission.cash_per_order, commission_value=1, slippage = 1, margin_long = 75, initial_capital = 1000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)// Backtest Date Range Inputs // StartTime = input.time(defval=timestamp('01 Jan 2010 06:00 +0000'), group="Backtest Date Range", title='Start Time')EndTime = input.time(defval=timestamp('01 Jan 2099 00:00 +0000'), group="Backtest Date Range", title='End Time')InDateRange = time>=StartTime and time<=EndTime// Wilder's RSI //lenrsi=input.int(2, title="RSI-length", group="RSI Settings")src = input.source(close, title="RSI Source", group="RSI Settings")RSI = ta.rsi(src, lenrsi)// VIX Volatility Warning Indicator & Filter //vix = request.security("CBOE:VIX", "60", high)mean = ta.sma(vix, 350)SquaredDifference = math.pow(vix-mean, 2)VolWarningLength = input.int(5, "Sustain Volatility Filter For X Bars", group="Volatility Filter Settings")SquaredDifferenceThreshold = input.int(500, "Variance Threshold", group="Volatility Filter Settings")VolatilityWarning = ta.highest(SquaredDifference, VolWarningLength)<SquaredDifferenceThresholdEarlyWarningInput = input.bool(false, "Use VIX Variance Filter?", group="Volatility Filter Settings", tooltip="This will prevent new signals from entering trades when the VIX is unusually high")ReversionFilter = EarlyWarningInput == true ? VolatilityWarning : close>0// Long Entry Rules //ReversionRule = RSI<input.float(20, "RSI Long Qualifier", tooltip="The selected RSI / reversion indicator value must be below this input after a gap down to place a trade", group="Trade Entry/Exit Settings", step=5)GapPerc = (1-(input.float(.25, "Minimum Gap % of Price", step=.1, group="Limit Entry Price Settings", minval=0)*.01))GapDown = open<close[1]*GapPercvar float LongEntryLimit=naPercLimit = (close*GapPerc)*(1-input.float(1, "Price % Entry Limit (close-gap-%)", tooltip="When the strategy issues a long buy signal, a limit order is generated at this % below the prior day's close price minus Gap % input", step=.1, minval=0, group="Limit Entry Price Settings")*.01)ATRLimit = (close*GapPerc)-(ta.atr(input.int(5, "ATR Length"))*input.float(20, "ATR Long Entry Limit (close-gap-%ofATR)", tooltip="When the strategy issues a long buy signal, a limit order is generated at this % of ATR below the prior day's close price minus Gap % input",step=5, minval=0, group="Limit Entry Price Settings")*.01)LimitInput = input.bool(false, "Use % of ATR entry limit instead of % of price entry limit?", group="Limit Entry Price Settings")Limit = LimitInput == true ? ATRLimit : PercLimitLongEntryLimit := ReversionRule==true ? Limit : naLongExit = RSI>input.float(70, "RSI Indicator Exit Value", tooltip="The strategy will exit the long position when the Reversion Indicator crosses over this value", group="Trade Entry/Exit Settings", step=5)// Stop //StopPercInput = 1-(input.float(99, "Stop %", step=0.25, group="Trade Entry/Exit Settings")*.01)PercStop = strategy.position_avg_price*StopPercInput// Entry and Exit Functions //if (InDateRange) strategy.entry("Long", strategy.long, limit=LongEntryLimit, when = ReversionRule==true and ReversionFilter, alert_message="Buy") strategy.exit("Exit", "Long", stop=PercStop, alert_message="Sell") strategy.cancel_all(when = GapDown==false) strategy.close("Long", when = LongExit, alert_message="Sell")if (not InDateRange) strategy.close_all()