Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

EMA and MACD with Trailing Stop Loss (by Coinrule)

A Pine Script v5 strategy built on EMA (close, 7), MACD (close, 12) and Crossover (macd_signal, macd). 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 30% of equity and pays 0.1% commission. 3 settings; defaults include Trail Long Loss (%) 3 and Trail Short Loss (%) 1.

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 EMA and MACD with Trailing Stop Loss (by Coinrule) repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 5 of 5 trades.
How did EMA and MACD with Trailing Stop Loss (by Coinrule) do on BTCUSDT 1D with default settings?
+3.28%
From 26 Jan 2024 to 21 Oct 2026, over 22 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does EMA and MACD with Trailing Stop Loss (by Coinrule) 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, 22 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 EMA and MACD with Trailing Stop Loss (by Coinrule) use a stop loss?
Yes
strategy.exit sets a stop on line 44.
Does EMA and MACD with Trailing Stop Loss (by Coinrule) run in Zeal Terminal?
Yes
It compiles in 9 ms and runs 1,000 bars in 26 ms, in the browser.

BTCUSDT · 1D · default settings

  • Plot
  • Plot
  • 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 (3)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,033$2,025Equity from $1,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 direction = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 43) so the engine trades the way the script says.
Net profit
+3.28%
$32.80
Buy and hold
+102.5%
same period
Closed trades
22
10 won · 12 lost
Profitable
45.5%
of closed trades
Profit factor
1.36
gross profit ÷ gross loss
Max drawdown
4.61%
−$49.31

One run with the author's default settings, $1,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

    1 setting does nothing

    “Show Date Range” is declared but never read.

  • 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.

SettingDefaultType and rangeFeeds
Show Date Rangetrueboolunused
Trail Long Loss (%)3float · min 0.0 · step 0.1longStopPrice stopValue
Trail Short Loss (%)1float · min 0.0 · step 0.1shortStopPrice stopValue

What it calculates

  • EMAclose, 7fastEMA · line 16
  • EMAclose, 14slowEMA · line 17
  • MACDclose, 12, 26, 9macd · line 23
  • MACDclose, 12, 26, 9macd_signal · line 23
  • MACDclose, 12, 26, 9macd_histogram · line 23
  • Crossovermacd_signal, macdbuyCondition2 · line 24

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 datashortTrailPercTrail Short Loss (%)setting · 1timetimeprice datalongTrailPercTrail Long Loss (%)setting · 3fastEMAEMA→ fastEMAslowEMAEMA→ slowEMAmacdMACD→ macdmacd_signalMACD→ macd_signalmacd_histogramMACD→ macd_histogrambuyCondition2Crossover→ buyCondition2stopValuestopValuevaluetimePeriodtimePeriodconditionnotInTradenotInTradeconditionbuyCondition1buyCondition1conditionlongStopPricelongStopPriceconditionshortStopPriceshortStopPriceconditionentry:LongOpen Longstrategy.entryexit:Exitexit Exitstrategy.exitplot:18plotplotplot:19plotplot

Order rules

OrderFires whenBy defaultLine
Open Long long(buyCondition1 and buyCondition2 and notInTrade and timePeriod)On43
Exit Exit stop and limitevery barOn44

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.

5/5trades 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+3.3%+102.5%2245%1.364.6%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−1.4%+5.5%1040%0.593.1%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−2.3%+9.8%1747%0.754.7%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−1.9%+8.4%1421%0.734.7%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+2.5%+16.7%2050%1.216.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+4.4%+21.5%1443%1.892.5%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+22.5%+148.3%2548%3.163.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+2.2%+17.1%1338%1.393.9%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 market1050%$37.03
Sideways market1145%$0.89
Falling market10%−$5.11

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

How bad could the drawdown be?

6.7%

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, 3.1% to 6.7%; the tick is what actually happened, 4.1%. Order luck alone moves it this much.

0%8%
All 22 trades
SideEntryExitBarsP&L
long15 Mar 2024 69,499.9019 Mar 2024 66,341.704−$14.20
long30 May 2024 68,352.207 Jun 2024 68,974.808$2.10
long8 Jun 2024 69,310.5011 Jun 2024 67,558.703−$8.07
long31 Jul 2024 64,628.002 Aug 2024 63,393.402−$6.19
long1 Oct 2024 60,805.809 Oct 2024 60,935.308$0.04
long26 Oct 2024 67,092.8031 Oct 2024 70,554.305$14.46
long3 Nov 2024 68,776.0012 Nov 2024 85,988.609$73.45
long26 Nov 2024 91,965.202 Dec 2024 94,536.206$8.25
long18 Dec 2024 100,204.0020 Dec 2024 94,538.002−$18.75
long28 Jan 2025 101,336.0031 Jan 2025 101,581.003$0.13
long17 May 2025 103,127.0019 May 2025 103,261.002−$0.22
long19 May 2025 105,574.0023 May 2025 108,345.004$7.63
long24 May 2025 107,762.0029 May 2025 106,152.005−$5.37
long12 Jun 2025 105,672.0017 Jun 2025 103,591.005−$6.84
long21 Jul 2025 117,380.0025 Jul 2025 116,356.004−$3.36
long17 Aug 2025 117,405.0019 Aug 2025 112,740.002−$13.03
long10 Oct 2025 112,775.0014 Oct 2025 111,711.004−$3.53
long28 Apr 2026 76,342.8013 May 2026 79,743.8015$13.08
long27 Jul 2026 63,755.9031 Jul 2026 62,836.604−$5.11
long2 Sep 2026 77,340.004 Sep 2026 78,832.302$5.36
long4 Sep 2026 79,660.808 Sep 2026 77,931.604−$7.38
long29 Sep 2026 83,663.707 Oct 2026 83,934.108$0.38

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
EMA and MACD with Trailing Stop Loss (by Coinrule) this page—v5 strategy+3.3%224.6%
Short Swing Bearish MACD Cross (By Coinrule) by Coinruleema macdv5 strategy−6.9%86.9%
MACD Trend Strategy by Zeal Terminalmacd emav5 strategy−1.0%213.5%
Fast EMA above Slow EMA with MACD (by Coinrule) by Coinruleema macdv5 strategy−4.5%187.7%
RSI of MACD Strategy [Long only] by mohaneemacd emav4 strategyNo trades on this market
Macd Divergence + MTF EMA by Trading_Solutions_macd emav5 strategyNo 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.

  • 1 setting uses the untyped input() (line 12). 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.

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 12: input() → input.bool()
⋯         commission_value=0.1)-showDate = input(defval=true, title='Show Date Range')+showDate = input.bool(defval=true, title='Show Date Range')timePeriod = time >= timestamp(syminfo.timezone, 2022, 1, 1, 0, 0)⋯

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, 22 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.

⋯// © Coinrule-//@version=5+//@version=6strategy('EMA and MACD with Trailing Stop Loss',⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 9 msWith a workaround 1,000 bars in 26 msNot measured
PineTSNot testedNot testedNot measured

Zeal Terminal's engine currently reads an order side written as direction = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 43) 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.

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/// © Coinrule//@version=5strategy('EMA and MACD with Trailing Stop Loss',         overlay=true,         initial_capital=1000,         process_orders_on_close=true,         default_qty_type=strategy.percent_of_equity,         default_qty_value=30,         commission_type=strategy.commission.percent,         commission_value=0.1)showDate = input(defval=true, title='Show Date Range')timePeriod = time >= timestamp(syminfo.timezone, 2022, 1, 1, 0, 0)notInTrade = strategy.position_size <= 0// EMAs fastEMA = ta.ema(close, 7)slowEMA = ta.ema(close, 14)plot(fastEMA, color = color.blue)plot(slowEMA, color = color.green)//buyCondition1 = ta.crossover(fastEMA, slowEMA)buyCondition1 = fastEMA > slowEMA// DMI and MACD inputs and calculations[macd, macd_signal, macd_histogram] = ta.macd(close, 12, 26, 9)buyCondition2 = ta.crossover(macd_signal, macd)// Configure trail stop level with input optionslongTrailPerc = input.float(title='Trail Long Loss (%)', minval=0.0, step=0.1, defval=3) * 0.01shortTrailPerc = input.float(title='Trail Short Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01// Determine trail stop loss priceslongStopPrice = 0.0shortStopPrice = 0.0longStopPrice := if strategy.position_size > 0    stopValue = close * (1 - longTrailPerc)    math.max(stopValue, longStopPrice[1])else    0shortStopPrice := if strategy.position_size < 0    stopValue = close * (1 + shortTrailPerc)    math.min(stopValue, shortStopPrice[1])else    999999    if (buyCondition1 and buyCondition2 and notInTrade and timePeriod)    strategy.entry(id="Long", direction = strategy.long)strategy.exit(id="Exit", stop = longStopPrice, limit = shortStopPrice)//if (sellCondition1 and sellCondition2 and notInTrade and timePeriod)//strategy.close(id="Close", when = sellCondition1 or sellCondition2)