Pine Script v4 · Strategy
Trend #4 - ATR+EMA channel
A Pine Script v4 strategy built on EMA or TR (true, 15) and EMA (close, 10). It has 12 order rules, 4 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays 0.05 commission. 11 settings; defaults include stoploss 1000, Blue EMA length 10 and Blue EMA multy 1.8.
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 Trend #4 - ATR+EMA channel repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did Trend #4 - ATR+EMA channel do on BTCUSDT 1D with default settings?
- +1.37%
- 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 Trend #4 - ATR+EMA channel 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 Trend #4 - ATR+EMA channel use a stop loss?
- Yes
- strategy.exit sets a stop on line 42, 47, 49, 51, 59, 65, 67, 69.
- Does Trend #4 - ATR+EMA channel run in Zeal Terminal?
- Yes
- It compiles in 29 ms and runs 1,000 bars in 51 ms, in the browser.
BTCUSDT · 1D · default settings
- +ATR
- -ATR
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.37%
- $1,368.05
- Buy and hold
- +102.5%
- same period
- Closed trades
- 29
- 23 won · 6 lost
- Profitable
- 79.3%
- of closed trades
- Profit factor
- 1.40
- gross profit ÷ gross loss
- Max drawdown
- 2.16%
- −$2,176.24
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.
Nothing to point out.
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, tr → ta.tr.
v4 → v5
VerifiedSame 29 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 5: input(type = input.time) → input.time()
- Line 6: input(type = input.time) → input.time()
- Line 13: input(type = input.bool) → input.bool()
- Line 14: input(type = input.bool) → input.bool()
- Line 15: input(type = input.bool) → input.bool()
10 more edits
- Line 17: ema → ta.ema
- Line 17: tr → ta.tr
- Line 18: ema → ta.ema
- Line 18: tr → ta.tr
- Line 26: ema → ta.ema
- Line 27: range is reserved in v5: renamed range_
- Line 28: ema → ta.ema
- Line 34: ema → ta.ema
- Line 41: strategy.entry(…, true) → strategy.entry(…, strategy.long)
- Line 46: strategy.entry(…, false) → strategy.entry(…, strategy.short)
⋯// © zxcv55602-//@version=4+//@version=5strategy("Trend #4 - ATR+EMA channel", overlay = true,slippage=2,commission_value=0.05)-date1 = input(title="Start Date", type=input.time, defval=timestamp("2020-01-01T00:00:00"))+date1 = input.time(title="Start Date", defval=timestamp("2020-01-01T00:00:00"))-date2 = input(title="Stop Date", type=input.time, defval=timestamp("2030-01-01T00:00:00"))+date2 = input.time(title="Stop Date", defval=timestamp("2030-01-01T00:00:00"))stoploss=input(1000, minval=1, title="stoploss")⋯redema=input(20, "Red EMA length", step=1)-longsignal=input(true,"long",type=input.bool)+longsignal=input.bool(true,"long")-shortsignal=input(true,"short",type=input.bool)+shortsignal=input.bool(true,"short")-Breakout=input(false,"Breakout mode",type=input.bool)+Breakout=input.bool(false,"Breakout mode")//-------------------upper_atr = ema(tr(true),atrlen) * mult_atr + close+upper_atr = ta.ema(ta.tr(true),atrlen) * mult_atr + close-lower_atr = close - ema(tr(true),atrlen) * mult_atr+lower_atr = close - ta.ema(ta.tr(true),atrlen) * mult_atrplot(upper_atr, "+ATR", color=color.new(color.white,80),style=plot.style_stepline)⋯//-------------------Keltma =ema(close, length)+Keltma =ta.ema(close, length)-range = high - low+range_ = high - low-rangema = ema(range, length)+rangema = ta.ema(range_, length)upperk =Keltma + rangema * multy⋯//-------------------red_ema=ema(close,redema)+red_ema=ta.ema(close,redema)plot(red_ema, color=color.red, title="")⋯ op_upper_atr:=upper_atr- strategy.entry("L",true,qty=(stoploss/(op_close-red_ema)),stop=red_ema)+ strategy.entry("L",strategy.long,qty=(stoploss/(op_close-red_ema)),stop=red_ema) strategy.exit("L",limit=op_upper_atr,comment="exit_L",stop=red_ema)⋯ op_lower_atr:=lower_atr- strategy.entry("S",false,qty=(stoploss/(red_ema-op_close)),stop=red_ema)+ strategy.entry("S",strategy.short,qty=(stoploss/(red_ema-op_close)),stop=red_ema) strategy.exit("S",limit=op_lower_atr,comment="exit_S",stop=red_ema)⋯Same 29 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 9: input() → input.float()
- Line 10: input() → input.int()
- Line 11: input() → input.float()
- Line 12: input() → input.int()
⋯date2 = input.time(title="Stop Date", defval=timestamp("2030-01-01T00:00:00"))-stoploss=input(1000, minval=1, title="stoploss")+stoploss=input.int(1000, minval=1, title="stoploss")-length = input(10, minval=1, title="Blue EMA length",step=1)+length = input.int(10, minval=1, title="Blue EMA length",step=1)-multy = input(1.8, step=0.1, title="Blue EMA multy")+multy = input.float(1.8, step=0.1, title="Blue EMA multy")-atrlen = input(15, "ATR Period")+atrlen = input.int(15, "ATR Period")-mult_atr = input(0.7, "ATR mult", step=0.1)+mult_atr = input.float(0.7, "ATR mult", step=0.1)-redema=input(20, "Red EMA length", step=1)+redema=input.int(20, "Red EMA length", step=1)longsignal=input.bool(true,"long")⋯Does it run in Pine Script v6?
Yes
Same 29 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © zxcv55602-//@version=5+//@version=6strategy("Trend #4 - ATR+EMA channel", overlay = true,slippage=2,commission_value=0.05)⋯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 |
|---|---|---|---|
| Start Date | timestamp("2020-01-01T00:00:00") | time | line 37 line 53 |
| Stop Date | timestamp("2030-01-01T00:00:00") | time | line 37 line 53 |
| stoploss | 1000 | int · min 1 | entry L entry S entry long entry short |
| Blue EMA length | 10 | int · min 1 · step 1 | Keltma rangema |
| Blue EMA multy | 1.8 | float · step 0.1 | upperk lowerk |
| ATR Period | 15 | int | upper_atr lower_atr |
| ATR mult | 0.7 | float · step 0.1 | upper_atr lower_atr |
| Red EMA length | 20 | int · step 1 | red_ema |
| long | true | bool | entry L exit L entry long exit long line 38 line 54 |
| short | true | bool | entry S exit S entry short exit short line 43 line 60 |
| Breakout mode | false | bool | line 37 line 53 |
What it calculates
- EMA / TR (
ema,ta.emain v5)tr(true), ATR Period (15)upper_atr · line 17 - EMA / TR (
ema,ta.emain v5)tr(true), ATR Period (15)lower_atr · line 18 - EMA (
ema,ta.emain v5)close, Blue EMA length (10)Keltma · line 26 - EMA (
ema,ta.emain v5)range, Blue EMA length (10)rangema · line 28 - EMA (
ema,ta.emain v5)close, Red EMA length (20)red_ema · line 34
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 L long | longsignal and strategy.position_size==0 and close>upperk and close>red_ema | On | 41 |
| Exit L stop and limit | longsignal and strategy.position_size==0 and close>upperk and close>red_ema | On | 42 |
| Open S short | shortsignal and strategy.position_size==0 and close<lowerk and close<red_ema | On | 46 |
| Exit S stop and limit | shortsignal and strategy.position_size==0 and close<lowerk and close<red_ema | On | 47 |
| Exit L stop and limit | strategy.position_size>0 | On | 49 |
| Exit S stop and limit | strategy.position_size<0 | On | 51 |
| Open long long | longsignal and strategy.position_size==0 and close[0]>high[1] and close>red_ema and close>upperk | On | 58 |
| Exit long stop and limit | longsignal and strategy.position_size==0 and close[0]>high[1] and close>red_ema and close>upperk | On | 59 |
| Open short short | shortsignal and strategy.position_size==0 and close[0]<low[1] and close<red_ema and close<lowerk | On | 64 |
| Exit short stop and limit | shortsignal and strategy.position_size==0 and close[0]<low[1] and close<red_ema and close<lowerk | On | 65 |
| Exit long stop and limit | strategy.position_size>0 | On | 67 |
| Exit short stop and limit | strategy.position_size<0 | On | 69 |
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.4% | +102.5% | 29 | 79% | 1.40 | 2.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.1% | +5.5% | 31 | 84% | 1.30 | 1.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +2.5% | +9.8% | 41 | 83% | 1.53 | 3.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 25 | 72% | 1.04 | 1.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 25 | 76% | 0.98 | 2.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.5% | +21.5% | 26 | 77% | 1.13 | 1.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.7% | +148.3% | 31 | 81% | 1.15 | 1.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −3.0% | +17.1% | 26 | 62% | 0.51 | 3.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 | 12 | 83% | $902.03 |
| Sideways market | 14 | 79% | $320.06 |
| Falling market | 3 | 67% | $145.96 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
2.2%
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, 0.9% to 2.2%; the tick is what actually happened, 2.1%. Order luck alone moves it this much.
All 29 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Feb 2024 51,795.20 | 26 Feb 2024 52,994.60 | 11 | $164.81 |
| long | 28 Feb 2024 57,037.40 | 28 Feb 2024 58,327.40 | 0 | $190.20 |
| long | 29 Feb 2024 62,432.10 | 4 Mar 2024 64,200.40 | 4 | $158.70 |
| long | 5 Mar 2024 68,245.70 | 11 Mar 2024 70,206.80 | 6 | $159.84 |
| long | 21 May 2024 71,446.60 | 31 May 2024 67,211.70 | 10 | −$643.58 |
| short | 25 Jun 2024 60,293.30 | 1 Jul 2024 63,751.20 | 6 | −$659.42 |
| short | 6 Aug 2024 54,018.80 | 8 Aug 2024 61,593.70 | 2 | −$856.37 |
| long | 11 Nov 2024 80,370.00 | 11 Nov 2024 82,340.40 | 0 | $220.87 |
| long | 12 Nov 2024 88,648.00 | 13 Nov 2024 91,187.10 | 1 | $160.84 |
| long | 14 Nov 2024 90,375.20 | 19 Nov 2024 93,316.40 | 5 | $202.26 |
| long | 22 Nov 2024 98,317.10 | 5 Dec 2024 101,023.00 | 13 | $198.22 |
| short | 27 Feb 2025 84,250.10 | 28 Feb 2025 81,565.40 | 1 | $244.98 |
| long | 23 Apr 2025 93,443.00 | 25 Apr 2025 95,745.00 | 2 | $261.78 |
| long | 9 May 2025 103,262.00 | 12 May 2025 105,340.00 | 3 | $214.77 |
| long | 11 Jul 2025 116,010.00 | 11 Jul 2025 118,030.00 | 0 | $254.27 |
| long | 12 Jul 2025 117,528.00 | 14 Jul 2025 119,614.00 | 2 | $241.65 |
| long | 3 Oct 2025 120,529.00 | 3 Oct 2025 122,379.00 | 0 | $275.62 |
| long | 4 Oct 2025 122,232.00 | 5 Oct 2025 124,257.00 | 1 | $263.61 |
| short | 1 Feb 2026 78,741.10 | 1 Feb 2026 76,237.20 | 0 | $254.17 |
| short | 2 Feb 2026 76,968.20 | 3 Feb 2026 74,451.40 | 1 | $238.35 |
| short | 6 Feb 2026 62,909.80 | 25 Feb 2026 69,527.50 | 19 | −$346.57 |
| short | 3 Jun 2026 66,760.80 | 3 Jun 2026 65,129.40 | 0 | $193.57 |
| short | 4 Jun 2026 64,142.70 | 4 Jun 2026 62,415.70 | 0 | $171.77 |
| short | 5 Jun 2026 63,886.00 | 5 Jun 2026 62,079.00 | 0 | $193.87 |
| short | 6 Jun 2026 61,056.40 | 15 Jun 2026 66,795.00 | 9 | −$542.34 |
| long | 20 Aug 2026 69,334.80 | 20 Aug 2026 70,539.30 | 0 | $232.77 |
| long | 21 Aug 2026 73,027.00 | 21 Aug 2026 74,472.70 | 0 | $177.04 |
| long | 22 Aug 2026 78,338.10 | 25 Aug 2026 80,171.20 | 3 | $148.41 |
| long | 22 Sep 2026 86,620.00 | 7 Oct 2026 83,527.60 | 15 | −$406.06 |
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 |
|---|---|---|---|---|---|
| Trend #4 - ATR+EMA channel this page | — | v4 strategy | +1.4% | 29 | 2.2% |
| Scalping Trading System bot Crypto and Stocks by exlux99 | ema tr | v5 strategy | +6.5% | 5 | 1.0% |
| Daily Investments Index Scalp by dailycryptoinvestments | ema tr | v5 strategy | −6.9% | 42 | 30.1% |
| Twin Range Filter Algo by OrcChieftain | ema tr | v4 strategy | +48.4% | 18 | 6.8% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 29 ms | With a workaround 1,000 bars in 51 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true, false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 41, 46) 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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © zxcv55602//@version=4strategy("Trend #4 - ATR+EMA channel", overlay = true,slippage=2,commission_value=0.05)date1 = input(title="Start Date", type=input.time, defval=timestamp("2020-01-01T00:00:00"))date2 = input(title="Stop Date", type=input.time, defval=timestamp("2030-01-01T00:00:00"))stoploss=input(1000, minval=1, title="stoploss")length = input(10, minval=1, title="Blue EMA length",step=1)multy = input(1.8, step=0.1, title="Blue EMA multy")atrlen = input(15, "ATR Period")mult_atr = input(0.7, "ATR mult", step=0.1)redema=input(20, "Red EMA length", step=1)longsignal=input(true,"long",type=input.bool)shortsignal=input(true,"short",type=input.bool)Breakout=input(false,"Breakout mode",type=input.bool)//------------------upper_atr = ema(tr(true),atrlen) * mult_atr + closelower_atr = close - ema(tr(true),atrlen) * mult_atrplot(upper_atr, "+ATR", color=color.new(color.white,80),style=plot.style_stepline)plot(lower_atr, "-ATR", color=color.new(color.white,80),style=plot.style_stepline)//------------------var op_upper_atr=0.0var op_lower_atr=0.0var op_close=0.0//------------------Keltma =ema(close, length)range = high - lowrangema = ema(range, length)upperk =Keltma + rangema * multylowerk = Keltma - rangema * multyplot(upperk, color=color.blue, title="")plot(lowerk, color=color.blue, title="")//------------------red_ema=ema(close,redema)plot(red_ema, color=color.red, title="")//------------------if time >= date1 and time <= date2 and Breakout==false if longsignal and strategy.position_size==0 and close>upperk and close>red_ema op_close:=close op_upper_atr:=upper_atr strategy.entry("L",true,qty=(stoploss/(op_close-red_ema)),stop=red_ema) strategy.exit("L",limit=op_upper_atr,comment="exit_L",stop=red_ema) if shortsignal and strategy.position_size==0 and close<lowerk and close<red_ema op_close:=close op_lower_atr:=lower_atr strategy.entry("S",false,qty=(stoploss/(red_ema-op_close)),stop=red_ema) strategy.exit("S",limit=op_lower_atr,comment="exit_S",stop=red_ema) if strategy.position_size>0 strategy.exit("L",limit=op_upper_atr,comment="exit_L",stop=red_ema) if strategy.position_size<0 strategy.exit("S",limit=op_lower_atr,comment="exit_S",stop=red_ema)//------------------if time >= date1 and time <= date2 and Breakout if longsignal and strategy.position_size==0 and close[0]>high[1] and close>red_ema and close>upperk op_upper_atr:=upper_atr op_lower_atr:=lower_atr op_close:=close strategy.entry("long",strategy.long,qty=stoploss/(op_close-red_ema),limit=op_upper_atr,stop=red_ema) strategy.exit("long",comment="exit_L1",limit=op_upper_atr,stop=red_ema) if shortsignal and strategy.position_size==0 and close[0]<low[1] and close<red_ema and close<lowerk op_upper_atr:=upper_atr op_lower_atr:=lower_atr op_close:=close strategy.entry("short",strategy.short,qty=stoploss/(red_ema-op_close),limit=op_lower_atr,stop=red_ema) strategy.exit("short",comment="exit_S1",limit=op_lower_atr,stop=red_ema) if strategy.position_size>0 strategy.exit("long",comment="exit_L",limit=op_upper_atr,stop=red_ema) if strategy.position_size<0 strategy.exit("short",comment="exit_S",limit=op_lower_atr,stop=red_ema)