HMA1_v1

This tool is designed to provide a smoother and more responsive way to track price trends on a trading chart. Unlike standard moving averages that can sometimes be "laggy" (reacting to past price changes too slowly) or too "noisy" (jumping around with every small price flicker), this script uses a specific multi-layered calculation to filter out market volatility while maintaining a close connection to current price movements.

How the Logic Works

The script functions by processing the price data through two distinct mathematical stages to create a refined trend line:

1. The Weighting Phase Instead of looking at all previous prices equally, the tool first calculates a specific type of weighted average. It takes a shorter window of recent price data and compares it against a longer window of historical data. By subtracting the longer history from the shorter history and doubling the result, the script isolates the "momentum" or the immediate direction of the price. This effectively strips away much of the background noise while highlighting where the price is heading right now.

2. The Smoothing Phase Once that momentum is calculated, the script performs a second pass. It applies another layer of weighted averaging to those intermediate results. This final step "rounds off" any jagged edges left behind by the first calculation. The result is a fluid, curved line that flows with the trend.

Key Features for Traders

  • Reduced Lag: Because of the specific way it weights recent data against older data, the line reacts more quickly to new price turns than a traditional moving average would.
  • Visual Clarity: The tool is designed to appear as a distinct yellow line on your chart. It provides a clear visual "path" for the price; when the line moves up, it suggests an upward trend, and when it curves down, it signals a potential reversal.
  • Customizable Sensitivity: You can adjust the timeframe (Period) of the calculation. A shorter period will make the line more sensitive to every small move in the market, while a longer period will provide a much smoother, broader view of the long-term trend.

In summary, this script acts as a sophisticated filter. It takes raw, messy market data and distills it into a clean, flowing line that helps you identify the prevailing direction of the market without being distracted by minor, insignificant price fluctuations.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
HMA1_v1
//+------------------------------------------------------------------+
//|                                                          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