简体   繁体   English

无法在MetaTrader Terminal 4 Strategy Tester中关闭我的未平仓交易

[英]Can't close my open trades in MetaTrader Terminal 4 Strategy Tester

I am trying to code my EA where if my close conditions are met, I want my EA to close all the open trades ( there are potentially more than 1 open trades ). 我正在尝试对我的EA进行编码,如果满足我的关闭条件,我希望我的EA关闭所有未平仓交易(可能有1个以上的未平仓交易)。

Here is my code for the closing trade section and when I run it through the Strategy Tester, I noticed it doesn't close my trades. 这是我关闭交易部分的代码,当我通过策略测试器运行它时,我注意到它没有关闭我的交易。

 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
 }

I don't know where do you set value for Ask , Bid and Lot variables. 我不知道您在哪里为AskBidLot变量设置值。 But they've to change when you loop through your open positions. 但是当您遍历未结头寸时,它们必须改变。

You can try this function to close all positions: 您可以尝试使用此功能关闭所有头寸:

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;
  }

Edit: 编辑:

if you don't want to deal with pending orders at all, use this code instead of OrdersTotal() : 如果您根本不想处理待处理的订单,请使用以下代码代替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);
   }

Then total orders would be 则总订单为

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MetaTrader4终端,策略测试器:在交易发生之前如何从[结果窗口]跳至[图表]? - MetaTrader4 Terminal, Strategy Tester: how to jump from a [ Results Window ] to the [ Graph ] BEFORE a Trade Happens? 策略测试器中的未知符号但 Metatrader 中的实时数据不是 - Unknown Symbols in Strategy Tester but not Live Data in Metatrader 我如何将 Metatrader 4 警报或电子邮件指标信号转换为 Expert Advisor 以进行交易? - How can I convert Metatrader 4 alert or email indicator signal to Expert Advisor to open trades? 元交易者 4 | 在 EA 重新初始化时不得进行任何交易 - Metatrader 4 | No trades to be placed on EA re-initialization 有人如何将数据从 MetaTrader 4/5 终端发送到外部服务器? - How can someone send data from MetaTrader 4/5 Terminal to external server? 如何使MetaTrader 5终端脚本将EURUSD的BID汇率过帐到端口443上的我的nodejs服务器? - How to make a MetaTrader 5 Terminal script to POST an EURUSD BID rate to my nodejs server on port 443? 如何从 MetaTrader Terminal 5 MQL 5 向在我的 MT5 主机上本地运行的 nodejs 服务器发送请求? - How to post from MetaTrader Terminal 5 MQL 5 a request to my nodejs server, which is running locally on my MT5 host? 如何在 MetaTrader4 终端中获取当前毫秒数? - How to get current milliseconds in MetaTrader4 Terminal? 如何在 MetaTrader 终端中使用逐笔价位数据进行回测? - How to do a backtesting with tick-level data in MetaTrader Terminal? MetaTrader 终端 4:根据历史数据调试智能交易系统 - MetaTrader Terminal 4: debugging an expert advisor on historical data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM