Pine Script v4 · Strategy
Simple MACD strategy
A Pine Script v4 strategy built on SMA or EMA (close, 12) and SMA (close, 200). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 10 settings; defaults include Fast Length 12, Slow Length 26 and Source close.
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 Simple MACD strategy 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 Simple MACD strategy do on BTCUSDT 1D with default settings?
- −10.54%
- From 26 Jan 2024 to 21 Oct 2026, over 12 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Simple MACD strategy 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 Simple MACD strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 44, 46.
- Does Simple MACD strategy run in Zeal Terminal?
- Yes
- It compiles in 12 ms and runs 1,000 bars in 30 ms, in the browser.
BTCUSDT · 1D · default settings
- MA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −10.54%
- −$10,543.10
- Buy and hold
- +102.5%
- same period
- Closed trades
- 12
- 3 won · 9 lost
- Profitable
- 25.0%
- of closed trades
- Profit factor
- 0.36
- gross profit ÷ gross loss
- Max drawdown
- 11.85%
- −$11,943.60
One run with the author's default settings, $100,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.
-
Likely bug
sma(close, movinga2) ignores the settings line 25
Line 25 hard-codes close where line 19 uses “Source”. Changing those settings moves one band but not this one.
sma(close, movinga2)sma(src, movinga2) -
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 sma → ta.sma, ema → ta.ema, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
VerifiedSame 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 5: input(type = input.integer) → input.int()
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.source) → input.source()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.bool) → input.bool()
11 more edits
- Line 10: input(type = input.bool) → input.bool()
- Line 19: sma → ta.sma
- Line 19: ema → ta.ema
- Line 20: sma → ta.sma
- Line 20: ema → ta.ema
- Line 22: sma → ta.sma
- Line 22: ema → ta.ema
- Line 24: input(type = input.integer) → input.int()
- Line 25: sma → ta.sma
- Line 27: crossover → ta.crossover
- Line 30: crossunder → ta.crossunder
⋯// © Hurmun-//@version=4+//@version=5strategy("Simple MACD strategy ", overlay=true, margin_long=100, margin_short=100)-fast_length = input(title="Fast Length", type=input.integer, defval=12)+fast_length = input.int(title="Fast Length", defval=12)-slow_length = input(title="Slow Length", type=input.integer, defval=26)+slow_length = input.int(title="Slow Length", defval=26)-src = input(title="Source", type=input.source, defval=close)+src = input.source(title="Source", defval=close)-signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)+signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)-sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)+sma_source = input.bool(title="Simple MA (Oscillator)", defval=false)-sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)+sma_signal = input.bool(title="Simple MA (Signal Line)", defval=false)// Plot colors⋯// Calculating-fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)+fast_ma = sma_source ? ta.sma(src, fast_length) : ta.ema(src, fast_length)-slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)+slow_ma = sma_source ? ta.sma(src, slow_length) : ta.ema(src, slow_length)macd = fast_ma - slow_ma-signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)+signal = sma_signal ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)hist = macd - signal-movinga2 = input(title="movinga 2", type=input.integer, defval=200)+movinga2 = input.int(title="movinga 2", defval=200)-movinga200 = sma(close, movinga2)+movinga200 = ta.sma(close, movinga2)plot(movinga200, "MA", color.orange)-longCondition = crossover(macd, signal) and macd < 0 and signal < 0 and close > movinga200+longCondition = ta.crossover(macd, signal) and macd < 0 and signal < 0 and close > movinga200if (longCondition) strategy.entry("My Long Entry Id", strategy.long)-shortCondition = crossunder(macd, signal) and macd > 0 and signal > 0 and close < movinga200+shortCondition = ta.crossunder(macd, signal) and macd > 0 and signal > 0 and close < movinga200if (shortCondition)⋯Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 34: input() → input.int()
- Line 35: input() → input.int()
- Line 37: input() → input.int()
⋯ -shortProfitPerc = input(title="Short Take Profit (%)", minval=0.0, step=0.1, defval=2) / 100+shortProfitPerc = input.int(title="Short Take Profit (%)", minval=0.0, step=0.1, defval=2) / 100-longProfitPerc = input(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=2) / 100+longProfitPerc = input.int(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=2) / 100 -stoploss = input(title="stoploss in %", minval = 0.0, step=1, defval=2) /100+stoploss = input.int(title="stoploss in %", minval = 0.0, step=1, defval=2) /100longStoploss = strategy.position_avg_price * (1 - stoploss)⋯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: 12 before, 11 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.
⋯// © Hurmun-//@version=5+//@version=6strategy("Simple MACD strategy ", overlay=true, margin_long=100, margin_short=100)⋯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 (written input.integer) | fast_ma |
| Slow Length | 26 | int (written input.integer) | slow_ma |
| Source | close | source | fast_ma slow_ma |
| Signal Smoothing | 9 | int (written input.integer) · min 1 · max 50 | signal |
| Simple MA (Oscillator) | false | bool | fast_ma slow_ma |
| Simple MA (Signal Line) | false | bool | signal |
| movinga 2 | 200 | int (written input.integer) | movinga200 |
| Short Take Profit (%) | 2 | int · min 0.0 · step 0.1 | shortExitPrice |
| Long Take Profit (%) | 2 | int · min 0.0 · step 0.1 | longExitPrice |
| stoploss in % | 2 | int · min 0.0 · step 1 | longStoploss shortStoploss |
What it calculates
- SMA / EMA (
sma,ta.smain v5)Source (close), Fast Length (12)fast_ma · line 19 - SMA / EMA (
sma,ta.smain v5)Source (close), Slow Length (26)slow_ma · line 20 - SMA / EMA (
sma,ta.smain v5)macd, Signal Smoothing (9)signal · line 22 - SMA (
sma,ta.smain v5)close, movinga 2 (200)movinga200 · line 25
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 My Long Entry Id long | (longCondition) | On | 29 |
| Open My Short Entry Id short | (shortCondition) | On | 32 |
| Exit XL TP stop and limit | (strategy.position_size > 0 ) | On | 44 |
| Exit XS TP stop and limit | (strategy.position_size < 0 ) | On | 46 |
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 | −10.5% | +102.5% | 12 | 25% | 0.36 | 11.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.3% | +5.5% | 11 | 45% | 1.03 | 4.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.2% | +9.8% | 16 | 50% | 0.73 | 0.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 11 | 55% | 1.18 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 11 | 82% | 4.81 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 14 | 50% | 0.77 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 12 | 42% | 1.06 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 15 | 47% | 0.93 | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (Signal Smoothing 6, movinga 2 100) made +11.0% before 26 Dec 2025 and +1.7% after it, against −4.2% for the defaults. 50% of the 56 combinations made money in-sample and 29% out-of-sample.
| movinga 2 ↓ · Signal Smoothing → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 100 | +8% | +11% | +4% | +2% | −5% | −3% | −1% |
| 120 | +8% | +11% | +4% | +2% | −5% | −0% | −0% |
| 140 | +5% | +9% | +4% | −0% | −8% | −2% | −2% |
| 170 | +4% | +6% | −0% | −5% | −6% | +2% | +2% |
| 200 | +0% | +7% | +1% | −6% | −8% | +1% | −3% |
| 240 | −2% | +4% | +0% | −4% | −10% | +1% | −3% |
| 280 | −4% | +6% | +1% | −3% | −9% | +2% | −2% |
| 340 | −4% | +6% | +1% | −3% | −9% | +2% | −2% |
| movinga 2 ↓ · Signal Smoothing → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 100 | +3% | +2% | −4% | −4% | −4% | −4% | −4% |
| 120 | +3% | +2% | −4% | −4% | −6% | −6% | −4% |
| 140 | +1% | +0% | −6% | −6% | −6% | −6% | −6% |
| 170 | +1% | +0% | −6% | −6% | −6% | −6% | −6% |
| 200 | +3% | +2% | −4% | −4% | −8% | −8% | −8% |
| 240 | +3% | +2% | −4% | −4% | −8% | −8% | −8% |
| 280 | +3% | +2% | −4% | −4% | −8% | −8% | −8% |
| 340 | +5% | +0% | −8% | −7% | −8% | −6% | −6% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 2 | 100% | $3,620.04 |
| Sideways market | 6 | 17% | −$8,083.55 |
| Falling market | 4 | 0% | −$6,079.61 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
14.3%
The 95th percentile of the worst drawdown when the same 12 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 10.5% to 14.3%; the tick is what actually happened, 11.2%. Order luck alone moves it this much.
All 12 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 2 Oct 2024 60,804.90 | 4 Oct 2024 62,021.00 | 2 | −$1,216.10 |
| long | 16 Jan 2025 100,497.00 | 17 Jan 2025 102,507.00 | 1 | $2,009.95 |
| long | 21 Feb 2025 98,305.00 | 22 Feb 2025 96,182.00 | 1 | −$2,123.02 |
| long | 7 Mar 2025 89,931.90 | 8 Mar 2025 86,801.70 | 1 | −$3,130.14 |
| long | 16 Mar 2025 84,338.40 | 17 Mar 2025 82,574.50 | 1 | −$1,763.92 |
| long | 7 Sep 2025 110,188.00 | 8 Sep 2025 112,392.00 | 1 | $2,203.76 |
| long | 27 Oct 2025 114,559.00 | 28 Oct 2025 112,268.00 | 1 | −$2,291.19 |
| short | 21 Jan 2026 88,427.70 | 22 Jan 2026 90,196.20 | 1 | −$1,768.56 |
| short | 29 Apr 2026 76,342.80 | 1 May 2026 77,869.60 | 2 | −$1,526.86 |
| short | 13 May 2026 80,504.50 | 15 May 2026 78,894.40 | 2 | $1,610.09 |
| short | 28 Jul 2026 63,755.90 | 30 Jul 2026 65,031.00 | 2 | −$1,275.12 |
| short | 12 Aug 2026 63,600.00 | 18 Aug 2026 64,872.00 | 6 | −$1,272.01 |
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 |
|---|---|---|---|---|---|
| Simple MACD strategy this page | — | v4 strategy | −10.5% | 12 | 11.8% |
| MACD Long Strat by TheGrindToday | ema sma | 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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 12 ms | Yes 1,000 bars in 30 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/// © Hurmun//@version=4strategy("Simple MACD strategy ", overlay=true, margin_long=100, margin_short=100)fast_length = input(title="Fast Length", type=input.integer, defval=12)slow_length = input(title="Slow Length", type=input.integer, defval=26)src = input(title="Source", type=input.source, defval=close)signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)// Plot colorscol_grow_above = #26A69Acol_grow_below = #FFCDD2col_fall_above = #B2DFDBcol_fall_below = #EF5350col_macd = #0094ffcol_signal = #ff6a00// Calculatingfast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)macd = fast_ma - slow_masignal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)hist = macd - signalmovinga2 = input(title="movinga 2", type=input.integer, defval=200)movinga200 = sma(close, movinga2)plot(movinga200, "MA", color.orange)longCondition = crossover(macd, signal) and macd < 0 and signal < 0 and close > movinga200if (longCondition) strategy.entry("My Long Entry Id", strategy.long)shortCondition = crossunder(macd, signal) and macd > 0 and signal > 0 and close < movinga200if (shortCondition) strategy.entry("My Short Entry Id", strategy.short) shortProfitPerc = input(title="Short Take Profit (%)", minval=0.0, step=0.1, defval=2) / 100longProfitPerc = input(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=2) / 100 stoploss = input(title="stoploss in %", minval = 0.0, step=1, defval=2) /100longStoploss = strategy.position_avg_price * (1 - stoploss)longExitPrice = strategy.position_avg_price * (1 + longProfitPerc)shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)shortStoploss = strategy.position_avg_price * (1 + stoploss) if (strategy.position_size > 0 ) strategy.exit(id="XL TP", limit=longExitPrice, stop=longStoploss)if (strategy.position_size < 0 ) strategy.exit(id="XS TP", limit=shortExitPrice, stop=shortStoploss)