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
- 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
VerifiedSame 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)⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Period | 34 | int · step 1 | leadLine1 leadLine2 |
| Fast Length | 3 | int (written input.integer) | fast_ma |
| Slow Length | 5 | int (written input.integer) | slow_ma |
| Source | close | source | ema a fast_ma slow_ma |
| Signal Smoothing | 2 | int (written input.integer) · min 1 · max 50 | signal |
| Simple MA(Oscillator) | false | bool | fast_ma slow_ma |
| Simple MA(Signal Line) | true | bool | signal |
| Long Take Profit 1 % | 1 | int · step 0.1 | long_take_level_1 |
| Long Take Profit 1 Qty | 10 | int · step 1 | exit TP1 |
| Long Take Profit 2% | 5 | int · step 0.1 | long_take_level_2 |
| Long Take Profit 2 Qty | 50 | int · step 1 | exit TP2 |
| SL Mutiplier | 2.2 | float · min 1 · step 0.1 | SL_floating_long |
| ATR period | 17 | int · min 1 · step 1 | SL_floating_long |
| Backtest Start Year | 2018 | int | testPeriodStart |
| Backtest Start Month | 1 | int | testPeriodStart |
| Backtest Start Day | 1 | int | testPeriodStart |
| Backtest Stop Year | 9999 | int | testPeriodStop |
| Backtest Stop Month | 12 | int | testPeriodStop |
| Backtest Stop Day | 31 | int | testPeriodStop |
What it calculates
- CUM (
cum,ta.cumin v5)Source (close)a · line 10 - EMA (
ema,ta.emain v5)close, Period (34)leadLine1 · line 17 - SMA (
sma,ta.smain v5)close, Period (34)leadLine2 · line 18 - Valuewhen (
valuewhen,ta.valuewhenin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | entry_longcrossover(macd,signal) and leadLine2 < leadLine1 | On | 72 |
| Exit TP1 limit | testPeriod() | On | 73 |
| Exit TP2 limit | testPeriod() | On | 74 |
| Close long | exit_longclose < SL_floating_long | On | 75 |
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 | +72.7% | +102.5% | 89 | 80% | 1.69 | 53.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.4% | +5.5% | 73 | 66% | 1.03 | 28.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +85.0% | +9.8% | 103 | 72% | 1.45 | 87.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +11.1% | +8.4% | 73 | 67% | 1.18 | 28.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +87.4% | +16.7% | 110 | 75% | 1.50 | 43.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.9% | +21.5% | 70 | 74% | 1.01 | 40.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +49.6% | +148.3% | 98 | 79% | 1.47 | 38.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −11.4% | +17.1% | 81 | 74% | 0.81 | 39.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 33 | 82% | −$7.24 |
| Sideways market | 39 | 74% | $22.36 |
| Falling market | 17 | 88% | $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.
All 89 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 5 Mar 2024 68,245.70 | 8 Mar 2024 68,928.20 | 3 | $0.09 |
| long | 5 Mar 2024 68,245.70 | 10 Mar 2024 68,863.50 | 5 | $0.08 |
| long | 5 Mar 2024 68,245.70 | 11 Mar 2024 71,587.10 | 6 | $2.41 |
| long | 5 Mar 2024 68,245.70 | 11 Mar 2024 71,587.10 | 6 | $1.44 |
| long | 9 Mar 2024 68,124.20 | 11 Mar 2024 71,587.10 | 2 | $1.00 |
| long | 9 Mar 2024 68,124.20 | 12 Mar 2024 72,078.10 | 3 | $0.54 |
| long | 9 Mar 2024 68,124.20 | 12 Mar 2024 72,078.10 | 3 | $2.70 |
| long | 9 Mar 2024 68,124.20 | 17 Mar 2024 65,300.60 | 8 | −$0.98 |
| long | 12 Mar 2024 72,078.10 | 17 Mar 2024 65,300.60 | 5 | −$9.48 |
| long | 18 Mar 2024 68,393.50 | 25 Mar 2024 68,797.00 | 7 | $0.05 |
| long | 18 Mar 2024 68,393.50 | 25 Mar 2024 68,797.00 | 7 | $0.05 |
| long | 18 Mar 2024 68,393.50 | 26 Mar 2024 71,489.30 | 8 | $2.22 |
| long | 18 Mar 2024 68,393.50 | 26 Mar 2024 71,489.30 | 8 | $1.33 |
| long | 21 Mar 2024 67,840.50 | 26 Mar 2024 71,489.30 | 5 | $1.08 |
| long | 21 Mar 2024 67,840.50 | 1 May 2024 60,672.00 | 41 | −$8.48 |
| long | 16 May 2024 66,206.50 | 17 May 2024 66,868.60 | 1 | $0.09 |
| long | 16 May 2024 66,206.50 | 18 May 2024 67,024.00 | 2 | $0.11 |
| long | 16 May 2024 66,206.50 | 20 May 2024 69,990.80 | 4 | $2.82 |
| long | 16 May 2024 66,206.50 | 20 May 2024 69,990.80 | 4 | $1.70 |
| long | 18 May 2024 67,024.00 | 20 May 2024 69,990.80 | 2 | $0.85 |
| long | 18 May 2024 67,024.00 | 21 May 2024 71,446.60 | 3 | $0.61 |
| long | 18 May 2024 67,024.00 | 21 May 2024 71,446.60 | 3 | $3.06 |
| long | 18 May 2024 67,024.00 | 24 Jun 2024 63,210.00 | 37 | −$1.40 |
| long | 21 May 2024 71,446.60 | 24 Jun 2024 63,210.00 | 34 | −$11.60 |
| long | 20 Jul 2024 66,660.00 | 21 Jul 2024 67,326.60 | 1 | $0.09 |
| long | 20 Jul 2024 66,660.00 | 27 Jul 2024 67,908.00 | 7 | $0.18 |
| long | 20 Jul 2024 66,660.00 | 5 Aug 2024 58,161.00 | 16 | −$10.28 |
| long | 27 Jul 2024 67,908.00 | 5 Aug 2024 58,161.00 | 9 | −$14.43 |
| long | 7 Aug 2024 56,022.00 | 8 Aug 2024 56,582.20 | 1 | $0.09 |
| long | 7 Aug 2024 56,022.00 | 8 Aug 2024 58,823.10 | 1 | $2.46 |
| long | 7 Aug 2024 56,022.00 | 30 Aug 2024 59,359.00 | 23 | $0.55 |
| long | 7 Aug 2024 56,022.00 | 30 Aug 2024 59,359.00 | 23 | $1.80 |
| long | 30 Aug 2024 59,359.00 | 30 Aug 2024 59,359.00 | 0 | −$0.01 |
| long | 30 Aug 2024 59,359.00 | 13 Sep 2024 59,826.70 | 14 | $0.07 |
| long | 30 Aug 2024 59,359.00 | 19 Sep 2024 62,188.50 | 20 | $2.35 |
| long | 30 Aug 2024 59,359.00 | 25 Feb 2025 91,552.90 | 179 | $11.99 |
| long | 3 Sep 2024 59,132.10 | 25 Feb 2025 91,552.90 | 175 | $54.73 |
| long | 1 Apr 2025 82,550.00 | 2 Apr 2025 85,158.40 | 1 | $0.31 |
| long | 1 Apr 2025 82,550.00 | 2 Apr 2025 86,677.50 | 1 | $2.46 |
| long | 1 Apr 2025 82,550.00 | 4 Apr 2025 83,375.50 | 3 | $0.09 |
| long | 1 Apr 2025 82,550.00 | 21 Apr 2025 87,211.80 | 20 | $1.67 |
| long | 4 Apr 2025 83,213.10 | 21 Apr 2025 87,211.80 | 17 | $0.93 |
| long | 4 Apr 2025 83,213.10 | 22 Apr 2025 87,516.20 | 18 | $0.48 |
| long | 4 Apr 2025 83,213.10 | 22 Apr 2025 87,516.20 | 18 | $2.42 |
| long | 4 Apr 2025 83,213.10 | 6 Jun 2025 101,509.00 | 63 | $5.10 |
| long | 22 Apr 2025 87,516.20 | 6 Jun 2025 101,509.00 | 45 | $15.90 |
| long | 7 Jul 2025 109,204.00 | 9 Jul 2025 110,296.00 | 2 | $0.09 |
| long | 7 Jul 2025 109,204.00 | 9 Jul 2025 110,296.00 | 2 | $0.09 |
| long | 7 Jul 2025 109,204.00 | 10 Jul 2025 114,500.00 | 3 | $2.38 |
| long | 7 Jul 2025 109,204.00 | 10 Jul 2025 114,500.00 | 3 | $1.43 |
| long | 9 Jul 2025 108,923.00 | 10 Jul 2025 114,500.00 | 1 | $1.01 |
| long | 9 Jul 2025 108,923.00 | 23 Jul 2025 119,954.00 | 14 | $0.91 |
| long | 9 Jul 2025 108,923.00 | 23 Jul 2025 119,954.00 | 14 | $4.56 |
| long | 9 Jul 2025 108,923.00 | 26 Aug 2025 110,112.00 | 48 | $0.26 |
| long | 23 Jul 2025 119,954.00 | 26 Aug 2025 110,112.00 | 34 | −$8.28 |
| long | 17 Sep 2025 116,789.00 | 24 Sep 2025 111,999.00 | 7 | −$4.18 |
| long | 19 Sep 2025 117,074.00 | 24 Sep 2025 111,999.00 | 5 | −$4.41 |
| long | 25 Sep 2025 113,307.00 | 29 Sep 2025 112,560.00 | 4 | −$0.07 |
| long | 25 Sep 2025 113,307.00 | 29 Sep 2025 112,560.00 | 4 | −$0.08 |
| long | 25 Sep 2025 113,307.00 | 1 Oct 2025 116,800.00 | 6 | $1.50 |
| long | 25 Sep 2025 113,307.00 | 1 Oct 2025 116,800.00 | 6 | $0.89 |
| long | 27 Sep 2025 109,643.00 | 1 Oct 2025 116,800.00 | 4 | $1.37 |
| long | 27 Sep 2025 109,643.00 | 13 Oct 2025 114,959.00 | 16 | $0.45 |
| long | 27 Sep 2025 109,643.00 | 13 Oct 2025 115,126.00 | 16 | $2.35 |
| long | 27 Sep 2025 109,643.00 | 4 Nov 2025 106,583.00 | 38 | −$0.62 |
| long | 13 Oct 2025 114,959.00 | 4 Nov 2025 106,583.00 | 22 | −$7.36 |
| long | 8 Dec 2025 90,395.30 | 9 Dec 2025 91,299.30 | 1 | $0.09 |
| long | 8 Dec 2025 90,395.30 | 12 Dec 2025 92,513.40 | 4 | $0.22 |
| long | 8 Dec 2025 90,395.30 | 13 Jan 2026 96,136.30 | 36 | $3.13 |
| long | 8 Dec 2025 90,395.30 | 13 Jan 2026 96,136.30 | 36 | $1.89 |
| long | 12 Dec 2025 92,513.40 | 13 Jan 2026 96,136.30 | 32 | $0.73 |
| long | 12 Dec 2025 92,513.40 | 1 Feb 2026 78,741.10 | 51 | −$12.11 |
| long | 22 Jan 2026 89,454.70 | 1 Feb 2026 78,741.10 | 10 | −$12.05 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 67,643.00 | 1 | $0.09 |
| long | 1 Mar 2026 66,973.30 | 3 Mar 2026 68,830.10 | 2 | $0.26 |
| long | 1 Mar 2026 66,973.30 | 4 Mar 2026 71,390.30 | 3 | $3.26 |
| long | 1 Mar 2026 66,973.30 | 4 Mar 2026 71,390.30 | 3 | $1.97 |
| long | 3 Mar 2026 68,830.10 | 4 Mar 2026 71,390.30 | 1 | $0.69 |
| long | 3 Mar 2026 68,830.10 | 10 Mar 2026 69,518.40 | 7 | $0.09 |
| long | 3 Mar 2026 68,830.10 | 13 Mar 2026 72,026.70 | 10 | $2.29 |
| long | 3 Mar 2026 68,830.10 | 23 May 2026 75,539.50 | 81 | $2.01 |
| long | 10 Mar 2026 68,432.20 | 23 May 2026 75,539.50 | 74 | $10.30 |
| long | 2 Jul 2026 60,024.00 | 3 Jul 2026 61,560.00 | 1 | $0.25 |
| long | 2 Jul 2026 60,024.00 | 4 Jul 2026 63,025.20 | 2 | $2.46 |
| long | 2 Jul 2026 60,024.00 | 10 Jul 2026 63,230.00 | 8 | $0.50 |
| long | 2 Jul 2026 60,024.00 | 10 Jul 2026 63,230.00 | 8 | $1.60 |
| long | 10 Jul 2026 63,230.00 | 10 Jul 2026 63,230.00 | 0 | −$0.01 |
| long | 10 Jul 2026 63,230.00 | 15 Jul 2026 65,044.00 | 5 | $0.27 |
| long | 10 Jul 2026 63,230.00 | 19 Aug 2026 67,483.10 | 40 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| MACD Strategy with trailing ATR stop this page | — | v4 strategy | +72.7% | 89 | 53.7% |
| OBV Pyr by RafaelZioni | ema cum sma | v4 strategy | −1.2% | 27 | 9.5% |
| Crypto EMA Trend Reversal Strategy by ADHDCRYPT0 | ema sma valuewhen | v4 strategy | +37.5% | 37 | 18.3% |
| Full CRYPTO pack macd, rsi, obv, ema strategy by SoftKill21 | ema sma cum | v4 strategy | No trades on this market | ||
| Strategy Myth-Busting #23 - 2xEMA+DPO- [MYN] by myncrypto | ema sma valuewhen | v5 strategy | No trades on this market | ||
| meta-capitulation by serafimovkliment | sma cum | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | With a workaround 1,000 bars in 82 ms | Not measured |
| PineTS | No 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")