MetaTrader5
Mastering the Hull Moving Average Indicator for MetaTrader 5
Hey fellow traders! Today, I'm diving into a nifty tool that can enhance your trading game—the Hull Moving Average (HMA) indicator for MetaTrader 5. I took a shot at implementing my own version of this indicator since I found some of the existing ones a bit confusing. Let’s break down how it works and how you can customize it to fit your trading style.
The HMA has four key input parameters:
InpHmaPeriod = 20
InpColorKind = single_color
InpColorIndex = color_index_3
InpMaxHistoryBars = 240
These parameters are pretty straightforward. The ENUM_COLOR_KIND allows you to choose between a single color or multi-color mode. By default, it’s set to single color. If you opt for multi-color, the HMA will use one color for upward trends and another for downward trends. In single color mode, you can set the HMA's color using ENUM_COLOR_INDEX, with grey as the default color. When the HMA is on an upslope, it turns green, while it’s red on a downslope. Check out the images below to see it in action!
Here’s the code for my custom HMA implementation:
//+------------------------------------------------------------------+
//| MelzHull.mq5 |
//| Copyright 2022, wm1@gmx.de |
//| https://melz.one |
//+------------------------------------------------------------------+
enum ENUM_COLOR_KIND { // single or alternating color
single_color,
multi_color
};
enum ENUM_COLOR_INDEX { // index of indicator_color1 colors
color_index_0,
color_index_1,
color_index_2,
color_index_3,
color_index_4,
color_index_5,
color_index_6
};
#property copyright "Copyright 2022 by W. Melz, wm1@gmx.de"
#property link "https://melz.one"
#property version "1.00"
#property description "Implementation of my Hull Moving Average"
#property indicator_chart_window // draw in chart window
#property indicator_buffers 4 // buffers for: fullWMA, halfWMA, vHull, cHull
#property indicator_plots 1 // plot only one line
#property indicator_type1 DRAW_COLOR_LINE // draw as color line
#property indicator_color1 clrGray, clrGreen, clrRed, clrBlue, clrGreenYellow, clrDodgerBlue, clrFireBrick
#property indicator_width1 1 // line width
#property indicator_label1 "HMA"
//--- input parameters
input int InpHmaPeriod = 20; // indicator period, default 20
input ENUM_COLOR_KIND InpColorKind = single_color; // kind of indicator color, single- or multi-color
input ENUM_COLOR_INDEX InpColorIndex = color_index_3; // set color of single-color indicator
input int InpMaxHistoryBars = 240; // calculate history bars, default 240, not more
//--- indicator buffers
double valueBuffer[]; // store Hull indicator values
double colorBuffer[]; // store Hull indicator color at bar
double fullWMABuffer[]; // store calculation of WMA full period
double halfWMABuffer[]; // store calculation the WMA half period
//--- Indicator global variables
int hmaPeriod, fullPeriod, halfPeriod, sqrtPeriod, maxHistoryBars; // store input value or default value
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
ENUM_INIT_RETCODE result = checkInput(); // check for correct input parameters
SetIndexBuffer(0,valueBuffer,INDICATOR_DATA); // store indicator buffer mapping
SetIndexBuffer(1,colorBuffer,INDICATOR_COLOR_INDEX); // store indicator candle color
SetIndexBuffer(2,fullWMABuffer,INDICATOR_CALCULATIONS); // store result of fullWMA calculation
SetIndexBuffer(3,halfWMABuffer,INDICATOR_CALCULATIONS); // store result of halfWMA calculation
IndicatorSetInteger(INDICATOR_DIGITS,_Digits); // set indicator digits
string shortName = StringFormat("HMA(%d)",hmaPeriod); // name for DataWindow and indicator subwindow label
IndicatorSetString(INDICATOR_SHORTNAME,shortName);
PlotIndexSetString(0,PLOT_LABEL,shortName);
// calculate global values for indicator
fullPeriod = hmaPeriod; // period from input
halfPeriod = fullPeriod / 2; // calculate half period
sqrtPeriod = (int)round(sqrt((double)fullPeriod)); // calculate square root of period
return(result); // success or failure, init finished
}
// Add the rest of your code here...
Feel free to tweak the parameters to suit your trading style. The HMA is an awesome tool to identify trends and potential reversals, helping you make more informed trading decisions.
Enjoy using this indicator, and may your trades be ever in your favor!
2023.09.21