Pine Script v5 · Strategy
MACD Trend Strategy
A Pine Script v5 strategy built on MACD (close, 12), EMA (close, 100) and Crossover (macdLine, signalLine). 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 10% of equity and pays 0.05% commission. 4 settings; defaults include Fast length 12, Slow length 26 and Signal length 9.
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 MACD Trend Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did MACD Trend Strategy do on BTCUSDT 1D with default settings?
- −1.03%
- From 26 Jan 2024 to 21 Oct 2026, over 21 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does MACD Trend Strategy have?
- 2
- Bullish cross in an uptrend; Bearish cross.
- Does MACD Trend Strategy 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: 21 before, 21 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 MACD Trend Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
BTCUSDT · 1D · default settings
- Histogram
- MACD
- Signal
- Zero
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.03%
- −$102.83
- Buy and hold
- +102.5%
- same period
- Closed trades
- 21
- 6 won · 15 lost
- Profitable
- 28.6%
- of closed trades
- Profit factor
- 0.81
- gross profit ÷ gross loss
- Max drawdown
- 3.52%
- −$360.48
One run with the author's default settings, $10,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
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
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 |
|---|---|---|---|
| Fast length | 12 | int · min 1 | macdLine signalLine hist |
| Slow length | 26 | int · min 1 | macdLine signalLine hist |
| Signal length | 9 | int · min 1 | macdLine signalLine hist |
| Trend filter length | 100 | int · min 1 | trend |
What it calculates
- MACDclose, Fast length (12), Slow length (26), Signal length (9)
macdLine · line 13 - MACDclose, Fast length (12), Slow length (26), Signal length (9)
signalLine · line 13 - MACDclose, Fast length (12), Slow length (26), Signal length (9)
hist · line 13 - EMAclose, Trend filter length (100)
trend · line 14 - CrossovermacdLine, signalLine
bullCross · line 16 - CrossundermacdLine, signalLine
bearCross · line 17
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 | bullCross and close > trend | On | 20 |
| Close Long | bearCross and strategy.position_size > 0 | On | 22 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| Bullish cross in an uptrend | alertcondition | MACD crossed above signal with price above the trend average | 29 |
| Bearish cross | alertcondition | MACD crossed below its signal line | 30 |
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.0% | +102.5% | 21 | 29% | 0.81 | 3.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.5% | +5.5% | 15 | 40% | 0.59 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.4% | +9.8% | 16 | 31% | 0.84 | 4.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −1.9% | +8.4% | 21 | 14% | 0.28 | 2.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +1.0% | +16.7% | 14 | 50% | 1.18 | 4.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +1.9% | +21.5% | 20 | 40% | 1.84 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +1.0% | +148.3% | 18 | 50% | 1.24 | 2.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1.3% | +17.1% | 20 | 40% | 1.94 | 0.4% | 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 | 13 | 31% | $121.20 |
| Sideways market | 8 | 25% | −$224.03 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
4.4%
The 95th percentile of the worst drawdown when the same 21 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 2.1% to 4.4%; the tick is what actually happened, 3.4%. Order luck alone moves it this much.
All 21 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 6 May 2024 64,012.00 | 12 May 2024 60,826.00 | 6 | −$50.72 |
| long | 13 May 2024 61,484.00 | 31 May 2024 68,352.20 | 18 | $110.03 |
| long | 6 Jun 2024 71,108.00 | 9 Jun 2024 69,310.40 | 3 | −$26.41 |
| long | 15 Oct 2024 66,084.00 | 27 Oct 2024 67,092.80 | 12 | $14.30 |
| long | 29 Oct 2024 69,962.20 | 4 Nov 2024 68,776.00 | 6 | −$18.02 |
| long | 7 Nov 2024 75,572.00 | 27 Nov 2024 91,965.10 | 20 | $216.33 |
| long | 17 Dec 2024 106,059.00 | 19 Dec 2024 100,204.00 | 2 | −$57.52 |
| long | 6 Jan 2025 98,363.60 | 10 Jan 2025 92,552.50 | 4 | −$61.15 |
| long | 16 Jan 2025 100,497.00 | 29 Jan 2025 101,336.00 | 13 | $7.43 |
| long | 21 Feb 2025 98,305.00 | 25 Feb 2025 91,552.90 | 4 | −$70.55 |
| long | 19 May 2025 106,454.00 | 20 May 2025 105,574.00 | 1 | −$9.32 |
| long | 22 May 2025 109,644.00 | 25 May 2025 107,762.00 | 3 | −$18.25 |
| long | 11 Jun 2025 110,274.00 | 13 Jun 2025 105,672.00 | 2 | −$42.85 |
| long | 27 Jun 2025 106,947.00 | 22 Jul 2025 117,380.00 | 25 | $96.39 |
| long | 23 Jul 2025 119,954.00 | 24 Jul 2025 118,756.00 | 1 | −$11.08 |
| long | 12 Aug 2025 118,686.00 | 18 Aug 2025 117,405.00 | 6 | −$11.87 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$50.36 |
| long | 27 Oct 2025 114,559.00 | 4 Nov 2025 106,583.00 | 8 | −$70.67 |
| long | 5 May 2026 79,861.00 | 13 May 2026 80,504.50 | 8 | $7.01 |
| long | 4 Sep 2026 81,270.40 | 5 Sep 2026 79,660.80 | 1 | −$20.69 |
| long | 22 Sep 2026 86,620.00 | 30 Sep 2026 83,663.60 | 8 | −$34.86 |
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 |
|---|---|---|---|---|---|
| MACD Trend Strategy this page | — | v5 strategy | −1.0% | 21 | 3.5% |
| Short Swing Bearish MACD Cross (By Coinrule) by Coinrule | ema macd | v5 strategy | −6.9% | 8 | 6.9% |
| Fast EMA above Slow EMA with MACD (by Coinrule) by Coinrule | ema macd | v5 strategy | −4.5% | 18 | 7.7% |
| EMA and MACD with Trailing Stop Loss (by Coinrule) by Coinrule | ema macd | v5 strategy | +3.3% | 22 | 4.6% |
| 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.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
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: 21 before, 21 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.
⋯// © Zeal Terminal-//@version=5+//@version=6strategy("MACD Trend Strategy", shorttitle = "MACD Trend", overlay = false, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 18 ms | Yes 1,000 bars in 31 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“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
Written by Zeal Terminal and published under the Mozilla Public License 2.0.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Zeal Terminal//@version=5strategy("MACD Trend Strategy", shorttitle = "MACD Trend", overlay = false, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 1) // Not investment advice. Goes long when MACD crosses above its signal line// above a rising trend average, and flat when it crosses back below.fastLen = input.int(12, "Fast length", minval = 1)slowLen = input.int(26, "Slow length", minval = 1)signalLen = input.int(9, "Signal length", minval = 1)trendLen = input.int(100, "Trend filter length", minval = 1) [macdLine, signalLine, hist] = ta.macd(close, fastLen, slowLen, signalLen)trend = ta.ema(close, trendLen) bullCross = ta.crossover(macdLine, signalLine)bearCross = ta.crossunder(macdLine, signalLine) if bullCross and close > trend strategy.entry("Long", strategy.long)if bearCross and strategy.position_size > 0 strategy.close("Long", comment = "MACD exit") plot(hist, "Histogram", style = plot.style_columns, color = hist >= 0 ? color.new(#26a69a, 40) : color.new(#ef5350, 40))plot(macdLine, "MACD", color = #42a5f5)plot(signalLine, "Signal", color = #ff7043)hline(0, "Zero", color = color.new(#787b86, 50)) alertcondition(bullCross and close > trend, "Bullish cross in an uptrend", "MACD crossed above signal with price above the trend average")alertcondition(bearCross, "Bearish cross", "MACD crossed below its signal line")