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
- 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
VerifiedSame 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⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| From Month | 1 | int (written input.integer) · min 1 · max 12 | start |
| From Day | 1 | int (written input.integer) · min 1 · max 31 | start |
| From Year | 2021 | int (written input.integer) · min 1970 | start |
| Thru Month | 1 | int (written input.integer) · min 1 · max 12 | finish |
| Thru Day | 1 | int (written input.integer) · min 1 · max 31 | finish |
| Thru Year | 2112 | int (written input.integer) · min 1970 | finish |
| Show Date Range | true | bool | unused |
| Take profit (no label in the script) | 3 | int | longTakeProfit |
| Length | 20 | int · min 2 | uptrend vStop |
| Source | close | source | max min uptrend stop vStop |
| vStop Multiplier | 2.0 | float · min 0.25 · step 0.25 | uptrend vStop |
What it calculates
- DMI (
dmi,ta.dmiin v5)14, 14pos_dm · line 17 - DMI (
dmi,ta.dmiin v5)14, 14neg_dm · line 17 - DMI (
dmi,ta.dmiin v5)14, 14avg_dm · line 17 - MACD (
macd,ta.macdin v5)close, 12, 26, 9macd · line 18 - MACD (
macd,ta.macdin v5)close, 12, 26, 9macd_signal · line 18 - MACD (
macd,ta.macdin v5)close, 12, 26, 9macd_histogram · line 18 - ATR (
atr,ta.atrin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | crossover(macd, macd_signal) and pos_dm > neg_dm and window() | On | 42 |
| Close long | closeLong and window() | On | 44 |
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 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −19.3% | +102.5% | 15 | 47% | 0.59 | 25.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −7.3% | +5.5% | 13 | 23% | 0.46 | 11.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −25.3% | +9.8% | 16 | 56% | 0.61 | 36.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.3% | +8.4% | 17 | 41% | 1.05 | 13.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +32.3% | +16.7% | 13 | 85% | 1.83 | 27.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −11.5% | +21.5% | 13 | 31% | 0.61 | 20.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +55.2% | +148.3% | 17 | 76% | 2.50 | 21.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +6.4% | +17.1% | 15 | 40% | 1.45 | 8.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 6 | 17% | −$24.17 |
| Sideways market | 8 | 63% | $1.19 |
| Falling market | 1 | 100% | $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.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 5 Jun 2024 71,108.00 | 13 Jun 2024 66,773.00 | 8 | −$6.28 |
| long | 14 Oct 2024 66,084.00 | 18 Oct 2024 68,428.00 | 4 | $3.13 |
| long | 28 Oct 2024 69,962.20 | 29 Oct 2024 72,736.40 | 1 | $3.64 |
| long | 6 Nov 2024 75,572.00 | 10 Nov 2024 80,370.00 | 4 | $6.17 |
| long | 16 Dec 2024 106,059.00 | 19 Dec 2024 97,461.90 | 3 | −$8.84 |
| long | 18 May 2025 106,454.00 | 22 May 2025 111,696.00 | 4 | $4.61 |
| long | 10 Jun 2025 110,274.00 | 17 Jun 2025 104,551.00 | 7 | −$5.51 |
| long | 22 Jul 2025 119,954.00 | 1 Aug 2025 113,298.00 | 10 | −$5.56 |
| long | 11 Aug 2025 118,686.00 | 13 Aug 2025 123,306.00 | 2 | $3.37 |
| long | 1 Oct 2025 118,595.00 | 3 Oct 2025 122,232.00 | 2 | $2.71 |
| long | 26 Oct 2025 114,559.00 | 3 Nov 2025 106,583.00 | 8 | −$6.96 |
| long | 25 Mar 2026 71,336.50 | 13 Apr 2026 74,418.00 | 19 | $3.72 |
| long | 4 May 2026 79,861.00 | 17 May 2026 77,457.70 | 13 | −$3.02 |
| long | 3 Sep 2026 81,270.40 | 10 Sep 2026 76,568.70 | 7 | −$5.45 |
| long | 21 Sep 2026 86,620.00 | 8 Oct 2026 81,754.40 | 17 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| MACD + DMI Scalping with Volatility Stop by (Coinrule) this page | — | v4 strategy | −19.3% | 15 | 25.5% |
| ADX - DMI Trend Strategy by millerrh | atr dmi | v4 strategy | −0.8% | 9 | 17.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | With a workaround 1,000 bars in 48 ms | Not measured |
| PineTS | No 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())