HMAlower_v1

Download script
HMAlower_v1

This tool is designed to provide a specialized moving average that helps traders identify price trends with greater smoothness and less "lag" than standard indicators. While typical moving averages can sometimes react too slowly to sudden market changes, this specific calculation—known as the Hull Moving Average (HMA)—is engineered to balance speed and stability.

How the Logic Works

The script calculates the trend by following a multi-step mathematical process:

1. Weighting the Price Data Instead of looking at all prices equally, the tool first focuses on the "Open" price of each candle. It applies a specific type of weighted average (Linear Weighted Moving Average). This gives more importance to recent price action, ensuring the indicator stays relevant to what is happening right now in the market.

2. Calculating the Variance The core logic involves a clever mathematical "tug-of-war." The script takes two different sets of moving averages:

  • One is set to half of your chosen period.
  • The other is set to your full chosen period.

By multiplying the shorter average by two and subtracting the longer average, the tool isolates the most significant momentum of the price movement. This step essentially "crushes" the data to highlight the direction of the trend while filtering out minor market noise.

3. Final Smoothing To ensure the resulting line doesn't jump around too erratically, the script performs one final calculation on that variance. It applies a final smoothing pass based on the square root of your chosen period. This produces a fluid, continuous line that follows the price closely but remains steady enough to be used as a reliable guide for identifying trend reversals and momentum shifts.

Key Features for Users

  • Customizable Period: You can adjust the "HMA_Period" setting to determine how many candles of historical data the tool should analyze. A shorter period makes it more reactive, while a longer period makes it more stable.
  • Visual Clarity: The indicator is designed to appear as a solid blue line directly on your main chart window, making it easy to see at a glance whether the market is trending upward or downward.
  • Precision: The script automatically adjusts its decimal places to match the specific asset you are trading (like Forex pairs or Gold), ensuring the visual data is as accurate as possible.
Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
HMAlower_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  Blue

//---- indicator parameters
extern int HMA_Period=20;
//---- 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,STYLE_SOLID,3);
   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),0,MODE_LWMA,PRICE_OPEN,i)*2-
                     iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_OPEN,i)-Point*40;
//---- 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