McGinley Dynamic Trading System: Amibroker AFL

McGinely Dynamic indicator devised by John R.McGinley has been stated as the most reliable indicator at Investopedia. This indicator is more responsive to raw data as compared to simple moving average or exponential moving average. McGinely dynamic is supposed to avoid whipsaws when compared with any moving averages. It looks like a moving average line yet it is a smoothing mechanism for prices that turns out to track far better than any moving average. It minimizes price separation, price whipsaws and hugs prices much more closely. In this post we will evaluate McGinley Dynamic Trading System and go through its backtest report.

Below is the mathematical formula for McGinely 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

Read our article on AFL tutorial here.

McGinley Dynamic Trading System: AFL Overview

Parameter Value
Preferred Timeframe McGinely Dynamic
Indicators Used 25 period McGinely Dynamic indicator
Buy Condition
  • Closing price crosses McGinley Dynamic Line upwards
  • Current close is greatest in last 3 bars
Short Condition
  • Closing price crosses McGinley Dynamic Line downwards
  • Current close is lowest in last 3 bars
Sell Condition
  • Stop Loss hit
  • Target Met
  • Short Condition met
Cover Condition
  • Stop Loss hit
  • Target Met
  • Buy Condition met
Stop Loss 10%
Targets 40%
Position Size 150 (fixed)
Initial Equity 200000
Brokerage 100 per order
Margin 10%

AFL Code

This indicator is originally coded by Karthik Marar, we have added backtest conditions and buy/sell signals.

//------------------------------------------------------
//
//  Formula Name:    McGinley Dynamic Indicator
//  Original Author: Karthik Marar
//  Modified By:     Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//------------------------------------------------------

_SECTION_BEGIN("McGinley Dynamic Indicator");

SetBarsRequired( sbrAll );
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));

//Initial Parameters
SetTradeDelays( 1,1,1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
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);;
Sell=Short;
Cover=Buy;

BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

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

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

StopLoss=Param("SL",10,1,10,1);
Target=Param("Target",40,5,40,5);
ApplyStop(Type=0,Mode=1,Amount=StopLoss);
ApplyStop(Type=1,Mode=1,Amount=Target);

/* 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);

_SECTION_END();

AFL Screenshot

Below is the screenshot of McGinley Dynamic Trading System for NSE Nifty

McGinley Dynamic Trading System

McGinley Dynamic Trading System: Backtest Report

This strategy has achieved a decent CAGR in last 12 years. Th drawdown is on a little higher side similar to any trend following strategy.

 Parameter Value
  NSE Nifty
Initial Capital  200000
Final Capital 1743095.50
Scrip Name NSE Nifty
Backtest Period 01-Jan-2004 to 19-Oct-2016
Timeframe Daily
Net Profit % 771.55%
Annual Return % 18.42%
Number of Trades 128
Winning Trade % 36.72%
Average holding Period 23.43 periods
Max consecutive losses 9
Max system % drawdown -17.90%
Max Trade % drawdown -70.97%

Download the detailed backtest report here.

Equity Curve and Profit Table

McGinley Dynamic Equity Curve

McGinley Dynamic Profit Table.PNG

Additional Amibroker settings for backtesting

Goto Symbol–>Information, and specify the lot size and margin requirement. The below screenshot shows lot size of 75 and margin requirement of 10% for NSE Nifty:

Symbol Info_Nifty

Disclaimer:

All the AFL’s posted in this section are for learning purpose. Trading Tuitions does not necessarily own these AFL’s and we don’t have any intellectual property rights on them. We might copy useful AFL’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com

4 Comments

  1. McGinley Dynamic Trading System- Pl provide us excel sheetlike other excel sheet based on this system.

Leave a Reply

Your email address will not be published. Required fields are marked *