mql4 Copy code
//+------------------------------------------------------------------+
//| EnhancedEA |
//| Copyright 2023 |
//| |
//+------------------------------------------------------------------+
#property strict
input double Lots = 0.1; // 初始仓位大小
input double InitialStopLoss = 300; // 初始止损
input double TrailingStop = 100; // 跟踪止损
input int Slippage = 3; // 允许滑点
input int MagicNumber = 123456; // 魔法号码
input double MaxSpread = 30; // 最大点差
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 检查是否允许交易或市场不可用
if (!IsTradeAllowed() || IsStopped() || OrdersTotal() > 0) return;
double ma5 = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);
double ma20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0);
double ma144 = iMA(NULL, 0, 144, 0, MODE_SMA, PRICE_CLOSE, 0);
double currentSpread = (Ask - Bid) / Point;
// 检查点差
if (currentSpread > MaxSpread)
{
Alert("点差超过限制,停止开仓。");
return;
}
bool upTrend = (ma5 > ma20 && ma20 > ma144);
bool downTrend = (ma5 < ma20 && ma20 < ma144);
bool goldenCross = false, deathCross = false;
// 检查10日内是否有金叉或死叉
for (int i = 1; i <= 10; i++)
{
double prev_ma5 = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, i);
double prev_ma20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, i);
if (prev_ma5 <= prev_ma20 && ma5 > ma20)
{
goldenCross = true;
break;
}
if (prev_ma5 >= prev_ma20 && ma5 < ma20)
{
deathCross = true;
break;
}
}
if (upTrend && goldenCross)
OpenBuyOrder();
else if (downTrend && deathCross)
OpenSellOrder();
ManagePositions(); // 处理已有头寸的止损调整
}
//+------------------------------------------------------------------+
//| Open buy order |
//+------------------------------------------------------------------+
void OpenBuyOrder()
{
for (int i = 0; i < 10; i++)
{
if (OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "Buy Order", MagicNumber, 0, clrGreen) >= 0)
return;
else
Alert("买入订单失败: ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| Open sell order |
//+------------------------------------------------------------------+
void OpenSellOrder()
{
for (int i = 0; i < 10; i++)
{
if (OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "Sell Order", MagicNumber, 0, clrRed) >= 0)
return;
else
Alert("卖出订单失败: ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| Manage existing positions |
//+------------------------------------------------------------------+
void ManagePositions()
{
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderMagicNumber() != MagicNumber) continue;
double newStopLoss;
bool modify = false;
if (OrderType() == OP_BUY)
{
newStopLoss = Bid - TrailingStop * Point;
if (OrderStopLoss() < newStopLoss)
modify = true;
}
else if (OrderType() == OP_SELL)
{
newStopLoss = Ask + TrailingStop * Point;
if (OrderStopLoss() > newStopLoss)
modify = true;
}
if (modify)
{
if (!OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrYellow))
Alert("无法修改订单: ", GetLastError());
}
}
}
}
//+------------------------------------------------------------------+