大大 发表于 2025-7-1 11:36:09

求MT5当前品种一键全平脚本

求购:当前品种订单一键全平,MT5脚本
账户一键清仓、EA不要

193578 发表于 2025-7-1 13:03:47

https://www.eahub.cn/thread-151673-1-1.html#pid1245999

TRADERS 发表于 2025-7-8 21:47:16

我发布了一个,免费的面板

星辰大海 发表于 2025-7-10 22:37:33

我有一个,而且是一键全平,不是一单一单平的那种,我自己写的

49044000 发表于 2025-7-14 13:59:27

//+------------------------------------------------------------------+
//|                                                CloseAll.mq5   |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                             https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property script_show_inputs
#property strict

// 输入参数
input bool CloseBuyPositions = true;    // 平仓所有买单
input bool CloseSellPositions = true;   // 平仓所有卖单
input intSlippage = 3;                // 允许的滑点(点数)

//+------------------------------------------------------------------+
//| 脚本程序开始函数                                                 |
//+------------------------------------------------------------------+
void OnStart()
{
   // 获取当前品种
   string symbol = Symbol();
   
   // 初始化交易请求和结果结构
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);
   
   // 设置交易请求公共参数
   request.action = TRADE_ACTION_DEAL;
   request.symbol = symbol;
   request.deviation = Slippage;
   
   // 获取所有持仓
   if(PositionSelect(symbol))
   {
      // 遍历所有持仓
      for(int i = PositionsTotal()-1; i >= 0; i--)
      {
         if(PositionGetSymbol(i) == symbol)
         {
            ulong ticket = PositionGetInteger(POSITION_TICKET);
            long type = PositionGetInteger(POSITION_TYPE);
            double volume = PositionGetDouble(POSITION_VOLUME);
            double price = PositionGetDouble(POSITION_PRICE_OPEN);
            
            // 检查是否需要平仓该类型订单
            if((type == POSITION_TYPE_BUY && CloseBuyPositions) ||
               (type == POSITION_TYPE_SELL && CloseSellPositions))
            {
               // 设置平仓参数
               request.position = ticket;
               request.volume = volume;
               
               // 根据持仓类型设置平仓价格和类型
               if(type == POSITION_TYPE_BUY)
               {
                  request.price = SymbolInfoDouble(symbol, SYMBOL_BID);
                  request.type = ORDER_TYPE_SELL;
               }
               else
               {
                  request.price = SymbolInfoDouble(symbol, SYMBOL_ASK);
                  request.type = ORDER_TYPE_BUY;
               }
               
               // 发送交易请求
               bool res = OrderSend(request, result);
               
               // 检查结果
               if(!res || result.retcode != TRADE_RETCODE_DONE)
               {
                  Print("平仓失败 #", ticket, " 错误: ", GetLastError(), " 结果代码: ", result.retcode);
               }
               else
               {
                  Print("成功平仓 #", ticket, " 手数: ", volume);
               }
            }
         }
      }
   }
   else
   {
      Print("当前品种 ", symbol, " 没有持仓");
   }
   
   Print("脚本执行完成");
}
//+------------------------------------------------------------------+

ddwin668 发表于 2025-7-28 18:31:19

我不知怎么把编译好的脚本作为附件给你,直接源码吧,自己编译,运行前记得打开MT5的算法交易功能。
#include <Trade\trade.mqh>
#include <Trade\PositionInfo.mqh>
CTrade trade;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   ulong ticket=0;
   int total= PositionsTotal();
   if(total<=0)return;
   for(int i=total-1;i>=0;i--)
   {
      if((ticket=PositionGetTicket(i))>0)
      {
         if(PositionGetSymbol(i)==Symbol())
         {
            trade.PositionClose(ticket);
         }
      }
   }
}

zyeabx666 发表于 2025-7-29 08:17:00

写一个也才三五十搞定,还用花时间寻求
页: [1]
查看完整版本: 求MT5当前品种一键全平脚本