Pine Script v4 · Strategy
Optimised RSI strategy for Reversals (by Coinrule)
A Pine Script v4 strategy built on SMA (close, 9) and RSI (close, 14). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays no commission. 11 settings; defaults include From Month 1, From Day 1 and From Year 2020.
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 Optimised RSI strategy for Reversals (by Coinrule) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 5 of 5 trades.
- How did Optimised RSI strategy for Reversals (by Coinrule) do on BTCUSDT 1D with default settings?
- +1.92%
- From 26 Jan 2024 to 21 Oct 2026, over 7 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Optimised RSI strategy for Reversals (by Coinrule) 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 Optimised RSI strategy for Reversals (by Coinrule) use a stop loss?
- Yes
- strategy.exit sets a stop on line 32.
- Does Optimised RSI strategy for Reversals (by Coinrule) run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 37 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.92%
- $19.17
- Buy and hold
- +102.5%
- same period
- Closed trades
- 7
- 3 won · 4 lost
- Profitable
- 42.9%
- of closed trades
- Profit factor
- 1.27
- gross profit ÷ gross loss
- Max drawdown
- 4.19%
- −$43.80
One run with the author's default settings, $1,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 commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
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 23. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 33
transp = on line 33. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 23
long = on line 23. v5 writes it as direction = strategy.long or strategy.short.
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, rsi → ta.rsi.
- 1 strategy call uses the when = argument (line 23). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 7 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.integer) → input.int()
- Line 10: input(type = input.integer) → input.int()
8 more edits
- Line 11: input(type = input.integer) → input.int()
- Line 12: input(type = input.bool) → input.bool()
- Line 18: sma → ta.sma
- Line 21: rsi → ta.rsi
- Line 23: long = true → direction = strategy.long
- Line 25: input(type = input.float) → input.float()
- Line 27: input(type = input.float) → input.float()
- Line 33: transp = 90 → color.new(showDate and window() ? color.gray : na, 90)
⋯// © Coinrule-//@version=4+//@version=5strategy(shorttitle='MARSI',title='Moving Average', overlay=true, initial_capital=1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)//Backtest dates-fromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)+fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-fromDay = input(defval = 1, title = "From Day", type = input.integer, minval = 1, maxval = 31)+fromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-fromYear = input(defval = 2020, title = "From Year", type = input.integer, minval = 1970)+fromYear = input.int(defval = 2020, title = "From Year", minval = 1970)-thruMonth = input(defval = 1, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)+thruMonth = input.int(defval = 1, title = "Thru Month", minval = 1, maxval = 12)-thruDay = input(defval = 1, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)+thruDay = input.int(defval = 1, title = "Thru Day", minval = 1, maxval = 31)-thruYear = input(defval = 2112, title = "Thru Year", type = input.integer, minval = 1970)+thruYear = input.int(defval = 2112, title = "Thru Year", minval = 1970)-showDate = input(defval = true, title = "Show Date Range", type = input.bool)+showDate = input.bool(defval = true, title = "Show Date Range")start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start window⋯inshort=input(9, title='MA short period')-MAshort= sma(close, inshort)+MAshort= ta.sma(close, inshort)// RSI inputs and calculationslengthRSI = input(14, title = 'RSI period', minval=1)-RSI = rsi(close, lengthRSI)+RSI = ta.rsi(close, lengthRSI)//Entry -strategy.entry(id="long", long = true, when = MAshort<close and RSI<40 and window())+strategy.entry(id="long", direction = strategy.long, when = MAshort<close and RSI<40 and window())//Exit-longLossPerc = input(title="Long Stop Loss (%)",- type=input.float, minval=0.0, step=0.1, defval=1.5) * 0.01+longLossPerc = input.float(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=1.5) * 0.01-longTakePerc = input(title="Long Take Profit (%)",- type=input.float, minval=0.0, step=0.1, defval=3) * 0.01+longTakePerc = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=3) * 0.01longSL = strategy.position_avg_price * (1 - longLossPerc)⋯ strategy.exit(id="TP/SL", stop=longSL, limit=longTP)-bgcolor(color = showDate and window() ? color.gray : na, transp = 90) +bgcolor(color = color.new(showDate and window() ? color.gray : na, 90)) plot(MAshort, color=color.purple, linewidth=4)Same 7 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 17: input() → input.int()
- Line 20: input() → input.int()
- Line 23: when = MAshort<close and RSI<40 and window() → if block
⋯//MA inputs and calculations-inshort=input(9, title='MA short period')+inshort=input.int(9, title='MA short period')MAshort= ta.sma(close, inshort)// RSI inputs and calculations-lengthRSI = input(14, title = 'RSI period', minval=1)+lengthRSI = input.int(14, title = 'RSI period', minval=1)RSI = ta.rsi(close, lengthRSI)//Entry -strategy.entry(id="long", direction = strategy.long, when = MAshort<close and RSI<40 and window())+if MAshort<close and RSI<40 and window()+ strategy.entry(id="long", direction = strategy.long)//Exit⋯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: 7 before, 7 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.
⋯// © Coinrule-//@version=5+//@version=6strategy(shorttitle='MARSI',title='Moving Average', overlay=true, initial_capital=1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)⋯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 |
|---|---|---|---|
| From Month | 1 | int (written input.integer) · min 1 · max 12 | start |
| From Day | 1 | int (written input.integer) · min 1 · max 31 | start |
| From Year | 2020 | int (written input.integer) · min 1970 | start |
| Thru Month | 1 | int (written input.integer) · min 1 · max 12 | finish |
| Thru Day | 1 | int (written input.integer) · min 1 · max 31 | finish |
| Thru Year | 2112 | int (written input.integer) · min 1970 | finish |
| Show Date Range | true | bool | bgcolor |
| MA short period | 9 | int | MAshort |
| RSI period | 14 | int · min 1 | RSI |
| Long Stop Loss (%) | 1.5 | float · min 0.0 · step 0.1 | longSL |
| Long Take Profit (%) | 3 | float · min 0.0 · step 0.1 | longTP |
What it calculates
- SMA (
sma,ta.smain v5)close, MA short period (9)MAshort · line 18 - RSI (
rsi,ta.rsiin v5)close, RSI period (14)RSI · line 21
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 | MAshort<close and RSI<40 and window() | On | 23 |
| Exit TP/SL stop and limit | (strategy.position_size > 0 and window()) | On | 32 |
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 | +1.9% | +102.5% | 7 | 43% | 1.27 | 4.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.6% | +5.5% | 4 | 25% | 0.65 | 4.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +8.9% | +9.8% | 11 | 45% | 1.90 | 7.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.1% | +8.4% | 6 | 33% | 0.98 | 4.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +1.6% | +16.7% | 7 | 43% | 1.18 | 8.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +11.5% | +21.5% | 8 | 63% | 3.38 | 3.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −4.3% | +148.3% | 5 | 20% | 0.39 | 10.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +7.6% | +17.1% | 4 | 75% | 5.66 | 1.5% | 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 | 0 | — | $0.00 |
| Sideways market | 3 | 67% | $35.30 |
| Falling market | 4 | 25% | −$16.13 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
6.7%
The 95th percentile of the worst drawdown when the same 7 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.0% to 6.7%; the tick is what actually happened, 3.0%. Order luck alone moves it this much.
All 7 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 13 Jul 2024 57,889.10 | 14 Jul 2024 59,625.80 | 1 | $30.00 |
| long | 27 Nov 2025 90,484.00 | 1 Dec 2025 89,126.70 | 4 | −$15.45 |
| long | 14 Feb 2026 68,854.00 | 15 Feb 2026 70,919.60 | 1 | $30.44 |
| long | 16 Feb 2026 68,832.60 | 17 Feb 2026 67,800.10 | 1 | −$15.67 |
| long | 21 Feb 2026 68,020.00 | 23 Feb 2026 66,999.70 | 2 | −$15.44 |
| long | 12 Jun 2026 63,626.00 | 14 Jun 2026 65,534.80 | 2 | $30.42 |
| long | 18 Jun 2026 64,509.40 | 19 Jun 2026 62,958.00 | 1 | −$25.11 |
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 |
|---|---|---|---|---|---|
| Optimised RSI strategy for Reversals (by Coinrule) this page | — | v4 strategy | +1.9% | 7 | 4.2% |
| 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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | With a workaround 1,000 bars in 37 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 23) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Coinrule//@version=4strategy(shorttitle='MARSI',title='Moving Average', overlay=true, initial_capital=1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)//Backtest datesfromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)fromDay = input(defval = 1, title = "From Day", type = input.integer, minval = 1, maxval = 31)fromYear = input(defval = 2020, title = "From Year", type = input.integer, minval = 1970)thruMonth = input(defval = 1, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)thruDay = input(defval = 1, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)thruYear = input(defval = 2112, title = "Thru Year", type = input.integer, minval = 1970)showDate = input(defval = true, title = "Show Date Range", type = input.bool)start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start windowfinish = timestamp(thruYear, thruMonth, thruDay, 23, 59) // backtest finish windowwindow() => time >= start and time <= finish ? true : false // create function "within window of time"//MA inputs and calculationsinshort=input(9, title='MA short period')MAshort= sma(close, inshort)// RSI inputs and calculationslengthRSI = input(14, title = 'RSI period', minval=1)RSI = rsi(close, lengthRSI)//Entry strategy.entry(id="long", long = true, when = MAshort<close and RSI<40 and window())//ExitlongLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1.5) * 0.01longTakePerc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01longSL = strategy.position_avg_price * (1 - longLossPerc)longTP = strategy.position_avg_price * (1 + longTakePerc)if (strategy.position_size > 0 and window()) strategy.exit(id="TP/SL", stop=longSL, limit=longTP)bgcolor(color = showDate and window() ? color.gray : na, transp = 90) plot(MAshort, color=color.purple, linewidth=4)