Summary: EURUSD趋势稳定EA是一款专为EURUSD趋势交易设计的MQL4智能交易系统。使用Supertrend指标作为主信号,配合ADX趋势强度过滤和ATR动态止损,适合H1周期。




EURUSD趋势稳定EA专为欧美(EURUSD)稳定趋势交易而设计。该EA使用Supertrend指标作为主要趋势方向信号,结合ADX(平均趋向指数)确保只在足够趋势强度时开仓。基于ATR的动态止损能够适应市场波动,而保本机制在价格有利移动后保护本金。EA同时只持有一单,并包含每日亏损保护。

推荐加载周期: H1
策略核心逻辑:
1. 趋势方向:Supertrend(ATR周期10,倍数3)判断趋势方向(绿色=上涨,红色=下跌)。
2. 趋势强度:ADX(14)必须大于25确认趋势市场存在。
3. 入场信号:价格收盘于Supertrend线之外,且ADX确认趋势强度,无现有持仓。
4. 风险管理:基于ATR的止损(2倍ATR),止盈3.5倍ATR,盈利达到1.5倍ATR后移动至保本。

```mql4
//+------------------------------------------------------------------+
//| EURUSDTrendStabilizerEA.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict

//--- 输入参数及注释
input double LotSize = 0.01; // 固定交易手数(每笔0.01手)
input int ATRPeriod = 10; // ATR周期(用于Supertrend和止损)
input double SupertrendMultiplier = 3.0; // Supertrend乘数因子
input int ADXPeriod = 14; // ADX周期(趋势强度过滤)
input int ADXThreshold = 25; // 最小ADX阈值(低于此值不交易)
input double ATRStopMultiplier = 2.0; // 止损倍数(ATR的倍数)
input double ATRTakeMultiplier = 3.5; // 止盈倍数(ATR的倍数)
input double BreakevenTrigger = 1.5; // 保本触发倍数(ATR倍数)
input int MagicNumber = 202416; // EA魔术号
input int MaxSpread = 25; // 最大允许点差(单位:点)
input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
input bool UseFridayClose = true; // 周五21:00 GMT前平仓

//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
bool breakevenApplied = false;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| EA退出函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}

//+------------------------------------------------------------------+
//| 计算Supertrend值 |
//+------------------------------------------------------------------+
double CalculateSupertrend(int shift, int &direction)
{
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, shift);
double hl2 = (iHigh(Symbol(), PERIOD_H1, shift) + iLow(Symbol(), PERIOD_H1, shift)) / 2;

double upperBand = hl2 + (SupertrendMultiplier * atr);
double lowerBand = hl2 - (SupertrendMultiplier * atr);

static double prevUpperBand = 0;
static double prevLowerBand = 0;
static int prevDirection = 0;

if(shift == 1)
{
prevUpperBand = 0;
prevLowerBand = 0;
prevDirection = 0;
}

int newDirection = 0;
double supertrend = 0;

if(shift == 1)
{
newDirection = 1;
supertrend = lowerBand;
}
else
{
if(prevDirection == 1)
{
if(iClose(Symbol(), PERIOD_H1, shift) > prevLowerBand)
{
newDirection = 1;
supertrend = (lowerBand > prevLowerBand) ? lowerBand : prevLowerBand;
}
else
{
newDirection = -1;
supertrend = (upperBand < prevUpperBand) ? upperBand : prevUpperBand;
}
}
else if(prevDirection == -1)
{
if(iClose(Symbol(), PERIOD_H1, shift) < prevUpperBand)
{
newDirection = -1;
supertrend = (upperBand < prevUpperBand) ? upperBand : prevUpperBand;
}
else
{
newDirection = 1;
supertrend = (lowerBand > prevLowerBand) ? lowerBand : prevLowerBand;
}
}
else
{
newDirection = 1;
supertrend = lowerBand;
}
}

prevUpperBand = upperBand;
prevLowerBand = lowerBand;
prevDirection = newDirection;
direction = newDirection;

return supertrend;
}

//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日净值保护
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
Comment("已达每日亏损上限,停止开新仓");
return;
}

// 周五收盘前平仓(规避周末跳空)
if(UseFridayClose && !fridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 21)
{
CloseAllOrders();
fridayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 5)
fridayCloseExecuted = false;
}

// 点差过滤
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("当前点差过大:", MarketInfo(Symbol(), MODE_SPREAD));
return;
}

// 仅在新K线开始时检测入场(H1)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];

// 检查现有持仓并管理保本
int posCount = CountPositions();
if(posCount > 0)
{
ManageBreakeven();
return;
}

breakevenApplied = false;

// 计算已收盘K线的指标
int supertrendDir = 0;
double supertrend = CalculateSupertrend(1, supertrendDir);
double adx = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);

if(atr <= 0) atr = 150 * Point;
if(adx < ADXThreshold)
{
Comment("ADX低于阈值:", adx);
return;
}

int cmd = -1;
double sl = 0, tp = 0;
double ask = Ask;
double bid = Bid;

// 买入信号:Supertrend看涨(direction=1)且价格高于Supertrend线
if(supertrendDir == 1 && close1 > supertrend)
{
cmd = OP_BUY;
sl = bid - (atr * ATRStopMultiplier);
tp = bid + (atr * ATRTakeMultiplier);
}
// 卖出信号:Supertrend看跌(direction=-1)且价格低于Supertrend线
else if(supertrendDir == -1 && close1 < supertrend)
{
cmd = OP_SELL;
sl = ask + (atr * ATRStopMultiplier);
tp = ask - (atr * ATRTakeMultiplier);
}

if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?ask:bid), 3, sl, tp, "EURUSD Trend EA", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
}
}

//+------------------------------------------------------------------+
//| 管理持仓的保本止损 |
//+------------------------------------------------------------------+
void ManageBreakeven()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(breakevenApplied) break;

double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(atr <= 0) atr = 150 * Point;

double breakevenLevel = atr * BreakevenTrigger;

if(OrderType() == OP_BUY)
{
double profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= breakevenLevel / Point && OrderStopLoss() < OrderOpenPrice())
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, clrNONE))
{
breakevenApplied = true;
Print("保本已应用 买入 #", OrderTicket());
}
}
}
else if(OrderType() == OP_SELL)
{
double profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= breakevenLevel / Point && (OrderStopLoss() > OrderOpenPrice() || OrderStopLoss() == 0))
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, clrNONE))
{
breakevenApplied = true;
Print("保本已应用 卖出 #", OrderTicket());
}
}
}
break;
}
}
}
}

//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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;
}

//+------------------------------------------------------------------+
//| 平仓当前品种下所有属于该EA的订单 |
//+------------------------------------------------------------------+
void CloseAllOrders()
{
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)
OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,仅供学习参考。
免责声明: 外汇交易具有重大亏损风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
```