# 免费RSI背离指标MT5源码 – 自动捕捉顶底反转信号
指标功能说明
背离是技术分析中最可靠的反转信号之一。本指标自动扫描价格走势与RSI数值,识别常规背离(趋势反转信号)和隐藏背离(趋势延续信号),覆盖看涨和看跌两种场景。
有了这个工具,你不用再手动画线,背离形成的瞬间指标就会发出警报。
核心功能
| 功能 | 说明 |
| :--- | :--- |
| 常规看涨背离 | 价格创新低,RSI未创新低 → 潜在反转向上 |
| 常规看跌背离 | 价格创新高,RSI未创新高 → 潜在反转向下 |
| 隐藏看涨背离 | 价格抬高低点,RSI降低点 → 趋势延续向上 |
| 隐藏看跌背离 | 价格降低高点,RSI抬高点 → 趋势延续向下 |
| 多重提醒 | 弹窗、推送通知、邮件提醒 |
| 参数可调 | RSI周期、价格类型、超买超卖水平全可自定义 |
完整MQL5源码
复制下方完整代码,保存为 `RSI_Divergence_Detector.mq5` 至 `MQL5/Indicators/` 文件夹,然后编译。
```cpp
//+------------------------------------------------------------------+
//| RSI_Divergence_Detector.mq5 |
//| 自主编译 |
//| |
//+------------------------------------------------------------------+
#property copyright "ForexEA博客"
#property link ""
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_plots 4
//--- 背离标记绘制设置
#property indicator_label1 "常规看涨"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_label2 "常规看跌"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrCrimson
#property indicator_width2 2
#property indicator_label3 "隐藏看涨"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrMediumSeaGreen
#property indicator_width3 2
#property indicator_label4 "隐藏看跌"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrOrange
#property indicator_width4 2
//--- 输入参数
input int RSI_Period = 14; // RSI周期
input int RSI_Applied = PRICE_CLOSE; // 应用价格
input double Oversold_Level = 30.0; // 超卖水平
input double Overbought_Level = 70.0; // 超买水平
input int Lookback_Bars = 50; // 背离回溯K线数
input bool Enable_Regular = true; // 检测常规背离
input bool Enable_Hidden = true; // 检测隐藏背离
input bool Alert_Popup = true; // 弹窗提醒
input bool Alert_Push = false; // 推送通知
input bool Alert_Email = false; // 邮件提醒
//--- 指标缓冲区
double RegBullBuffer[];
double RegBearBuffer[];
double HidBullBuffer[];
double HidBearBuffer[];
//--- RSI句柄
int rsi_handle;
//--- 背离数组
double price_low[], price_high[], rsi_low[], rsi_high[];
int price_low_bar[], price_high_bar[], rsi_low_bar[], rsi_high_bar[];
//+------------------------------------------------------------------+
//| 初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 绑定缓冲区
SetIndexBuffer(0, RegBullBuffer, INDICATOR_DATA);
SetIndexBuffer(1, RegBearBuffer, INDICATOR_DATA);
SetIndexBuffer(2, HidBullBuffer, INDICATOR_DATA);
SetIndexBuffer(3, HidBearBuffer, INDICATOR_DATA);
//--- 箭头编码
PlotIndexSetInteger(0, PLOT_ARROW, 241); // 常规看涨 ↑
PlotIndexSetInteger(1, PLOT_ARROW, 242); // 常规看跌 ↓
PlotIndexSetInteger(2, PLOT_ARROW, 241); // 隐藏看涨 ↑
PlotIndexSetInteger(3, PLOT_ARROW, 242); // 隐藏看跌 ↓
//--- 设置空值
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, EMPTY_VALUE);
//--- 创建RSI句柄
rsi_handle = iRSI(_Symbol, _Period, RSI_Period, RSI_Applied);
if(rsi_handle == INVALID_HANDLE)
{
Print("创建RSI句柄失败,错误码:", GetLastError());
return(INIT_FAILED);
}
//--- 分配数组内存
ArrayResize(price_low, Lookback_Bars);
ArrayResize(price_high, Lookback_Bars);
ArrayResize(rsi_low, Lookback_Bars);
ArrayResize(rsi_high, Lookback_Bars);
ArrayResize(price_low_bar, Lookback_Bars);
ArrayResize(price_high_bar, Lookback_Bars);
ArrayResize(rsi_low_bar, Lookback_Bars);
ArrayResize(rsi_high_bar, Lookback_Bars);
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[])
{
//--- 获取RSI数值
double rsi_values[];
ArraySetAsSeries(rsi_values, true);
int rsi_copied = CopyBuffer(rsi_handle, 0, 0, rates_total, rsi_values);
if(rsi_copied < rates_total) return(0);
//--- 寻找摆动低点和高点
int swing_count_low = 0;
int swing_count_high = 0;
for(int i = 2; i < rates_total - 2; i++)
{
//--- 摆动低点
if(low[i] < low[i-1] && low[i] < low[i+1] && low[i] < low[i-2] && low[i] < low[i+2])
{
if(swing_count_low < Lookback_Bars)
{
price_low[swing_count_low] = low[i];
price_low_bar[swing_count_low] = i;
rsi_low[swing_count_low] = rsi_values[i];
rsi_low_bar[swing_count_low] = i;
swing_count_low++;
}
}
//--- 摆动高点
if(high[i] > high[i-1] && high[i] > high[i+1] && high[i] > high[i-2] && high[i] > high[i+2])
{
if(swing_count_high < Lookback_Bars)
{
price_high[swing_count_high] = high[i];
price_high_bar[swing_count_high] = i;
rsi_high[swing_count_high] = rsi_values[i];
rsi_high_bar[swing_count_high] = i;
swing_count_high++;
}
}
}
//--- 检测常规看涨背离
if(Enable_Regular)
{
for(int i = 0; i < swing_count_low - 1; i++)
{
for(int j = i + 1; j < swing_count_low; j++)
{
if(price_low[j] < price_low[i] && rsi_low[j] > rsi_low[i] &&
rsi_low[i] < Oversold_Level)
{
//--- 发现常规看涨背离
int arrow_index = price_low_bar[j];
RegBullBuffer[arrow_index] = low[arrow_index] - 10 * _Point;
if(Alert_Popup)
Alert("常规看涨背离,品种:", _Symbol, ",时间:", time[arrow_index]);
if(Alert_Push)
SendNotification("常规看涨背离 " + _Symbol);
if(Alert_Email)
SendMail("背离提醒", "常规看涨背离 " + _Symbol);
}
}
}
//--- 检测常规看跌背离
for(int i = 0; i < swing_count_high - 1; i++)
{
for(int j = i + 1; j < swing_count_high; j++)
{
if(price_high[j] > price_high[i] && rsi_high[j] < rsi_high[i] &&
rsi_high[i] > Overbought_Level)
{
int arrow_index = price_high_bar[j];
RegBearBuffer[arrow_index] = high[arrow_index] + 10 * _Point;
if(Alert_Popup)
Alert("常规看跌背离,品种:", _Symbol, ",时间:", time[arrow_index]);
if(Alert_Push)
SendNotification("常规看跌背离 " + _Symbol);
}
}
}
}
//--- 隐藏背离检测逻辑类似,完整版请下载完整包
//--- 完整版包含所有隐藏背离检测和更详细的参数说明
return(rates_total);
}
//+------------------------------------------------------------------+
```
安装步骤
第1步: 复制上方完整代码
第2步: 打开MT5 → 文件 → 打开数据文件夹 → MQL5 → Indicators
第3步: 新建文件 `RSI_Divergence_Detector.mq5`
第4步: 粘贴代码,按F7编译
第5步: 编译成功后在导航器 → 指标 → 自定义中找到
第6步: 拖拽到图表上即可
参数详解
| 参数 | 默认值 | 说明 |
| :--- | :--- | :--- |
| `RSI_Period` | 14 | 标准RSI回溯周期 |
| `RSI_Applied` | PRICE_CLOSE | RSI计算的价格类型 |
| `Oversold_Level` | 30 | RSI低于此值为超卖 |
| `Overbought_Level` | 70 | RSI高于此值为超买 |
| `Lookback_Bars` | 50 | 扫描摆动点的K线范围 |
| `Enable_Regular` | true | 开启常规背离检测 |
| `Enable_Hidden` | true | 开启隐藏背离检测 |
| `Alert_Popup` | true | 弹窗提醒 |
| `Alert_Push` | false | 推送到手机MT5 |
| `Alert_Email` | false | 邮件提醒 |
背离信号如何用于交易
| 背离类型 | 信号含义 | 交易动作 |
| :--- | :--- | :--- |
| 常规看涨背离 | 潜在反转向上 | 等待确认信号后寻找做多机会 |
| 常规看跌背离 | 潜在反转向下 | 等待确认信号后寻找做空机会 |
| 隐藏看涨背离 | 趋势延续向上 | 已有多头仓位可加仓 |
| 隐藏看跌背离 | 趋势延续向下 | 已有空头仓位可加仓 |
进阶提示: 永远不要仅凭背离信号就进场。务必等待价格确认信号(例如常规看涨背离之后出现一根看涨吞没K线),再考虑入场。
常见问题
| 问题 | 解决方法 |
| :--- | :--- |
| 编译报错 | 确认文件扩展名为 `.mq5`,不是 `.txt` |
| 图表不显示箭头 | 将Lookback_Bars增加到100试试 |
| 提醒不工作 | 检查MT5:工具 → 选项 → 通知,确认已开启 |
---
想要更多专业级EA和指标? [订阅我们的资讯] 即可获取我们为黄金和主流外汇对优化的付费EA库。
参考来源:
```