MetaTrader5
使用Hull移动平均线指标提升交易策略
大家好!今天我们来聊聊Hull移动平均线(Hull Moving Average, HMA)这个强大的指标,特别是在MetaTrader 5平台上的应用。我自己对其他HMA实现的来源并不是很了解,因此决定自己动手实现一个,下面来看看具体的参数设置和代码实现。
输入参数
这个指标有四个输入参数,分别是:
InpHmaPeriod = 20
InpColorKind = single_color(单色)
InpColorIndex = color_index_3
InpMaxHistoryBars = 240
这些参数的意义很明显。枚举类型ENUM_COLOR_KIND用于切换单色和多色,默认是单色模式。在多色模式下,Hull MA在上涨时显示一种颜色,下跌时显示另一种颜色。在单色模式下,ENUM_COLOR_INDEX则设置Hull MA的单一颜色。在多色模式下,默认颜色为灰色,向上时为绿色,向下时为红色。以下是相应的两张图片展示:
代码实现
以下是Hull移动平均线的代码实现:
//+------------------------------------------------------------------+
//| MelzHull.mq5 |
//| Copyright 2022, wm1@gmx.de |
//| https://melz.one |
//+------------------------------------------------------------------+
enum ENUM_COLOR_KIND { // 单色或多色
single_color,
multi_color
};
enum ENUM_COLOR_INDEX { // 指标颜色索引
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"
#property link "https://melz.one"
#property version "1.00"
#property indicator_chart_window // 在图表窗口中绘制
#property indicator_buffers 4 // 缓冲区数量
#property indicator_plots 1 // 只绘制一条线
#property indicator_type1 DRAW_COLOR_LINE // 以颜色线形式绘制
#property indicator_color1 clrGray, clrGreen, clrRed, clrBlue, clrGreenYellow, clrDodgerBlue, clrFireBrick
#property indicator_width1 1 // 线宽
#property indicator_label1 "HMA" // 指标名称
// 输入参数
input int InpHmaPeriod = 20; // 默认周期20
input ENUM_COLOR_KIND InpColorKind = single_color; // 指标颜色类型
input ENUM_COLOR_INDEX InpColorIndex = color_index_3; // 单色指标颜色
input int InpMaxHistoryBars = 240; // 最大历史条数240
// 指标缓冲区
double valueBuffer[]; // 存储Hull指标值
double colorBuffer[]; // 存储指标颜色
double fullWMABuffer[]; // 存储WMA全周期计算结果
double halfWMABuffer[]; // 存储WMA半周期计算结果
// 指标全局变量
int hmaPeriod, fullPeriod, halfPeriod, sqrtPeriod, maxHistoryBars;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit() {
ENUM_INIT_RETCODE result = checkInput(); // 检查输入参数
SetIndexBuffer(0, valueBuffer, INDICATOR_DATA); // 存储指标缓冲区映射
SetIndexBuffer(1, colorBuffer, INDICATOR_COLOR_INDEX); // 存储指标颜色
SetIndexBuffer(2, fullWMABuffer, INDICATOR_CALCULATIONS); // 存储全WMA计算结果
SetIndexBuffer(3, halfWMABuffer, INDICATOR_CALCULATIONS); // 存储半WMA计算结果
IndicatorSetInteger(INDICATOR_DIGITS, _Digits); // 设置指标小数位数
string shortName = StringFormat("HMA(%d)", hmaPeriod); // 指标名称
IndicatorSetString(INDICATOR_SHORTNAME, shortName);
PlotIndexSetString(0, PLOT_LABEL, shortName);
// 计算指标全局值
fullPeriod = hmaPeriod; // 输入周期
halfPeriod = fullPeriod / 2; // 计算半周期
sqrtPeriod = (int)round(sqrt((double)fullPeriod)); // 计算周期的平方根
return (result); // 成功或失败,初始化结束
}
//+------------------------------------------------------------------+
使用这个指标可以帮助你更好地捕捉市场趋势,提升交易决策的准确性。希望大家能享受使用这个工具的过程!
2023.09.21