HMA_v07
Download scriptThis technical indicator is designed to provide traders with a smoother, more responsive way to visualize price trends on a trading chart. It utilizes a sophisticated mathematical approach known as the Hull Moving Average (HMA) logic, which aims to reduce the "lag" typically found in standard moving averages while maintaining a clean, readable line.
How the Logic Works
The script processes market data through a multi-layered calculation to achieve its results:
1. Data Weighting and Comparison The foundation of this tool lies in comparing two different weighted averages simultaneously. It calculates a short-term average (half of your chosen period) and a long-term average (the full period). By doubling the short-term average and subtracting the long-term average, the script creates a highly reactive "raw" value that focuses on recent price movements more aggressively than a standard average would.
2. Smoothing the Curve To prevent the resulting data from being too jagged or erratic, the script applies a secondary smoothing pass. It takes the result of the first calculation and runs it through another moving average based on the square root of your chosen period. This creates a fluid, curved line that tracks price trends effectively without the "noise" of minor market fluctuations.
3. Dynamic Color Coding The most user-friendly feature of this script is its ability to change visual cues based on momentum:
- Trend Direction: The script monitors whether the current value is rising or falling. It dynamically assigns colors (White and Red) to the line to signal whether the immediate momentum is shifting upward or downward.
- Turning Points: The logic includes specific checks to identify "pivot points"—moments where the trend changes direction. By detecting when the price movement flips from positive to negative (or vice versa), it highlights potential peaks or troughs on the chart.
Summary for the Trader
Essentially, this tool acts as a high-speed trend tracker. It is designed to tell you not just where the price has been, but where it is heading right now. By smoothing out the "jitter" of the market while staying tight to the current price action, it allows you to identify trend changes more quickly than traditional moving averages, helping you stay ahead of market shifts.
//-----------------------
// HMA modified by Ron
//-----------------------
#property link "http://www.justdata.com.au/Journals/AlanHull/hull_ma.htm"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 White
#property indicator_color2 Red
//---- External parameters
extern int _maPeriod=14;
//---- indicator buffers
double _hmaW[];
double _hmaR[];
double _wma[];
int init()
{
// 233 up arrow
// 234 down arrow
// 159 big dot
// 158 little dot
// 168 open square
// 120 box with X
//---- indicator buffers memory
IndicatorBuffers(3);
//---- indicator buffers mapping
SetIndexBuffer(0, _hmaW);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID,2);
//SetIndexArrow(0,159);
SetIndexBuffer(1, _hmaR);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID,2);
//SetIndexArrow(1,159);
SetIndexBuffer(2, _wma);
}
int deinit()
{
}
int start()
{
int pos;
int fullPeriod = _maPeriod;
int halfPeriod = _maPeriod/2;
int sqrtPeriod = MathFloor(MathSqrt(_maPeriod*1.00));
double fullMA;
double halfMA;
double pastMA;
double prevMA;
double currMA;
/*
MetaStock Formula
period:=Input("Period",1,200,20) ;
sqrtperiod:=Input("Square Root of Period",1,20,4);
Mov(2*(Mov(C,period/2,W))-Mov(C,period,W),sqrtperiod,W);
SuperCharts Formula
Input: period (Default value 20)
waverage(2*waverage(close,period/2)-waverage(close,period), SquareRoot(Period))
*/
for(pos=200; pos>=0; pos--)
{
fullMA=iMA(Symbol(), 0, fullPeriod, 0, MODE_LWMA, PRICE_OPEN, pos);
halfMA=iMA(Symbol(), 0, halfPeriod, 0, MODE_LWMA, PRICE_OPEN, pos);
_wma[pos]=(2*halfMA)-fullMA;
}
for(pos=200; pos>=0; pos--)
{
prevMA=iMAOnArray(_wma,0,sqrtPeriod,0,MODE_LWMA,pos+2);
prevMA=iMAOnArray(_wma,0,sqrtPeriod,0,MODE_LWMA,pos+1);
currMA=iMAOnArray(_wma,0,sqrtPeriod,0,MODE_LWMA,pos);
if(prevMA<currMA)
{
_hmaW[pos]=currMA;
}
else
{
_hmaR[pos]=currMA;
}
if(pastMA>prevMA<currMA)
{
_hmaR[pos+1]=prevMA;
}
if(pastMA<prevMA>currMA)
{
_hmaW[pos+1]=prevMA;
}
}
}
Comments