Summary: 专业RSI背离检测器MT4指标源码,可识别经典背离和隐藏背离。提供完整代码、参数详解和安装步骤,伪原创改写方便SEO收录。




本文提供一个完整的MQL4指标源码,能够自动检测RSI背离信号。价格与RSI之间的背离是技术分析中最可靠的反转信号之一。当检测到看涨或看跌背离时,指标会在图表上绘制趋势线和箭头。

指标功能:
  • 检测经典背离和隐藏背离

  • 可调节RSI周期和超买/超卖水平

  • 在主图上绘制可视化的趋势线和箭头

  • 弹出警报和声音提示


  • 参数详解:
  • `RSIPeriod` – RSI计算周期(默认14)

  • `Overbought` – 超买水平(默认70)

  • `Oversold` – 超卖水平(默认30)

  • `MinBarsBetween` – 最小K线间隔,避免重复信号

  • `DrawLines` – 显示/隐藏趋势线

  • `ShowAlert` – 启用/禁用弹窗警报

  • `AlertSound` – 警报声音文件名


  • 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. 在输入参数选项卡中调整参数

    如何解读背离信号:
  • 看涨背离: 价格创更低低点,RSI创更高低点 → 潜在上涨反转

  • 看跌背离: 价格创更高高点,RSI创更低高点 → 潜在下跌反转

  • 当RSI处于超卖区(看涨)或超买区(看跌)时,信号最可靠


  • 参数调优技巧:
  • 较短RSI周期(7-9):信号更多,但假信号也更多

  • 较长RSI周期(21-25):信号更少,但可靠性更高

  • 可将超买/超卖调整为80/20以获得更强过滤


  • 常见问题排查:
  • 若指标不显示线条,检查`DrawLines`是否为true

  • 若无警报弹出,确认MT4设置中允许警报(工具→选项→警报)

  • 声音文件需放在MT4的Sounds文件夹内


  • 参考来源: 自主编译,基于经典背离检测方法论。

    需要更高级的指标套装(包含多时间框架背离检测、自动交易系统)?欢迎查看我们的付费精选包,提供终身更新和技术支持。