Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Three EMAs Trend-following Strategy (by Coinrule)

A Pine Script v4 strategy built on ATR (atrlen). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.1% commission. 14 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 Three EMAs Trend-following Strategy (by Coinrule) repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 7 of 7 trades.
How did Three EMAs Trend-following Strategy (by Coinrule) do on BTCUSDT 1D with default settings?
+104.90%
From 26 Jan 2024 to 21 Oct 2026, over 22 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Three EMAs Trend-following Strategy (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 Three EMAs Trend-following Strategy (by Coinrule) use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does Three EMAs Trend-following Strategy (by Coinrule) run in Zeal Terminal?
Yes
It compiles in 11 ms and runs 1,000 bars in 50 ms, in the browser.

BTCUSDT · 1D · default settings

  • Vstop
  • EMA Short
  • EMA Mid
  • EMA Long
  • 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 (14)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$207$203Equity from $100
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 long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 42) so the engine trades the way the script says.
Net profit
+104.90%
$104.90
Buy and hold
+102.5%
same period
Closed trades
22
18 won · 4 lost
Profitable
81.8%
of closed trades
Profit factor
3.63
gross profit ÷ gross loss
Max drawdown
21.04%
−$31.09

One run with the author's default settings, $100 starting capital, commission as the script sets it. A long 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

    1 setting does nothing

    “Show Date Range” is declared but never read.

  • Check

    No stop loss

    Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.

  • Check

    Every trade uses all the equity

    default_qty_value is 100% of equity.

  • Note

    4 settings have no label

    In the settings dialog ema_1 (line 15), ema_2 (line 16), ema_3 (line 17), Take_profit (line 18) 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.

  • Note

    Uses an argument v6 removed

    when = on line 42, 44. The upgraded version below moves each call inside an if block.

  • Note

    Uses long =, which v5 renamed line 42

    long = on line 42. 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 ema → ta.ema, atr → ta.atr, max → math.max, min → math.min, crossover → ta.crossover, crossunder → ta.crossunder.
  • 2 strategy calls use the when = argument (line 42, 44). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 2: //@version=4 → //@version=5
  • Line 5: input(type = input.integer) → input.int()
  • 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()
16 more edits
  • Line 10: input(type = input.integer) → input.int()
  • Line 11: input(type = input.bool) → input.bool()
  • Line 15: ema → ta.ema
  • Line 16: ema → ta.ema
  • Line 17: ema → ta.ema
  • Line 28: atr → ta.atr
  • Line 28: tr → ta.tr
  • Line 29: max → math.max
  • Line 30: min → math.min
  • Line 31: max → math.max
  • Line 31: min → math.min
  • Line 39: crossover → ta.crossover
  • Line 39: crossover → ta.crossover
  • Line 39: crossover → ta.crossover
  • Line 40: crossunder → ta.crossunder
  • Line 42: long = true → direction = strategy.long
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=4+//@version=5strategy(shorttitle='Three EMAs Trend-following Strategy',title='Three EMAs Trend-following Strategy (by Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)//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⋯window()  => time >= start and time <= finish ? true : false       // create function "within window of time"-ema_1 = ema(close, input(7))+ema_1 = ta.ema(close, input(7))-ema_2 = ema(close, input(12))+ema_2 = ta.ema(close, input(12))-ema_3 = ema(close, input(21))+ema_3 = ta.ema(close, input(21))Take_profit= ((input (4))/100)⋯    var stop    = 0.0-    atrM        = nz(atr(atrlen) * atrfactor, tr)+    atrM        = nz(ta.atr(atrlen) * atrfactor, ta.tr)-    max         := max(max, src)+    max         := math.max(max, src)-    min         := min(min, src)+    min         := math.min(min, src)-    stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)+    stop        := nz(uptrend ? math.max(stop, max - atrM) : math.min(stop, min + atrM), src)    uptrend     := src - stop >= 0.0⋯[vStop, uptrend] = volStop(src, length, factor)-go_long = crossover(close, ema_1) and crossover(close, ema_2) and crossover(close, ema_3)+go_long = ta.crossover(close, ema_1) and ta.crossover(close, ema_2) and ta.crossover(close, ema_3)-closeLong = close > longTakeProfit or crossunder(close, vStop)+closeLong = close > longTakeProfit or ta.crossunder(close, vStop)//Entry -strategy.entry(id="long", long = true, when = go_long and window())+strategy.entry(id="long", direction = strategy.long, when = go_long and window())//Exit⋯

Clean v5

VerifiedDownload the v5 script

Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 15: input() → input.int()
  • Line 16: input() → input.int()
  • Line 17: input() → input.int()
  • Line 18: input() → input.int()
  • Line 20: input() → input.int()
  • Line 21: input() → input.source()
3 more edits
  • Line 22: input() → input.float()
  • Line 42: when = go_long and window() → if block
  • Line 44: when = closeLong and window() → if block
⋯window()  => time >= start and time <= finish ? true : false       // create function "within window of time"-ema_1 = ta.ema(close, input(7))+ema_1 = ta.ema(close, input.int(7))-ema_2 = ta.ema(close, input(12))+ema_2 = ta.ema(close, input.int(12))-ema_3 = ta.ema(close, input(21))+ema_3 = ta.ema(close, input.int(21))-Take_profit= ((input (4))/100)+Take_profit= ((input.int (4))/100)longTakeProfit = strategy.position_avg_price * (1 + Take_profit)-length = input(20, "Length", minval = 2)+length = input.int(20, "Length", minval = 2)-src = input(close, "Source")+src = input.source(close, "Source")-factor = input(3.0, "Multiplier", minval = 0.25, step = 0.25)+factor = input.float(3.0, "Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) =>⋯//Entry -strategy.entry(id="long", direction = strategy.long, when = go_long and window())+if go_long and window()+    strategy.entry(id="long", direction = strategy.long)//Exit-strategy.close("long", when = closeLong and window())+if closeLong and window()+    strategy.close("long")plot(vStop,"Vstop", color.black, linewidth=2)⋯

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: 22 before, 4 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.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=5+//@version=6strategy(shorttitle='Three EMAs Trend-following Strategy',title='Three EMAs Trend-following Strategy (by Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
From Month1int (written input.integer) · min 1 · max 12start
From Day1int (written input.integer) · min 1 · max 31start
From Year2020int (written input.integer) · min 1970start
Thru Month1int (written input.integer) · min 1 · max 12finish
Thru Day1int (written input.integer) · min 1 · max 31finish
Thru Year2112int (written input.integer) · min 1970finish
Show Date Rangetrueboolunused
EMA 1 (no label in the script)7intgo_long EMA Short
EMA 2 (no label in the script)12intgo_long EMA Mid
EMA 3 (no label in the script)21intgo_long EMA Long
Take profit (no label in the script)4intlongTakeProfit
Length20int · min 2uptrend vStop
Sourceclosesourcemax min uptrend stop vStop
Multiplier3.0float · min 0.25 · step 0.25uptrend vStop

What it calculates

  • ATR (atr, ta.atr in v5)atrlenatrM · line 28

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.

high · low · closehigh · low · closeprice datafromYearFrom Yearsetting · 2020fromMonthFrom Monthsetting · 1fromDayFrom Daysetting · 1thruYearThru Yearsetting · 2112thruMonthThru Monthsetting · 1thruDayThru Daysetting · 1Take_profitTake profitsetting · 4closecloseprice datalengthLengthsetting · 20factorMultipliersetting · 3.0ema_1EMA 1setting · 7ema_2EMA 2setting · 12ema_3EMA 3setting · 21atrMATR→ atrMstartstartvaluefinishfinishvaluelongTakeProfitlongTakeProfitvaluemaxmaxvalueminminvalueuptrend, vStopuptrend · vStop2 valuesstopstopvaluego_longgo_longconditioncloseLongcloseLongconditionentry:longOpen longstrategy.entryclose:longClose longstrategy.closeplot:45Vstopplotplot:46EMA Shortplotplot:47EMA Midplotplot:48EMA Longplot

Order rules

OrderFires whenBy defaultLine
Open long longgo_long and window()On42
Close longcloseLong and window()On44

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.

7/7trades 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.

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+104.9%+102.5%2282%3.6321.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−1.2%+5.5%1338%0.9411.5%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+30.5%+9.8%1182%1.7259.9%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−4.5%+8.4%1242%0.8615.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+36.7%+16.7%2171%1.3854.3%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−24.8%+21.5%1436%0.4239.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−34.5%+148.3%1765%0.6660.7%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+12.5%+17.1%1540%1.779.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 barsTradesWonP&L
Rising market1182%$54.48
Sideways market683%$26.89
Falling market580%$23.74

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

15.3%

The 95th percentile of the worst drawdown when the same 22 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 7.1% to 15.3%; the tick is what actually happened, 8.9%. Order luck alone moves it this much.

0%18%
All 22 trades
SideEntryExitBarsP&L
long20 Mar 2024 67,840.5028 Mar 2024 70,780.608$4.13
long4 Apr 2024 68,487.808 Apr 2024 71,620.004$4.54
long13 May 2024 62,940.1015 May 2024 66,206.502$5.41
long21 Aug 2024 61,156.0023 Aug 2024 64,037.202$5.14
long17 Sep 2024 60,314.0019 Sep 2024 62,948.002$4.96
long6 Oct 2024 62,819.9014 Oct 2024 66,084.008$6.19
long5 Nov 2024 69,372.006 Nov 2024 75,572.001$11.37
long24 Dec 2024 98,663.6027 Dec 2024 94,299.003−$6.54
long2 Jan 2025 96,984.806 Jan 2025 102,236.004$7.03
long14 Jan 2025 96,560.9015 Jan 2025 100,497.001$5.50
long2 Mar 2025 94,270.008 May 2025 103,262.0067$13.77
long7 Jun 2025 105,552.009 Jun 2025 110,263.002$6.87
long16 Jun 2025 106,795.0022 Jun 2025 100,964.006−$9.51
long23 Jun 2025 105,334.003 Jul 2025 109,585.0010$6.08
long22 Aug 2025 116,936.0025 Sep 2025 108,994.0034−$11.51
long30 Dec 2025 88,485.505 Jan 2026 93,859.706$8.99
long2 Mar 2026 68,830.104 Mar 2026 72,666.802$8.71
long23 Mar 2026 70,906.4029 Mar 2026 66,010.906−$12.13
long5 Apr 2026 69,034.207 Apr 2026 71,924.202$6.33
long9 Jul 2026 63,230.0021 Jul 2026 66,556.2012$8.35
long30 Jul 2026 64,780.0019 Aug 2026 69,334.8020$11.84
long14 Sep 2026 78,189.2021 Sep 2026 86,620.007$19.59

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
Three EMAs Trend-following Strategy (by Coinrule) this page—v4 strategy+104.9%2221.0%
SOJA PIVOT by ParaBellum68atrv4 strategy+3.3%918.2%
Volatility System by EduardoMattjeatrv5 strategy+150.2%221.5%
ATR Stop Buy Strategy by phobo3satrv4 strategyNo trades on this market
VBand Strategy by Saravanan_Ragavanatrv4 strategyNo trades on this market
Volatility Breakout Strategy [Angel Algo] by AngelAlgoatrv5 strategy+3.1%183.6%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 11 msWith a workaround 1,000 bars in 50 msNot measured
PineTSNo 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 42) 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///@version=4strategy(shorttitle='Three EMAs Trend-following Strategy',title='Three EMAs Trend-following Strategy (by Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)//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"ema_1 = ema(close, input(7))ema_2 = ema(close, input(12))ema_3 = ema(close, input(21))Take_profit= ((input (4))/100)longTakeProfit = strategy.position_avg_price * (1 + Take_profit)length = input(20, "Length", minval = 2)src = input(close, "Source")factor = input(3.0, "Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) =>    var max     = src    var min     = src    var uptrend = true    var stop    = 0.0    atrM        = nz(atr(atrlen) * atrfactor, tr)    max         := max(max, src)    min         := min(min, src)    stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)    uptrend     := src - stop >= 0.0    if uptrend != nz(uptrend[1], true)        max    := src        min    := src        stop   := uptrend ? max - atrM : min + atrM    [stop, uptrend][vStop, uptrend] = volStop(src, length, factor)go_long = crossover(close, ema_1) and crossover(close, ema_2) and crossover(close, ema_3)closeLong = close > longTakeProfit or crossunder(close, vStop)//Entry strategy.entry(id="long", long = true, when = go_long and window())//Exitstrategy.close("long", when = closeLong and window())plot(vStop,"Vstop", color.black, linewidth=2)plot(ema_1,"EMA Short", color.green, linewidth=1)plot(ema_2,"EMA Mid", color.purple, linewidth=1)plot(ema_3,"EMA Long", color.red, linewidth=1)