Pine Script v4 · Strategy
3 x EMA + Stochastic RSI + ATR
A Pine Script v4 strategy built on EMA (close, 8), RSI (close, 14) and SMA or Stoch (3). 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 EMA1 Length 8, EMA2 Length 14 and EMA3 Length 50.
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 3 x EMA + Stochastic RSI + ATR repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did 3 x EMA + Stochastic RSI + ATR do on BTCUSDT 1D with default settings?
- −42.33%
- From 26 Jan 2024 to 21 Oct 2026, over 18 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can 3 x EMA + Stochastic RSI + ATR be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
- Does 3 x EMA + Stochastic RSI + ATR use a stop loss?
- Yes
- strategy.exit sets a stop on line 51.
- Does 3 x EMA + Stochastic RSI + ATR run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 52 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −42.33%
- −$42,330.10
- Buy and hold
- +102.5%
- same period
- Closed trades
- 18
- 8 won · 10 lost
- Profitable
- 44.4%
- of closed trades
- Profit factor
- 0.43
- gross profit ÷ gross loss
- Max drawdown
- 44.76%
- −$44,755.10
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.
-
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 36. 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 ema → ta.ema, rsi → ta.rsi, sma → ta.sma, stoch → ta.stoch, atr → ta.atr, crossover → ta.crossover.
- 1 strategy call uses the when = argument (line 36). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
DiffersTrades differ: 18 before, 18 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 11: ema → ta.ema
- Line 12: ema → ta.ema
- Line 13: ema → ta.ema
- Line 25: rsi → ta.rsi
- Line 26: sma → ta.sma
4 more edits
- Line 26: stoch → ta.stoch
- Line 27: sma → ta.sma
- Line 33: atr → ta.atr
- Line 35: crossover → ta.crossover
⋯ -//@version=4+//@version=5strategy("3 x EMA + Stochastic RSI + ATR", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)⋯ema3Length = input(50, "EMA3 Length", minval = 1)-ema1 = ema(close, ema1Length)+ema1 = ta.ema(close, ema1Length)-ema2 = ema(close, ema2Length)+ema2 = ta.ema(close, ema2Length)-ema3 = ema(close, ema3Length)+ema3 = ta.ema(close, ema3Length) ⋯src = input(close, title="RSI Source")-rsi1 = rsi(src, lengthRSI)+rsi1 = ta.rsi(src, lengthRSI)-k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)+k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)-d = sma(k, smoothD)+d = ta.sma(k, smoothD) ⋯stopLossMultiplier= input(3.0, "Stop-loss Multiplier")-atrSeries = atr(atrPeriod)[1]+atrSeries = ta.atr(atrPeriod)[1] -longCondition = ema1 > ema2 and ema2 > ema3 and crossover(k, d)+longCondition = ema1 > ema2 and ema2 > ema3 and ta.crossover(k, d)strategy.entry("long", strategy.long, when = longCondition)⋯Clean v5
VerifiedSame 18 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 20: input() → input.int()
- Line 21: input() → input.int()
- Line 22: input() → input.int()
6 more edits
- Line 23: input() → input.int()
- Line 24: input() → input.source()
- Line 30: input() → input.int()
- Line 31: input() → input.float()
- Line 32: input() → input.float()
- Line 36: when = longCondition → if block
⋯// 3x EMA-ema1Length = input(8, "EMA1 Length", minval = 1)+ema1Length = input.int(8, "EMA1 Length", minval = 1)-ema2Length = input(14, "EMA2 Length", minval = 1)+ema2Length = input.int(14, "EMA2 Length", minval = 1)-ema3Length = input(50, "EMA3 Length", minval = 1)+ema3Length = input.int(50, "EMA3 Length", minval = 1)ema1 = ta.ema(close, ema1Length)⋯// Stochastic RSI-smoothK = input(3, "K", minval=1)+smoothK = input.int(3, "K", minval=1)-smoothD = input(3, "D", minval=1)+smoothD = input.int(3, "D", minval=1)-lengthRSI = input(14, "RSI Length", minval=1)+lengthRSI = input.int(14, "RSI Length", minval=1)-lengthStoch = input(14, "Stochastic Length", minval=1)+lengthStoch = input.int(14, "Stochastic Length", minval=1)-src = input(close, title="RSI Source")+src = input.source(close, title="RSI Source")rsi1 = ta.rsi(src, lengthRSI)⋯// ATR-atrPeriod = input(14, "ATR Period")+atrPeriod = input.int(14, "ATR Period")-takeProfitMultiplier= input(2.0, "Take-profit Multiplier")+takeProfitMultiplier= input.float(2.0, "Take-profit Multiplier")-stopLossMultiplier= input(3.0, "Stop-loss Multiplier")+stopLossMultiplier= input.float(3.0, "Stop-loss Multiplier")atrSeries = ta.atr(atrPeriod)[1]⋯longCondition = ema1 > ema2 and ema2 > ema3 and ta.crossover(k, d)-strategy.entry("long", strategy.long, when = longCondition)+if longCondition+ strategy.entry("long", strategy.long) ⋯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: 18 before, 18 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=6strategy("3 x EMA + Stochastic RSI + ATR", overlay = true, 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 |
|---|---|---|---|
| EMA1 Length | 8 | int · min 1 | ema1 |
| EMA2 Length | 14 | int · min 1 | ema2 |
| EMA3 Length | 50 | int · min 1 | ema3 |
| K | 3 | int · min 1 | k |
| D | 3 | int · min 1 | d |
| RSI Length | 14 | int · min 1 | rsi1 |
| Stochastic Length | 14 | int · min 1 | k |
| RSI Source | close | source | rsi1 |
| ATR Period | 14 | int | atrSeries |
| Take-profit Multiplier | 2.0 | float | takeProfit |
| Stop-loss Multiplier | 3.0 | float | stopLoss |
What it calculates
- EMA (
ema,ta.emain v5)close, EMA1 Length (8)ema1 · line 11 - EMA (
ema,ta.emain v5)close, EMA2 Length (14)ema2 · line 12 - EMA (
ema,ta.emain v5)close, EMA3 Length (50)ema3 · line 13 - RSI (
rsi,ta.rsiin v5)RSI Source (close), RSI Length (14)rsi1 · line 25 - SMA / Stoch (
sma,ta.smain v5)stoch(rsi1, rsi1, rsi1, lengthStoch), K (3)k · line 26 - SMA (
sma,ta.smain v5)k, D (3)d · line 27 - ATR (
atr,ta.atrin v5)ATR Period (14)atrSeries · line 33
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 | longConditionema1 > ema2 and ema2 > ema3 and crossover(k, d) | On | 36 |
| Exit take profit / stop loss stop and limit | (strategy.position_size > 0) | On | 51 |
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 | −42.3% | +102.5% | 18 | 44% | 0.43 | 44.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −2.5% | +5.5% | 17 | 53% | 0.89 | 13.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −30.3% | +9.8% | 15 | 53% | 0.64 | 41.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −2.6% | +8.4% | 14 | 57% | 0.91 | 11.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −60.2% | +16.7% | 17 | 47% | 0.41 | 65.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.6% | +21.5% | 18 | 61% | 1.01 | 20.3% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −8.4% | +148.3% | 23 | 65% | 0.93 | 44.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +3.5% | +17.1% | 20 | 65% | 1.14 | 17.1% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (EMA1 Length 7, EMA2 Length 7) made 0.0% before 26 Dec 2025 and 0.0% after it, against −1.7% for the defaults. 0% of the 64 combinations made money in-sample and 0% out-of-sample.
| EMA2 Length ↓ · EMA1 Length → | 4 | 5 | 6 | 7 | 8 | 10 | 11 | 14 |
|---|---|---|---|---|---|---|---|---|
| 7 | −36% | −36% | −36% | 0% | −26% | −31% | −31% | −31% |
| 8 | −47% | −47% | −47% | −46% | 0% | −39% | −39% | −35% |
| 10 | −47% | −46% | −41% | −41% | −41% | 0% | −39% | −2% |
| 12 | −41% | −41% | −41% | −41% | −41% | −41% | −41% | −1% |
| 14 | −41% | −41% | −41% | −41% | −41% | −41% | −41% | 0% |
| 17 | −41% | −41% | −41% | −41% | −41% | −41% | −41% | −35% |
| 20 | −41% | −41% | −41% | −41% | −41% | −35% | −35% | −39% |
| 24 | −41% | −41% | −41% | −41% | −35% | −31% | −31% | −39% |
| EMA2 Length ↓ · EMA1 Length → | 4 | 5 | 6 | 7 | 8 | 10 | 11 | 14 |
|---|---|---|---|---|---|---|---|---|
| 7 | −2% | −2% | −2% | 0% | −2% | −1% | −1% | −2% |
| 8 | −2% | −2% | −2% | −2% | 0% | −1% | −1% | −2% |
| 10 | −2% | −2% | −2% | −2% | −2% | 0% | −1% | −2% |
| 12 | −2% | −2% | −2% | −2% | −2% | −2% | −2% | −8% |
| 14 | −2% | −2% | −2% | −2% | −2% | −2% | −2% | 0% |
| 17 | −2% | −2% | −2% | −2% | −2% | −2% | −2% | −2% |
| 20 | −2% | −2% | −2% | −2% | −2% | −2% | −2% | −6% |
| 24 | −7% | −7% | −7% | −7% | −7% | −8% | −8% | −7% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 11 | 36% | −$38,360.40 |
| Sideways market | 6 | 50% | −$7,979.45 |
| Falling market | 1 | 100% | $4,009.75 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
56.0%
The 95th percentile of the worst drawdown when the same 18 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 42.3% to 56.0%; the tick is what actually happened, 42.3%. Order luck alone moves it this much.
All 18 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 19 Mar 2024 67,610.00 | 1 May 2024 56,887.00 | 43 | −$15,860.00 |
| long | 18 May 2024 67,024.00 | 24 Jun 2024 58,784.20 | 37 | −$10,344.00 |
| long | 28 Jul 2024 67,896.50 | 3 Aug 2024 60,535.40 | 6 | −$8,000.72 |
| long | 28 Sep 2024 65,769.90 | 10 Oct 2024 59,198.30 | 12 | −$6,574.17 |
| long | 21 Oct 2024 69,032.00 | 29 Oct 2024 72,877.80 | 8 | $3,299.23 |
| long | 6 Nov 2024 69,372.00 | 7 Nov 2024 75,572.00 | 1 | $5,587.62 |
| long | 22 Nov 2024 98,317.10 | 16 Dec 2024 105,438.00 | 24 | $4,932.72 |
| long | 23 Jan 2025 103,707.00 | 25 Feb 2025 90,236.90 | 33 | −$9,486.76 |
| long | 2 May 2025 96,489.90 | 8 May 2025 102,319.00 | 6 | $3,839.16 |
| long | 19 May 2025 106,454.00 | 22 May 2025 111,874.00 | 3 | $3,431.05 |
| long | 28 May 2025 108,938.00 | 22 Jun 2025 99,919.50 | 25 | −$5,863.32 |
| long | 4 Jul 2025 109,585.00 | 10 Jul 2025 114,961.00 | 6 | $3,186.85 |
| long | 28 Jul 2025 119,416.00 | 24 Aug 2025 111,054.00 | 27 | −$4,771.66 |
| long | 18 Sep 2025 116,448.00 | 25 Sep 2025 109,146.00 | 7 | −$3,974.00 |
| long | 16 Apr 2026 74,810.00 | 4 May 2026 79,859.80 | 18 | $4,009.75 |
| long | 11 May 2026 82,210.10 | 18 May 2026 76,105.60 | 7 | −$4,708.60 |
| long | 5 Sep 2026 79,660.80 | 21 Sep 2026 84,706.80 | 16 | $3,718.47 |
| long | 5 Oct 2026 86,530.00 | 15 Oct 2026 79,943.10 | 10 | −$4,751.68 |
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 |
|---|---|---|---|---|---|
| 3 x EMA + Stochastic RSI + ATR this page | — | v4 strategy | −42.3% | 18 | 44.8% |
| Atrend by OTS by prim722 | ema rsi sma atr | v4 strategy | +12.1% | 76 | 34.4% |
| EMA crossover (daily TF) by tradeswithashish | ema sma rsi atr | v5 strategy | +11.0% | 12 | 11.6% |
| Stochastic Moving Average by LucasVivien | stoch sma ema atr | v5 strategy | +2.8% | 7 | 4.4% |
| Delta-RSI Strategy (with filters) by tbiktag | rsi ema atr sma | v4 strategy | −28.8% | 71 | 83.8% |
| [KL] Bollinger bands + RSI Strategy by DojiEmoji | sma atr ema rsi | v4 strategy | +28.2% | 30 | 14.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | Yes 1,000 bars in 52 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © FreddieChopin //@version=4strategy("3 x EMA + Stochastic RSI + ATR", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100) // 3x EMAema1Length = input(8, "EMA1 Length", minval = 1)ema2Length = input(14, "EMA2 Length", minval = 1)ema3Length = input(50, "EMA3 Length", minval = 1)ema1 = ema(close, ema1Length)ema2 = ema(close, ema2Length)ema3 = ema(close, ema3Length) plot(ema1, color = color.green)plot(ema2, color = color.orange)plot(ema3, color = color.red) // Stochastic RSIsmoothK = input(3, "K", minval=1)smoothD = input(3, "D", minval=1)lengthRSI = input(14, "RSI Length", minval=1)lengthStoch = input(14, "Stochastic Length", minval=1)src = input(close, title="RSI Source")rsi1 = rsi(src, lengthRSI)k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)d = sma(k, smoothD) // ATRatrPeriod = input(14, "ATR Period")takeProfitMultiplier= input(2.0, "Take-profit Multiplier")stopLossMultiplier= input(3.0, "Stop-loss Multiplier")atrSeries = atr(atrPeriod)[1] longCondition = ema1 > ema2 and ema2 > ema3 and crossover(k, d)strategy.entry("long", strategy.long, when = longCondition) float stopLoss = nafloat takeProfit = na if (strategy.position_size > 0) if (na(stopLoss[1])) stopLoss := strategy.position_avg_price - atrSeries * stopLossMultiplier else stopLoss := stopLoss[1] if (na(takeProfit[1])) takeProfit := strategy.position_avg_price + atrSeries * takeProfitMultiplier else takeProfit := takeProfit[1] strategy.exit("take profit / stop loss", limit = takeProfit, stop = stopLoss) plot(stopLoss, color = color.red, linewidth = 2, style = plot.style_linebr)plot(takeProfit, color = color.green, linewidth = 2, style = plot.style_linebr)