本教程提供可直接使用的MQL5指标源码,用于检测RSI振荡器的常规背离和隐藏背离。背离交易是价格行为与动量分析的核心方法,使该工具对人工或半自动策略具有极高价值。
为什么选择RSI背离指标?
多数免费指标仅标记基本峰值。我们的版本在RSI子窗口绘制清晰趋势线,区分背离类型并发送警报。非常适合寻找带高级功能的免费MT5指标下载的交易者。
完整MQL5源码
```cpp
//+------------------------------------------------------------------+
//| RSI_Divergence_V1.mq5 |
//| Generated by AutoCompile AI |
//| |
//+------------------------------------------------------------------+
#property copyright "AutoCompile AI"
#property link ""
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 2
//--- 绘制RSI
#property indicator_label1 "RSI"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 绘制背离线
#property indicator_label2 "Divergence"
#property indicator_type2 DRAW_SECTION
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--- 输入参数
input int RSI_Period=14; // RSI周期
input ENUM_APPLIED_PRICE RSI_Applied=PRICE_CLOSE; // RSI应用价格
input int Lookback=50; // 扫描背离的K线数量
input bool ShowRegularBull=true; // 显示常规看涨背离
input bool ShowRegularBear=true; // 显示常规看跌背离
input bool ShowHiddenBull=true; // 显示隐藏看涨背离
input bool ShowHiddenBear=true; // 显示隐藏看跌背离
input bool AlertOn=true; // 启用警报
//--- 指标缓冲区
double RSI_Buffer[];
double Div_Buffer_High[];
double Div_Buffer_Low[];
double Div_Color[];
//--- 全局变量
int rsi_handle;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 设置缓冲区
SetIndexBuffer(0, RSI_Buffer, INDICATOR_DATA);
SetIndexBuffer(1, Div_Buffer_High, INDICATOR_DATA);
SetIndexBuffer(2, Div_Buffer_Low, INDICATOR_DATA);
SetIndexBuffer(3, Div_Color, INDICATOR_COLOR_INDEX);
//--- 设置背离线属性
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_SECTION);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrRed);
PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID);
PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 2);
//--- 获取RSI句柄
rsi_handle = iRSI(_Symbol, _Period, RSI_Period, RSI_Applied);
if(rsi_handle == INVALID_HANDLE)
{
Print("无法创建RSI句柄");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标反初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(rsi_handle != INVALID_HANDLE)
IndicatorRelease(rsi_handle);
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int limit = rates_total - prev_calculated;
if(limit > 1) limit = rates_total - 1;
//--- 获取RSI值
double rsi_val[];
ArraySetAsSeries(rsi_val, true);
if(CopyBuffer(rsi_handle, 0, 0, rates_total, rsi_val) < rates_total - limit)
return(0);
//--- 填充RSI缓冲区
for(int i=limit; i>=0 && !IsStopped(); i--)
RSI_Buffer[i] = rsi_val[i];
//--- 清空背离缓冲区
ArrayInitialize(Div_Buffer_High, EMPTY_VALUE);
ArrayInitialize(Div_Buffer_Low, EMPTY_VALUE);
//--- 检测背离
for(int i=Lookback; i
// 常规看涨背离:价格更低低点,RSI更高低点
if(ShowRegularBull && low[i] < low[i+2] && rsi_val[i] > rsi_val[i+2])
{
Div_Buffer_Low[i] = rsi_val[i];
Div_Buffer_Low[i+2] = rsi_val[i+2];
if(AlertOn) Alert("常规看涨背离 在 ", time[i]);
}
// 常规看跌背离:价格更高高点,RSI更低高点
if(ShowRegularBear && high[i] > high[i+2] && rsi_val[i] < rsi_val[i+2])
{
Div_Buffer_High[i] = rsi_val[i];
Div_Buffer_High[i+2] = rsi_val[i+2];
if(AlertOn) Alert("常规看跌背离 在 ", time[i]);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
```
如何编译与使用
1. 将代码保存为 `RSI_Divergence_V1.mq5`,放入MT5数据目录的 `Indicators` 文件夹。
2. 打开MetaEditor,按F7编译,确保无错误。
3. 附加到任意图表。通过输入参数调整RSI周期和背离类型。
参数说明
此RSI背离指标帮助交易者快速识别潜在反转点,无需手动扫描。如需更高级的自动化策略,我们的高级版EA套件将此逻辑与交易执行集成。订阅以获取新工具更新。
参考来源:AutoCompile AI - 原创MQL5实现,2025年。
```