IC Markets

Demystifying the Hull Moving Average (HMA) – A Beginner's Guide + Free MT4 Code!

Discuss 1-minute to 15-minute price action setups, fading intraday momentum, key support/resistance zones, and proven short-term trading methodologies.
Post Reply
PTScalper
Site Admin
Posts: 246
Joined: Mon Jul 20, 2026 1:28 pm

Demystifying the Hull Moving Average (HMA) – A Beginner's Guide + Free MT4 Code!

Post by PTScalper »

Hi Scalpers,

i hope everybody is fine and enjoy summer with at least some good profitable trades :-)

If you are new to trading, you have probably already experimented with Simple Moving Averages (SMA) or Exponential Moving Averages (EMA). They are great for identifying trends, but they all share one frustrating problem: lag.

By the time a traditional moving average crosses or changes direction, the price has often already made its big move.

Enter the Hull Moving Average (HMA).

What is the Hull Moving Average?
Its created by Australian trader Alan Hull in 2005, the HMA was designed to solve the age-old dilemma of moving averages: How do you make a line that reacts quickly to current price changes, but remains smooth enough to filter out market noise?

Alan Hull cracked the code by using a clever mathematical formula involving Weighted Moving Averages (WMA) and square roots. You don't need to be a math genius to use it, but the result is a moving average that practically hugs the price action and drastically reduces lag.

Why Newbies Love the HMA
Speed: It reacts to price reversals much faster than an SMA or EMA.

Smoothness: Despite its speed, it doesn't get "choppy" or give as many false signals during small price spikes.

Simplicity: It is incredibly easy to read. You trade based on the slope of the line.

How to Trade with the HMA
As a beginner, keep it simple. The most common way to use the HMA is as a directional filter:

Uptrend: When the HMA turns upwards, the trend is generally bullish (look for buy setups).
Downtrend: When the HMA turns downwards, the trend is generally bearish (look for sell setups).
Exit/Caution: When the HMA flattens out or flips direction, it’s a strong early warning that the current trend is losing momentum.
Important Note: No indicator is a crystal ball! Never trade solely on an HMA changing direction. Combine it with price action, support/resistance, or another indicator (like RSI or MACD) to confirm your entries.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 246
Joined: Mon Jul 20, 2026 1:28 pm

Re: Demystifying the Hull Moving Average (HMA) – A Beginner's Guide + Free MT4 Code!

Post by PTScalper »

Your Free MT4 (MQL4) Code

MT4 doesn't come with the HMA built-in by default, so I’ve written a clean, lightweight custom indicator for you.

How to install it:

1.) Open MT4 and press F4 to open the MetaEditor.

2.) Click New -> Custom Indicator -> click Next until you finish (don't worry about the inputs, we will overwrite them).

3.) Delete all the default code that appears.

4.) Copy and paste the code below into the blank editor.

5.) Click Compile (or press F7).

6.) Restart MT4 or refresh your Navigator panel, and drag the "Hull Moving Average" onto your chart!

Code: Select all

//+------------------------------------------------------------------+
//|                                           HullMovingAverage.mq4  |
//|                                    Beginner Friendly HMA Script  |
//+------------------------------------------------------------------+
#property copyright "Forum Community"
#property link      ""
#property version   "1.00"
#property strict

// Let MT4 know this indicator goes on the main chart
#property indicator_chart_window 

// We need 2 buffers (one for the final line, one for background math)
#property indicator_buffers 2 

// Customize the visual look of the HMA line
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_style1 STYLE_SOLID

//--- User Inputs (You can change these in MT4 settings)
input int HMAPeriod = 14;                                // HMA Period
input ENUM_APPLIED_PRICE PriceType = PRICE_CLOSE;        // Applied Price

//--- Indicator Buffers
double HMABuffer[];    // The line you see on the chart
double DiffBuffer[];   // Hidden buffer for math calculations

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Setup the visible HMA line
   SetIndexBuffer(0, HMABuffer);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "HMA(" + IntegerToString(HMAPeriod) + ")");
   
   // Setup the hidden math buffer
   SetIndexBuffer(1, DiffBuffer);
   SetIndexStyle(1, DRAW_NONE); // Keeps it invisible

   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[])
  {
   // Math setup for HMA formula
   int halfPeriod = (int)MathFloor(HMAPeriod / 2.0);
   int sqrtPeriod = (int)MathFloor(MathSqrt(HMAPeriod));

   // Calculate how many bars we need to update
   int limit = rates_total - prev_calculated;
   
   // If it's the very first time loading, calculate everything (minus the periods we need for math)
   if(prev_calculated == 0) limit = rates_total - HMAPeriod - sqrtPeriod - 1;
   if(limit < 0) return 0;
   
   // If we are just updating live ticks, only recalculate the current bar and previous bar
   if(prev_calculated > 0) limit++; 

   // STEP 1: Calculate the difference between the Half WMA and Full WMA
   for(int i = limit; i >= 0; i--)
     {
      double wmaHalf = iMA(NULL, 0, halfPeriod, 0, MODE_LWMA, PriceType, i);
      double wmaFull = iMA(NULL, 0, HMAPeriod, 0, MODE_LWMA, PriceType, i);
      
      // HMA Formula part 1: (2 * WMA(n/2)) - WMA(n)
      DiffBuffer[i] = (2.0 * wmaHalf) - wmaFull;
     }

   // STEP 2: Calculate the WMA of the difference using the Square Root of the period
   for(int i = limit; i >= 0; i--)
     {
      double sum = 0.0;
      double weightSum = 0.0;
      
      // Manual WMA calculation on our DiffBuffer
      for(int j = 0; j < sqrtPeriod; j++)
        {
         int weight = sqrtPeriod - j;
         if((i + j) < rates_total) // Prevent out-of-bounds errors
           {
            sum += DiffBuffer[i + j] * weight;
            weightSum += weight;
           }
        }
        
      // HMA Formula part 2: The final smoothed result!
      if(weightSum > 0) HMABuffer[i] = sum / weightSum;
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
I hope this helps some of you clear up the lag on your charts! Feel free to ask if you have any questions about how to read it or how to install the code.
What currency pairs or timeframes are you planning to test the HMA out on first?
Take a care.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Post Reply