无法在MetaTrader Terminal 4策略测试程序中关闭我的未平仓交易

时间:2017-10-01 09:33:40

标签: mql4 metatrader4

我正在尝试编写EA,如果我的条件满足,我希望我的EA关闭所有未平仓交易(可能有超过1个未平仓交易)。

这是关闭交易部分的代码,当我通过策略测试程序运行时,我注意到它没有关闭我的交易。

 Total=0;                                     // Amount of orders
 for(int i=1; i<=OrdersTotal(); i++)          // Loop through orders
 {
  if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one
    {                                       // Analyzing orders:
     if (OrderSymbol()!=Symb)continue;      // Another security
      Total++;                               // Counter of market orders
    }
 }   


   while(true)                                  // Loop of closing orders
 {

  if (OrderType()==0 && Close_Buy_Condition==true)                // Order Buy is opened & Close Buy Condition is true
    {                                       
   for (c=Total-1; c>=0; c--)
    { 
     RefreshRates();                        // Refresh rates
     Ans=OrderClose(OrderTicket(),Lot,Bid,Slippage);      // Closing Buy
    }
    }


  if (OrderType()==1 && Close_Sell_Condition==true)                // Order Sell is opened & Close Sell Condition is true
    {                                       
   for (d=Total-1; d>=0; d--)
    {
     RefreshRates();                        // Refresh rates
     Ans=OrderClose(OrderTicket(),Lot,Ask,Slippage);      // Closing Sell

    }
    }

  break;                                    // Exit while
 }

1 个答案:

答案 0 :(得分:0)

我不知道您在哪里为AskBidLot变量设置了价值。但是,当你循环开仓时,他们会改变。

您可以尝试此功能关闭所有位置:

bool CloseAllPositions()
  {
   double total;
   int cnt;
   while(OrdersTotal()>0)
     {
      //  Close pending orders first, you can remove this section if you don't want pending orders to be deleted
      total=OrdersTotal();
      for(cnt=total-1; cnt>=0; cnt--)
        {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
           {
            switch(OrderType())
              {
               case OP_BUYLIMIT  :OrderDelete(OrderTicket()); break;
               case OP_SELLLIMIT :OrderDelete(OrderTicket()); break;
               case OP_BUYSTOP   :OrderDelete(OrderTicket()); break;
               case OP_SELLSTOP  :OrderDelete(OrderTicket()); break;
              }
           }
        }

      // then close opened orders 
      total=OrdersTotal();
      for(cnt=total-1; cnt>=0; cnt--)
        {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
           {
            switch(OrderType())
              {
               case OP_BUY:
                  if (Close_Buy_Condition==true) {
                    OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);
                  }
                  break;

               case OP_SELL:
                  if (Close_Sell_Condition==true) {
                    OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet);
                  }
                  break;
              }
           }
        }
     }
   return true;
  }

编辑:

如果您根本不想处理待处理的订单,请使用此代码而不是OrdersTotal()

int GetNumOpenPositions() {
    int total = OrdersTotal();

    double OpenBuyOrders = 0, OpenSellOrders = 0, PendBuys =0, PendSells =0;
     for( i=0;i<total;i++)       
       {
         OrderSelect(i, SELECT_BY_POS );
        if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
             {
               int type = OrderType();

               if (type == OP_BUY )       {OpenBuyOrders=OpenBuyOrders+1;}
               if (type == OP_SELL )      {OpenSellOrders=OpenSellOrders+1;}
               if (type == OP_BUYSTOP )   {PendBuys=PendBuys+1;}
               if (type == OP_SELLSTOP )  {PendSells=PendSells+1;}

             }
        }
       return (OpenBuyOrders + OpenSellOrders);
   }

然后总订单

相关问题