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
- 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Show Date Range | true | bool | unused |
| Trail Long Loss (%) | 3 | float · min 0.0 · step 0.1 | longStopPrice stopValue |
| Trail Short Loss (%) | 1 | float · min 0.0 · step 0.1 | shortStopPrice stopValue |
What it calculates
- EMAclose, 7
fastEMA · line 16 - EMAclose, 14
slowEMA · line 17 - MACDclose, 12, 26, 9
macd · line 23 - MACDclose, 12, 26, 9
macd_signal · line 23 - MACDclose, 12, 26, 9
macd_histogram · line 23 - Crossovermacd_signal, macd
buyCondition2 · 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | (buyCondition1 and buyCondition2 and notInTrade and timePeriod) | On | 43 |
| Exit Exit stop and limit | every bar | On | 44 |
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 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +3.3% | +102.5% | 22 | 45% | 1.36 | 4.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.4% | +5.5% | 10 | 40% | 0.59 | 3.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −2.3% | +9.8% | 17 | 47% | 0.75 | 4.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −1.9% | +8.4% | 14 | 21% | 0.73 | 4.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +2.5% | +16.7% | 20 | 50% | 1.21 | 6.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +4.4% | +21.5% | 14 | 43% | 1.89 | 2.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +22.5% | +148.3% | 25 | 48% | 3.16 | 3.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.2% | +17.1% | 13 | 38% | 1.39 | 3.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 10 | 50% | $37.03 |
| Sideways market | 11 | 45% | $0.89 |
| Falling market | 1 | 0% | −$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.
All 22 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Mar 2024 69,499.90 | 19 Mar 2024 66,341.70 | 4 | −$14.20 |
| long | 30 May 2024 68,352.20 | 7 Jun 2024 68,974.80 | 8 | $2.10 |
| long | 8 Jun 2024 69,310.50 | 11 Jun 2024 67,558.70 | 3 | −$8.07 |
| long | 31 Jul 2024 64,628.00 | 2 Aug 2024 63,393.40 | 2 | −$6.19 |
| long | 1 Oct 2024 60,805.80 | 9 Oct 2024 60,935.30 | 8 | $0.04 |
| long | 26 Oct 2024 67,092.80 | 31 Oct 2024 70,554.30 | 5 | $14.46 |
| long | 3 Nov 2024 68,776.00 | 12 Nov 2024 85,988.60 | 9 | $73.45 |
| long | 26 Nov 2024 91,965.20 | 2 Dec 2024 94,536.20 | 6 | $8.25 |
| long | 18 Dec 2024 100,204.00 | 20 Dec 2024 94,538.00 | 2 | −$18.75 |
| long | 28 Jan 2025 101,336.00 | 31 Jan 2025 101,581.00 | 3 | $0.13 |
| long | 17 May 2025 103,127.00 | 19 May 2025 103,261.00 | 2 | −$0.22 |
| long | 19 May 2025 105,574.00 | 23 May 2025 108,345.00 | 4 | $7.63 |
| long | 24 May 2025 107,762.00 | 29 May 2025 106,152.00 | 5 | −$5.37 |
| long | 12 Jun 2025 105,672.00 | 17 Jun 2025 103,591.00 | 5 | −$6.84 |
| long | 21 Jul 2025 117,380.00 | 25 Jul 2025 116,356.00 | 4 | −$3.36 |
| long | 17 Aug 2025 117,405.00 | 19 Aug 2025 112,740.00 | 2 | −$13.03 |
| long | 10 Oct 2025 112,775.00 | 14 Oct 2025 111,711.00 | 4 | −$3.53 |
| long | 28 Apr 2026 76,342.80 | 13 May 2026 79,743.80 | 15 | $13.08 |
| long | 27 Jul 2026 63,755.90 | 31 Jul 2026 62,836.60 | 4 | −$5.11 |
| long | 2 Sep 2026 77,340.00 | 4 Sep 2026 78,832.30 | 2 | $5.36 |
| long | 4 Sep 2026 79,660.80 | 8 Sep 2026 77,931.60 | 4 | −$7.38 |
| long | 29 Sep 2026 83,663.70 | 7 Oct 2026 83,934.10 | 8 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| EMA and MACD with Trailing Stop Loss (by Coinrule) this page | — | v5 strategy | +3.3% | 22 | 4.6% |
| Short Swing Bearish MACD Cross (By Coinrule) by Coinrule | ema macd | v5 strategy | −6.9% | 8 | 6.9% |
| MACD Trend Strategy by Zeal Terminal | macd ema | v5 strategy | −1.0% | 21 | 3.5% |
| Fast EMA above Slow EMA with MACD (by Coinrule) by Coinrule | ema macd | v5 strategy | −4.5% | 18 | 7.7% |
| RSI of MACD Strategy [Long only] by mohanee | macd ema | v4 strategy | No trades on this market | ||
| Macd Divergence + MTF EMA by Trading_Solutions_ | macd ema | v5 strategy | No 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.
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
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | With a workaround 1,000 bars in 26 ms | Not measured |
| PineTS | Not tested | Not tested | Not 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)