Pine Script v4 · Strategy
MACD+ Strategy [SystemAlpha]
A Pine Script v4 strategy built on EMA (close, 12), Crossover (delta, 0) and Crossunder (delta, 0). It has 2 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.04% commission. 10 settings; defaults include Fast Length 12, Slow Length 26 and MACD 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+ Strategy [SystemAlpha] repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 22 of 22 trades.
- How did MACD+ Strategy [SystemAlpha] do on BTCUSDT 1D with default settings?
- +2.85%
- From 26 Jan 2024 to 21 Oct 2026, over 76 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can MACD+ Strategy [SystemAlpha] 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+ Strategy [SystemAlpha] use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MACD+ Strategy [SystemAlpha] run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 33 ms, in the browser.
BTCUSDT · 1D · default settings
- EMA
- EMA
- Crossover
- Crossunder
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +2.85%
- $285.32
- Buy and hold
- +102.5%
- same period
- Closed trades
- 76
- 25 won · 51 lost
- Profitable
- 32.9%
- of closed trades
- Profit factor
- 1.01
- gross profit ÷ gross loss
- Max drawdown
- 48.01%
- −$7,454.91
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A short 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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
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, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
VerifiedSame 76 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 14: ema → ta.ema
- Line 14: ema → ta.ema
- Line 15: ema → ta.ema
- Line 18: crossover → ta.crossover
- Line 19: crossunder → ta.crossunder
⋯// © systemalphatrader-//@version=4+//@version=5strategy(title="MACD Strategy", shorttitle="MACD Strategy [PUBLIC]", overlay=true, initial_capital=10000, currency='USD', ⋯MACDLength = input(9, title="MACD Length")-MACD = ema(close, fastLength) - ema(close, slowlength)+MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)-aMACD = ema(MACD, MACDLength)+aMACD = ta.ema(MACD, MACDLength)delta = MACD - aMACD// Calc breakouts-buySignal = crossover(delta, 0)+buySignal = ta.crossover(delta, 0)-shortSignal = crossunder(delta,0)+shortSignal = ta.crossunder(delta,0)// == /MACD ==⋯Same 76 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
- Line 25: input() → input.int()
- Line 26: input() → input.int()
- Line 27: input() → input.int()
4 more edits
- Line 28: input() → input.int()
- Line 29: input() → input.int()
- Line 30: input() → input.int()
- Line 38: input() → input.string()
⋯// == MACD ==-fastLength = input(12, title="Fast Length")+fastLength = input.int(12, title="Fast Length")-slowlength = input(26, title="Slow Length")+slowlength = input.int(26, title="Slow Length")-MACDLength = input(9, title="MACD Length")+MACDLength = input.int(9, title="MACD Length")MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)⋯// === BACKTEST RANGE ===-From_Year = input(defval = 2017, title = "BACKTEST: From Year")+From_Year = input.int(defval = 2017, title = "BACKTEST: From Year")-From_Month = input(defval = 1, title = "BACKTEST: From Month", minval = 1, maxval = 12)+From_Month = input.int(defval = 1, title = "BACKTEST: From Month", minval = 1, maxval = 12)-From_Day = input(defval = 1, title = "BACKTEST: From Day", minval = 1, maxval = 31)+From_Day = input.int(defval = 1, title = "BACKTEST: From Day", minval = 1, maxval = 31)-To_Year = input(defval = 9999, title = "BACKTEST: To Year")+To_Year = input.int(defval = 9999, title = "BACKTEST: To Year")-To_Month = input(defval = 1, title = "BACKTEST: To Month", minval = 1, maxval = 12)+To_Month = input.int(defval = 1, title = "BACKTEST: To Month", minval = 1, maxval = 12)-To_Day = input(defval = 1, title = "BACKTEST: To Day", minval = 1, maxval = 31)+To_Day = input.int(defval = 1, title = "BACKTEST: To Day", minval = 1, maxval = 31)Start = timestamp(From_Year, From_Month, From_Day, 00, 00) // backtest start window⋯// Make input option to configure trade direction-tradeDirection = input(title="Trade Direction", defval="Both", options=["Long", "Short", "Both"]) +tradeDirection = input.string(title="Trade Direction", defval="Both", options=["Long", "Short", "Both"]) // Translate input into trading conditions⋯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: 76 before, 77 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.
⋯// © systemalphatrader-//@version=5+//@version=6strategy(title="MACD Strategy", shorttitle="MACD Strategy [PUBLIC]", overlay=true, initial_capital=10000, currency='USD', ⋯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 | MACD |
| Slow Length | 26 | int | MACD |
| MACD Length | 9 | int | aMACD |
| BACKTEST: From Year | 2017 | int | Start |
| BACKTEST: From Month | 1 | int · min 1 · max 12 | Start |
| BACKTEST: From Day | 1 | int · min 1 · max 31 | Start |
| BACKTEST: To Year | 9999 | int | Finish |
| BACKTEST: To Month | 1 | int · min 1 · max 12 | Finish |
| BACKTEST: To Day | 1 | int · min 1 · max 31 | Finish |
| Trade Direction | Both | string · options ["Long", "Short", "Both"] | longOK shortOK |
What it calculates
- EMA (
ema,ta.emain v5)close, Fast Length (12)MACD · line 14 - EMA (
ema,ta.emain v5)MACD, MACD Length (9)aMACD · line 15 - Crossover (
crossover,ta.crossoverin v5)delta, 0buySignal · line 18 - Crossunder (
crossunder,ta.crossunderin v5)delta, 0shortSignal · line 19
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 | longCondition | On | 47 |
| Open Short short | shortcondition | On | 50 |
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 | +2.9% | +102.5% | 76 | 33% | 1.01 | 48.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +20.4% | +5.5% | 68 | 35% | 1.28 | 13.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −54.6% | +9.8% | 72 | 32% | 0.78 | 77.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −2.8% | +8.4% | 76 | 26% | 0.98 | 31.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +45.2% | +16.7% | 63 | 49% | 1.11 | 73.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −10.5% | +21.5% | 70 | 33% | 0.91 | 36.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −58.0% | +148.3% | 68 | 43% | 0.60 | 63.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −2.8% | +17.1% | 77 | 32% | 0.97 | 22.8% | 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 | 27 | 30% | −$2,377.42 |
| Sideways market | 34 | 41% | $3,300.10 |
| Falling market | 15 | 20% | −$633.25 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
61.6%
The 95th percentile of the worst drawdown when the same 76 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 33.7% to 61.6%; the tick is what actually happened, 47.4%. Order luck alone moves it this much.
All 76 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 16 Mar 2024 69,499.80 | 6 May 2024 64,012.00 | 51 | $781.62 |
| long | 6 May 2024 64,012.00 | 12 May 2024 60,826.00 | 6 | −$545.00 |
| short | 12 May 2024 60,826.00 | 13 May 2024 61,484.00 | 1 | −$118.97 |
| long | 13 May 2024 61,484.00 | 31 May 2024 68,352.20 | 18 | $1,121.67 |
| short | 31 May 2024 68,352.20 | 6 Jun 2024 71,108.00 | 6 | −$462.32 |
| long | 6 Jun 2024 71,108.00 | 9 Jun 2024 69,310.50 | 3 | −$280.95 |
| short | 9 Jun 2024 69,310.50 | 13 Jul 2024 57,889.10 | 34 | $1,721.89 |
| long | 13 Jul 2024 57,889.10 | 1 Aug 2024 64,628.00 | 19 | $1,411.80 |
| short | 1 Aug 2024 64,628.00 | 20 Aug 2024 59,438.50 | 19 | $1,083.98 |
| long | 20 Aug 2024 59,438.50 | 31 Aug 2024 59,124.00 | 11 | −$89.59 |
| short | 31 Aug 2024 59,124.00 | 13 Sep 2024 58,132.30 | 13 | $233.69 |
| long | 13 Sep 2024 58,132.30 | 2 Oct 2024 60,804.90 | 19 | $670.91 |
| short | 2 Oct 2024 60,804.90 | 15 Oct 2024 66,084.00 | 13 | −$1,361.17 |
| long | 15 Oct 2024 66,084.00 | 27 Oct 2024 67,092.80 | 12 | $204.86 |
| short | 27 Oct 2024 67,092.80 | 29 Oct 2024 69,962.20 | 2 | −$626.43 |
| long | 29 Oct 2024 69,962.20 | 4 Nov 2024 68,776.00 | 6 | −$243.98 |
| short | 4 Nov 2024 68,776.00 | 7 Nov 2024 75,572.00 | 3 | −$1,345.52 |
| long | 7 Nov 2024 75,572.00 | 27 Nov 2024 91,965.20 | 20 | $2,626.45 |
| short | 27 Nov 2024 91,965.20 | 17 Dec 2024 106,059.00 | 20 | −$2,278.19 |
| long | 17 Dec 2024 106,059.00 | 19 Dec 2024 100,204.00 | 2 | −$700.12 |
| short | 19 Dec 2024 100,204.00 | 6 Jan 2025 98,363.60 | 18 | $207.45 |
| long | 6 Jan 2025 98,363.60 | 10 Jan 2025 92,552.50 | 4 | −$718.97 |
| short | 10 Jan 2025 92,552.50 | 16 Jan 2025 100,497.00 | 6 | −$978.84 |
| long | 16 Jan 2025 100,497.00 | 29 Jan 2025 101,336.00 | 13 | $77.74 |
| short | 29 Jan 2025 101,336.00 | 21 Feb 2025 98,305.00 | 23 | $302.59 |
| long | 21 Feb 2025 98,305.00 | 25 Feb 2025 91,552.90 | 4 | −$742.81 |
| short | 25 Feb 2025 91,552.90 | 7 Mar 2025 89,931.90 | 10 | $168.31 |
| long | 7 Mar 2025 89,931.90 | 9 Mar 2025 86,222.50 | 2 | −$425.35 |
| short | 9 Mar 2025 86,222.50 | 16 Mar 2025 84,338.40 | 7 | $204.17 |
| long | 16 Mar 2025 84,338.40 | 7 Apr 2025 78,430.00 | 22 | −$701.11 |
| short | 7 Apr 2025 78,430.00 | 12 Apr 2025 83,423.80 | 5 | −$593.24 |
| long | 12 Apr 2025 83,423.80 | 18 May 2025 103,127.00 | 36 | $2,024.62 |
| short | 18 May 2025 103,127.00 | 19 May 2025 106,454.00 | 1 | −$351.62 |
| long | 19 May 2025 106,454.00 | 20 May 2025 105,574.00 | 1 | −$93.20 |
| short | 20 May 2025 105,574.00 | 22 May 2025 109,644.00 | 2 | −$400.95 |
| long | 22 May 2025 109,644.00 | 25 May 2025 107,762.00 | 3 | −$175.70 |
| short | 25 May 2025 107,762.00 | 11 Jun 2025 110,274.00 | 17 | −$231.78 |
| long | 11 Jun 2025 110,274.00 | 13 Jun 2025 105,672.00 | 2 | −$398.69 |
| short | 13 Jun 2025 105,672.00 | 27 Jun 2025 106,947.00 | 14 | −$115.57 |
| long | 27 Jun 2025 106,947.00 | 22 Jul 2025 117,380.00 | 25 | $857.08 |
| short | 22 Jul 2025 117,380.00 | 23 Jul 2025 119,954.00 | 1 | −$220.99 |
| long | 23 Jul 2025 119,954.00 | 24 Jul 2025 118,756.00 | 1 | −$102.45 |
| short | 24 Jul 2025 118,756.00 | 12 Aug 2025 118,686.00 | 19 | −$1.98 |
| long | 12 Aug 2025 118,686.00 | 18 Aug 2025 117,405.00 | 6 | −$108.86 |
| short | 18 Aug 2025 117,405.00 | 7 Sep 2025 110,188.00 | 20 | $563.53 |
| long | 7 Sep 2025 110,188.00 | 24 Sep 2025 111,999.00 | 17 | $153.89 |
| short | 24 Sep 2025 111,999.00 | 2 Oct 2025 118,595.00 | 8 | −$597.30 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$468.93 |
| short | 11 Oct 2025 112,774.00 | 27 Oct 2025 114,559.00 | 16 | −$148.63 |
| long | 27 Oct 2025 114,559.00 | 4 Nov 2025 106,583.00 | 8 | −$618.61 |
| short | 4 Nov 2025 106,583.00 | 28 Nov 2025 91,333.90 | 24 | $1,162.62 |
| long | 28 Nov 2025 91,333.90 | 18 Dec 2025 86,243.20 | 20 | −$527.29 |
| short | 18 Dec 2025 86,243.20 | 21 Dec 2025 88,360.90 | 3 | −$223.30 |
| long | 21 Dec 2025 88,360.90 | 21 Jan 2026 88,427.70 | 31 | −$0.39 |
| short | 21 Jan 2026 88,427.70 | 16 Feb 2026 68,832.60 | 26 | $1,895.20 |
| long | 16 Feb 2026 68,832.60 | 23 Mar 2026 67,859.00 | 35 | −$156.46 |
| short | 23 Mar 2026 67,859.00 | 26 Mar 2026 71,336.50 | 3 | −$537.27 |
| long | 26 Mar 2026 71,336.50 | 27 Mar 2026 68,820.30 | 1 | −$352.72 |
| short | 27 Mar 2026 68,820.30 | 7 Apr 2026 68,853.70 | 11 | −$12.11 |
| long | 7 Apr 2026 68,853.70 | 29 Apr 2026 76,342.80 | 22 | $1,016.30 |
| short | 29 Apr 2026 76,342.80 | 5 May 2026 79,861.00 | 6 | −$489.34 |
| long | 5 May 2026 79,861.00 | 13 May 2026 80,504.50 | 8 | $72.13 |
| short | 13 May 2026 80,504.50 | 14 Jun 2026 64,458.00 | 32 | $1,989.18 |
| long | 14 Jun 2026 64,458.00 | 28 Jun 2026 60,029.00 | 14 | −$834.04 |
| short | 28 Jun 2026 60,029.00 | 30 Jun 2026 60,260.20 | 2 | −$51.98 |
| long | 30 Jun 2026 60,260.20 | 1 Jul 2026 58,624.70 | 1 | −$310.55 |
| short | 1 Jul 2026 58,624.70 | 2 Jul 2026 60,024.00 | 1 | −$266.73 |
| long | 2 Jul 2026 60,024.00 | 28 Jul 2026 63,755.90 | 26 | $646.72 |
| short | 28 Jul 2026 63,755.90 | 8 Aug 2026 64,923.20 | 11 | −$213.89 |
| long | 8 Aug 2026 64,923.20 | 12 Aug 2026 63,600.00 | 4 | −$232.36 |
| short | 12 Aug 2026 63,600.00 | 19 Aug 2026 64,725.40 | 7 | −$198.75 |
| long | 19 Aug 2026 64,725.40 | 3 Sep 2026 77,340.00 | 15 | $2,045.62 |
| short | 3 Sep 2026 77,340.00 | 4 Sep 2026 81,270.40 | 1 | −$650.09 |
| long | 4 Sep 2026 81,270.40 | 5 Sep 2026 79,660.80 | 1 | −$245.92 |
| short | 5 Sep 2026 79,660.80 | 22 Sep 2026 86,620.00 | 17 | −$1,031.28 |
| long | 22 Sep 2026 86,620.00 | 30 Sep 2026 83,663.70 | 8 | −$372.30 |
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+ Strategy [SystemAlpha] this page | — | v4 strategy | +2.9% | 76 | 48.0% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 33 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/// © systemalphatrader//@version=4strategy(title="MACD Strategy", shorttitle="MACD Strategy [PUBLIC]", overlay=true, initial_capital=10000, currency='USD', default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.04)// == /MAIN INPUT ==///////////////////////////////////////*STRATEGY LOGIC *///////////////////////////////////////// == MACD ==fastLength = input(12, title="Fast Length")slowlength = input(26, title="Slow Length")MACDLength = input(9, title="MACD Length")MACD = ema(close, fastLength) - ema(close, slowlength)aMACD = ema(MACD, MACDLength)delta = MACD - aMACD// Calc breakoutsbuySignal = crossover(delta, 0)shortSignal = crossunder(delta,0)// == /MACD ==////////////////////////////* STRATEGY COMPONENT *////////////////////////////// === BACKTEST RANGE ===From_Year = input(defval = 2017, title = "BACKTEST: From Year")From_Month = input(defval = 1, title = "BACKTEST: From Month", minval = 1, maxval = 12)From_Day = input(defval = 1, title = "BACKTEST: From Day", minval = 1, maxval = 31)To_Year = input(defval = 9999, title = "BACKTEST: To Year")To_Month = input(defval = 1, title = "BACKTEST: To Month", minval = 1, maxval = 12)To_Day = input(defval = 1, title = "BACKTEST: To Day", minval = 1, maxval = 31)Start = timestamp(From_Year, From_Month, From_Day, 00, 00) // backtest start windowFinish = timestamp(To_Year, To_Month, To_Day, 23, 59) // backtest finish windowtestPeriod() => time >= Start and time <= Finish ? true : false// === /BACKTEST RANGE ===// === STRATEGY ===// Make input option to configure trade directiontradeDirection = input(title="Trade Direction", defval="Both", options=["Long", "Short", "Both"]) // Translate input into trading conditionslongOK = (tradeDirection == "Long") or (tradeDirection == "Both")shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")// == STRATEGY ENTRIES/EXITS ==longCondition = longOK and buySignal and testPeriod()shortcondition = shortOK and shortSignal and testPeriod()// === STRATEGY - LONG POSITION EXECUTION ===if longCondition strategy.entry("Long", strategy.long)// === STRATEGY - SHORT POSITION EXECUTION ===if shortcondition strategy.entry("Short", strategy.short)// === /STRATEGY ===//EOF