Pine Script v4 · Strategy
McGinley Dynamic Indicator
A Pine Script v4 strategy built on EMA (close, 21). It has 7 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a profit target and no stop loss. Each trade is one contract and pays no commission. 11 settings; defaults include MA1 21, MA2 42 and Start Date 28.
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 McGinley Dynamic Indicator repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did McGinley Dynamic Indicator 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 McGinley Dynamic Indicator 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 McGinley Dynamic Indicator use a stop loss?
- No
- Its exits set a profit target but no stop.
- Does McGinley Dynamic Indicator run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 58 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- 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, $100,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
strategy.exit is used without a stop, loss or trailing stop.
-
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 40, 42, 46, 48. 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, max → math.max, pow → math.pow, crossunder → ta.crossunder, crossover → ta.crossover.
- 4 strategy calls use the when = argument (line 40, 42, 46, 48). 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 8: ema → ta.ema
- Line 9: ema → ta.ema
- Line 13: input(type = input.integer) → input.int()
- Line 14: input(type = input.integer) → input.int()
- Line 15: input(type = input.integer) → input.int()
10 more edits
- Line 16: input(type = input.integer) → input.int()
- Line 17: input(type = input.integer) → input.int()
- Line 18: input(type = input.integer) → input.int()
- Line 22: input(type = input.integer) → input.int()
- Line 24: input(type = input.float) → input.float()
- Line 26: input(type = input.source) → input.source()
- Line 29: max → math.max
- Line 29: pow → math.pow
- Line 37: crossunder → ta.crossunder
- Line 43: crossover → ta.crossover
⋯// © LucasZancheta-//@version=4+//@version=5strategy(shorttitle="Maguila", title="McGinley Dynamic Indicator", overlay=true)⋯MA2Period=input(42, title="MA2")-MA1 = ema(close, MA1Period)+MA1 = ta.ema(close, MA1Period)-MA2 = ema(close, MA2Period)+MA2 = ta.ema(close, MA2Period)aboveAverage = MA1 >= MA2⋯//Período do backtest-startDate = input(title="Start Date", type=input.integer, defval=28, minval=1, maxval=31)+startDate = input.int(title="Start Date", defval=28, minval=1, maxval=31)-startMonth = input(title="Start Month", type=input.integer, defval=5, minval=1, maxval=12)+startMonth = input.int(title="Start Month", defval=5, minval=1, maxval=12)-startYear = input(title="Start Year", type=input.integer, defval=2019, minval=1800, maxval=2100)+startYear = input.int(title="Start Year", defval=2019, minval=1800, maxval=2100)-endDate = input(title="End Date", type=input.integer, defval=28, minval=1, maxval=31)+endDate = input.int(title="End Date", defval=28, minval=1, maxval=31)-endMonth = input(title="End Month", type=input.integer, defval=5, minval=1, maxval=12)+endMonth = input.int(title="End Month", defval=5, minval=1, maxval=12)-endYear = input(title="End Year", type=input.integer, defval=2020, minval=1800, maxval=2100)+endYear = input.int(title="End Year", defval=2020, minval=1800, maxval=2100)//Verifica se o candle está dentro do período do backtest⋯//Número de periodos da média móvel-period = input(title="Períodos", type=input.integer, defval=20)+period = input.int(title="Períodos", defval=20)//Constante K (0.6)-k = input(title="Constante K", type=input.float, defval=0.6)+k = input.float(title="Constante K", defval=0.6)//Preço de fechamento -closePrice = input(title="Preço", type=input.source, defval=close)+closePrice = input.source(title="Preço", defval=close)mdi = 0.0//Fórmula de McGinley-mdi := na(mdi[1]) ? closePrice : mdi[1] + (closePrice - mdi[1]) / max((k * period * pow(closePrice / mdi[1], 4)), 1)+mdi := na(mdi[1]) ? closePrice : mdi[1] + (closePrice - mdi[1]) / math.max((k * period * math.pow(closePrice / mdi[1], 4)), 1)//Regra de coloração ⋯//Estratégia-buySignal = aboveAverage and closePrice > mdi and crossunder(low, MA1) and close > MA1 +buySignal = aboveAverage and closePrice > mdi and ta.crossunder(low, MA1) and close > MA1 buyLoss = closePrice < mdi and close < MA1 and close < MA2⋯ strategy.close("Compra", qty=1, when= buyLoss, comment="Loss na operação")-sellSignal = hunderAverage and closePrice < mdi and crossover(high, MA1) and close < MA1+sellSignal = hunderAverage and closePrice < mdi and ta.crossover(high, MA1) and close < MA1sellLoss = closePrice > mdi and close > MA1 and close > MA2⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 40: when = buySignal → if block
- Line 42: when = buyLoss → if block
- Line 46: when = sellSignal → if block
- Line 48: when = sellLoss → if block
⋯//Médias móveis-MA1Period=input(21, title="MA1")+MA1Period=input.int(21, title="MA1")-MA2Period=input(42, title="MA2")+MA2Period=input.int(42, title="MA2")MA1 = ta.ema(close, MA1Period)⋯if (inDateRange)- strategy.entry("Compra", strategy.long, qty=1, when= buySignal)+ if buySignal+ strategy.entry("Compra", strategy.long, qty=1) strategy.exit("Gain da compra", "Compra", qty=1, profit=20)- strategy.close("Compra", qty=1, when= buyLoss, comment="Loss na operação")+ if buyLoss+ strategy.close("Compra", qty=1, comment="Loss na operação")sellSignal = hunderAverage and closePrice < mdi and ta.crossover(high, MA1) and close < MA1⋯if (inDateRange)- strategy.entry("Venda", strategy.short, qty=1, when= sellSignal)+ if sellSignal+ strategy.entry("Venda", strategy.short, qty=1) strategy.exit("Gain da venda", "Venda", qty=1, profit=20)- strategy.close("Venda", qty=1, when= sellLoss, comment="Loss na operação")+ if sellLoss+ strategy.close("Venda", qty=1, comment="Loss na operação")if (not inDateRange)⋯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.
⋯// © LucasZancheta-//@version=5+//@version=6strategy(shorttitle="Maguila", title="McGinley Dynamic Indicator", overlay=true)⋯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 |
|---|---|---|---|
| MA1 | 21 | int | MA1 |
| MA2 | 42 | int | MA2 |
| Start Date | 28 | int (written input.integer) · min 1 · max 31 | inDateRange |
| Start Month | 5 | int (written input.integer) · min 1 · max 12 | inDateRange |
| Start Year | 2019 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
| End Date | 28 | int (written input.integer) · min 1 · max 31 | inDateRange |
| End Month | 5 | int (written input.integer) · min 1 · max 12 | inDateRange |
| End Year | 2020 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
| Períodos | 20 | int (written input.integer) | mdi |
| Constante K | 0.6 | float | mdi |
| Preço | close | source | mdi mdiColor buySignal buyLoss sellSignal sellLoss |
What it calculates
- EMA (
ema,ta.emain v5)close, MA1 (21)MA1 · line 8 - EMA (
ema,ta.emain v5)close, MA2 (42)MA2 · line 9
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 Compra long | buySignalaboveAverage and closePrice > mdi and crossunder(low, MA1) and close > MA1 | On | 40 |
| Exit Gain da compra limit | (inDateRange) | On | 41 |
| Close Compra | buyLossclosePrice < mdi and close < MA1 and close < MA2 | On | 42 |
| Open Venda short | sellSignalhunderAverage and closePrice < mdi and crossover(high, MA1) and close < MA1 | On | 46 |
| Exit Gain da venda limit | (inDateRange) | On | 47 |
| Close Venda | sellLossclosePrice > mdi and close > MA1 and close > MA2 | On | 48 |
| Close | (not inDateRange) | On | 50 |
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 |
|---|---|---|---|---|---|
| McGinley Dynamic Indicator this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | With a workaround 1,000 bars in 58 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/// © LucasZancheta//@version=4strategy(shorttitle="Maguila", title="McGinley Dynamic Indicator", overlay=true)//Médias móveisMA1Period=input(21, title="MA1")MA2Period=input(42, title="MA2")MA1 = ema(close, MA1Period)MA2 = ema(close, MA2Period)aboveAverage = MA1 >= MA2hunderAverage = MA2 >= MA1//Período do backteststartDate = input(title="Start Date", type=input.integer, defval=28, minval=1, maxval=31)startMonth = input(title="Start Month", type=input.integer, defval=5, minval=1, maxval=12)startYear = input(title="Start Year", type=input.integer, defval=2019, minval=1800, maxval=2100)endDate = input(title="End Date", type=input.integer, defval=28, minval=1, maxval=31)endMonth = input(title="End Month", type=input.integer, defval=5, minval=1, maxval=12)endYear = input(title="End Year", type=input.integer, defval=2020, minval=1800, maxval=2100)//Verifica se o candle está dentro do período do backtestinDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))//Número de periodos da média móvelperiod = input(title="Períodos", type=input.integer, defval=20)//Constante K (0.6)k = input(title="Constante K", type=input.float, defval=0.6)//Preço de fechamento closePrice = input(title="Preço", type=input.source, defval=close)mdi = 0.0//Fórmula de McGinleymdi := na(mdi[1]) ? closePrice : mdi[1] + (closePrice - mdi[1]) / max((k * period * pow(closePrice / mdi[1], 4)), 1)//Regra de coloração mdiColor = closePrice > mdi ? color.green : closePrice < mdi ? color.red : color.black//Inserindo as informações no gráfico plot(MA1, color=color.blue, linewidth=2)plot(MA2, color=color.purple, linewidth=2)barcolor(mdiColor)//EstratégiabuySignal = aboveAverage and closePrice > mdi and crossunder(low, MA1) and close > MA1 buyLoss = closePrice < mdi and close < MA1 and close < MA2if (inDateRange) strategy.entry("Compra", strategy.long, qty=1, when= buySignal) strategy.exit("Gain da compra", "Compra", qty=1, profit=20) strategy.close("Compra", qty=1, when= buyLoss, comment="Loss na operação")sellSignal = hunderAverage and closePrice < mdi and crossover(high, MA1) and close < MA1sellLoss = closePrice > mdi and close > MA1 and close > MA2if (inDateRange) strategy.entry("Venda", strategy.short, qty=1, when= sellSignal) strategy.exit("Gain da venda", "Venda", qty=1, profit=20) strategy.close("Venda", qty=1, when= sellLoss, comment="Loss na operação")if (not inDateRange) strategy.close_all()