Pine Script v5 · Strategy
sma RSI & sudden buy and sell Strategy v1
A Pine Script v5 strategy built on EMA (price, 20), RSI (price, 14) and Highest (price, 20). It has 14 order rules, 7 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. 12 settings; defaults include Length 14, Inst length 10 and Bars 10.
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 sma RSI & sudden buy and sell Strategy v1 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 sma RSI & sudden buy and sell Strategy v1 do on BTCUSDT 1D with default settings?
- +7.63%
- From 26 Jan 2024 to 21 Oct 2026, over 15 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does sma RSI & sudden buy and sell Strategy v1 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: 15 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 sma RSI & sudden buy and sell Strategy v1 use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does sma RSI & sudden buy and sell Strategy v1 run in Zeal Terminal?
- Yes
- It compiles in 44 ms and runs 1,000 bars in 50 ms, in the browser.
BTCUSDT · 1D · default settings
- EMA
- EMA
- EMA
- EMA
- EMA
- EMA
- Highest
- Lowest
- Crossover
- Crossover
- Crossover
- Crossover
- Crossover
- Crossover
- SMA
- Crossover
- Net profit
- +7.63%
- $76,272.30
- Buy and hold
- +102.5%
- same period
- Closed trades
- 15
- 7 won · 8 lost
- Profitable
- 46.7%
- of closed trades
- Profit factor
- 2.60
- gross profit ÷ gross loss
- Max drawdown
- 3.71%
- −$37,083.00
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A short 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
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
12 settings have no label
In the settings dialog length (line 6), inst_length (line 7), bars (line 12), lookbackno2 (line 13), input_ema20 (line 17), input_ema50 (line 19), input_ema100 (line 21), input_ema200 (line 23), input_ema400 (line 25), input_ema800 (line 27), over40 (line 118), over60 (line 119) 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Length (no label in the script) | 14 | int | vrsi buy_diff_rsi sell_diff_rsi sma |
| Inst length (no label in the script) | 10 | int | line 94 line 98 |
| Bars (no label in the script) | 10 | int | line 102 line 110 |
| Lookbackno2 (no label in the script) | 20 | int | hi2 lo2 |
| Input ema20 (no label in the script) | 20 | int | ema20 |
| Input ema50 (no label in the script) | 50 | int | ema50 |
| Input ema100 (no label in the script) | 100 | int | ema100 |
| Input ema200 (no label in the script) | 200 | int | ema200 |
| Input ema400 (no label in the script) | 400 | int | ema400 |
| Input ema800 (no label in the script) | 800 | int | ema800 |
| Over40 (no label in the script) | 40 | int | cuu |
| Over60 (no label in the script) | 60 | int | coo |
What it calculates
- EMAprice, Input ema20 (20)
ema20 · line 18 - EMAprice, Input ema50 (50)
ema50 · line 20 - EMAprice, Input ema100 (100)
ema100 · line 22 - EMAprice, Input ema200 (200)
ema200 · line 24 - EMAprice, Input ema400 (400)
ema400 · line 26 - EMAprice, Input ema800 (800)
ema800 · line 28 - RSIprice, Length (14)
vrsi · line 29 - Highestprice, Lookbackno2 (20)
hi2 · line 30 - Lowestprice, Lookbackno2 (20)
lo2 · line 31 - RSIclose[1], Length (14)
buy_diff_rsi · line 32 - RSIclose[1], Length (14)
sell_diff_rsi · line 33 - Crossoverprice, ema800
co_800 · line 51 - Crossoverprice, ema400
co_400 · line 52 - Crossoverprice, ema200
co_200 · line 53 - Crossoverprice, ema100
co_100 · line 54 - Crossoverprice, ema50
co_50 · line 55 - Crossoverprice, ema20
co_20 · line 56 - SMAvrsi, Length (14)
sma · line 120 - Crossoversma, Over60 (60)
coo · line 121 - Crossundersma, Over40 (40)
cuu · line 122
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 Sell | (co_200) | On | 75 |
| Open Buy long | (co_200) | On | 76 |
| Close Buy | (vrsi > 90) | On | 83 |
| Open Sell short | (vrsi > 90) | On | 84 |
| Close Sell | (vrsi < 10) | On | 86 |
| Open Buy long | (vrsi < 10) | On | 87 |
| Close Sell | (sudBC < price) | On | 104 |
| Open Buy long | (sudBC < price) | On | 105 |
| Close Buy | (sudSC > price) | On | 112 |
| Open Sell short | (sudSC > price) | On | 113 |
| Close Sell | (coo) | On | 124 |
| Open Buy long | (coo) | On | 125 |
| Close Buy | (cuu) | On | 127 |
| Open Sell short | (cuu) | On | 128 |
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 | +7.6% | +102.5% | 15 | 47% | 2.60 | 3.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.5% | +5.5% | 13 | 46% | 1.90 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.6% | +9.8% | 9 | 78% | 6.82 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 15 | 27% | 0.74 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 19 | 32% | 0.23 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 13 | 38% | 1.13 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 19 | 42% | 1.93 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 12 | 33% | 0.93 | 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 | 4 | 50% | $11,432.80 |
| Sideways market | 10 | 40% | $44,058.50 |
| Falling market | 1 | 100% | $20,781.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.5%
The 95th percentile of the worst drawdown when the same 15 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.4% to 3.5%; the tick is what actually happened, 3.5%. Order luck alone moves it this much.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 14 May 2024 62,940.10 | 26 Jun 2024 61,806.00 | 43 | −$1,134.08 |
| short | 26 Jun 2024 61,806.00 | 28 Jul 2024 67,896.50 | 32 | −$6,090.48 |
| long | 28 Jul 2024 67,896.50 | 13 Aug 2024 59,346.60 | 16 | −$8,549.85 |
| short | 13 Aug 2024 59,346.60 | 29 Sep 2024 65,858.00 | 47 | −$6,511.36 |
| long | 29 Sep 2024 65,858.00 | 11 Oct 2024 60,326.40 | 12 | −$5,531.60 |
| short | 11 Oct 2024 60,326.40 | 23 Oct 2024 67,426.00 | 12 | −$7,099.61 |
| long | 23 Oct 2024 67,426.00 | 20 Dec 2024 97,461.90 | 58 | $30,035.90 |
| short | 20 Dec 2024 97,461.90 | 20 Apr 2025 85,077.00 | 121 | $12,384.90 |
| long | 20 Apr 2025 85,077.00 | 3 Jun 2025 105,858.00 | 44 | $20,781.00 |
| short | 3 Jun 2025 105,858.00 | 4 Jul 2025 109,585.00 | 31 | −$3,726.77 |
| long | 4 Jul 2025 109,585.00 | 25 Aug 2025 113,494.00 | 52 | $3,908.82 |
| short | 25 Aug 2025 113,494.00 | 21 Apr 2026 75,841.00 | 239 | $37,652.60 |
| long | 21 Apr 2026 75,841.00 | 3 Jun 2026 66,760.80 | 43 | −$9,080.13 |
| short | 3 Jun 2026 66,760.80 | 18 Jun 2026 64,509.40 | 15 | $2,251.44 |
| long | 18 Jun 2026 64,509.40 | 18 Oct 2026 81,491.00 | 122 | $16,981.60 |
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 |
|---|---|---|---|---|---|
| sma RSI & sudden buy and sell Strategy v1 this page | — | v5 strategy | +7.6% | 15 | 3.7% |
| MZ SRSI Strategy V1.0 by MightyZinger | ema sma highest lowest rsi | v4 strategy | No trades on this market | ||
| STRATEGY R18-F-BTC by SenatorVonShaft | ema highest lowest rsi | v4 strategy | −8.6% | 11 | 14.4% |
| Forex bot full strategy with risk management by exlux99 | lowest highest ema sma | v4 strategy | −26.4% | 11 | 38.9% |
| Stochastic RSI by Zeal Terminal | rsi highest lowest sma | v5 indicator | indicator | ||
| Bear & Bull Zone Signal Strategy by Solutions1978 | rsi ema highest lowest sma | v4 strategy | No trades on this market | ||
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.
- 5 settings use the untyped input() (line 6, 7, 12, 118, 119). 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.
Same 15 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 12: input() → input.int()
- Line 118: input() → input.int()
- Line 119: input() → input.int()
⋯price = close-length = input( 14 )+length = input.int( 14 )-inst_length = input( 10 )+inst_length = input.int( 10 )var rbc = 0⋯var float rsiSP = 0.0-bars = input(10)+bars = input.int(10)lookbackno2 = input.int(20)⋯ sudS := 0-over40 = input( 40 )+over40 = input.int( 40 )-over60 = input( 60 )+over60 = input.int( 60 )sma =ta.sma(vrsi, length)⋯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: 15 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.
⋯// © samwillington-//@version=5+//@version=6strategy("sma RSI & sudden buy and sell Strategy v1", overlay=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 44 ms | Yes 1,000 bars in 50 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/// © samwillington//@version=5strategy("sma RSI & sudden buy and sell Strategy v1", overlay=true)price = closelength = input( 14 )inst_length = input( 10 )var rbc = 0var float rsiBP = 0.0var rsc = 0var float rsiSP = 0.0bars = input(10)lookbackno2 = input.int(20)rsi_buy = 0rsi_sell = 0//EMA inputsinput_ema20 = input.int(20)ema20 = ta.ema(price, input_ema20)input_ema50 = input.int(50)ema50 = ta.ema(price, input_ema50)input_ema100 = input.int(100)ema100 = ta.ema(price, input_ema100)input_ema200 = input.int(200)ema200 = ta.ema(price, input_ema200)input_ema400 = input.int(400)ema400 = ta.ema(price, input_ema400)input_ema800 = input.int(800)ema800 = ta.ema(price, input_ema800)vrsi = ta.rsi(price, length)hi2 = ta.highest(price, lookbackno2)lo2 = ta.lowest(price, lookbackno2)buy_diff_rsi = vrsi - ta.rsi(close[1], length)sell_diff_rsi = ta.rsi(close[1],length) - vrsi//RSI high lowvar int sudS = 0var int sudB = 0var float sudSO = 0.0var float sudSC = 0.0var float sudBO = 0.0var float sudBC = 0.0var sudBuy = 0var sudSell = 0 var countB = 0var countS = 0var co_800 = falsevar co_400 = falsevar co_200 = falsevar co_100 = falsevar co_50 = falsevar co_20 = falseco_800 := ta.crossover(price , ema800)co_400 := ta.crossover(price , ema400)co_200 := ta.crossover(price , ema200)co_100 := ta.crossover(price , ema100)co_50 := ta.crossover(price , ema50)co_20 := ta.crossover(price , ema20)if(ta.crossunder(price , ema20)) co_20 := falseif(ta.crossunder(price , ema50)) co_50 := falseif(ta.crossunder(price , ema100)) co_100 := falseif(ta.crossunder(price , ema200)) co_200 := falseif(ta.crossunder(price , ema400)) co_400 := falseif(ta.crossunder(price , ema800)) co_800 := false if((price> ema800) and (price > ema400)) if(co_20) if(co_50) if(co_100) if(co_200) strategy.close("Sell") strategy.entry("Buy", strategy.long, comment="spl Buy") co_20 := false co_50 := false co_100 := false co_200 := false// too much rsiif(vrsi > 90) strategy.close("Buy") strategy.entry("Sell", strategy.short, comment="RSI too overbuy")if(vrsi < 10) strategy.close("Sell") strategy.entry("Buy", strategy.long, comment="RSI too oversold")var sudbcount = 0 // counting no. of bars till sudden risevar sudscount = 0 // counting no. of bars till sudden decreaseif(sudB == 1) sudbcount := sudbcount + 1if(sudS == 1) sudscount := sudscount + 1if((buy_diff_rsi > inst_length) and (hi2 > price)) sudB := 1 sudBO := open sudBC := closeif((sell_diff_rsi > inst_length) ) sudS := 1 sudSO := open sudSC := close if(sudbcount == bars) if(sudBC < price) strategy.close("Sell") strategy.entry("Buy", strategy.long, comment="sudd buy") sudbcount := 0 sudB := 0 sudbcount := 0 sudB := 0if(sudscount == bars) if(sudSC > price) strategy.close("Buy") strategy.entry("Sell", strategy.short, comment="sudd sell") sudscount := 0 sudS := 0 sudscount := 0 sudS := 0over40 = input( 40 )over60 = input( 60 )sma =ta.sma(vrsi, length)coo = ta.crossover(sma, over60)cuu = ta.crossunder(sma, over40)if (coo) strategy.close("Sell") strategy.entry("Buy", strategy.long, comment="modified buy")if (cuu) strategy.close("Buy") strategy.entry("Sell", strategy.short, comment="modefied sell")//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)