Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (10)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$89,457$202,527Equity from $100,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
−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

Verified

Same 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)⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
Fast Length12int (written input.integer)fast_ma
Slow Length26int (written input.integer)slow_ma
Sourceclosesourcefast_ma slow_ma
Signal Smoothing9int (written input.integer) · min 1 · max 50signal
Simple MA (Oscillator)falseboolfast_ma slow_ma
Simple MA (Signal Line)falseboolsignal
movinga 2200int (written input.integer)movinga200
Short Take Profit (%)2int · min 0.0 · step 0.1shortExitPrice
Long Take Profit (%)2int · min 0.0 · step 0.1longExitPrice
stoploss in %2int · min 0.0 · step 1longStoploss shortStoploss

What it calculates

  • SMA / EMA (sma, ta.sma in v5)Source (close), Fast Length (12)fast_ma · line 19
  • SMA / EMA (sma, ta.sma in v5)Source (close), Slow Length (26)slow_ma · line 20
  • SMA / EMA (sma, ta.sma in v5)macd, Signal Smoothing (9)signal · line 22
  • SMA (sma, ta.sma in 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.

closecloseprice datafast_lengthFast Lengthsetting · 12slow_lengthSlow Lengthsetting · 26signal_lengthSignal Smoothingsetting · 9movinga2movinga 2setting · 200stoplossstoploss in %setting · 2longProfitPercLong Take Profit (%)setting · 2shortProfitPercShort Take Profit (%)setting · 2fast_maSMA / EMA→ fast_maslow_maSMA / EMA→ slow_masignalSMA / EMA→ signalmovinga200SMA→ movinga200macdmacdvaluehisthistvaluelongStoploss, shortStoplosslongStoploss · shortSto…2 valueslongExitPricelongExitPricevalueshortExitPriceshortExitPricevaluelongConditionlongConditionconditionshortConditionshortConditionconditioncol_grow_above, col_grow_below, col_fall_above, col_fall_below, col_macd, col_signalColourcol_grow_above · col_grow_bel…entry:My Long Entry IdOpen My Long Entry Idstrategy.entryentry:My Short Entry IdOpen My Short Entry Idstrategy.entryexit:XL TPexit XL TPstrategy.exitexit:XS TPexit XS TPstrategy.exitplot:26MAplot

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(longCondition)On29
Open My Short Entry Id short(shortCondition)On32
Exit XL TP stop and limit(strategy.position_size > 0 )On44
Exit XS TP stop and limit(strategy.position_size < 0 )On46

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.

5/5trades 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−10.5%+102.5%1225%0.3611.8%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+0.3%+5.5%1145%1.034.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.2%+9.8%1650%0.730.5%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.0%+8.4%1155%1.180.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%1182%4.810.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%1450%0.770.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.0%+148.3%1242%1.060.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.0%+17.1%1547%0.930.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.

Before 26 Dec 2025 · in-sample
movinga 2 ↓ · Signal Smoothing →5689111315
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%
From 26 Dec 2025 · out-of-sample
movinga 2 ↓ · Signal Smoothing →5689111315
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 barsTradesWonP&L
Rising market2100%$3,620.04
Sideways market617%−$8,083.55
Falling market40%−$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.

0%16%
All 12 trades
SideEntryExitBarsP&L
short2 Oct 2024 60,804.904 Oct 2024 62,021.002−$1,216.10
long16 Jan 2025 100,497.0017 Jan 2025 102,507.001$2,009.95
long21 Feb 2025 98,305.0022 Feb 2025 96,182.001−$2,123.02
long7 Mar 2025 89,931.908 Mar 2025 86,801.701−$3,130.14
long16 Mar 2025 84,338.4017 Mar 2025 82,574.501−$1,763.92
long7 Sep 2025 110,188.008 Sep 2025 112,392.001$2,203.76
long27 Oct 2025 114,559.0028 Oct 2025 112,268.001−$2,291.19
short21 Jan 2026 88,427.7022 Jan 2026 90,196.201−$1,768.56
short29 Apr 2026 76,342.801 May 2026 77,869.602−$1,526.86
short13 May 2026 80,504.5015 May 2026 78,894.402$1,610.09
short28 Jul 2026 63,755.9030 Jul 2026 65,031.002−$1,275.12
short12 Aug 2026 63,600.0018 Aug 2026 64,872.006−$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.

ScriptSharesTypeNetTradesMax drawdown
Simple MACD strategy this page—v4 strategy−10.5%1211.8%
MACD Long Strat by TheGrindTodayema smav4 strategy+24.0%7920.1%
Stock trending strategy by exlux99ema smav4 strategy+35.3%2114.7%
4-Hour Stochastic EMA Trend by slymnturkogluema smav4 strategyNo trades on this market
4X EMA and volume strategy by SoftKill21ema smav4 strategy+1.8%325.8%
Simple Momentum Strategy Based on SMA, EMA and Volume by slip_streamsma emav4 strategy+7.4%1827.4%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 12 msYes 1,000 bars in 30 msNot measured
PineTSNo 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)