0

作为我的智能交易系统的一部分,努力编写CLOSE ALL TRADES代码。MQL5

代码或任何关于关闭所有交易的想法MQL5都会非常有帮助。

由于我是 EA 写作的新手,所以请在写作中轻松一点。

4

4 回答 4

2

欢迎来到野生世界MQL4/5

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

#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
                                                                                                      );
                }
          }
  }
//+------------------------------------------------------------------+
于 2017-02-22T09:19:58.767 回答
0

这对我有用 - 关闭一个交易品种的所有市场买入和/或卖出头寸。建议对模拟净额结算和对冲账户进行测试以进行验证和澄清。

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

或者

 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()执行中使用是瞬时的

于 2019-04-30T18:26:51.703 回答
0

交易是通过使用OrderSend()函数向未平仓头寸发送订单以及放置、修改或删除挂单来完成的。每个交易订单都涉及所请求操作的类型。

 #define EXPERT_MAGIC 123456   // MagicNumber of the expert
//+------------------------------------------------------------------+
//| Closing all positions                                            |
//+------------------------------------------------------------------+
void OnStart()



{
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
     {
      //--- parameters 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
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  magic);
      //--- if the MagicNumber matches
      if(magic==EXPERT_MAGIC)
        {
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         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
         //--- set the price and order type depending on the position type
         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;
           }
         //--- output information about the closure
         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         //---
        }
     }
  }
//+------------------------------------------------------------------+

MQL5 文档中的更多示例: https ://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions

于 2019-06-15T14:56:41.253 回答
0

比这更简单:

 //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); 
 } 
于 2021-06-08T21:30:20.667 回答