Pine Script v4 · Strategy
MacD 200 Day Moving Average Signal Crossover Strategy
A Pine Script v4 strategy built on SMA or EMA (close, 12) and SMA (close, 200). 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.26 commission. 7 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 MacD 200 Day Moving Average Signal Crossover Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did MacD 200 Day Moving Average Signal Crossover Strategy do on BTCUSDT 1D with default settings?
- −71.98%
- From 26 Jan 2024 to 21 Oct 2026, over 3 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can MacD 200 Day Moving Average Signal Crossover 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 MacD 200 Day Moving Average Signal Crossover Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MacD 200 Day Moving Average Signal Crossover Strategy run in Zeal Terminal?
- Yes
- It compiles in 11 ms and runs 1,000 bars in 31 ms, in the browser.
BTCUSDT · 1D · default settings
- Moving Average Normalized
- Histogram
- MACD
- Signal
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −71.98%
- −$7,198.49
- Buy and hold
- +102.5%
- same period
- Closed trades
- 3
- 0 won · 3 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- 0.00
- gross profit ÷ gross loss
- Max drawdown
- 79.65%
- −$7,965.17
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.
-
Likely bug
sma(close, moving_avg_length) ignores the settings line 27
Line 27 hard-codes close where line 21 uses “Source”. Changing those settings moves one band but not this one.
sma(close,moving_avg_length)sma(src, moving_avg_length) -
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.
-
Note
Uses transp =, which v5 removed line 30
transp = on line 30, 31, 32. v5 folds the transparency into the colour with color.new(c, transp).
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.
Same 3 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: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.source) → input.source()
- Line 10: input(type = input.integer) → input.int()
- Line 11: input(type = input.bool) → input.bool()
12 more edits
- Line 12: input(type = input.bool) → input.bool()
- Line 21: sma → ta.sma
- Line 21: ema → ta.ema
- Line 22: sma → ta.sma
- Line 22: ema → ta.ema
- Line 24: sma → ta.sma
- Line 24: ema → ta.ema
- Line 26: input(type = input.integer) → input.int()
- Line 27: sma → ta.sma
- Line 30: transp = 0 → color.new((hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), 0)
- Line 31: transp = 0 → color.new(col_macd, 0)
- Line 32: transp = 0 → color.new(col_signal, 0)
⋯// © x11joe-//@version=4+//@version=5//This strategy is based on a youtube strategy that suggested I do this...so I did!⋯// Getting inputs-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-moving_avg_length = input(title="Moving Average Length", type=input.integer, defval=200)+moving_avg_length = input.int(title="Moving Average Length", defval=200)-moving_avg = sma(close,moving_avg_length)+moving_avg = ta.sma(close,moving_avg_length)moving_avg_normalized = close - moving_avgplot(moving_avg_normalized, title="Moving Average Normalized", style=plot.style_line, color=color.orange,linewidth=3)-plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )+plot(hist, title="Histogram", style=plot.style_columns, color=color.new((hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), 0) )-plot(macd, title="MACD", color=col_macd, transp=0)+plot(macd, title="MACD", color=color.new(col_macd, 0))-plot(signal, title="Signal", color=col_signal, transp=0)+plot(signal, title="Signal", color=color.new(col_signal, 0))if(macd>signal and macd<0 and close>moving_avg)⋯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: 3 before, 44 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.
⋯// © x11joe-//@version=5+//@version=6//This strategy is based on a youtube strategy that suggested I do this...so I did!⋯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 |
| Moving Average Length | 200 | int (written input.integer) | moving_avg |
What it calculates
- SMA / EMA (
sma,ta.smain v5)Source (close), Fast Length (12)fast_ma · line 21 - SMA / EMA (
sma,ta.smain v5)Source (close), Slow Length (26)slow_ma · line 22 - SMA / EMA (
sma,ta.smain v5)macd, Signal Smoothing (9)signal · line 24 - SMA (
sma,ta.smain v5)close, Moving Average Length (200)moving_avg · line 27
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 buy long | (macd>signal and macd<0 and close>moving_avg) | On | 34 |
| Open sell short | (close<moving_avg and macd<signal and macd>0) | On | 36 |
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.
- 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 | −72.0% | +102.5% | 3 | 0% | 0.00 | 79.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −29.9% | +5.5% | 5 | 0% | 0.00 | 34.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −64.0% | +9.8% | 4 | 25% | 0.06 | 82.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −13.9% | +8.4% | 1 | 0% | 0.00 | 17.9% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −82.1% | +16.7% | 5 | 0% | 0.00 | 90.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −15.1% | +21.5% | 5 | 20% | 0.16 | 17.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −8.8% | +148.3% | 5 | 20% | 0.73 | 48.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −12.2% | +17.1% | 7 | 14% | 0.19 | 15.0% | 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 | 1 | 0% | −$405.81 |
| Sideways market | 2 | 0% | −$6,785.38 |
| 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?
71.7%
The 95th percentile of the worst drawdown when the same 3 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 71.7% to 71.7%; the tick is what actually happened, 71.7%. Order luck alone moves it this much.
All 3 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Aug 2024 64,037.20 | 2 Oct 2024 60,804.90 | 39 | −$554.00 |
| short | 2 Oct 2024 60,804.90 | 16 Jan 2025 100,497.00 | 106 | −$6,231.38 |
| long | 16 Jan 2025 100,497.00 | 21 Jan 2026 88,427.70 | 370 | −$405.81 |
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 200 Day Moving Average Signal Crossover Strategy this page | — | v4 strategy | −72.0% | 3 | 79.7% |
| 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 11 ms | Yes 1,000 bars in 31 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/// © x11joe//@version=4//This strategy is based on a youtube strategy that suggested I do this...so I did!strategy(title="MacD 200 Day Moving Average Signal Crossover Strategy", overlay=false, precision=2,commission_value=0.26, initial_capital=10000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=100)// Getting inputsfast_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 - signalmoving_avg_length = input(title="Moving Average Length", type=input.integer, defval=200)moving_avg = sma(close,moving_avg_length)moving_avg_normalized = close - moving_avgplot(moving_avg_normalized, title="Moving Average Normalized", style=plot.style_line, color=color.orange,linewidth=3)plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )plot(macd, title="MACD", color=col_macd, transp=0)plot(signal, title="Signal", color=col_signal, transp=0)if(macd>signal and macd<0 and close>moving_avg) strategy.entry("buy",strategy.long)if(close<moving_avg and macd<signal and macd>0) strategy.entry("sell",strategy.short)