首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何关闭MQL5代码(脚本,EA )的所有交易?

如何关闭MQL5代码(脚本,EA )的所有交易?
EN

Stack Overflow用户
提问于 2017-02-20 16:27:40
回答 4查看 15.9K关注 0票数 0

作为我的专家顾问的一部分,在用CLOSE ALL TRADES编写MQL5代码的过程中苦苦挣扎。

代码或任何关于关闭MQL5中的所有交易的想法都将非常有用。

由于我是新的EA写作,请不要轻易地写。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-02-22 09:19:58

欢迎来到MQL4/5的野生世界

您可能会受到这个原始示例的启发,因为MQL5已经改变了控制终端/服务器交互的方式:

代码语言:javascript
复制
#define                                  ID_OWN_MAG_NUM 9999999         // ALWAYS:  use MagicNumber of the expert, to avoid MIX
//+----------------------
//| Closing all positions, MATCHING this ID_OWN_MAG_NUM ( avoid deleting others ...
//+----------------------
void OnStart()
{                                                                       // .DEF + .INIT the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;

   int             total = PositionsTotal();                                        // .GET  number of open positions
   for (  int i = total - 1; i >= 0; i-- )                              // .ITER  over all open positions
   {                                                                    //        .GET  params of the order:
          ulong  position_ticket  = PositionGetTicket(       i );                               //  - ticket of the position
          string position_symbol  = PositionGetString(       POSITION_SYMBOL );                 //  - symbol 
          int    digits           = (int) SymbolInfoInteger( position_symbol,       
                                                             SYMBOL_DIGITS      
                                                             );                                 //  - number of decimal places
          ulong  magic            = PositionGetInteger(      POSITION_MAGIC );                  //  - MagicNumber of the position
          double volume           = PositionGetDouble(       POSITION_VOLUME );                 //  - volume of the position
          ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE) PositionGetInteger( POSITION_TYPE );   //  - type of the position

          PrintFormat( "Tkt[#%I64u] %s  %s  %.2f  %s MagNUM[%I64d]",    // .GUI:    print details about the position    
                       position_ticket,
                       position_symbol,
                       EnumToString(type),
                       volume,
                       DoubleToString( PositionGetDouble( POSITION_PRICE_OPEN ), digits ),
                       magic
                       );

          if (  magic == ID_OWN_MAG_NUM )                               // .IF MATCH:
          {     ZeroMemory( request );                                  //     .CLR data
                ZeroMemory( result  );                                  //     .CLR data
                                                                        //     .SET:
                request.action    = TRADE_ACTION_DEAL;                  //          - type of trade operation
                request.position  = position_ticket;                    //          - ticket of the position
                request.symbol    = position_symbol;                    //          - symbol 
                request.volume    = volume;                             //          - volume of the position
                request.deviation = 5;                                  //          - allowed deviation from the price
                request.magic     = EXPERT_MAGIC;                       //          - MagicNumber of the position

                if (  type == POSITION_TYPE_BUY )
                {     request.price = SymbolInfoDouble( position_symbol, SYMBOL_BID );
                      request.type  = ORDER_TYPE_SELL;
                      }
                else
                {
                      request.price = SymbolInfoDouble( position_symbol, SYMBOL_ASK );
                      request.type  = ORDER_TYPE_BUY;
                      }

                PrintFormat(       "WILL TRY: Close Tkt[#%I64d] %s %s",                      position_ticket,
                                                                                             position_symbol,
                                                                                             EnumToString( type )
                                                                                             );
                if ( !OrderSend( request,result ) )
                      PrintFormat( "INF:  OrderSend(Tkt[#%I64d], ... ) call ret'd error %d", position_ticket,
                                                                                             GetLastError()
                                                                                             );
                PrintFormat(       "INF:            Tkt[#%I64d] retcode=%u  deal=%I64u  order=%I64u", position_ticket,
                                                                                                      result.retcode,
                                                                                                      result.deal,
                                                                                                      result.order
                                                                                                      );
                }
          }
  }
//+------------------------------------------------------------------+
票数 2
EN

Stack Overflow用户

发布于 2021-06-08 21:30:20

比这更简单:

代码语言:javascript
复制
 //positions
 for(int i=PositionsTotal()-1;i>=0;i--)
 {
   ulong positionticket = PositionGetTicket(i);
   trade.PositionClose(positionticket,-1);
   Print("eliminando posición "+positionticket);
  
 } 
 //orders
 for(int i=OrdersTotal()-1;i>=0;i--)
 {
   ulong orderticket = OrderGetTicket(i);
   trade.OrderDelete(orderticket);
   Print("eliminando operation "+orderticket); 
 } 
票数 1
EN

Stack Overflow用户

发布于 2019-04-30 18:26:51

这对我是有效的-关闭所有的市场买卖和/或出售的位置作为一个符号。建议测试演示网和套期保值帐户,以进行验证和澄清。

代码语言:javascript
复制
for(int i=PositionsTotal()-1; i>=0; i--)
    {
       ulong ticket=PositionGetTicket(i);
       trade.PositionClose(ticket);   
    }  

代码语言:javascript
复制
 if(abc == xyz)
  {
    CloseBuySellSymbol(0);
  }

void CloseBuySellSymbol()
 {
     for(int i=PositionsTotal()-1; i>=0; i--)
    {
       ulong ticket=PositionGetTicket(i);
       trade.PositionClose(ticket);   
    }  
 }

如果在void OnTick()中运行,则执行可能会被勾选延迟

如果用于void OnChartEvent()执行是瞬时的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42349617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档