找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 11|回复: 0

多空对冲 EA(安全稳定版)

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-24 15:19:00 | 显示全部楼层 |阅读模式
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;

}

//+------------------------------------------------------------------+
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-12 18:36 , Processed in 0.115493 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表