Skip to main content

Exploring the McGinley Dynamic Trading System

Discover the McGinley Dynamic Trading System, developed by John R. McGinley. It’s recognized as Investopedia’s most reliable indicator, offering unique advantages over traditional moving averages.

The McGinley Dynamic Indicator 

Compared to simple and exponential moving averages, the McGinley Dynamic is highly responsive to raw data. It’s designed to minimize whipsaws and closely track prices, outperforming conventional moving averages.

Formula for the McGinley Dynamic Indicator

MD = MD-1 + (Price – MD-1) / (N * (Price / MD-1) 4

MD-1: Indicator value for the previous candle or bar

Price: Price of the security

N: Smoothing factor

For more on AFL coding and creating your own trading systems, visit this link.

McGinley Dynamic Trading System – AFL Overview

Preferred TimeframeMcGinley Dynamic
Indicators Used25-period McGinley Dynamic indicator
Buy Condition
  • Closing price crosses the McGinley Dynamic Line upwards
  • Current close is the highest in the last 3 bars
Short Condition
  • Closing price crosses the McGinley Dynamic Line downwards
  • Current close is the lowest in the last 3 bars
Sell Condition
  • Stop Loss triggered
  • Target met
  • Short condition met
Cover Condition
  • Stop Loss triggered
  • Target met
  • Buy condition met
Stop Loss10%
Position Size150 (fixed)
Initial Equity200,000
Brokerage100 per order

AFL Code

//  Formula Name:    Anti-Martingale Trading system
//  Website:

_SECTION_BEGIN("McGinley Dynamic Indicator");

SetBarsRequired( sbrAll );
_N(Title = StrFormat("{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//Initial Parameters
SetTradeDelays( 1,1,1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption( "AllowPositionShrinking", True );

N = Param("McGinley Dynamic N ",25,5,40,5);
MD[0] = C[0];

for( i = 1; i < BarCount; i++ )
MD[ i ] = MD[ i - 1 ] + (C[i]-MD[i-1])/( N*(C[i] / MD[i-1])^4)  ;

Plot( Close, "Price",colorWhite, styleCandle );
Plot(MD,"McGinley Dynamic",colorYellow,1|styleThick);

Buy=C>MD AND C>Ref(C,-1) AND Ref(C,-1)>Ref(C,-2);
Short=C<MD AND C<Ref(C,-1) AND Ref(C,-1)<Ref(C,-2);;


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover );  


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);



AFL Screenshot

View the screenshot of the McGinley Dynamic Trading System for NSE Nifty below:

McGinley Dynamic Trading System

McGinley Dynamic Trading System – Backtest Report

This strategy has shown a respectable Compound Annual Growth Rate (CAGR) over the last 12 years, albeit with a slightly higher drawdown typical of trend-following strategies.

NSE Nifty
Initial Capital200000
Final Capital1973764.04
Scrip NameNSE Nifty
Backtest Period01-Jan-2004 to 19-Oct-2016
Net Profit %771.55%
Annual Return %18.42%
Number of Trades128
Winning Trade %36.72%
Average holding Period23.43 periods
Max consecutive losses9
Max system % drawdown-17.90%
Max Trade % drawdown-70.97%

For a detailed backtest report, download it here.

Equity Curve and Profit Table

Explore the equity curve and profit table for the McGinley Dynamic Trading System:



Additional Amibroker Settings for Backtesting

To optimize your backtesting in Amibroker, visit Symbol–>Information, and specify the lot size and margin requirement. The screenshot below illustrates a lot size of 75 and a margin requirement of 10% for NSE Nifty:

One Comment

Leave a Reply