HMA

This script is designed to create a specialized technical analysis tool called the Hull Moving Average (HMA). Its primary goal is to track price trends more smoothly and with less delay than traditional moving averages, making it easier for traders to identify the current direction of the market.

How the Logic Works

To achieve its specific results, the script processes data through a multi-step mathematical sequence rather than simply averaging the current price:

1. Data Weighting and Comparison Instead of treating every past price point equally, the script uses a "Weighted Moving Average" (WMA). It looks at two different timeframes simultaneously based on your chosen settings:

  • It calculates a WMA for the full period you specify.
  • It calculates a second WMA for half of that period.

By doubling the value of the shorter timeframe and subtracting the longer timeframe from it, the script creates a "difference" value. This mathematical contrast helps highlight recent price momentum while filtering out some of the "noise" found in standard averages.

2. Smoothing the Result The result of the first step is still somewhat reactive to sudden price spikes. To fix this, the script performs a second calculation:

  • It takes the difference calculated above and runs it through one final Weighted Moving Average.
  • The "length" of this final average is determined by the square root of your original period.

3. Visual Output The final result is a single, continuous line plotted on your trading chart. Because of this two-step process (calculating the difference and then smoothing that difference), the resulting line follows the price very closely but maintains a fluid, curved appearance.

Summary for Traders

In simple terms, this script isn't just "averaging" the past; it is comparing short-term momentum against long-term trends to find the "true" path of the price. It is designed to be faster than a standard moving average (meaning it reacts quicker to changes) while remaining smooth enough to prevent traders from being misled by minor, insignificant price fluctuations.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
HMA
/*+------------------------------------------------------------------+
 | FileName: i_Hull_MA.mq4
 | Description: This indicator calculates the Hull Moving Average
 |  
 | Original equation is:
 | ---------------------
 | waverage(2*waverage(close,period/2)-waverage(close,period), SquareRoot(Period)
	 Implementation below is more efficient with lengthy Weighted Moving Averages.
	 In addition, the length needs to be converted to an integer value after it is halved and
	 its square root is obtained in order for this to work with Weighted Moving Averaging

 | Version: 000 20050903 17:06 GMT
 +------------------------------------------------------------------------------------------+*/
#property link      "http://www.justdata.com.au/Journals/AlanHull/hull_ma.htm"

#property indicator_chart_window
#property indicator_buffers 1

#property indicator_color1 Silver

//---- External parameters
extern int _maPeriod=120;

//---- indicator buffers
double _hma[];
double _wma[];

//----
int ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
	int    draw_begin;
	string short_name;
	IndicatorBuffers(7);

	//---- indicator buffers mapping
	SetIndexBuffer(0, _hma);
	SetIndexEmptyValue(0, 0.0);
	SetIndexStyle(0, DRAW_LINE);
	
	SetIndexBuffer(1, _wma);
	SetIndexEmptyValue(1, 0.0);
	
	IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

	//---- initialization done
	return(0);
}

//+------------------------------------------------------------------+
//| CalculateIndicators                                                                 |
//+------------------------------------------------------------------+
void calculateIndicators(int i) {
	int period=_maPeriod;
	double sqrtPeriod = MathSqrt(period*1.00);
	int halfPeriod=period/2;

	double wma1 = iMA(Symbol(), 0, period, 0, MODE_LWMA, PRICE_CLOSE, i);
	double wma2 = iMA(Symbol(), 0, halfPeriod, 0, MODE_LWMA, PRICE_CLOSE, i);
	_wma[i] = 2*wma2-wma1;
	
	_hma[i]=iMAOnArray(_wma, 0, sqrtPeriod, 0, MODE_LWMA, i);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start() {
	int i, shift, countedBars=IndicatorCounted();
	int maxBars=_maPeriod*2;

	if(Bars<_maPeriod) return(-1);
  if(countedBars == 0) countedBars = maxBars;
	int limit=Bars-countedBars+maxBars;
	
	//---- moving average
	for(i=limit; i>=0; i--) {
		calculateIndicators(i);
	} 

	return(0);
}

Comments