Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

MACD Strategy with trailing ATR stop

A Pine Script v4 strategy built on CUM (close), EMA (close, 34) and SMA (close, 34). It has 4 order rules, 1 entry among them, and trades long only with the default settings. Exits use a profit target and no stop loss. Each trade commits 100 in cash and pays 0.04% commission. 19 settings; defaults include Period 34, Fast Length 3 and Slow Length 5.

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 with trailing ATR stop repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 17 of 17 trades.
How did MACD Strategy with trailing ATR stop do on BTCUSDT 1D with default settings?
+72.67%
From 26 Jan 2024 to 21 Oct 2026, over 89 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can MACD Strategy with trailing ATR stop 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 with trailing ATR stop use a stop loss?
No
Its exits set a profit target but no stop.
Does MACD Strategy with trailing ATR stop run in Zeal Terminal?
Yes
It compiles in 19 ms and runs 1,000 bars in 82 ms, in the browser.

BTCUSDT · 1D · default settings

  • EMA
  • SMA
  • 1st Long Take Profit
  • 2nd Long Take Profit
  • Long Stop Loss
  • 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 (19)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$211$203Equity from $100
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. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
Net profit
+72.67%
$72.67
Buy and hold
+102.5%
same period
Closed trades
89
71 won · 18 lost
Profitable
79.8%
of closed trades
Profit factor
1.69
gross profit ÷ gross loss
Max drawdown
53.73%
−$58.18

One run with the author's default settings, $100 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

    strategy.exit is used without a stop, loss or trailing stop.

  • Note

    No slippage

    Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.

  • Note

    Uses an argument v6 removed

    when = on line 72, 75. The upgraded version below moves each call inside an if block.

  • Note

    Uses transp =, which v5 removed line 19

    transp = on line 19, 20, 21. 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 cum → ta.cum, max → math.max, abs → math.abs, ema → ta.ema, sma → ta.sma, crossover → ta.crossover, valuewhen → ta.valuewhen.
  • 2 strategy calls use the when = argument (line 72, 75). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

Same 89 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 10: cum → ta.cum
  • Line 10: max → math.max
  • Line 10: max → math.max
  • Line 13: max → math.max
  • Line 13: max → math.max
15 more edits
  • Line 13: abs → math.abs
  • Line 13: abs → math.abs
  • Line 17: ema → ta.ema
  • Line 18: sma → ta.sma
  • Line 19: transp = 50 → color.new(#53b987, 50)
  • Line 20: transp = 50 → color.new(#eb4d5c, 50)
  • Line 21: transp = 60 → color.new(leadLine1 > leadLine2 ? #53b987 : #eb4d5c, 60)
  • Line 23: input(type = input.integer) → input.int()
  • Line 24: input(type = input.integer) → input.int()
  • Line 25: input(type = input.source) → input.source()
  • Line 26: input(type = input.integer) → input.int()
  • Line 27: input(type = input.bool) → input.bool()
  • Line 28: input(type = input.bool) → input.bool()
  • Line 56: crossover → ta.crossover
  • Line 57: valuewhen → ta.valuewhen
⋯// © Deobald-//@version=4+//@version=5strategy("MACD Strategy", overlay=true, pyramiding=2, commission_type=strategy.commission.percent, commission_value=0.04, initial_capital=100,  default_qty_type = strategy.cash, default_qty_value = 100, currency = currency.USD)⋯    ema := nz(ema[1] + sf*(src - ema[1]),src)-Sma(src,p) => a = cum(src), (a - a[max(p,0)])/max(p,0)+Sma(src,p) => a = ta.cum(src), (a - a[math.max(p,0)])/math.max(p,0)Atr(p) =>    atr = 0.-    Tr = max(high - low, max(abs(high - close[1]), abs(low - close[1])))+    Tr = math.max(high - low, math.max(math.abs(high - close[1]), math.abs(low - close[1])))    atr := nz(atr[1] + (Tr - atr[1])/p,Tr)⋯ribbon_period = input(34, "Period", step=1)-leadLine1 = ema(close, ribbon_period)+leadLine1 = ta.ema(close, ribbon_period)-leadLine2 = sma(close, ribbon_period)+leadLine2 = ta.sma(close, ribbon_period)-p3 = plot(leadLine1, color= #53b987, title="EMA", transp = 50, linewidth = 1)+p3 = plot(leadLine1, color= color.new(#53b987, 50), title="EMA", linewidth = 1)-p4 = plot(leadLine2, color= #eb4d5c, title="SMA", transp = 50, linewidth = 1)+p4 = plot(leadLine2, color= color.new(#eb4d5c, 50), title="SMA", linewidth = 1)-fill(p3, p4, transp = 60, color = leadLine1 > leadLine2 ? #53b987 : #eb4d5c)+fill(p3, p4, color = color.new(leadLine1 > leadLine2 ? #53b987 : #eb4d5c, 60))// MACD-fast_length = input(title="Fast Length", type=input.integer, defval=3)+fast_length = input.int(title="Fast Length", defval=3)-slow_length = input(title="Slow Length", type=input.integer, defval=5)+slow_length = input.int(title="Slow Length", defval=5)-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 = 2)+signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 2)-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=true)+sma_signal = input.bool(title="Simple MA(Signal Line)", defval=true)// Plot colors⋯// Strategy-entry_long=crossover(macd,signal) and leadLine2 < leadLine1+entry_long=ta.crossover(macd,signal) and leadLine2 < leadLine1-entry_price_long=valuewhen(entry_long,close,0)+entry_price_long=ta.valuewhen(entry_long,close,0)SL_floating_long = entry_price_long - multiplier*Atr(ATR_period)⋯

Clean v5

VerifiedDownload the v5 script

Same 89 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 16: input() → input.int()
  • Line 46: input() → input.int()
  • Line 47: input() → input.int()
  • Line 48: input() → input.int()
  • Line 49: input() → input.int()
  • Line 53: input() → input.float()
9 more edits
  • Line 54: input() → input.int()
  • Line 61: input() → input.int()
  • Line 62: input() → input.int()
  • Line 63: input() → input.int()
  • Line 65: input() → input.int()
  • Line 66: input() → input.int()
  • Line 67: input() → input.int()
  • Line 72: when = entry_long → if block
  • Line 75: when = exit_long → if block
⋯/// TREND-ribbon_period = input(34, "Period", step=1)+ribbon_period = input.int(34, "Period", step=1)leadLine1 = ta.ema(close, ribbon_period)⋯// TAKE PROFIT AND STOP LOSS-long_tp1_inp = input(1, title='Long Take Profit 1 %', step=0.1)/100+long_tp1_inp = input.int(1, title='Long Take Profit 1 %', step=0.1)/100-long_tp1_qty = input(10, title="Long Take Profit 1 Qty", step=1)+long_tp1_qty = input.int(10, title="Long Take Profit 1 Qty", step=1)-long_tp2_inp = input(5, title='Long Take Profit 2%', step=0.1)/100+long_tp2_inp = input.int(5, title='Long Take Profit 2%', step=0.1)/100-long_tp2_qty = input(50, title="Long Take Profit 2 Qty", step=1)+long_tp2_qty = input.int(50, title="Long Take Profit 2 Qty", step=1)long_take_level_1 = strategy.position_avg_price * (1 + long_tp1_inp)⋯// Stop Loss-multiplier = input(2.2, "SL Mutiplier", minval=1, step=0.1)+multiplier = input.float(2.2, "SL Mutiplier", minval=1, step=0.1)-ATR_period=input(17,"ATR period", minval=1, step=1)+ATR_period=input.int(17,"ATR period", minval=1, step=1)// Strategy⋯///// BACKTEST PERIOD ///////-testStartYear = input(2018, "Backtest Start Year")+testStartYear = input.int(2018, "Backtest Start Year")-testStartMonth = input(1, "Backtest Start Month")+testStartMonth = input.int(1, "Backtest Start Month")-testStartDay = input(1, "Backtest Start Day")+testStartDay = input.int(1, "Backtest Start Day")testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)-testStopYear = input(9999, "Backtest Stop Year")+testStopYear = input.int(9999, "Backtest Stop Year")-testStopMonth = input(12, "Backtest Stop Month")+testStopMonth = input.int(12, "Backtest Stop Month")-testStopDay = input(31, "Backtest Stop Day")+testStopDay = input.int(31, "Backtest Stop Day")testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)⋯if testPeriod()-    strategy.entry("long", strategy.long, comment="Long", when=entry_long)+    if entry_long+        strategy.entry("long", strategy.long, comment="Long")    strategy.exit("TP1","long", qty_percent=long_tp1_qty, limit=long_take_level_1)//, trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick)    strategy.exit("TP2", qty_percent=long_tp2_qty, limit=long_take_level_2) //, trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick)-    strategy.close("long", when=exit_long, comment="exit long" )+    if exit_long+        strategy.close("long", comment="exit long" )// LONG POSITION⋯

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: 89 before, 1 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.

⋯// © Deobald-//@version=5+//@version=6strategy("MACD Strategy", overlay=true, pyramiding=2, commission_type=strategy.commission.percent, commission_value=0.04, initial_capital=100,  default_qty_type = strategy.cash, default_qty_value = 100, currency = currency.USD)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Period34int · step 1leadLine1 leadLine2
Fast Length3int (written input.integer)fast_ma
Slow Length5int (written input.integer)slow_ma
Sourceclosesourceema a fast_ma slow_ma
Signal Smoothing2int (written input.integer) · min 1 · max 50signal
Simple MA(Oscillator)falseboolfast_ma slow_ma
Simple MA(Signal Line)trueboolsignal
Long Take Profit 1 %1int · step 0.1long_take_level_1
Long Take Profit 1 Qty10int · step 1exit TP1
Long Take Profit 2%5int · step 0.1long_take_level_2
Long Take Profit 2 Qty50int · step 1exit TP2
SL Mutiplier2.2float · min 1 · step 0.1SL_floating_long
ATR period17int · min 1 · step 1SL_floating_long
Backtest Start Year2018inttestPeriodStart
Backtest Start Month1inttestPeriodStart
Backtest Start Day1inttestPeriodStart
Backtest Stop Year9999inttestPeriodStop
Backtest Stop Month12inttestPeriodStop
Backtest Stop Day31inttestPeriodStop

What it calculates

  • CUM (cum, ta.cum in v5)Source (close)a · line 10
  • EMA (ema, ta.ema in v5)close, Period (34)leadLine1 · line 17
  • SMA (sma, ta.sma in v5)close, Period (34)leadLine2 · line 18
  • Valuewhen (valuewhen, ta.valuewhen in v5)entry_long, close, 0entry_price_long · line 57

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 dataribbon_periodPeriodsetting · 34lowlowprice datahighhighprice datafast_lengthFast Lengthsetting · 3slow_lengthSlow Lengthsetting · 5signal_lengthSignal Smoothingsetting · 2long_tp1_inpLong Take Profit 1 %setting · 1long_tp2_inpLong Take Profit 2%setting · 5multiplierSL Mutipliersetting · 2.2ATR_periodATR periodsetting · 17testStartYearBacktest Start Yearsetting · 2018testStartMonthBacktest Start Monthsetting · 1testStartDayBacktest Start Daysetting · 1testStopYearBacktest Stop Yearsetting · 9999testStopMonthBacktest Stop Monthsetting · 12testStopDayBacktest Stop Daysetting · 31aCUM→ aleadLine1EMA→ leadLine1leadLine2SMA→ leadLine2entry_price_longValuewhen→ entry_price_longemaemavaluesfsfvalueatratrvalueTrTrvaluefast_mafast_mavalueslow_maslow_mavaluemacdmacdvaluesignalsignalvaluehisthistvaluelong_take_level_1long_take_level_1valuelong_take_level_2long_take_level_2valueSL_floating_longSL_floating_longvaluetestPeriodStarttestPeriodStartvaluetestPeriodStoptestPeriodStopvalueentry_longentry_longconditionexit_longexit_longconditioncol_grow_above, col_grow_below, col_fall_above, col_fall_below, col_macd, col_signalColourcol_grow_above · col_grow_bel…entry:longOpen longstrategy.entryexit:TP1exit TP1strategy.exitexit:TP2exit TP2strategy.exitclose:longClose longstrategy.closeplot:19EMAplotplot:20SMAplotfill:21Fillfillplot:771st Long Take Profitplotplot:782nd Long Take Profitplotplot:79Long Stop Lossplot

Order rules

OrderFires whenBy defaultLine
Open long longentry_longcrossover(macd,signal) and leadLine2 < leadLine1On72
Exit TP1 limittestPeriod()On73
Exit TP2 limittestPeriod()On74
Close longexit_longclose < SL_floating_longOn75

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.

17/17trades 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+72.7%+102.5%8980%1.6953.7%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+1.4%+5.5%7366%1.0328.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+85.0%+9.8%10372%1.4587.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+11.1%+8.4%7367%1.1828.8%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+87.4%+16.7%11075%1.5043.6%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.9%+21.5%7074%1.0140.1%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+49.6%+148.3%9879%1.4738.5%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−11.4%+17.1%8174%0.8139.6%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 barsTradesWonP&L
Rising market3382%−$7.24
Sideways market3974%$22.36
Falling market1788%$57.60

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

39.3%

The 95th percentile of the worst drawdown when the same 89 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 18.8% to 39.3%; the tick is what actually happened, 37.4%. Order luck alone moves it this much.

0%45%
All 89 trades
SideEntryExitBarsP&L
long5 Mar 2024 68,245.708 Mar 2024 68,928.203$0.09
long5 Mar 2024 68,245.7010 Mar 2024 68,863.505$0.08
long5 Mar 2024 68,245.7011 Mar 2024 71,587.106$2.41
long5 Mar 2024 68,245.7011 Mar 2024 71,587.106$1.44
long9 Mar 2024 68,124.2011 Mar 2024 71,587.102$1.00
long9 Mar 2024 68,124.2012 Mar 2024 72,078.103$0.54
long9 Mar 2024 68,124.2012 Mar 2024 72,078.103$2.70
long9 Mar 2024 68,124.2017 Mar 2024 65,300.608−$0.98
long12 Mar 2024 72,078.1017 Mar 2024 65,300.605−$9.48
long18 Mar 2024 68,393.5025 Mar 2024 68,797.007$0.05
long18 Mar 2024 68,393.5025 Mar 2024 68,797.007$0.05
long18 Mar 2024 68,393.5026 Mar 2024 71,489.308$2.22
long18 Mar 2024 68,393.5026 Mar 2024 71,489.308$1.33
long21 Mar 2024 67,840.5026 Mar 2024 71,489.305$1.08
long21 Mar 2024 67,840.501 May 2024 60,672.0041−$8.48
long16 May 2024 66,206.5017 May 2024 66,868.601$0.09
long16 May 2024 66,206.5018 May 2024 67,024.002$0.11
long16 May 2024 66,206.5020 May 2024 69,990.804$2.82
long16 May 2024 66,206.5020 May 2024 69,990.804$1.70
long18 May 2024 67,024.0020 May 2024 69,990.802$0.85
long18 May 2024 67,024.0021 May 2024 71,446.603$0.61
long18 May 2024 67,024.0021 May 2024 71,446.603$3.06
long18 May 2024 67,024.0024 Jun 2024 63,210.0037−$1.40
long21 May 2024 71,446.6024 Jun 2024 63,210.0034−$11.60
long20 Jul 2024 66,660.0021 Jul 2024 67,326.601$0.09
long20 Jul 2024 66,660.0027 Jul 2024 67,908.007$0.18
long20 Jul 2024 66,660.005 Aug 2024 58,161.0016−$10.28
long27 Jul 2024 67,908.005 Aug 2024 58,161.009−$14.43
long7 Aug 2024 56,022.008 Aug 2024 56,582.201$0.09
long7 Aug 2024 56,022.008 Aug 2024 58,823.101$2.46
long7 Aug 2024 56,022.0030 Aug 2024 59,359.0023$0.55
long7 Aug 2024 56,022.0030 Aug 2024 59,359.0023$1.80
long30 Aug 2024 59,359.0030 Aug 2024 59,359.000−$0.01
long30 Aug 2024 59,359.0013 Sep 2024 59,826.7014$0.07
long30 Aug 2024 59,359.0019 Sep 2024 62,188.5020$2.35
long30 Aug 2024 59,359.0025 Feb 2025 91,552.90179$11.99
long3 Sep 2024 59,132.1025 Feb 2025 91,552.90175$54.73
long1 Apr 2025 82,550.002 Apr 2025 85,158.401$0.31
long1 Apr 2025 82,550.002 Apr 2025 86,677.501$2.46
long1 Apr 2025 82,550.004 Apr 2025 83,375.503$0.09
long1 Apr 2025 82,550.0021 Apr 2025 87,211.8020$1.67
long4 Apr 2025 83,213.1021 Apr 2025 87,211.8017$0.93
long4 Apr 2025 83,213.1022 Apr 2025 87,516.2018$0.48
long4 Apr 2025 83,213.1022 Apr 2025 87,516.2018$2.42
long4 Apr 2025 83,213.106 Jun 2025 101,509.0063$5.10
long22 Apr 2025 87,516.206 Jun 2025 101,509.0045$15.90
long7 Jul 2025 109,204.009 Jul 2025 110,296.002$0.09
long7 Jul 2025 109,204.009 Jul 2025 110,296.002$0.09
long7 Jul 2025 109,204.0010 Jul 2025 114,500.003$2.38
long7 Jul 2025 109,204.0010 Jul 2025 114,500.003$1.43
long9 Jul 2025 108,923.0010 Jul 2025 114,500.001$1.01
long9 Jul 2025 108,923.0023 Jul 2025 119,954.0014$0.91
long9 Jul 2025 108,923.0023 Jul 2025 119,954.0014$4.56
long9 Jul 2025 108,923.0026 Aug 2025 110,112.0048$0.26
long23 Jul 2025 119,954.0026 Aug 2025 110,112.0034−$8.28
long17 Sep 2025 116,789.0024 Sep 2025 111,999.007−$4.18
long19 Sep 2025 117,074.0024 Sep 2025 111,999.005−$4.41
long25 Sep 2025 113,307.0029 Sep 2025 112,560.004−$0.07
long25 Sep 2025 113,307.0029 Sep 2025 112,560.004−$0.08
long25 Sep 2025 113,307.001 Oct 2025 116,800.006$1.50
long25 Sep 2025 113,307.001 Oct 2025 116,800.006$0.89
long27 Sep 2025 109,643.001 Oct 2025 116,800.004$1.37
long27 Sep 2025 109,643.0013 Oct 2025 114,959.0016$0.45
long27 Sep 2025 109,643.0013 Oct 2025 115,126.0016$2.35
long27 Sep 2025 109,643.004 Nov 2025 106,583.0038−$0.62
long13 Oct 2025 114,959.004 Nov 2025 106,583.0022−$7.36
long8 Dec 2025 90,395.309 Dec 2025 91,299.301$0.09
long8 Dec 2025 90,395.3012 Dec 2025 92,513.404$0.22
long8 Dec 2025 90,395.3013 Jan 2026 96,136.3036$3.13
long8 Dec 2025 90,395.3013 Jan 2026 96,136.3036$1.89
long12 Dec 2025 92,513.4013 Jan 2026 96,136.3032$0.73
long12 Dec 2025 92,513.401 Feb 2026 78,741.1051−$12.11
long22 Jan 2026 89,454.701 Feb 2026 78,741.1010−$12.05
long1 Mar 2026 66,973.302 Mar 2026 67,643.001$0.09
long1 Mar 2026 66,973.303 Mar 2026 68,830.102$0.26
long1 Mar 2026 66,973.304 Mar 2026 71,390.303$3.26
long1 Mar 2026 66,973.304 Mar 2026 71,390.303$1.97
long3 Mar 2026 68,830.104 Mar 2026 71,390.301$0.69
long3 Mar 2026 68,830.1010 Mar 2026 69,518.407$0.09
long3 Mar 2026 68,830.1013 Mar 2026 72,026.7010$2.29
long3 Mar 2026 68,830.1023 May 2026 75,539.5081$2.01
long10 Mar 2026 68,432.2023 May 2026 75,539.5074$10.30
long2 Jul 2026 60,024.003 Jul 2026 61,560.001$0.25
long2 Jul 2026 60,024.004 Jul 2026 63,025.202$2.46
long2 Jul 2026 60,024.0010 Jul 2026 63,230.008$0.50
long2 Jul 2026 60,024.0010 Jul 2026 63,230.008$1.60
long10 Jul 2026 63,230.0010 Jul 2026 63,230.000−$0.01
long10 Jul 2026 63,230.0015 Jul 2026 65,044.005$0.27
long10 Jul 2026 63,230.0019 Aug 2026 67,483.1040$3.23

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
MACD Strategy with trailing ATR stop this page—v4 strategy+72.7%8953.7%
OBV Pyr by RafaelZioniema cum smav4 strategy−1.2%279.5%
Crypto EMA Trend Reversal Strategy by ADHDCRYPT0ema sma valuewhenv4 strategy+37.5%3718.3%
Full CRYPTO pack macd, rsi, obv, ema strategy by SoftKill21ema sma cumv4 strategyNo trades on this market
Strategy Myth-Busting #23 - 2xEMA+DPO- [MYN] by myncryptoema sma valuewhenv5 strategyNo trades on this market
meta-capitulation by serafimovklimentsma cumv5 strategyNo trades on this market

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 19 msWith a workaround 1,000 bars in 82 msNot measured
PineTSNo v5 and v6 only—Not measured

The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.

“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/// © Deobald//@version=4strategy("MACD Strategy", overlay=true, pyramiding=2, commission_type=strategy.commission.percent, commission_value=0.04, initial_capital=100,  default_qty_type = strategy.cash, default_qty_value = 100, currency = currency.USD)// FUNCTIONSEma(src,p) =>    ema = 0.    sf = 2/(p+1)    ema := nz(ema[1] + sf*(src - ema[1]),src)Sma(src,p) => a = cum(src), (a - a[max(p,0)])/max(p,0)Atr(p) =>    atr = 0.    Tr = max(high - low, max(abs(high - close[1]), abs(low - close[1])))    atr := nz(atr[1] + (Tr - atr[1])/p,Tr)/// TRENDribbon_period = input(34, "Period", step=1)leadLine1 = ema(close, ribbon_period)leadLine2 = sma(close, ribbon_period)p3 = plot(leadLine1, color= #53b987, title="EMA", transp = 50, linewidth = 1)p4 = plot(leadLine2, color= #eb4d5c, title="SMA", transp = 50, linewidth = 1)fill(p3, p4, transp = 60, color = leadLine1 > leadLine2 ? #53b987 : #eb4d5c)// MACDfast_length = input(title="Fast Length", type=input.integer, defval=3)slow_length = input(title="Slow Length", type=input.integer, defval=5)src = input(title="Source", type=input.source, defval=close)signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 2)sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=true)// 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 - signal//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)// TAKE PROFIT AND STOP LOSSlong_tp1_inp = input(1, title='Long Take Profit 1 %', step=0.1)/100long_tp1_qty = input(10, title="Long Take Profit 1 Qty", step=1)long_tp2_inp = input(5, title='Long Take Profit 2%', step=0.1)/100long_tp2_qty = input(50, title="Long Take Profit 2 Qty", step=1)long_take_level_1 = strategy.position_avg_price * (1 + long_tp1_inp)long_take_level_2 = strategy.position_avg_price * (1 + long_tp2_inp)// Stop Lossmultiplier = input(2.2, "SL Mutiplier", minval=1, step=0.1)ATR_period=input(17,"ATR period", minval=1, step=1)// Strategyentry_long=crossover(macd,signal) and leadLine2 < leadLine1entry_price_long=valuewhen(entry_long,close,0)SL_floating_long = entry_price_long - multiplier*Atr(ATR_period)exit_long= close < SL_floating_long ///// BACKTEST PERIOD ///////testStartYear = input(2018, "Backtest Start Year")testStartMonth = input(1, "Backtest Start Month")testStartDay = input(1, "Backtest Start Day")testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)testStopYear = input(9999, "Backtest Stop Year")testStopMonth = input(12, "Backtest Stop Month")testStopDay = input(31, "Backtest Stop Day")testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)testPeriod() =>    time >= testPeriodStart and time <= testPeriodStop ? true : falseif testPeriod()    strategy.entry("long", strategy.long, comment="Long", when=entry_long)    strategy.exit("TP1","long", qty_percent=long_tp1_qty, limit=long_take_level_1)//, trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick)    strategy.exit("TP2", qty_percent=long_tp2_qty, limit=long_take_level_2) //, trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick)    strategy.close("long", when=exit_long, comment="exit long" )// LONG POSITIONplot(strategy.position_size > 0 ? long_take_level_1 : na, style=plot.style_linebr, color=color.green, linewidth=1, title="1st Long Take Profit")plot(strategy.position_size > 0 ? long_take_level_2 : na, style=plot.style_linebr, color=color.green, linewidth=1, title="2nd Long Take Profit")plot(strategy.position_size > 0 ? SL_floating_long : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Stop Loss")