This tool is a specialized technical analysis indicator designed for the MetaTrader platform. It calculates and displays the Hull Moving Average (HMA) on your trading chart. Unlike standard moving averages, which often suffer from "lag" (the delay between a price movement and the average's reaction), this script utilizes a multi-layered mathematical approach to provide a smoother, more responsive trend line.
How the Logic Works
The script processes market data through a two-stage calculation process to achieve its unique visual output:
1. The Weighting Phase Instead of looking at price points in isolation, the logic first calculates a difference between two separate "Linearly Weighted Moving Averages" (LWMA). It compares a shorter average (half the length of your chosen period) against a longer average. By multiplying the shorter average by two and subtracting the longer one, the script captures the momentum and "velocity" of the price movement more effectively than a standard calculation.
2. The Smoothing Phase The results from the first phase are then fed into a secondary smoothing calculation. This step takes the data generated in the previous step and applies another moving average based on the square root of your chosen period. This final "pass" rounds off any jagged edges or minor noise, resulting in a flowing line that stays much closer to the actual price action while remaining visually clear.
Key Characteristics for Traders
- Reduced Lag: The primary objective of this logic is to minimize the delay between price changes and the indicator's reaction, allowing traders to identify trend shifts more quickly.
- Visual Clarity: By using these nested calculations, the indicator provides a "smooth" experience on the chart, making it easier to see the prevailing direction of the market without the distracting fluctuations often found in simpler indicators.
- Customizable Sensitivity: You can adjust the "HMA Period" to determine how fast or slow the indicator reacts. A smaller number will make the line more reactive to immediate price spikes, while a larger number will provide a broader view of long-term trends.
In summary, this script transforms raw price data into a refined trend line by balancing speed and smoothness, helping you identify market direction with greater precision.
/*-----------------------------+
| |
| 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=4;
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