HMA_Color_v03
Download scriptThis tool is designed to provide a visual representation of market momentum by calculating the Hull Moving Average (HMA), a technical indicator known for its ability to reduce "lag"—the delay between a price movement and the indicator's reaction—while maintaining a smooth appearance.
How the Logic Works
The script operates through a multi-stage calculation process to interpret price action:
1. The Core Calculation Instead of simply averaging the price over a set timeframe, the logic creates a sophisticated mathematical relationship between two different moving averages. It looks at a "short-term" average (half of the user-defined period) and compares it against a "long-term" average (the full period). By doubling the short-term data and subtracting the long-term data, the script isolates the most recent price velocity.
2. Smoothing the Data To ensure the resulting information isn't too erratic or "noisy," the logic applies a third layer of smoothing. It calculates the square root of your chosen period to determine how much extra filtering is needed. This creates a final value that tracks the price closely but stays fluid and easy for the human eye to follow.
3. Visual Direction Tracking The primary goal of this script is to show you the direction of the trend in real-time. It monitors whether the calculated average is currently moving upward or downward:
- Upward Momentum: When the current value is higher than the previous one, the script identifies an bullish shift.
- Downward Momentum: When the current value drops below the previous one, it identifies a bearish shift.
Visual Output
The indicator translates these mathematical shifts into color-coded visual cues on your chart. Rather than just showing a single line, it highlights changes in direction:
- It uses distinct colors to differentiate between upward and downward movements.
- By highlighting these transitions, the tool helps you quickly identify "turning points" where the market's short-term momentum may be shifting from one trend to another.
In summary, this script simplifies complex momentum math into a clear, visual guide that highlights the immediate direction of the market while filtering out insignificant price fluctuations.
//+----------------+
//| HMA.mq4 by Ron|
//+----------------+
/*
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))
Added color on direction change
*/
#property copyright "Ron Thompson"
#property link "http://www.lightpatch.com/forex/"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Yellow
#property indicator_color3 Red
//User input
extern int HMA_Period=14;
extern int BarsToDraw=200;
// display buffers
double ind_buffer0[];
double ind_buffer1[];
double ind_buffer2[];
// computation buffers
double buffer[];
//Custom init
int init()
{
IndicatorBuffers(4);
SetIndexBuffer(0,ind_buffer0) ;
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(1,ind_buffer1);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(2,ind_buffer2);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(3, buffer);
}
//Custom DE-init
int deinit()
{
}
// Main Program loop
int start()
{
int i;
int counted_bars=IndicatorCounted();
double tmp, tmpPrevious;
double fullMA;
double halfMA;
int fullPeriod = HMA_Period;
int halfPeriod = HMA_Period/2;
int sqrtPeriod = MathFloor(MathSqrt(HMA_Period*1.00));
//---- MA difference counted in the 1-st buffer
for(i=0; i<BarsToDraw; i++)
{
halfMA=iMA(NULL,0,halfPeriod,0,MODE_LWMA,PRICE_CLOSE,i)*2;
fullMA=iMA(NULL,0,fullPeriod,0,MODE_LWMA,PRICE_CLOSE,i);
buffer[i]=halfMA-fullMA;
}
//calculate bar[0]
tmp=iMAOnArray(buffer,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,0);
for(i=1; i<BarsToDraw; i++)
{
tmpPrevious=iMAOnArray(buffer,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);
if (tmpPrevious > tmp)
{
ind_buffer0[i] = EMPTY_VALUE;
ind_buffer1[i] = EMPTY_VALUE;
ind_buffer2[i] = tmpPrevious+0.0005;
ind_buffer2[i-1] = tmp-0.0005;
}
else if (tmpPrevious < tmp)
{
ind_buffer0[i] = tmpPrevious+0.0005;
ind_buffer0[i-1] = tmp-0.0005;
ind_buffer1[i] = EMPTY_VALUE;
ind_buffer2[i] = EMPTY_VALUE;
}
else
{
ind_buffer0[i] = CLR_NONE;
ind_buffer1[i] = tmpPrevious+0.0005;
ind_buffer2[i-1] = tmp-0.0005;
ind_buffer2[i] = CLR_NONE;
}
tmp = tmpPrevious;
}//for
}
Comments