MT4 删除所有挂单脚本 免费公式 源码

指标名称:删除所有挂单

版本:MT4 ver. 2.00

在外汇交易中,挂单具有重要的地位,很多交易策略都围绕它们展开。本文将为您介绍挂单的概念,并指导如何在 MetaTrader 平台中提交和删除多笔反向挂单。文末还提供了一款免费开源脚本,帮助您在 MT4 中轻松删除所有挂单。

图片

什么是挂单?

在外汇交易中,主要有两种订单类型:

1. 市价订单:即刻提交并以当前市场价格执行的订单。

2. 挂单:提交给经纪商,但需在特定价格水平触发时才执行的订单。

挂单主要分为以下四个子类型:

  • - 买入止损单:在市场价格高于特定价格时触发。

    图片

  • - 买入限价单:在市场价格低于特定价格时触发。

    图片

  • - 卖出止损单:在市场价格低于特定价格时触发。

    图片

  • - 卖出限价单:在市场价格高于特定价格时触发。

图片

不同类型的订单适用于不同的交易方向、入场价位和当前市场价格情况。

MetaTrader 中的挂单操作

在 MetaTrader 平台(MT4)中,您可以轻松创建上述四种挂单类型。然而,MT4 有一个限制,即您一次只能更新或删除一笔挂单。

要创建挂单,您可以通过订单界面选择挂单类型,设定开盘价格以及可选的止损和止盈水平。如果需要修改或删除挂单,则需要逐个管理。

图片

为什么要使用反向挂单?

有时,交易者需要同时提交买入和卖出的反向挂单,以便更好地对冲或管理风险。反向挂单的主要用途是等待市场触发特定条件,快速执行订单。对于一些依赖价格波动的策略来说,反向挂单是重要的工具。

例如,在新闻发布或市场重要事件前后,价格可能迅速波动,反向挂单可以帮助交易者在特定价位迅速进场。此外,在突破交易策略中,反向挂单也非常有用,以便在区间突破时抓住机会。同样地,在回撤策略中,反向挂单也能帮助在价格反转时实现快速进出场。

图片

如何在 MT4 中同时开立反向挂单?

由于 MT4 的界面限制,您无法通过常规方式同时管理多笔挂单。不过,有一款工具可以帮您实现这一操作:MetaTrader 4 的“一触式交易专业版”智能交易系统可以帮助您只需一次点击便提交反向订单,包含开盘价格、距离及其他参数的设定。

如何删除多笔反向挂单

MT4 默认情况下无法一次删除多笔反向挂单,但通过免费的脚本工具,您可以轻松做到这一点。该脚本可以帮助您一次性删除所有反向挂单,操作简单而高效。

可用的脚本参数

图片

该脚本还支持一系列输入参数,帮助您筛选和删除特定的挂单,进一步优化交易管理的流程。

完整代码展示:


//+------------------------------------------------------------------+
//|                                                   删除挂单脚本.mq4 |
//|                                Copyright © 2009-2024, www.QChaos.com |
//|                                          https://www.qchaos.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 量化混沌, www.qchaos.com"
#property link      "https://www.qchaos.com"
#property version       "2.00"
#property strict
#property description "---------------------------------------------"
#property description "EA、指标公式分享"
#property description "EA、指标编写业务承接"
#property description "---------------------------------------------"
#property description "---------------------------------------------"

#property show_inputs

enum ENUM_ORDER_TYPES{
   ALL_ORDERS=1,        //ALL PENDING ORDERS
   ONLY_BUY_STOP=2,     //BUY STOP ONLY
   ONLY_BUY_LIMIT=3,    //BUY LIMIT ONLY
   ONLY_SELL_STOP=4,    //SELL STOP ONLY
   ONLY_SELL_LIMIT=5    //SELL LIMIT ONLY
};

input bool OnlyCurrentSymbol=false;   // 仅删除当前图表的订单
input ENUM_ORDER_TYPES OrderTypeFilter=ALL_ORDERS;     // 要删除的订单类型
input string OrderCommentFilter="";    // 仅删除具有此备注的订单
input int OrderMagicFilter=0;          // 仅删除匹配此魔术号的订单
input bool OnlyMagicNumber=false;      // 仅删除匹配魔术号的订单
input int MagicNumber=0;              // 匹配的魔术号
input bool OnlyWithComment=false;    // 仅删除具有以下备注的订单
input string MatchingComment="";       // 匹配的备注
input int Delay=0;                    // 删除订单之间的延迟(以毫秒为单位)

void DeleteOrders(){
   
   //Log in the terminal the total of orders, current and past
   Print(OrdersTotal());
      
   //Start a loop to scan all the orders
   //The loop starts from the last otherwise it would miss orders
   for(int i=(OrdersTotal()-1);i>=0;i--){
      
      //If the order cannot be selected throw and log an error
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      } 

      //check if the order matches the filter and if not skip the order and move to the next
      if(OrderTypeFilter==ONLY_SELL_LIMIT && OrderType()!=OP_SELLLIMIT) continue;   //don't action if the order type doesn't match
      if(OrderTypeFilter==ONLY_SELL_STOP && OrderType()!=OP_SELLSTOP) continue;     //don't action if the order type doesn't match
      if(OrderTypeFilter==ONLY_BUY_LIMIT && OrderType()!=OP_BUYLIMIT) continue;     //don't action if the order type doesn't match
      if(OrderTypeFilter==ONLY_BUY_STOP && OrderType()!=OP_BUYSTOP) continue;       //don't action if the order type doesn't match
      if(OnlyCurrentSymbol && OrderSymbol()!=Symbol()) continue;
      if(OnlyMagicNumber && OrderMagicNumber()!=MagicNumber) continue;
      if(OnlyWithComment && StringCompare(OrderComment(),MatchingComment)!=0) continue;
      //You could add additional filters like expiry or open price or so on
      
      //Create the required variables
      //Result variable, to check if the operation is successful or not
      bool res=false;
      
      //Deleting the order using the correct price depending on the type of order
      if(OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP){
         res=OrderDelete(OrderTicket());
      }
      
      //If there was an error log it
      if(res==false) Print("ERROR - Unable to delete the order - ",OrderTicket()," - ",GetLastError());

      Sleep(Delay);
   }
}


void OnStart(){

   //Call the function when the script is run
   DeleteOrders();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值