Author: Copyright 2018, MetaQuotes Software Corp.

Overview of the Ahrens MACD Indicator

The Ahrens Moving Average Convergence Divergence (MACD) is a sophisticated technical analysis tool designed to identify trends and momentum in financial markets. While it shares its name with the classic MACD, it utilizes a specific mathematical approach known as the "Ahrens Moving Average" to provide smoother data processing and potentially more accurate signals by reducing market noise.

Core Logic and Mechanism

The script operates by calculating several layers of moving averages to derive actionable insights. Here is a breakdown of how the logic flows:

1. The Foundation: Ahrens Moving Averages

Unlike standard moving averages that might give equal weight to all data points in a period, this script calculates a specific "Median Price" for every candle (the midpoint between the high and low). It then uses this median price to build two primary components:

  • Fast Ahrens MA: A moving average that reacts quickly to recent price changes.
  • Slow Ahrens MA: A moving average that tracks long-term trends more deliberately.

The "Ahrens" logic specifically looks at the difference between the current median price and a median of previous values, ensuring that the resulting line is less erratic than a standard simple moving average.

2. The MACD Calculation

Once the Fast and Slow Ahrens Moving Averages are established, the script calculates the MACD Line. This represents the distance between the fast-moving trend and the slow-moving trend.

  • When the gap is widening in a positive direction, it suggests increasing upward momentum.
  • When the gap narrows or flips, it suggests a potential change in the trend's strength.

3. The Signal Line and Histogram

To make the data easier for traders to read, the script adds two more layers:

  • Signal Line: This is a secondary moving average calculated based on the MACD Line itself. It acts as a "smoothing" agent; when the MACD line crosses this Signal Line, it often highlights potential entry or exit points.
  • Histogram: This is the visual representation of the difference between the MACD Line and the Signal Line. It provides an immediate visual cue of the "velocity" of the market move.

Visual Representation

The script is designed to display three distinct elements in a separate window on your chart:

  • The Histogram: A multi-colored bar chart that changes color based on whether the momentum is increasing or decreasing. This helps traders quickly identify the "strength" of a current move.
  • The MACD Line (Red): The primary trend indicator showing the spread between fast and slow prices.
  • The Signal Line (Green): The smoothed average used to identify specific turning points in the trend.

Summary for the User

In simple terms, this script filters out the "jitter" of the market to show you the underlying trend. It calculates how fast the price is moving relative to its historical average and compares that movement against a smoothed signal. By looking at where the lines cross and how the histogram bars grow or shrink in color, users can better identify when a trend is gaining strength, losing steam, or potentially reversing.

Price Data Components
6 Views
0 Downloads
0 Favorites
AhrensMACD
ÿþ//+------------------------------------------------------------------+

//|                                                   AhrensMACD.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Ahrens Moving Average Convergence Divergence"

#property indicator_separate_window

#property indicator_buffers 7

#property indicator_plots   3

//--- plot Hist

#property indicator_label1  "Ahrens Histogram"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrDeepSkyBlue,clrDarkOrange,clrDarkGray

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot MACD

#property indicator_label2  "Ahrens MACD"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Signal

#property indicator_label3  "Ahrens Signal"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input uint     InpPeriodFast  =  12;   // Fast Ahrens MA period

input uint     InpPeriodSlow  =  26;   // Slow Ahrens MA period

input uint     InpPeriodSig   =  9;    // Signal line period

//--- indicator buffers

double         BufferHist[];

double         BufferColors[];

double         BufferMACD[];

double         BufferSignal[];

double         BufferFastAHRMA[];

double         BufferSlowAHRMA[];

double         BufferSignalAHRMA[];

//--- global variables

int            period_fast;

int            period_slow;

int            period_signal;

int            period_max;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- set global variables

   period_fast=int(InpPeriodFast<1 ? 1 : InpPeriodFast);

   period_slow=int(InpPeriodSlow==period_fast ? period_fast+1 : InpPeriodSlow<1 ? 1 : InpPeriodSlow);

   period_signal=int(InpPeriodSig<1 ? 1 : InpPeriodSig);

   period_max=fmax(period_fast,fmax(period_slow,period_signal));

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferHist,INDICATOR_DATA);

   SetIndexBuffer(1,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,BufferMACD,INDICATOR_DATA);

   SetIndexBuffer(3,BufferSignal,INDICATOR_DATA);

   SetIndexBuffer(4,BufferFastAHRMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferSlowAHRMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferSignalAHRMA,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Ahrens MACD ("+(string)period_fast+","+(string)period_slow+","+(string)period_signal+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferHist,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferMACD,true);

   ArraySetAsSeries(BufferSignal,true);

   ArraySetAsSeries(BufferFastAHRMA,true);

   ArraySetAsSeries(BufferSlowAHRMA,true);

   ArraySetAsSeries(BufferSignalAHRMA,true);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<fmax(period_max+1,4)) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-period_max-2;

      ArrayInitialize(BufferHist,0);

      ArrayInitialize(BufferColors,2);

      ArrayInitialize(BufferMACD,0);

      ArrayInitialize(BufferSignal,0);

      ArrayInitialize(BufferFastAHRMA,0);

      ArrayInitialize(BufferSlowAHRMA,0);

      ArrayInitialize(BufferSignalAHRMA,0);

     }



//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      double MedianPrice=(high[i]+low[i])/2.0;

      double MedianMA1=(BufferFastAHRMA[i+1]+BufferFastAHRMA[i+period_fast])/2.0;

      BufferFastAHRMA[i]=BufferFastAHRMA[i+1]+((MedianPrice-MedianMA1)/period_fast);

      //---

      double MedianMA2=(BufferSlowAHRMA[i+1]+BufferSlowAHRMA[i+period_slow])/2.0;

      BufferSlowAHRMA[i]=BufferSlowAHRMA[i+1]+((MedianPrice-MedianMA2)/period_slow);

      BufferMACD[i]=BufferFastAHRMA[i]-BufferSlowAHRMA[i];

      //---

      double MedianMA3=(BufferSignal[i+1]+BufferSignal[i+period_signal])/2.0;

      BufferSignal[i]=  BufferSignal[i+1]+((BufferMACD[i]-MedianMA3)/period_signal);

      BufferHist[i]=BufferMACD[i]-BufferSignal[i];

      BufferColors[i]=(BufferHist[i]>BufferHist[i+1] ? 0 : BufferHist[i]<BufferHist[i+1] ? 1 : 2);

     }

     

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments