Pine Script v4 · Strategy
MACD BTC Long-Short Strategy v1.0
A Pine Script v4 strategy built on Crossover (close, ema), Crossunder (close, ema) and EMA (close, 21). It has 14 order rules, 7 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 10000.00 in cash and pays no commission. 16 settings; defaults include Re-enter Delay 2, Sculp Delay 8 and Close Delay 1.
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 BTC Long-Short Strategy v1.0 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 BTC Long-Short Strategy v1.0 do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Can MACD BTC Long-Short Strategy v1.0 be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does MACD BTC Long-Short Strategy v1.0 use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MACD BTC Long-Short Strategy v1.0 run in Zeal Terminal?
- Yes
- It compiles in 35 ms and runs 1,000 bars in 53 ms, in the browser.
BTCUSDT · 1D · default settings
- Crossover
- Crossunder
- EMA
- SMA
- SMA
- SMA
- Crossover
- Crossunder
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $10,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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
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.
-
Note
Uses an argument v6 removed
when = on line 128, 129, 130, 131, 133, 135, 143. The upgraded version below moves each call inside an if block.
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 ema → ta.ema, sma → ta.sma, crossover → ta.crossover, crossunder → ta.crossunder.
- 7 strategy calls use the when = argument (line 128, 129, 130, 131, 133, 135, 143). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.integer) → input.int()
- Line 10: input(type = input.integer) → input.int()
22 more edits
- Line 11: input(type = input.integer) → input.int()
- Line 12: input(type = input.source) → input.source()
- Line 13: input(type = input.integer) → input.int()
- Line 14: input(type = input.bool) → input.bool()
- Line 15: input(type = input.bool) → input.bool()
- Line 17: input(type = input.integer) → input.int()
- Line 19: input(type = input.integer) → input.int()
- Line 21: input(type = input.integer) → input.int()
- Line 23: input(type = input.integer) → input.int()
- Line 25: input(type = input.integer) → input.int()
- Line 27: input(type = input.integer) → input.int()
- Line 48: ema → ta.ema
- Line 57: sma → ta.sma
- Line 57: ema → ta.ema
- Line 58: sma → ta.sma
- Line 58: ema → ta.ema
- Line 60: sma → ta.sma
- Line 60: ema → ta.ema
- Line 65: crossover → ta.crossover
- Line 66: crossunder → ta.crossunder
- Line 70: crossover → ta.crossover
- Line 71: crossunder → ta.crossunder
⋯// © Puckapao-//@version=4+//@version=5strategy(title="MACD", shorttitle="MACD", overlay=true, initial_capital=10000.00, currency="USD", default_qty_type=strategy.cash, default_qty_value=10000.00)// Getting inputs-reenter_delay = input(title="Re-enter Delay", type=input.integer, defval=2)+reenter_delay = input.int(title="Re-enter Delay", defval=2)-sculp_delay = input(title="Sculp Delay", type=input.integer, defval=8)+sculp_delay = input.int(title="Sculp Delay", defval=8)-close_delay = input(title="Close Delay", type=input.integer, defval=1)+close_delay = input.int(title="Close Delay", defval=1)-ema_period = input(title="EMA Period", type=input.integer, defval=21)+ema_period = input.int(title="EMA Period", defval=21)-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=true)+sma_signal = input.bool(title="Simple MA(Signal Line)", defval=true)// Date Settings-startDate = input(title="Start Date", type=input.integer,+startDate = input.int(title="Start Date", defval=19, minval=1, maxval=31)-startMonth = input(title="Start Month", type=input.integer,+startMonth = input.int(title="Start Month", defval=09, minval=1, maxval=12)-startYear = input(title="Start Year", type=input.integer,+startYear = input.int(title="Start Year", defval=2017, minval=1800, maxval=2100)-endDate = input(title="End Date", type=input.integer,+endDate = input.int(title="End Date", defval=31, minval=1, maxval=31)-endMonth = input(title="End Month", type=input.integer,+endMonth = input.int(title="End Month", defval=3, minval=1, maxval=12)-endYear = input(title="End Year", type=input.integer,+endYear = input.int(title="End Year", defval=2021, minval=1800, maxval=2100)⋯underwater = false-ema = ema(close, ema_period)+ema = ta.ema(close, ema_period)// 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⋯// plot(signal, title="Signal", color=col_signal, transp=0)-cross_up = crossover(macd, signal)+cross_up = ta.crossover(macd, signal)-cross_down = crossunder(macd, signal)+cross_down = ta.crossunder(macd, signal)if (inDateRange)⋯ under_macd = macd < 0 and signal < 0 ? true : false- overwater := crossover(close, ema)+ overwater := ta.crossover(close, ema)- underwater := crossunder(close, ema)+ underwater := ta.crossunder(close, ema) slowdown := hist >= 0 ? (hist[1] > hist ? true : false) : (hist[1] < hist ? true : false)⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 128: when = slowdown → if block
- Line 129: when = slowdown → if block
- Line 130: when = slowdown → if block
- Line 131: when = slowdown → if block
- Line 133: when = slowdown → if block
- Line 135: when = slowdown → if block
- Line 143: when = underwater → if block
⋯ if (slowdown == true and close_cnt >= close_delay)- strategy.close("long", when = slowdown, comment = "close long", alert_message = "close long")+ if slowdown+ strategy.close("long", comment = "close long", alert_message = "close long")- strategy.close("short", when = slowdown, comment = "close short", alert_message = "close short")+ if slowdown+ strategy.close("short", comment = "close short", alert_message = "close short")- strategy.close("re-long", when = slowdown, comment = "close re-long", alert_message = "close re-long")+ if slowdown+ strategy.close("re-long", comment = "close re-long", alert_message = "close re-long")- strategy.close("re-short", when = slowdown, comment = "close re-short", alert_message = "close re-short")+ if slowdown+ strategy.close("re-short", comment = "close re-short", alert_message = "close re-short") if (hist >= 0)- strategy.close("sculp-long", when = slowdown, comment = "close sculp-long", alert_message = "close sculp-long")+ if slowdown+ strategy.close("sculp-long", comment = "close sculp-long", alert_message = "close sculp-long") else- strategy.close("sculp-short", when = slowdown, comment = "close sculp-short", alert_message = "close sculp-short")+ if slowdown+ strategy.close("sculp-short", comment = "close sculp-short", alert_message = "close sculp-short") ⋯ - strategy.close("ema-long", when = underwater, comment = "close ema-long", alert_message = "close ema-long")+ if underwater+ strategy.close("ema-long", comment = "close ema-long", alert_message = "close ema-long")plotchar(slowdown, "close?", "")⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Puckapao-//@version=5+//@version=6strategy(title="MACD", shorttitle="MACD", overlay=true, initial_capital=10000.00, currency="USD", default_qty_type=strategy.cash, default_qty_value=10000.00)⋯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 |
|---|---|---|---|
| Re-enter Delay | 2 | int (written input.integer) | line 77 line 112 |
| Sculp Delay | 8 | int (written input.integer) | line 84 line 102 |
| Close Delay | 1 | int (written input.integer) | close long close short close re-long close re-short close ema-long line 91 line 127 |
| EMA Period | 21 | int (written input.integer) | ema |
| 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) | true | bool | signal |
| Start Date | 19 | int (written input.integer) · min 1 · max 31 | inDateRange |
| Start Month | 09 | int (written input.integer) · min 1 · max 12 | inDateRange |
| Start Year | 2017 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
| End Date | 31 | int (written input.integer) · min 1 · max 31 | inDateRange |
| End Month | 3 | int (written input.integer) · min 1 · max 12 | inDateRange |
| End Year | 2021 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
What it calculates
- Crossover (
crossover,ta.crossoverin v5)close, emaoverwater · line 70 - Crossunder (
crossunder,ta.crossunderin v5)close, emaunderwater · line 71 - EMA (
ema,ta.emain v5)close, EMA Period (21)ema · line 48 - SMA / EMA (
sma,ta.smain v5)Source (close), Fast Length (12)fast_ma · line 57 - SMA / EMA (
sma,ta.smain v5)Source (close), Slow Length (26)slow_ma · line 58 - SMA / EMA (
sma,ta.smain v5)macd, Signal Smoothing (9)signal · line 60 - Crossover (
crossover,ta.crossoverin v5)macd, signalcross_up · line 65 - Crossunder (
crossunder,ta.crossunderin v5)macd, signalcross_down · line 66
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 | (cross_up == true) | On | 97 |
| Open short short | (cross_down == true) | On | 99 |
| Open sculp-short short | (hist >= 0) | On | 104 |
| Open sculp-long long | (hist >= 0) | On | 106 |
| Open re-long long | (hist >= 0) | On | 114 |
| Open re-short short | (hist >= 0) | On | 116 |
| Open ema-long long | (overwater == true) | On | 123 |
| Close long | slowdown | On | 128 |
| Close short | slowdown | On | 129 |
| Close re-long | slowdown | On | 130 |
| Close re-short | slowdown | On | 131 |
| Close sculp-long | slowdown | On | 133 |
| Close sculp-short | slowdown | On | 135 |
| Close ema-long | underwater | On | 143 |
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 | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
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 BTC Long-Short Strategy v1.0 this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| 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 35 ms | Yes 1,000 bars in 53 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/// © Puckapao//@version=4strategy(title="MACD", shorttitle="MACD", overlay=true, initial_capital=10000.00, currency="USD", default_qty_type=strategy.cash, default_qty_value=10000.00)// Getting inputsreenter_delay = input(title="Re-enter Delay", type=input.integer, defval=2)sculp_delay = input(title="Sculp Delay", type=input.integer, defval=8)close_delay = input(title="Close Delay", type=input.integer, defval=1)ema_period = input(title="EMA Period", type=input.integer, defval=21)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=true)// Date SettingsstartDate = input(title="Start Date", type=input.integer, defval=19, minval=1, maxval=31)startMonth = input(title="Start Month", type=input.integer, defval=09, minval=1, maxval=12)startYear = input(title="Start Year", type=input.integer, defval=2017, minval=1800, maxval=2100)endDate = input(title="End Date", type=input.integer, defval=31, minval=1, maxval=31)endMonth = input(title="End Month", type=input.integer, defval=3, minval=1, maxval=12)endYear = input(title="End Year", type=input.integer, defval=2021, minval=1800, maxval=2100) inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))reenter_cnt = 0reenter_cnt := nz(reenter_cnt[1])sculp_cnt = 0sculp_cnt := nz(sculp_cnt[1])close_cnt = 0close_cnt := nz(close_cnt[1])on_long = falseon_long := nz(on_long[1])on_short = falseon_short := nz(on_short[1])sculp = falsereenter = falseslowdown = falseoverwater = falseunderwater = falseema = ema(close, ema_period)// 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)cross_up = crossover(macd, signal)cross_down = crossunder(macd, signal)if (inDateRange) over_macd = macd > 0 and signal > 0 ? true : false under_macd = macd < 0 and signal < 0 ? true : false overwater := crossover(close, ema) underwater := crossunder(close, ema) slowdown := hist >= 0 ? (hist[1] > hist ? true : false) : (hist[1] < hist ? true : false) reenter := hist >= 0 ? (hist[1] < hist ? true : false) : (hist[1] > hist ? true : false) sculp := (hist >= 0 ? (hist[1] > hist ? true : false) : (hist[1] < hist ? true : false)) if(reenter == true) if(reenter_cnt < reenter_delay) reenter_cnt := reenter_cnt + 1 else if(reenter_cnt > 0) reenter_cnt := reenter_cnt - 1 if(sculp == true) if(sculp_cnt < sculp_delay) sculp_cnt := sculp_cnt + 1 else if(sculp_cnt > 0) sculp_cnt := sculp_cnt - 1 if(slowdown == true) if(close_cnt < close_delay) close_cnt := close_cnt + 1 else close_cnt := close_cnt - 1 // Entry if (cross_up == true) strategy.entry("long", strategy.long, comment = "long", alert_message = "long") if (cross_down == true) strategy.entry("short", strategy.short, comment = "short", alert_message = "short") // Sculp bottom / top if (sculp == true and sculp_cnt >= sculp_delay) if (hist >= 0) strategy.entry("sculp-short", strategy.short, comment = "sculp-short", alert_message = "sculp-short") else strategy.entry("sculp-long", strategy.long, comment = "sculp-long", alert_message = "sculp-long") sculp_cnt := 0 sculp := false // Re-Entry if (reenter == true and reenter_cnt >= reenter_delay) if (hist >= 0) strategy.entry("re-long", strategy.long, comment = "re-long", alert_message = "re-long") else strategy.entry("re-short", strategy.short, comment = "re-short", alert_message = "re-short") reenter_cnt := 0 reenter := false // Over EMA if (overwater == true) strategy.entry("ema-long", strategy.long, comment = "ema-long", alert_message = "ema-long") // Close if (slowdown == true and close_cnt >= close_delay) strategy.close("long", when = slowdown, comment = "close long", alert_message = "close long") strategy.close("short", when = slowdown, comment = "close short", alert_message = "close short") strategy.close("re-long", when = slowdown, comment = "close re-long", alert_message = "close re-long") strategy.close("re-short", when = slowdown, comment = "close re-short", alert_message = "close re-short") if (hist >= 0) strategy.close("sculp-long", when = slowdown, comment = "close sculp-long", alert_message = "close sculp-long") else strategy.close("sculp-short", when = slowdown, comment = "close sculp-short", alert_message = "close sculp-short") if (slowdown) if (hist >= 0) on_long := false else on_short := false strategy.close("ema-long", when = underwater, comment = "close ema-long", alert_message = "close ema-long")plotchar(slowdown, "close?", "")plotchar(close_cnt, "close delay", "")plotchar(reenter, "reenter?", "")plotchar(reenter_cnt, "reenter delay", "")plotchar(sculp, "sculp?", "")plotchar(sculp_cnt, "sculp delay", "")plotchar(overwater, "over ema", "")plotchar(underwater, "under ema", "")