This script calculates and displays a technical indicator called the Hull Moving Average (HMA) on a price chart. Here's the breakdown of how it works, explained in a way that doesn't require programming knowledge:
-
What is the Hull Moving Average (HMA)? It's a type of moving average that aims to smooth out price data and reduce the lag commonly found in other moving averages. The goal is to provide a clearer picture of the price trend.
-
What the script does: The script's job is to automatically calculate the HMA based on historical price data and then plot the HMA line on the chart. This allows traders to visually analyze the HMA in relation to price movements.
-
The Inputs (Extern):
- HMA Period: This is the main setting that controls the HMA's sensitivity. A smaller period makes the HMA react faster to price changes but can also lead to more false signals. A larger period makes it smoother but slower to react. The script is defaulting to using a period of 4.
- Method: This determines the type of moving average to use in the calculation. The script defaulting to use MODE_SMA which is Simple Moving Average.
- Price: This allows you to select which price the moving average is calculated from. It is defaulted to the close price.
-
The Calculation: The script calculates the HMA in two main steps:
-
Weighted Average Calculation: It first calculates a weighted average by taking two simple moving averages with different periods (HMA_Period/2 and HMA_Period) and taking the difference in these calculations to create a weighted average.
-
Smoothing the Weighted Average: It then applies a final smoothing step using a moving average with a period equal to the square root of HMA_Period. This is applied to the previously calculated weighted average to further reduce the lag.
-
-
Displaying the Result: The script takes the final calculated HMA values and draws a line on the chart. This line represents the Hull Moving Average, which can be used to identify trends and potential trading signals.
-
Important Considerations:
- The script starts calculating the HMA from a certain point.
- It handles cases where there isn't enough historical data to accurately calculate the HMA at the beginning.
In essence, this script automates the calculation and display of the HMA, providing traders with a visual tool to help them make informed trading decisions based on price trends.
//+------------------------------------------------------------------+
//| 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=4;
extern int method=3; // MODE_SMA
extern int price=0; // PRICE_CLOSE
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()
{
double ma1, ma2;
int limit,i, HMA_Period2, HMA_PeriodSQ2;
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;
HMA_Period2 = MathFloor(HMA_Period/2);
HMA_PeriodSQ2 = MathFloor(MathSqrt(HMA_Period));
//---- MA difference counted in the 1-st buffer
for(i=0; i<limit; i++)
{
ma1 = iMA(NULL,0,HMA_Period2,MAshift,method,price,i);
ma2 = iMA(NULL,0,HMA_Period,MAshift,method,price,i);
ind_buffer1[i]=2 * ma1 - ma2;
}
//---- HMA counted in the 0-th buffer
for(i=limit -1 ; i>=0; i--)
ind_buffer0[i]=iMAOnArray(ind_buffer1,0,HMA_PeriodSQ2,0,method,i);
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---