input double Lot = 0.01;
input int TakeProfit = 50; // 盈利平仓点数
input int StopLoss = 100; // 止损
input int TrailStop = 30; // 移动止损
input int Magic = 12345;
//+------------------------------------------------------------------+
void OnTick()
{
bool hasBuy = HasOrder(OP_BUY);
bool hasSell = HasOrder(OP_SELL);
// 没有多空单 → 开一对对冲单
if(!hasBuy && !hasSell)
{
OpenBuy();
OpenSell();
}
// 盈利单平仓 + 亏损单移动止损
HedgeManage();
}
//+------------------------------------------------------------------+
void OpenBuy()
{
double sl = Ask - StopLoss * Point;
double tp = Ask + TakeProfit * Point;
OrderSend(_Symbol,OP_BUY,Lot,Ask,3,sl,tp,"HEDGE BUY",Magic,0,clrGreen);
}
void OpenSell()
{
double sl = Bid + StopLoss * Point;
double tp = Bid - TakeProfit * Point;
OrderSend(_Symbol,OP_SELL,Lot,Bid,3,sl,tp,"HEDGE SELL",Magic,0,clrRed);
}
//+------------------------------------------------------------------+
void HedgeManage()
{
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()==Magic)
{
double profit = OrderProfit() + OrderSwap() + OrderCommission();
// 盈利单立即平仓
if(profit > 0)
{
OrderClose(OrderTicket(),OrderLots(),(OrderType()==OP_BUY)?Ask:Bid,3);
continue;
}
// 亏损单移动止损
if(profit <= 0)
{
if(OrderType()==OP_BUY)
{
double newSL = Bid - TrailStop * Point;
if(newSL > OrderStopLoss())
OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0);
}
if(OrderType()==OP_SELL)
{
double newSL = Ask + TrailStop * Point;
if(newSL < OrderStopLoss())
OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0);
}
}
}
}
}
多空对冲 EA(安全稳定版)
//+------------------------------------------------------------------+
bool HasOrder(int type)
{
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==_Symbol && OrderType()==type && OrderMagicNumber()==Magic)
return true;
}
}
return false;
}
//+------------------------------------------------------------------+ |