Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (11)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$100,000$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. 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.
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 verified

Neither 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 verified

Neither 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.

SettingDefaultType and rangeFeeds
MA121intMA1
MA242intMA2
Start Date28int (written input.integer) · min 1 · max 31inDateRange
Start Month5int (written input.integer) · min 1 · max 12inDateRange
Start Year2019int (written input.integer) · min 1800 · max 2100inDateRange
End Date28int (written input.integer) · min 1 · max 31inDateRange
End Month5int (written input.integer) · min 1 · max 12inDateRange
End Year2020int (written input.integer) · min 1800 · max 2100inDateRange
Períodos20int (written input.integer)mdi
Constante K0.6floatmdi
Preçoclosesourcemdi mdiColor buySignal buyLoss sellSignal sellLoss

What it calculates

  • EMA (ema, ta.ema in v5)close, MA1 (21)MA1 · line 8
  • EMA (ema, ta.ema in 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.

closecloseprice dataMA1PeriodMA1setting · 21MA2PeriodMA2setting · 42kConstante Ksetting · 0.6periodPeríodossetting · 20timetimeprice datastartYearStart Yearsetting · 2019startMonthStart Monthsetting · 5startDateStart Datesetting · 28endYearEnd Yearsetting · 2020endMonthEnd Monthsetting · 5endDateEnd Datesetting · 28lowlowprice datahighhighprice dataMA1EMA→ MA1MA2EMA→ MA2mdimdivalueinDateRangeinDateRangeconditionbuySignalbuySignalconditionbuyLossbuyLossconditionsellSignalsellSignalconditionsellLosssellLossconditionaboveAverage, hunderAverageaboveAverage · hunderAv…2 conditionsmdiColorColourmdiColorentry:CompraOpen Comprastrategy.entryexit:Gain da compraexit Gain da comprastrategy.exitclose:CompraClose Comprastrategy.closeentry:VendaOpen Vendastrategy.entryexit:Gain da vendaexit Gain da vendastrategy.exitclose:VendaClose Vendastrategy.closeclose_all:close_all strategy.close_allplot:33plotplotplot:34plotplotbarcolor:35barcolorbarcolor

Order rules

OrderFires whenBy defaultLine
Open Compra longbuySignalaboveAverage and closePrice > mdi and crossunder(low, MA1) and close > MA1On40
Exit Gain da compra limit(inDateRange)On41
Close ComprabuyLossclosePrice < mdi and close < MA1 and close < MA2On42
Open Venda shortsellSignalhunderAverage and closePrice < mdi and crossover(high, MA1) and close < MA1On46
Exit Gain da venda limit(inDateRange)On47
Close VendasellLossclosePrice > mdi and close > MA1 and close > MA2On48
Close (not inDateRange)On50

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.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D0.0%+102.5%00%—0.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h0.0%+5.5%00%—0.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D0.0%+9.8%00%—0.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h0.0%+8.4%00%—0.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D0.0%+16.7%00%—0.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h0.0%+21.5%00%—0.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D0.0%+148.3%00%—0.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h0.0%+17.1%00%—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.

ScriptSharesTypeNetTradesMax drawdown
McGinley Dynamic Indicator this page—v4 strategy0.0%00.0%
EMA Slope AyE by fajardoarelysemav4 strategyNo trades on this market
ROoT by realisticDove62527emav5 strategy−4.7%2259.4%
sachin5986 by mohitesachin78emav4 strategy+32.1%3517.6%
sonu1997 by Sonu1997emav4 strategy+960.7%21,103.8%
Moving Average Crossover by Muh4mm4demav4 strategy+19,990.1%301,274.2%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 15 msWith a workaround 1,000 bars in 58 msNot measured
PineTSNo 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()