设为首页 收藏本站 切换语言
| 发表于 2025-8-17 00:41:13 | 显示全部楼层 |复制链接
//+------------------------------------------------------------------+
//| 网格交易策略参数                                                |
//+------------------------------------------------------------------+
input double LotSize = 0.1;           // 每次交易的手数
input int MaxBuyOrders = 5;           // 最大买单数
input int MaxSellOrders = 5;          // 最大卖单数
input double GridDistance = 20;       // 网格的距离,单位为点
input int Slippage = 3;               // 最大滑点
input int StopLoss = 100;             // 止损,单位为点
input int TakeProfit = 100;           // 止盈,单位为点

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // 初始化信息
   Print("网格交易EA已成功启动");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // 卸载时清理工作
   Print("网格交易EA已卸载");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // 获取当前的买单和卖单数量
   int buyOrders = 0;
   int sellOrders = 0;

   // 计算当前持有的买单和卖单数量
   for (int i = 0; i < OrdersTotal(); i++)
     {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if (OrderType() == OP_BUY) buyOrders++;
         if (OrderType() == OP_SELL) sellOrders++;
        }
     }

   // 获取当前价格
   double askPrice = Ask;
   double bidPrice = Bid;

   // 检查买单数量是否少于最大买单限制
   if (buyOrders < MaxBuyOrders)
     {
      // 执行一个新的买单
      double openBuyPrice = askPrice + GridDistance * Point; // 设置网格间距
      int ticket = OrderSend(Symbol(), OP_BUY, LotSize, openBuyPrice, Slippage, openBuyPrice - StopLoss * Point, openBuyPrice + TakeProfit * Point, "Buy Grid", 0, 0, clrGreen);
      
      if (ticket > 0)
        {
         Print("已成功开设买单,订单票号:", ticket);
        }
      else
        {
         Print("买单开设失败,错误代码:", GetLastError());
        }
     }

   // 检查卖单数量是否少于最大卖单限制
   if (sellOrders < MaxSellOrders)
     {
      // 执行一个新的卖单
      double openSellPrice = bidPrice - GridDistance * Point; // 设置网格间距
      int ticket = OrderSend(Symbol(), OP_SELL, LotSize, openSellPrice, Slippage, openSellPrice + StopLoss * Point, openSellPrice - TakeProfit * Point, "Sell Grid", 0, 0, clrRed);
      
      if (ticket > 0)
        {
         Print("已成功开设卖单,订单票号:", ticket);
        }
      else
        {
         Print("卖单开设失败,错误代码:", GetLastError());
        }
     }

   // 监控所有订单,查看是否有订单已平仓,并立即补单
   CheckAndReopenOrders();
  }

//+------------------------------------------------------------------+
//| 检查并补开已平仓的订单                                           |
//+------------------------------------------------------------------+
void CheckAndReopenOrders()
  {
   // 遍历当前所有订单
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      // 选择订单
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         // 如果订单已经平仓,补充相同方向的订单
         if (OrderCloseTime() != 0)
           {
            // 只在这里声明 ticket 变量
            int ticket;

            // 如果是买单
            if (OrderType() == OP_BUY)
              {
               // 补开买单
               double openBuyPrice = Ask + GridDistance * Point;
               ticket = OrderSend(Symbol(), OP_BUY, LotSize, openBuyPrice, Slippage, openBuyPrice - StopLoss * Point, openBuyPrice + TakeProfit * Point, "Reopen Buy Grid", 0, 0, clrGreen);
               if (ticket > 0)
                 {
                  Print("已成功补开买单,订单票号:", ticket);
                 }
               else
                 {
                  Print("买单补开失败,错误代码:", GetLastError());
                 }
              }
            // 如果是卖单
            if (OrderType() == OP_SELL)
              {
               // 补开卖单
               double openSellPrice = Bid - GridDistance * Point;
               ticket = OrderSend(Symbol(), OP_SELL, LotSize, openSellPrice, Slippage, openSellPrice + StopLoss * Point, openSellPrice - TakeProfit * Point, "Reopen Sell Grid", 0, 0, clrRed);
               if (ticket > 0)
                 {
                  Print("已成功补开卖单,订单票号:", ticket);
                 }
               else
                 {
                  Print("卖单补开失败,错误代码:", GetLastError());
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
举报

评论 使用道具

精彩评论4

xhy800
D
| 发表于 2025-8-17 01:30:19 | 显示全部楼层
对冲网格  这是啥?
举报

点赞 评论 使用道具

VX274960576
C
| 发表于 2025-8-19 22:11:30 | 显示全部楼层
什么 来的
举报

点赞 评论 使用道具

VX274960576
C
| 发表于 2025-8-21 10:37:13 | 显示全部楼层
感谢分享了
举报

点赞 评论 使用道具

VX274960576
C
| 发表于 2025-8-22 20:59:53 | 显示全部楼层
有人试过吗
举报

点赞 评论 使用道具

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

简体中文
繁體中文
English(英语)
日本語(日语)
Deutsch(德语)
Русский язык(俄语)
بالعربية(阿拉伯语)
Türkçe(土耳其语)
Português(葡萄牙语)
ภาษาไทย(泰国语)
한어(朝鲜语/韩语)
Français(法语)