Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (4)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOctHistogram$9,897$20,253Equity from $10,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.
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.

SettingDefaultType and rangeFeeds
Fast length12int · min 1macdLine signalLine hist
Slow length26int · min 1macdLine signalLine hist
Signal length9int · min 1macdLine signalLine hist
Trend filter length100int · min 1trend

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, signalLinebullCross · line 16
  • CrossundermacdLine, signalLinebearCross · 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.

closecloseprice datafastLenFast lengthsetting · 12slowLenSlow lengthsetting · 26signalLenSignal lengthsetting · 9trendLenTrend filter lengthsetting · 100macdLineMACD→ macdLinesignalLineMACD→ signalLinehistMACD→ histtrendEMA→ trendbullCrossCrossover→ bullCrossbearCrossCrossunder→ bearCrossentry:LongOpen Longstrategy.entryclose:LongClose Longstrategy.closeplot:24Histogramplotplot:25MACDplotplot:26Signalplothline:27hlinehlinealertcondition:29Bullish cross in an upt…alertconditionalertcondition:30Bearish crossalertcondition

Order rules

OrderFires whenBy defaultLine
Open Long longbullCross and close > trendOn20
Close LongbearCross and strategy.position_size > 0On22

Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.

Alerts

AlertCallMessageLine
Bullish cross in an uptrendalertconditionMACD crossed above signal with price above the trend average29
Bearish crossalertconditionMACD crossed below its signal line30

Does it repaint?

No

No repainting patterns in the source, and the live replay matched history.

3/3trades 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.0%+102.5%2129%0.813.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.5%+5.5%1540%0.590.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−1.4%+9.8%1631%0.844.7%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−1.9%+8.4%2114%0.282.4%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+1.0%+16.7%1450%1.184.2%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+1.9%+21.5%2040%1.841.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+1.0%+148.3%1850%1.242.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+1.3%+17.1%2040%1.940.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 barsTradesWonP&L
Rising market1331%$121.20
Sideways market825%−$224.03
Falling market0—$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.

0%5%
All 21 trades
SideEntryExitBarsP&L
long6 May 2024 64,012.0012 May 2024 60,826.006−$50.72
long13 May 2024 61,484.0031 May 2024 68,352.2018$110.03
long6 Jun 2024 71,108.009 Jun 2024 69,310.403−$26.41
long15 Oct 2024 66,084.0027 Oct 2024 67,092.8012$14.30
long29 Oct 2024 69,962.204 Nov 2024 68,776.006−$18.02
long7 Nov 2024 75,572.0027 Nov 2024 91,965.1020$216.33
long17 Dec 2024 106,059.0019 Dec 2024 100,204.002−$57.52
long6 Jan 2025 98,363.6010 Jan 2025 92,552.504−$61.15
long16 Jan 2025 100,497.0029 Jan 2025 101,336.0013$7.43
long21 Feb 2025 98,305.0025 Feb 2025 91,552.904−$70.55
long19 May 2025 106,454.0020 May 2025 105,574.001−$9.32
long22 May 2025 109,644.0025 May 2025 107,762.003−$18.25
long11 Jun 2025 110,274.0013 Jun 2025 105,672.002−$42.85
long27 Jun 2025 106,947.0022 Jul 2025 117,380.0025$96.39
long23 Jul 2025 119,954.0024 Jul 2025 118,756.001−$11.08
long12 Aug 2025 118,686.0018 Aug 2025 117,405.006−$11.87
long2 Oct 2025 118,595.0011 Oct 2025 112,774.009−$50.36
long27 Oct 2025 114,559.004 Nov 2025 106,583.008−$70.67
long5 May 2026 79,861.0013 May 2026 80,504.508$7.01
long4 Sep 2026 81,270.405 Sep 2026 79,660.801−$20.69
long22 Sep 2026 86,620.0030 Sep 2026 83,663.608−$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.

ScriptSharesTypeNetTradesMax drawdown
MACD Trend Strategy this page—v5 strategy−1.0%213.5%
Short Swing Bearish MACD Cross (By Coinrule) by Coinruleema macdv5 strategy−6.9%86.9%
Fast EMA above Slow EMA with MACD (by Coinrule) by Coinruleema macdv5 strategy−4.5%187.7%
EMA and MACD with Trailing Stop Loss (by Coinrule) by Coinruleema macdv5 strategy+3.3%224.6%
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.

  • No v6-only features. v6 is not needed to run this script.

Clean v5

Verified

Nothing 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 18 msYes 1,000 bars in 31 msNot measured
PineTSNot testedNot testedNot 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")