本文提供一个完整的MQL4指标源码,能够自动检测RSI背离信号。价格与RSI之间的背离是技术分析中最可靠的反转信号之一。当检测到看涨或看跌背离时,指标会在图表上绘制趋势线和箭头。
指标功能:
参数详解:
MQL4源码:
```mql4
//+------------------------------------------------------------------+
//| RSIDivergenceDetector.mq4 |
//| |
//| 自主编译 / Self-compiled |
//+------------------------------------------------------------------+
#property copyright "外汇交易工具"
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_color1 Red
#property indicator_color2 Green
//--- 输入参数
input int RSIPeriod = 14;
input double Overbought = 70.0;
input double Oversold = 30.0;
input int MinBarsBetween = 10;
input bool DrawLines = true;
input bool ShowAlert = true;
input string AlertSound = "alert.wav";
//--- 全局变量
double rsiVal[];
datetime lastAlertTime = 0;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorShortName("RSI背离检测器");
SetIndexBuffer(0,rsiVal);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标反初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,"Div_");
}
//+------------------------------------------------------------------+
//| 自定义指标核心计算函数 |
//+------------------------------------------------------------------+
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 - 2;
// 计算RSI数值
ArrayResize(rsiVal,rates_total);
for(int i=limit; i>=0 && !IsStopped(); i--)
{
rsiVal[i] = iRSI(_Symbol,0,RSIPeriod,PRICE_CLOSE,i);
}
// 检测背离
for(int i=2; i
// 看涨背离:价格创新低,RSI未创新低(更高低点)
if(low[i] < low[i+1] && rsiVal[i] > rsiVal[i+1] && rsiVal[i] < Oversold)
{
if(DrawLines)
DrawDivLine(i,i+1,"Bullish",Green);
if(ShowAlert && TimeCurrent() - lastAlertTime > 60)
{
Alert(_Symbol," 检测到看涨RSI背离");
lastAlertTime = TimeCurrent();
if(AlertSound != "") PlaySound(AlertSound);
}
}
// 看跌背离:价格创新高,RSI未创新高(更低高点)
if(high[i] > high[i+1] && rsiVal[i] < rsiVal[i+1] && rsiVal[i] > Overbought)
{
if(DrawLines)
DrawDivLine(i,i+1,"Bearish",Red);
if(ShowAlert && TimeCurrent() - lastAlertTime > 60)
{
Alert(_Symbol," 检测到看跌RSI背离");
lastAlertTime = TimeCurrent();
if(AlertSound != "") PlaySound(AlertSound);
}
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| 绘制背离趋势线 |
//+------------------------------------------------------------------+
void DrawDivLine(int idx1, int idx2, string type, color lineColor)
{
string objName = "Div_" + type + "_" + IntegerToString(TimeCurrent()) + "_" + IntegerToString(idx1);
ObjectCreate(0,objName,OBJ_TREND,0,Time[idx1],0,Time[idx2],0);
ObjectSetInteger(0,objName,OBJPROP_COLOR,lineColor);
ObjectSetInteger(0,objName,OBJPROP_STYLE,STYLE_DOT);
ObjectSetInteger(0,objName,OBJPROP_WIDTH,1);
ObjectSetInteger(0,objName,OBJPROP_BACK,false);
ObjectSetInteger(0,objName,OBJPROP_SELECTABLE,false);
ObjectSetInteger(0,objName,OBJPROP_HIDDEN,true);
}
//+------------------------------------------------------------------+
```
编译与安装步骤:
1. 在MT4中打开MetaEditor(按F4键)
2. 文件 → 新建 → 自定义指标 → 下一步
3. 粘贴上述源码,按F7编译
4. 通过导航窗口(Ctrl+N)将指标拖到图表上
5. 在输入参数选项卡中调整参数
如何解读背离信号:
参数调优技巧:
常见问题排查:
参考来源: 自主编译,基于经典背离检测方法论。
需要更高级的指标套装(包含多时间框架背离检测、自动交易系统)?欢迎查看我们的付费精选包,提供终身更新和技术支持。