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

Please visit Trading Tuitions Academy to learn AFL coding and create your own Trading systems.

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.

Download AFL code from here.

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


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

Related Posts


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

  2. Hello, I saved this AFL file into my Custom folder, and if I hit Apply, it shows up as a nice price graph for the current symbol along with Buy and Sell markers (quite promising, actually). But a Backtest across that same (or other) time interval shows no trades. I’ve tried a number of tweaks to the code, but nothing yet. Any help?

  3. Never mind, I just got back to Amibroker after a long time away. I’d been using systems that specified the periodicity expressly, and this one didn’t… and my settings were defaulting to Monthly, which didn’t work at all. Daily is working now just fine, looking forward to explore this! Thanks for the resource.

  4. Thanks for sharing this.
    I’m having an error 61 and I don’t know how to fix it. If you can help me with it. It would be great. Here’s the line where the error appeared. Cheers!
    SetBarsRequired( sbrAll );
    _N(Title = StrFormat(“{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C ));

    • Hi Dave,

      Please try commenting (add // in the beginning) in the 3rd line that starts with _N

    • Please change that line with this one;
      _N(Title = StrFormat(“{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
      It will clear the error.

Leave a Reply

Your email address will not be published.