HMA_Angle2
Download scriptThis tool is designed to help traders visualize the momentum and direction of price trends by calculating the "angle" of a specific moving average. Instead of just looking at where the price is currently, this indicator measures how aggressively the trend is moving over a set period of time.
Core Logic Overview
The script works by comparing two different points on a Hull Moving Average (HMA)—a type of moving average known for its smoothness and ability to reduce lag. By measuring the difference between these two points, the tool determines whether the market is trending upward, downward, or remaining flat.
How it Works
- Trend Measurement: The script identifies a starting point and an ending point on the Hull Moving Average (based on user-defined shifts). It calculates the vertical distance between these two points.
- Angle Conversion: It converts that vertical distance into a numerical "angle." To ensure these angles are visible and easy to read on your trading chart, the logic applies a scaling factor that adjusts automatically depending on the currency pair (such as distinguishing between standard forex pairs and JPY pairs).
- Filtering Noise: To prevent small, insignificant price wiggles from creating false signals, the script uses a "Threshold." The indicator only highlights significant movements; if the angle is too small to be meaningful, it remains neutral.
Visual Representation
The tool displays its findings in a separate window below your main chart using three distinct visual markers:
- Upward Momentum (Lime Green): When the price trend is moving up with enough strength to exceed your specified threshold, the indicator shows a positive value. This suggests strong bullish momentum.
- Downward Momentum (FireBrick): When the price trend is moving down with significant strength, it reflects a negative value. This indicates strong bearish momentum.
- Neutral/Flat Movement (Yellow): If the angle of the trend is very shallow—meaning the market is consolidating or moving sideways—the indicator stays centered near zero.
Summary for Traders
Essentially, this script acts as a momentum filter. It tells you not just what the price is doing, but how fast it is moving in a specific direction. By watching the switch between the green and red highlights, you can better identify when a trend is gaining significant power or when the market is losing steam and entering a period of indecision.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| HMA_Angle.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_separate_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Yellow
#property indicator_color3 FireBrick
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
//---- indicator parameters
extern int HMA_Period=34;
extern double AngleTreshold=15;
extern int StartHMAShift=4;
extern int EndHMAShift=0;
//---- indicator buffers
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];
//---- additional buffers
int draw_begin0;
double mFactor;
int ShiftDif;
string Sym;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- 3 indicator buffers mapping
if(!SetIndexBuffer(0,UpBuffer) &&
!SetIndexBuffer(1,DownBuffer) &&
!SetIndexBuffer(2,ZeroBuffer))
Print("cannot set indicator buffers!");
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
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_Angle("+HMA_Period+","+AngleTreshold+","+StartHMAShift+","+EndHMAShift+")");
SetIndexLabel(0,"HMA Angle");
if(EndHMAShift >= StartHMAShift)
{
Print("Error: EndHMAShift >= StartHMAShift");
StartHMAShift = 6;
EndHMAShift = 0;
}
mFactor = 100000.0;
Sym = StringSubstr(Symbol(),3,3);
if (Sym == "JPY") mFactor = 1000.0;
ShiftDif = StartHMAShift-EndHMAShift;
mFactor /= ShiftDif;
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
double ma1, ma2;
double fEndMA, fStartMA;
double fAngle;
int limit,i;
double angle;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0)
return(-1);
if(counted_bars<1)
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for(i=0; i<limit; i++)
{
fEndMA=iCustom(NULL, 0, "HMA",HMA_Period,0,i+EndHMAShift);
fStartMA=iCustom(NULL, 0, "HMA",HMA_Period,0,i+StartHMAShift);
// 10000.0 : Multiply by 10000 so that the fAngle is not too small
// for the indicator Window.
fAngle = mFactor * (fEndMA - fStartMA)/2.0;
//Print (mFactor, fEndMA, fStartMA, fAngle);
DownBuffer[i] = 0.0;
UpBuffer[i] = 0.0;
ZeroBuffer[i] = 0.0;
if(fAngle > AngleTreshold)
{
UpBuffer[i] = fAngle;
}
else if (fAngle < -AngleTreshold)
{
DownBuffer[i] = fAngle;
}
else ZeroBuffer[i] = fAngle;
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments