This tool is designed to provide traders with a specialized visual aid for analyzing price trends on a financial chart. Specifically, it calculates and displays the Hull Moving Average (HMA), which is a technical indicator valued for its ability to smooth out price fluctuations while maintaining minimal "lag"—the delay that often occurs between a price movement and its reflection on a standard moving average.
Core Logic and Workflow
The script processes market data through a multi-step mathematical sequence to create a cleaner, more responsive trend line:
1. Data Weighting and Calculation Rather than simply averaging the price over a set number of days or bars, the logic begins by calculating two different weighted averages simultaneously. It looks at a shorter timeframe (half of the user-defined period) and compares it against a longer timeframe (the full period). By doubling the value of the shorter average and subtracting the longer one, the script effectively captures the "momentum" of the price movement.
2. Smoothing the Velocity The result of that first calculation is then subjected to a second layer of smoothing. The tool takes the square root of your chosen period to determine how much further to smooth the data. This creates a refined curve that eliminates "noise" (minor, insignificant price wiggles) while staying very close to the actual current price action.
3. Trend Identification and Peak Tracking The script performs an additional final pass to evaluate the direction of the trend. It compares the current value of the calculated average against the previous one. If the average is moving downward or leveling off, the logic identifies these shifts. This helps in visualizing whether the momentum is strengthening, weakening, or reversing.
User Experience
On your chart, this script provides a visual representation of the "heartbeat" of the market. By adjusting the primary period, you can decide how sensitive you want the indicator to be:
- Shorter Periods: Make the line react quickly to sudden price jumps, ideal for fast-paced trading.
- Longer Periods: Create a smoother, broader curve that helps identify long-term trends and major structural shifts in the market.
Ultimately, the logic serves to strip away the visual clutter of volatile price bars, allowing you to see the underlying trend more clearly.
//+------------------------------------------------------------------+
//| HMA.mq4
//| Copyright © 2006 WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104
//| wizardserg@mail.ru
//| Adapted March 7 2006 by request from Secxces by FloFri
//+------------------------------------------------------------------+
#property copyright "MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104"
#property link "wizardserg@mail.ru"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
extern int period=21;
extern int method=0; // MODE_SMA
extern int price=0; // PRICE_CLOSE
//---- buffers
double ExtMapBuffer1[], ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexBuffer(1, ExtMapBuffer2);
ArraySetAsSeries(ExtMapBuffer1, true);
ArraySetAsSeries(ExtMapBuffer2, true);
IndicatorShortName("Hull Moving Average("+period+")");
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
// ???? ????? ?????? ??????
return(0);
}
//+------------------------------------------------------------------+
//| ?????????? ??????? |
//+------------------------------------------------------------------+
double WMA(int x, int p)
{
return(iMA(NULL, 0, p, 0, method, price, x));
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars = IndicatorCounted();
if(counted_bars < 0)
return(-1);
int x = 0;
int p = MathSqrt(period);
int e = Bars - counted_bars + period + 1;
double vect[];
if(e > Bars)
e = Bars;
ArrayResize(vect, e);
ArraySetAsSeries(vect, true);
for(x = 0; x < e; x++)
{
vect[x] = 2*WMA(x, period/2) - WMA(x, period);
// Print("Bar date/time: ", TimeToStr(Time[x]), " close: ", Close[x], " vect[", x, "] = ", vect[x], " 2*WMA(p/2) = ", 2*WMA(x, period/2), " WMA(p) = ", WMA(x, period));
}
for(x = 0; x < e-period; x++) {
ExtMapBuffer1[x] = iMAOnArray(vect, 0, p, 0, method, x);
}
for(x = 0; x < e-period; x++) {
if( ExtMapBuffer1[x] <= ExtMapBuffer1[x+1] ) {
ExtMapBuffer2[x] = ExtMapBuffer1[x];
//ExtMapBuffer1[x+1] = EMPTY_VALUE;
}
}
Comment(ExtMapBuffer1[0]);
return(0);
}
//+------------------------------------------------------------------+
Comments