Pine Script v4 · Strategy
MACD Long Strat
A Pine Script v4 strategy built on EMA (close, fast) and SMA (macd, 9). 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 is one contract and pays no commission. It has no settings.
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 Long Strat repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 23 of 23 trades.
- How did MACD Long Strat do on BTCUSDT 1D with default settings?
- +23.95%
- From 26 Jan 2024 to 21 Oct 2026, over 79 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can MACD Long Strat 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 MACD Long Strat use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MACD Long Strat run in Zeal Terminal?
- Yes
- It compiles in 6 ms and runs 1,000 bars in 19 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +23.95%
- $23,954.80
- Buy and hold
- +102.5%
- same period
- Closed trades
- 79
- 39 won · 40 lost
- Profitable
- 49.4%
- of closed trades
- Profit factor
- 1.24
- gross profit ÷ gross loss
- Max drawdown
- 20.12%
- −$24,807.30
One run with the author's default settings, $100,000 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
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
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, sma → ta.sma.
Same 79 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 7: ema → ta.ema
- Line 8: ema → ta.ema
- Line 10: sma → ta.sma
⋯// © TheGrindToday-//@version=4+//@version=5strategy("MACD Long Strat", overlay=false)⋯fast = 6, slow = 26-fastMA = ema(close, fast)+fastMA = ta.ema(close, fast)-slowMA = ema(close, slow)+slowMA = ta.ema(close, slow)macd = fastMA - slowMA-signal = sma(macd, 9)+signal = ta.sma(macd, 9)histogram = macd-signal⋯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: 79 before, 74 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.
⋯// © TheGrindToday-//@version=5+//@version=6strategy("MACD Long Strat", overlay=false)⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
The script declares no settings.
What it calculates
- EMA (
ema,ta.emain v5)close, fastfastMA · line 7 - EMA (
ema,ta.emain v5)close, slowslowMA · line 8 - SMA (
sma,ta.smain v5)macd, 9signal · line 10
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 | (LongEntryCondition and histogram_reversing_negative) | On | 19 |
| Close Long | (exitConditionLong) | On | 21 |
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 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 | +24.0% | +102.5% | 79 | 49% | 1.24 | 20.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +11.2% | +5.5% | 80 | 49% | 1.36 | 9.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.6% | +9.8% | 78 | 41% | 0.91 | 2.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.4% | +8.4% | 83 | 45% | 1.28 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 83 | 41% | 0.88 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 79 | 42% | 1.20 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.4% | +148.3% | 83 | 47% | 1.39 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 81 | 42% | 1.27 | 0.1% | 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 | 26 | 54% | $12,907.80 |
| Sideways market | 33 | 52% | $26,991.00 |
| Falling market | 20 | 40% | −$15,944.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
31.6%
The 95th percentile of the worst drawdown when the same 79 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 14.4% to 31.6%; the tick is what actually happened, 18.0%. Order luck alone moves it this much.
All 79 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 2 Mar 2024 62,387.90 | 4 Mar 2024 63,114.00 | 2 | $726.07 |
| long | 22 Mar 2024 65,501.30 | 31 Mar 2024 69,582.20 | 9 | $4,080.89 |
| long | 6 Apr 2024 67,820.60 | 13 Apr 2024 67,116.50 | 7 | −$704.11 |
| long | 20 Apr 2024 63,818.00 | 26 Apr 2024 64,498.30 | 6 | $680.32 |
| long | 5 May 2024 63,892.00 | 9 May 2024 61,193.00 | 4 | −$2,699.00 |
| long | 17 May 2024 65,235.20 | 20 May 2024 66,274.00 | 3 | $1,038.79 |
| long | 3 Jun 2024 67,765.60 | 8 Jun 2024 69,355.60 | 5 | $1,589.98 |
| long | 17 Jun 2024 66,676.90 | 25 Jun 2024 60,293.30 | 8 | −$6,383.56 |
| long | 27 Jun 2024 60,865.00 | 4 Jul 2024 60,208.60 | 7 | −$656.41 |
| long | 10 Jul 2024 58,050.00 | 19 Jul 2024 63,987.90 | 9 | $5,937.92 |
| long | 8 Aug 2024 55,133.80 | 16 Aug 2024 57,541.10 | 8 | $2,407.29 |
| long | 21 Aug 2024 59,013.80 | 27 Aug 2024 62,834.00 | 6 | $3,820.20 |
| long | 4 Sep 2024 57,487.70 | 7 Sep 2024 53,963.00 | 3 | −$3,524.77 |
| long | 10 Sep 2024 57,042.00 | 17 Sep 2024 58,214.00 | 7 | $1,171.98 |
| long | 20 Sep 2024 62,948.00 | 22 Sep 2024 63,349.00 | 2 | $400.97 |
| long | 28 Sep 2024 65,769.90 | 29 Sep 2024 65,858.00 | 1 | $88.05 |
| long | 6 Oct 2024 62,058.00 | 11 Oct 2024 60,326.40 | 5 | −$1,731.61 |
| long | 13 Oct 2024 63,206.20 | 19 Oct 2024 68,428.00 | 6 | $5,221.76 |
| long | 29 Oct 2024 69,962.20 | 2 Nov 2024 69,496.00 | 4 | −$466.21 |
| long | 8 Nov 2024 75,857.90 | 15 Nov 2024 87,325.60 | 7 | $11,467.70 |
| long | 23 Nov 2024 98,892.00 | 24 Nov 2024 97,672.40 | 1 | −$1,219.60 |
| long | 1 Dec 2024 96,408.00 | 10 Dec 2024 97,276.50 | 9 | $868.49 |
| long | 13 Dec 2024 100,004.00 | 19 Dec 2024 100,204.00 | 6 | $199.72 |
| long | 26 Dec 2024 99,429.60 | 31 Dec 2024 92,792.10 | 5 | −$6,637.56 |
| long | 2 Jan 2025 94,591.80 | 9 Jan 2025 95,060.60 | 7 | $468.83 |
| long | 15 Jan 2025 96,560.90 | 20 Jan 2025 101,332.00 | 5 | $4,770.72 |
| long | 31 Jan 2025 104,723.00 | 2 Feb 2025 100,636.00 | 2 | −$4,087.28 |
| long | 9 Feb 2025 96,444.80 | 17 Feb 2025 96,118.10 | 8 | −$326.63 |
| long | 21 Feb 2025 98,305.00 | 23 Feb 2025 96,551.00 | 2 | −$1,754.00 |
| long | 3 Mar 2025 94,270.00 | 9 Mar 2025 86,222.50 | 6 | −$8,047.53 |
| long | 13 Mar 2025 83,680.10 | 22 Mar 2025 84,088.80 | 9 | $408.67 |
| long | 25 Mar 2025 87,498.20 | 27 Mar 2025 86,909.20 | 2 | −$588.99 |
| long | 5 Apr 2025 83,889.90 | 7 Apr 2025 78,430.00 | 2 | −$5,459.87 |
| long | 11 Apr 2025 79,607.30 | 17 Apr 2025 84,030.40 | 6 | $4,423.08 |
| long | 23 Apr 2025 93,443.00 | 27 Apr 2025 94,628.00 | 4 | $1,185.01 |
| long | 8 May 2025 97,030.50 | 13 May 2025 102,791.00 | 5 | $5,760.82 |
| long | 20 May 2025 105,574.00 | 25 May 2025 107,762.00 | 5 | $2,188.17 |
| long | 3 Jun 2025 105,858.00 | 6 Jun 2025 101,509.00 | 3 | −$4,349.31 |
| long | 8 Jun 2025 105,552.00 | 13 Jun 2025 105,672.00 | 5 | $119.59 |
| long | 25 Jun 2025 106,083.00 | 1 Jul 2025 107,147.00 | 6 | $1,063.51 |
| long | 4 Jul 2025 109,585.00 | 5 Jul 2025 107,984.00 | 1 | −$1,600.52 |
| long | 11 Jul 2025 116,010.00 | 16 Jul 2025 117,758.00 | 5 | $1,748.07 |
| long | 24 Jul 2025 118,756.00 | 25 Jul 2025 118,341.00 | 1 | −$415.02 |
| long | 28 Jul 2025 119,416.00 | 30 Jul 2025 117,951.00 | 2 | −$1,464.81 |
| long | 5 Aug 2025 115,055.00 | 15 Aug 2025 118,295.00 | 10 | $3,240.06 |
| long | 24 Aug 2025 115,438.00 | 26 Aug 2025 110,112.00 | 2 | −$5,326.08 |
| long | 28 Aug 2025 111,262.00 | 30 Aug 2025 108,377.00 | 2 | −$2,884.61 |
| long | 2 Sep 2025 109,237.00 | 15 Sep 2025 115,268.00 | 13 | $6,030.58 |
| long | 29 Sep 2025 112,164.00 | 7 Oct 2025 124,659.00 | 8 | $12,494.60 |
| long | 15 Oct 2025 113,028.00 | 17 Oct 2025 108,194.00 | 2 | −$4,833.86 |
| long | 20 Oct 2025 108,643.00 | 29 Oct 2025 112,898.00 | 9 | $4,255.67 |
| long | 9 Nov 2025 102,313.00 | 14 Nov 2025 99,692.00 | 5 | −$2,620.92 |
| long | 20 Nov 2025 91,555.00 | 21 Nov 2025 86,637.20 | 1 | −$4,917.74 |
| long | 24 Nov 2025 86,830.00 | 1 Dec 2025 90,360.00 | 7 | $3,530.01 |
| long | 4 Dec 2025 93,429.90 | 6 Dec 2025 89,330.00 | 2 | −$4,099.91 |
| long | 21 Dec 2025 88,360.90 | 25 Dec 2025 87,669.40 | 4 | −$691.47 |
| long | 3 Jan 2026 89,995.10 | 8 Jan 2026 91,364.20 | 5 | $1,369.02 |
| long | 15 Jan 2026 96,951.80 | 18 Jan 2026 95,147.80 | 3 | −$1,804.01 |
| long | 25 Jan 2026 89,225.30 | 30 Jan 2026 84,650.20 | 5 | −$4,575.18 |
| long | 8 Feb 2026 69,289.40 | 18 Feb 2026 67,503.50 | 10 | −$1,785.85 |
| long | 27 Feb 2026 67,485.20 | 1 Mar 2026 66,973.30 | 2 | −$511.93 |
| long | 4 Mar 2026 68,338.00 | 7 Mar 2026 68,114.00 | 3 | −$223.99 |
| long | 12 Mar 2026 70,191.90 | 19 Mar 2026 71,246.60 | 7 | $1,054.69 |
| long | 25 Mar 2026 70,556.70 | 28 Mar 2026 66,407.30 | 3 | −$4,149.46 |
| long | 1 Apr 2026 68,284.50 | 13 Apr 2026 70,741.60 | 12 | $2,457.07 |
| long | 24 Apr 2026 78,257.50 | 25 Apr 2026 77,437.10 | 1 | −$820.35 |
| long | 3 May 2026 78,686.80 | 9 May 2026 80,193.20 | 6 | $1,506.34 |
| long | 21 May 2026 77,552.20 | 28 May 2026 74,449.30 | 7 | −$3,102.93 |
| long | 1 Jun 2026 73,674.40 | 2 Jun 2026 71,408.90 | 1 | −$2,265.49 |
| long | 8 Jun 2026 63,332.00 | 18 Jun 2026 64,509.40 | 10 | $1,177.39 |
| long | 29 Jun 2026 59,577.00 | 8 Jul 2026 63,364.00 | 9 | $3,786.99 |
| long | 16 Jul 2026 64,756.30 | 17 Jul 2026 63,830.20 | 1 | −$926.08 |
| long | 22 Jul 2026 66,556.10 | 24 Jul 2026 65,099.00 | 2 | −$1,457.17 |
| long | 4 Aug 2026 63,520.00 | 11 Aug 2026 63,970.00 | 7 | $450.01 |
| long | 19 Aug 2026 64,725.40 | 25 Aug 2026 78,992.80 | 6 | $14,267.30 |
| long | 5 Sep 2026 79,660.80 | 8 Sep 2026 79,112.00 | 3 | −$548.77 |
| long | 14 Sep 2026 76,842.00 | 17 Sep 2026 76,206.00 | 3 | −$636.01 |
| long | 18 Sep 2026 76,417.00 | 25 Sep 2026 84,410.20 | 7 | $7,993.23 |
| long | 3 Oct 2026 84,518.00 | 8 Oct 2026 83,321.80 | 5 | −$1,196.19 |
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 Long Strat this page | — | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
| BTC WaveTrend R-R=1-1.5 by TrendCrypto2022 | ema sma | v5 strategy | +0.2% | 28 | 2.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 6 ms | Yes 1,000 bars in 19 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © TheGrindToday//@version=4strategy("MACD Long Strat", overlay=false)//fast = 12, slow = 26fast = 6, slow = 26fastMA = ema(close, fast)slowMA = ema(close, slow)macd = fastMA - slowMAsignal = sma(macd, 9)histogram = macd-signalmacdpos = histogram[0] > 0macdneg = histogram[0] < 0histogram_reversing_negative = histogram[1] > histogram[2]LongEntryCondition = histogram > histogram[1] ShortEntryCondition = histogram < histogram[1]exitConditionLong = histogram[0] < histogram[2]if (LongEntryCondition and histogram_reversing_negative) strategy.entry("Long", strategy.long)if (exitConditionLong) strategy.close("Long") plot(histogram)