HMA1

Overview of the Hull Moving Average (HMA) Indicator

This script is designed for the MetaTrader platform to provide traders with a specialized technical analysis tool known as the Hull Moving Average. Unlike standard moving averages, which can often lag behind current price action, the logic within this script aims to create a much more responsive and "fluid" visual representation of price trends. It achieves this by using a multi-layered smoothing calculation.

How the Logic Works

The indicator operates through a two-step mathematical process to filter out market noise while maintaining high sensitivity to price changes:

1. The Difference Calculation First, the script calculates two different types of Weighted Moving Averages (WMA) based on the price at which trades are opened. It takes a shorter period (half of the total user-defined period) and multiplies it by two, then subtracts a longer moving average. This specific mathematical "difference" is used to capture the momentum and immediate direction of the market.

2. The Final Smoothing The result from the first step is then passed through a second smoothing layer. By applying another Weighted Moving Average to those previous results, the script "rounds off" the jagged edges of the initial calculation. This creates the final smooth line seen on the chart.

Key Features & Behavior

  • Reduced Lag: By combining weighted averages in this specific sequence, the indicator reacts faster to price turns than a traditional moving average would.
  • Visual Smoothness: Because of the double-layered weighting, the resulting yellow line appears much smoother and more continuous on your chart, making it easier to identify the underlying trend at a glance.
  • Customizable Sensitivity: The user can adjust the "Period" parameter to control how much historical data is considered. A shorter period makes the line react more aggressively to price spikes, while a longer period creates a more stable, broader view of the market trend.
  • Dynamic Drawing: The script automatically calculates where on the chart it should begin drawing to ensure that there is enough historical data available to maintain accuracy from the very first visible point.
Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
HMA1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                          HMA.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 1
#property  indicator_color1  Yellow

//---- indicator parameters
extern int HMA_Period=7;
extern int MAshift=0;
//---- indicator buffers
double     ind_buffer0[];
double     ind_buffer1[];

int        draw_begin0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping
   IndicatorBuffers(2);
   if(!SetIndexBuffer(0,ind_buffer0) && !SetIndexBuffer(1,ind_buffer1))
      Print("cannot set indicator buffers!");
//   ArraySetAsSeries(ind_buffer1,true);
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   draw_begin0=HMA_Period+MathFloor(MathSqrt(HMA_Period));
   SetIndexDrawBegin(0,draw_begin0);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("HMA("+HMA_Period+")");
   SetIndexLabel(0,"Hull Moving Average");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit,i;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<1)
     {
      for(i=1;i<=draw_begin0;i++) ind_buffer0[Bars-i]=0;
      for(i=1;i<=HMA_Period;i++) ind_buffer1[Bars-i]=0;
     }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- MA difference counted in the 1-st buffer
   for(i=0; i<limit; i++)
      ind_buffer1[i]=iMA(NULL,0,MathFloor(HMA_Period/2),MAshift,MODE_LWMA,PRICE_OPEN,i)*2-
                     iMA(NULL,0,HMA_Period,MAshift,MODE_LWMA,PRICE_OPEN,i);
//---- HMA counted in the 0-th buffer
   for(i=0; i<limit; i++)
      ind_buffer0[i]=iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);
//---- done
   return(0);
  }

Comments