Summary: 适用于黄金和外汇的低风险MQL4 EA。使用趋势过滤器、ATR止损和每日交易次数限制。适合在XAUUSD H1周期稳定运行。
本EA专为黄金(XAUUSD)的低风险、稳定运行设计,也可用于主要外汇品种。不使用马丁格尔、网格或高频策略。逻辑采用简单移动平均线趋势过滤,ATR波动率止损止盈,以及时间段过滤避免高滑点时段。每根K线最多开一单以降低过度交易。在MQL4 build 600+环境下编译无警告。
建议加载在H1周期以获得最佳效果。支持5位或4位报价的经纪商。
```mql4
//+------------------------------------------------------------------+
//| StableGoldEA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| 输入参数 |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // 固定手数 (建议0.01起)
input int MAPeriod = 20; // 趋势过滤均线周期
input int ATRPeriod = 14; // ATR计算周期
input double ATRMultiplierSL = 1.5; // 止损 = ATR * 倍数
input double ATRMultiplierTP = 2.5; // 止盈 = ATR * 倍数
input int MaxDailyTrades = 1; // 每日最大交易次数
input int StartHour = 8; // 允许交易开始小时 (服务器时间)
input int EndHour = 20; // 允许交易结束小时 (服务器时间)
input int MagicNumber = 202401; // EA魔术编号
// 全局变量
double atrValue;
datetime lastTradeDay;
int tradesToday;
//+------------------------------------------------------------------+
//| 专家初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
if(LotSize <= 0 || MAPeriod < 1 || ATRPeriod < 1)
return(INIT_PARAMETERS_INCORRECT);
tradesToday = 0;
lastTradeDay = 0;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 专家反初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| 专家主循环函数 |
//+------------------------------------------------------------------+
void OnTick()
{
// 检查新K线开仓 (每根K线最多一单)
static datetime lastBarTime = 0;
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];
// 每日交易次数重置
if(lastTradeDay != Day())
{
lastTradeDay = Day();
tradesToday = 0;
}
if(tradesToday >= MaxDailyTrades)
return;
// 时间过滤
if(!IsTradeTime())
return;
// 计算ATR
atrValue = iATR(Symbol(), PERIOD_CURRENT, ATRPeriod, 1);
if(atrValue <= 0 || atrValue > 1000 * Point)
return;
// 趋势过滤: 价格高于MA做多,低于做空 (简单移动平均)
double ma = iMA(Symbol(), PERIOD_CURRENT, MAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
if(ma == 0)
return;
double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
// 无持仓时开仓
if(CountPositions() == 0)
{
// 做多信号
if(Close[1] > ma && Open[0] > ma)
{
double sl = ask - ATRMultiplierSL * atrValue;
double tp = ask + ATRMultiplierTP * atrValue;
sl = NormalizeDouble(sl, Digits);
tp = NormalizeDouble(tp, Digits);
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, ask, 3, sl, tp, "StableGoldEA", MagicNumber, 0, clrGreen);
if(ticket > 0)
tradesToday++;
}
// 做空信号
else if(Close[1] < ma && Open[0] < ma)
{
double sl = bid + ATRMultiplierSL * atrValue;
double tp = bid - ATRMultiplierTP * atrValue;
sl = NormalizeDouble(sl, Digits);
tp = NormalizeDouble(tp, Digits);
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, bid, 3, sl, tp, "StableGoldEA", MagicNumber, 0, clrRed);
if(ticket > 0)
tradesToday++;
}
}
}
//+------------------------------------------------------------------+
//| 统计当前品种的持仓数量 |
//+------------------------------------------------------------------+
int CountPositions()
{
int count = 0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
count++;
}
}
return count;
}
//+------------------------------------------------------------------+
//| 检查是否在允许的交易时间段 |
//+------------------------------------------------------------------+
bool IsTradeTime()
{
datetime now = TimeCurrent();
int hour = TimeHour(now);
if(hour >= StartHour && hour < EndHour)
return true;
return false;
}
//+------------------------------------------------------------------+
```
参考来源: 基于常见的低风险外汇趋势跟踪及波动率管理策略。
免责声明: 本EA仅用于学习和教育目的。过往表现不代表未来收益。实盘交易前请在模拟账户上充分测试。
```