【多空对冲】多空对冲 EA(安全稳定版

| 发表于 昨天 21:35 | 显示全部楼层 |复制链接
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; image.png image.png
      }
   }
   return false;
}
//+------------------------------------------------------------------+
举报

评论 使用道具

精彩评论2

rocfeng
D
| 发表于 昨天 22:09 | 显示全部楼层
极简系统
举报

点赞 评论 使用道具

alohahaha13
DD
| 发表于 昨天 22:19 来自手机 | 显示全部楼层
好用嗎?
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册