本文提供一份完整的、可编译的MQL4专家顾问(EA)源码,核心功能是检测常规RSI背离和隐藏RSI背离。背离交易是一种强大的技术分析手段,用于识别潜在的趋势反转和延续信号。本EA将检测过程自动化,并根据您设定的参数生成入场信号,同时配置止损、止盈和移动止损功能。
该EA的核心逻辑是扫描价格极值点(摆动高点和低点),并与RSI指标的极值点进行对比。当检测到经典的看涨或看跌背离时,EA会执行相应的买入或卖出订单。该EA适用于任何时间框架和货币对,但建议先在H1或H4等较高时间框架上进行测试,以提高信号可靠性。
主要功能特性:
以下是完整的EA源码。请将其复制到MetaEditor中,编译后附加到图表即可使用。注意,本EA不使用DLL,无需额外授权。
```mql4
//+------------------------------------------------------------------+
//| RSI_Divergence_EA.mq4 |
//| Copyright 2025, ForexSourceLab |
//| https://www.example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, ForexSourceLab"
#property link "https://www.example.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| 输入参数 |
//+------------------------------------------------------------------+
input int RSI_Period = 14; // RSI周期
input int Overbought = 70; // 超买水平
input int Oversold = 30; // 超卖水平
input double LotSize = 0.1; // 固定交易手数
input int StopLoss = 50; // 止损点数
input int TakeProfit = 100; // 止盈点数
input int TrailingStart = 30; // 移动止损激活点数
input int TrailingStep = 10; // 移动止损步长
input int MagicNumber = 202506; // EA魔术编号
input bool UseHiddenDivergence = true; // 启用隐藏背离检测
//+------------------------------------------------------------------+
//| 全局变量 |
//+------------------------------------------------------------------+
double lastHighPrice, lastLowPrice;
double lastHighRSI, lastLowRSI;
datetime lastAlertTime = 0;
//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
if(RSI_Period <= 0) return(INIT_PARAMETERS_INCORRECT);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| EA反初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| EA核心Tick函数 |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 检查新K线,避免同一根K线重复开仓
static datetime lastBarTime = 0;
if(Time[0] == lastBarTime) return;
lastBarTime = Time[0];
//--- 计算近期RSI值
double rsiVal = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0);
double rsiVal1 = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 1);
double rsiVal2 = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 2);
double rsiVal3 = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 3);
//--- 近期价格极值
double high0 = High[0], high1 = High[1], high2 = High[2];
double low0 = Low[0], low1 = Low[1], low2 = Low[2];
//--- 检测常规看涨背离:价格创新低,RSI未创新低(RSI形成更高低点)
if(low1 < low2 && low1 < low0 && rsiVal1 > rsiVal2 && rsiVal1 < Oversold)
{
if(CountOrders(MagicNumber, OP_BUY) == 0)
{
OpenOrder(OP_BUY);
lastAlertTime = Time[0];
}
}
//--- 检测常规看跌背离:价格创新高,RSI未创新高(RSI形成更低高点)
if(high1 > high2 && high1 > high0 && rsiVal1 < rsiVal2 && rsiVal1 > Overbought)
{
if(CountOrders(MagicNumber, OP_SELL) == 0)
{
OpenOrder(OP_SELL);
lastAlertTime = Time[0];
}
}
//--- 隐藏背离检测(可选)
if(UseHiddenDivergence)
{
// 隐藏看涨背离:价格形成更高低点,RSI形成更低低点
if(low1 > low2 && low1 < high1 && rsiVal1 < rsiVal2 && rsiVal1 < Oversold)
{
if(CountOrders(MagicNumber, OP_BUY) == 0)
{
OpenOrder(OP_BUY);
lastAlertTime = Time[0];
}
}
// 隐藏看跌背离:价格形成更低高点,RSI形成更高高点
if(high1 < high2 && high1 > low1 && rsiVal1 > rsiVal2 && rsiVal1 > Overbought)
{
if(CountOrders(MagicNumber, OP_SELL) == 0)
{
OpenOrder(OP_SELL);
lastAlertTime = Time[0];
}
}
}
//--- 移动止损管理
TrailingStop();
}
//+------------------------------------------------------------------+
//| 开仓函数 |
//+------------------------------------------------------------------+
void OpenOrder(int cmd)
{
double price = (cmd == OP_BUY) ? Ask : Bid;
double sl = 0, tp = 0;
if(StopLoss > 0)
{
sl = (cmd == OP_BUY) ? price - StopLoss * Point * 10 : price + StopLoss * Point * 10;
}
if(TakeProfit > 0)
{
tp = (cmd == OP_BUY) ? price + TakeProfit * Point * 10 : price - TakeProfit * Point * 10;
}
int ticket = OrderSend(Symbol(), cmd, LotSize, price, 3, sl, tp, "RSI Div EA", MagicNumber, 0, clrNONE);
if(ticket < 0)
{
Print("OrderSend failed with error #", GetLastError());
}
}
//+------------------------------------------------------------------+
//| 订单计数函数 |
//+------------------------------------------------------------------+
int CountOrders(int magic, int type)
{
int count = 0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic && OrderType() == type)
{
count++;
}
}
}
return count;
}
//+------------------------------------------------------------------+
//| 移动止损函数 |
//+------------------------------------------------------------------+
void TrailingStop()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY)
{
if(Bid - OrderOpenPrice() > TrailingStart * Point * 10)
{
double newSL = Bid - TrailingStep * Point * 10;
if(newSL > OrderStopLoss())
{
if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE))
Print("移动止损修改错误: ", GetLastError());
}
}
}
else if(OrderType() == OP_SELL)
{
if(OrderOpenPrice() - Ask > TrailingStart * Point * 10)
{
double newSL = Ask + TrailingStep * Point * 10;
if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
{
if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE))
Print("移动止损修改错误: ", GetLastError());
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
```
使用说明:
1. 在MT4中打开MetaEditor(快捷键F4)。
2. 新建一个专家顾问,将上述代码粘贴进去。
3. 编译(F7),确保无错误。
4. 将EA附加到图表,在“输入参数”选项卡中调整各参数。
5. 启用“允许实时交易”,并确保MT4自动交易按钮为开启状态。
参数详细说明:
本EA是学习MQL4编程的理想入门范例。您可以根据需要添加额外过滤条件、时间管理模块或多时间框架确认功能。在实际实盘使用前,请务必进行充分的历史回测和参数优化,以适配您的交易风格。
扩展建议: 您可以在此基础上增加ATR动态止损、分批平仓或移动止盈等高级功能,进一步提升EA的适应性和稳健性。
如果您希望获得包含完整风控模块、多币种配对、新闻过滤以及长期技术支持的进阶版EA,欢迎订阅我们的付费EA套餐,我们将为您提供持续更新和一对一指导服务。
参考来源:自主编译,基于标准MQL4背离检测逻辑实现。