Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (11)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$101,368$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. 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.
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

Verified

Same 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)⋯

Clean v5

VerifiedDownload the v5 script

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.

Download the v6 script

⋯// © 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.

SettingDefaultType and rangeFeeds
Start Datetimestamp("2020-01-01T00:00:00")timeline 37 line 53
Stop Datetimestamp("2030-01-01T00:00:00")timeline 37 line 53
stoploss1000int · min 1entry L entry S entry long entry short
Blue EMA length10int · min 1 · step 1Keltma rangema
Blue EMA multy1.8float · step 0.1upperk lowerk
ATR Period15intupper_atr lower_atr
ATR mult0.7float · step 0.1upper_atr lower_atr
Red EMA length20int · step 1red_ema
longtrueboolentry L exit L entry long exit long line 38 line 54
shorttrueboolentry S exit S entry short exit short line 43 line 60
Breakout modefalseboolline 37 line 53

What it calculates

  • EMA / TR (ema, ta.ema in v5)tr(true), ATR Period (15)upper_atr · line 17
  • EMA / TR (ema, ta.ema in v5)tr(true), ATR Period (15)lower_atr · line 18
  • EMA (ema, ta.ema in v5)close, Blue EMA length (10)Keltma · line 26
  • EMA (ema, ta.ema in v5)range, Blue EMA length (10)rangema · line 28
  • EMA (ema, ta.ema in 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.

closecloseprice dataatrlenATR Periodsetting · 15mult_atrATR multsetting · 0.7lengthBlue EMA lengthsetting · 10redemaRed EMA lengthsetting · 20lowlowprice datahighhighprice datamultyBlue EMA multysetting · 1.8upper_atrEMA / TR→ upper_atrlower_atrEMA / TR→ lower_atrKeltmaEMA→ KeltmarangemaEMA→ rangemared_emaEMA→ red_emaop_upper_atrop_upper_atrvalueop_lower_atrop_lower_atrvalueop_closeop_closevaluerangerangevalueupperk, lowerkupperk · lowerk2 valuesentry:LOpen Lstrategy.entryexit:Lexit Lstrategy.exitentry:SOpen Sstrategy.entryexit:Sexit Sstrategy.exitentry:longOpen longstrategy.entryexit:longexit longstrategy.exitentry:shortOpen shortstrategy.entryexit:shortexit shortstrategy.exitplot:19+ATRplotplot:20-ATRplotplot:31plotplot:32plotplot:35plot

Order rules

OrderFires whenBy defaultLine
Open L longlongsignal and strategy.position_size==0 and close>upperk and close>red_emaOn41
Exit L stop and limitlongsignal and strategy.position_size==0 and close>upperk and close>red_emaOn42
Open S shortshortsignal and strategy.position_size==0 and close<lowerk and close<red_emaOn46
Exit S stop and limitshortsignal and strategy.position_size==0 and close<lowerk and close<red_emaOn47
Exit L stop and limitstrategy.position_size>0On49
Exit S stop and limitstrategy.position_size<0On51
Open long longlongsignal and strategy.position_size==0 and close[0]>high[1] and close>red_ema and close>upperkOn58
Exit long stop and limitlongsignal and strategy.position_size==0 and close[0]>high[1] and close>red_ema and close>upperkOn59
Open short shortshortsignal and strategy.position_size==0 and close[0]<low[1] and close<red_ema and close<lowerkOn64
Exit short stop and limitshortsignal and strategy.position_size==0 and close[0]<low[1] and close<red_ema and close<lowerkOn65
Exit long stop and limitstrategy.position_size>0On67
Exit short stop and limitstrategy.position_size<0On69

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.

11/11trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+1.4%+102.5%2979%1.402.2%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+1.1%+5.5%3184%1.301.8%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+2.5%+9.8%4183%1.533.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.1%+8.4%2572%1.041.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.1%+16.7%2576%0.982.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.5%+21.5%2677%1.131.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.7%+148.3%3181%1.151.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−3.0%+17.1%2662%0.513.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 barsTradesWonP&L
Rising market1283%$902.03
Sideways market1479%$320.06
Falling market367%$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.

0%3%
All 29 trades
SideEntryExitBarsP&L
long15 Feb 2024 51,795.2026 Feb 2024 52,994.6011$164.81
long28 Feb 2024 57,037.4028 Feb 2024 58,327.400$190.20
long29 Feb 2024 62,432.104 Mar 2024 64,200.404$158.70
long5 Mar 2024 68,245.7011 Mar 2024 70,206.806$159.84
long21 May 2024 71,446.6031 May 2024 67,211.7010−$643.58
short25 Jun 2024 60,293.301 Jul 2024 63,751.206−$659.42
short6 Aug 2024 54,018.808 Aug 2024 61,593.702−$856.37
long11 Nov 2024 80,370.0011 Nov 2024 82,340.400$220.87
long12 Nov 2024 88,648.0013 Nov 2024 91,187.101$160.84
long14 Nov 2024 90,375.2019 Nov 2024 93,316.405$202.26
long22 Nov 2024 98,317.105 Dec 2024 101,023.0013$198.22
short27 Feb 2025 84,250.1028 Feb 2025 81,565.401$244.98
long23 Apr 2025 93,443.0025 Apr 2025 95,745.002$261.78
long9 May 2025 103,262.0012 May 2025 105,340.003$214.77
long11 Jul 2025 116,010.0011 Jul 2025 118,030.000$254.27
long12 Jul 2025 117,528.0014 Jul 2025 119,614.002$241.65
long3 Oct 2025 120,529.003 Oct 2025 122,379.000$275.62
long4 Oct 2025 122,232.005 Oct 2025 124,257.001$263.61
short1 Feb 2026 78,741.101 Feb 2026 76,237.200$254.17
short2 Feb 2026 76,968.203 Feb 2026 74,451.401$238.35
short6 Feb 2026 62,909.8025 Feb 2026 69,527.5019−$346.57
short3 Jun 2026 66,760.803 Jun 2026 65,129.400$193.57
short4 Jun 2026 64,142.704 Jun 2026 62,415.700$171.77
short5 Jun 2026 63,886.005 Jun 2026 62,079.000$193.87
short6 Jun 2026 61,056.4015 Jun 2026 66,795.009−$542.34
long20 Aug 2026 69,334.8020 Aug 2026 70,539.300$232.77
long21 Aug 2026 73,027.0021 Aug 2026 74,472.700$177.04
long22 Aug 2026 78,338.1025 Aug 2026 80,171.203$148.41
long22 Sep 2026 86,620.007 Oct 2026 83,527.6015−$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.

ScriptSharesTypeNetTradesMax drawdown
Trend #4 - ATR+EMA channel this page—v4 strategy+1.4%292.2%
Scalping Trading System bot Crypto and Stocks by exlux99ema trv5 strategy+6.5%51.0%
Daily Investments Index Scalp by dailycryptoinvestmentsema trv5 strategy−6.9%4230.1%
Twin Range Filter Algo by OrcChieftainema trv4 strategy+48.4%186.8%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 29 msWith a workaround 1,000 bars in 51 msNot measured
PineTSNo 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)