Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

MACD + DMI Scalping with Volatility Stop by (Coinrule)

A Pine Script v4 strategy built on DMI (14, 14), MACD (close, 12) and ATR (atrlen). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.1% commission. 11 settings; defaults include From Month 1, From Day 1 and From Year 2021.

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 + DMI Scalping with Volatility Stop by (Coinrule) repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
How did MACD + DMI Scalping with Volatility Stop by (Coinrule) do on BTCUSDT 1D with default settings?
−19.26%
From 26 Jan 2024 to 21 Oct 2026, over 15 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can MACD + DMI Scalping with Volatility Stop by (Coinrule) 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 + DMI Scalping with Volatility Stop by (Coinrule) use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does MACD + DMI Scalping with Volatility Stop by (Coinrule) run in Zeal Terminal?
Yes
It compiles in 10 ms and runs 1,000 bars in 48 ms, in the browser.

BTCUSDT · 1D · default settings

  • 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 (11)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$81$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. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference. Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 42) so the engine trades the way the script says.
Net profit
−19.26%
−$19.26
Buy and hold
+102.5%
same period
Closed trades
15
7 won · 8 lost
Profitable
46.7%
of closed trades
Profit factor
0.59
gross profit ÷ gross loss
Max drawdown
25.48%
−$27.17

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

    1 setting does nothing

    “Show Date Range” is declared but never read.

  • Check

    No stop loss

    Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.

  • Check

    Every trade uses all the equity

    default_qty_value is 100% of equity.

  • Note

    1 setting has no label

    In the settings dialog Take_profit (line 19) shows an empty name. The labels on this page come from the variable names.

  • 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 42, 44. The upgraded version below moves each call inside an if block.

  • Note

    Uses long =, which v5 renamed line 42

    long = on line 42. v5 writes it as direction = strategy.long or strategy.short.

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 dmi → ta.dmi, macd → ta.macd, atr → ta.atr, max → math.max, min → math.min, crossunder → ta.crossunder, crossover → ta.crossover.
  • 2 strategy calls use the when = argument (line 42, 44). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

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

  • Line 2: //@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()
13 more edits
  • Line 11: input(type = input.integer) → input.int()
  • Line 12: input(type = input.bool) → input.bool()
  • Line 17: dmi → ta.dmi
  • Line 18: macd → ta.macd
  • Line 29: atr → ta.atr
  • Line 29: tr → ta.tr
  • Line 30: max → math.max
  • Line 31: min → math.min
  • Line 32: max → math.max
  • Line 32: min → math.min
  • Line 40: crossunder → ta.crossunder
  • Line 42: long = true → direction = strategy.long
  • Line 42: crossover → ta.crossover
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=4+//@version=5strategy(shorttitle='(MACD + DMI Scalping with Volatility Stop',title='MACD + DMI Scalping with Volatility Stop by (Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)⋯//Backtest dates-fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)+fromMonth = input.int(defval = 1,    title = "From Month", minval = 1, maxval = 12)-fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)+fromDay   = input.int(defval = 1,    title = "From Day", minval = 1, maxval = 31)-fromYear  = input(defval = 2021, title = "From Year",       type = input.integer, minval = 1970)+fromYear  = input.int(defval = 2021, title = "From Year", minval = 1970)-thruMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)+thruMonth = input.int(defval = 1,    title = "Thru Month", minval = 1, maxval = 12)-thruDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)+thruDay   = input.int(defval = 1,    title = "Thru Day", minval = 1, maxval = 31)-thruYear  = input(defval = 2112, title = "Thru Year",       type = input.integer, minval = 1970)+thruYear  = input.int(defval = 2112, title = "Thru Year", minval = 1970)-showDate  = input(defval = true, title = "Show Date Range", type = input.bool)+showDate  = input.bool(defval = true, title = "Show Date Range")start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window⋯// DMI and MACD inputs and calculations-[pos_dm, neg_dm, avg_dm] = dmi(14, 14)+[pos_dm, neg_dm, avg_dm] = ta.dmi(14, 14)-[macd, macd_signal, macd_histogram] = macd(close, 12, 26, 9)+[macd, macd_signal, macd_histogram] = ta.macd(close, 12, 26, 9)Take_profit= ((input (3))/100)⋯    var stop    = 0.0-    atrM        = nz(atr(atrlen) * atrfactor, tr)+    atrM        = nz(ta.atr(atrlen) * atrfactor, ta.tr)-    max         := max(max, src)+    max         := math.max(max, src)-    min         := min(min, src)+    min         := math.min(min, src)-    stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)+    stop        := nz(uptrend ? math.max(stop, max - atrM) : math.min(stop, min + atrM), src)    uptrend     := src - stop >= 0.0⋯[vStop, uptrend] = volStop(src, length, factor)-closeLong = close > longTakeProfit or crossunder(close, vStop)+closeLong = close > longTakeProfit or ta.crossunder(close, vStop)//Entry -strategy.entry(id="long", long = true, when = crossover(macd, macd_signal) and pos_dm > neg_dm and window())+strategy.entry(id="long", direction = strategy.long, when = ta.crossover(macd, macd_signal) and pos_dm > neg_dm and window())//Exit⋯

Clean v5

VerifiedDownload the v5 script

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

  • Line 19: input() → input.int()
  • Line 21: input() → input.int()
  • Line 22: input() → input.source()
  • Line 23: input() → input.float()
  • Line 42: when = ta.crossover(macd, macd_signal) and pos_dm > neg_dm and window() → if block
  • Line 44: when = closeLong and window() → if block
⋯[macd, macd_signal, macd_histogram] = ta.macd(close, 12, 26, 9)-Take_profit= ((input (3))/100)+Take_profit= ((input.int (3))/100)longTakeProfit = strategy.position_avg_price * (1 + Take_profit)-length = input(20, "Length", minval = 2)+length = input.int(20, "Length", minval = 2)-src = input(close, "Source")+src = input.source(close, "Source")-factor = input(2.0, "vStop Multiplier", minval = 0.25, step = 0.25)+factor = input.float(2.0, "vStop Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) =>⋯//Entry -strategy.entry(id="long", direction = strategy.long, when = ta.crossover(macd, macd_signal) and pos_dm > neg_dm and window())+if ta.crossover(macd, macd_signal) and pos_dm > neg_dm and window()+    strategy.entry(id="long", direction = strategy.long)//Exit-strategy.close("long", when = closeLong and window())+if closeLong and window()+    strategy.close("long")

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

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=5+//@version=6strategy(shorttitle='(MACD + DMI Scalping with Volatility Stop',title='MACD + DMI Scalping with Volatility Stop by (Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)⋯

Settings

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

SettingDefaultType and rangeFeeds
From Month1int (written input.integer) · min 1 · max 12start
From Day1int (written input.integer) · min 1 · max 31start
From Year2021int (written input.integer) · min 1970start
Thru Month1int (written input.integer) · min 1 · max 12finish
Thru Day1int (written input.integer) · min 1 · max 31finish
Thru Year2112int (written input.integer) · min 1970finish
Show Date Rangetrueboolunused
Take profit (no label in the script)3intlongTakeProfit
Length20int · min 2uptrend vStop
Sourceclosesourcemax min uptrend stop vStop
vStop Multiplier2.0float · min 0.25 · step 0.25uptrend vStop

What it calculates

  • DMI (dmi, ta.dmi in v5)14, 14pos_dm · line 17
  • DMI (dmi, ta.dmi in v5)14, 14neg_dm · line 17
  • DMI (dmi, ta.dmi in v5)14, 14avg_dm · line 17
  • MACD (macd, ta.macd in v5)close, 12, 26, 9macd · line 18
  • MACD (macd, ta.macd in v5)close, 12, 26, 9macd_signal · line 18
  • MACD (macd, ta.macd in v5)close, 12, 26, 9macd_histogram · line 18
  • ATR (atr, ta.atr in v5)atrlenatrM · line 29

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.

high · low · closehigh · low · closeprice dataclosecloseprice datafromYearFrom Yearsetting · 2021fromMonthFrom Monthsetting · 1fromDayFrom Daysetting · 1thruYearThru Yearsetting · 2112thruMonthThru Monthsetting · 1thruDayThru Daysetting · 1Take_profitTake profitsetting · 3lengthLengthsetting · 20factorvStop Multipliersetting · 2.0pos_dmDMI→ pos_dmneg_dmDMI→ neg_dmavg_dmDMI→ avg_dmmacdMACD→ macdmacd_signalMACD→ macd_signalmacd_histogramMACD→ macd_histogramatrMATR→ atrMstartstartvaluefinishfinishvaluelongTakeProfitlongTakeProfitvaluemaxmaxvalueminminvalueuptrend, vStopuptrend · vStop2 valuesstopstopvaluecloseLongcloseLongconditionentry:longOpen longstrategy.entryclose:longClose longstrategy.close

Order rules

OrderFires whenBy defaultLine
Open long longcrossover(macd, macd_signal) and pos_dm > neg_dm and window()On42
Close longcloseLong and window()On44

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.

4/4trades 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.

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−19.3%+102.5%1547%0.5925.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−7.3%+5.5%1323%0.4611.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−25.3%+9.8%1656%0.6136.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+1.3%+8.4%1741%1.0513.2%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+32.3%+16.7%1385%1.8327.6%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−11.5%+21.5%1331%0.6120.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+55.2%+148.3%1776%2.5021.5%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+6.4%+17.1%1540%1.458.9%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 market617%−$24.17
Sideways market863%$1.19
Falling market1100%$3.72

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

How bad could the drawdown be?

32.8%

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

0%38%
All 15 trades
SideEntryExitBarsP&L
long5 Jun 2024 71,108.0013 Jun 2024 66,773.008−$6.28
long14 Oct 2024 66,084.0018 Oct 2024 68,428.004$3.13
long28 Oct 2024 69,962.2029 Oct 2024 72,736.401$3.64
long6 Nov 2024 75,572.0010 Nov 2024 80,370.004$6.17
long16 Dec 2024 106,059.0019 Dec 2024 97,461.903−$8.84
long18 May 2025 106,454.0022 May 2025 111,696.004$4.61
long10 Jun 2025 110,274.0017 Jun 2025 104,551.007−$5.51
long22 Jul 2025 119,954.001 Aug 2025 113,298.0010−$5.56
long11 Aug 2025 118,686.0013 Aug 2025 123,306.002$3.37
long1 Oct 2025 118,595.003 Oct 2025 122,232.002$2.71
long26 Oct 2025 114,559.003 Nov 2025 106,583.008−$6.96
long25 Mar 2026 71,336.5013 Apr 2026 74,418.0019$3.72
long4 May 2026 79,861.0017 May 2026 77,457.7013−$3.02
long3 Sep 2026 81,270.4010 Sep 2026 76,568.707−$5.45
long21 Sep 2026 86,620.008 Oct 2026 81,754.4017−$4.98

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 + DMI Scalping with Volatility Stop by (Coinrule) this page—v4 strategy−19.3%1525.5%
ADX - DMI Trend Strategy by millerrhatr dmiv4 strategy−0.8%917.2%

Where it runs

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

Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 42) so the engine trades the way the script says.

“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///@version=4strategy(shorttitle='(MACD + DMI Scalping with Volatility Stop',title='MACD + DMI Scalping with Volatility Stop by (Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)// Works better on 3h, 1h, 2h, 4h//Backtest datesfromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)fromYear  = input(defval = 2021, title = "From Year",       type = input.integer, minval = 1970)thruMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)thruDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)thruYear  = input(defval = 2112, title = "Thru Year",       type = input.integer, minval = 1970)showDate  = input(defval = true, title = "Show Date Range", type = input.bool)start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start windowfinish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish windowwindow()  => time >= start and time <= finish ? true : false       // create function "within window of time"// DMI and MACD inputs and calculations[pos_dm, neg_dm, avg_dm] = dmi(14, 14)[macd, macd_signal, macd_histogram] = macd(close, 12, 26, 9)Take_profit= ((input (3))/100)longTakeProfit = strategy.position_avg_price * (1 + Take_profit)length = input(20, "Length", minval = 2)src = input(close, "Source")factor = input(2.0, "vStop Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) =>    var max     = src    var min     = src    var uptrend = true    var stop    = 0.0    atrM        = nz(atr(atrlen) * atrfactor, tr)    max         := max(max, src)    min         := min(min, src)    stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)    uptrend     := src - stop >= 0.0    if uptrend != nz(uptrend[1], true)        max    := src        min    := src        stop   := uptrend ? max - atrM : min + atrM    [stop, uptrend][vStop, uptrend] = volStop(src, length, factor)closeLong = close > longTakeProfit or crossunder(close, vStop)//Entry strategy.entry(id="long", long = true, when = crossover(macd, macd_signal) and pos_dm > neg_dm and window())//Exitstrategy.close("long", when = closeLong and window())