简体   繁体   English

我如何更正 MQL4 OrderModify() 调用中的错误代码 0?

[英]How do I correct an error code 0 in MQL4 OrderModify() call?

I have this code below which I am using to test trailing stop function. It works on some currency pairs and doesn't work in others.我在下面有这段代码,我用它来测试追踪止损 function。它适用于某些货币对,但不适用于其他货币对。 It's returning error code 0 (zero).它返回错误代码 0(零)。 I will be grateful if somebody can help me out如果有人能帮助我,我将不胜感激


void modifyTrade() {
  TrailingStop = MarketInfo(Symbol(), MODE_STOPLEVEL);
   if(TrailingStop>0)
     {
     for(int z = 0; z<OrdersTotal(); z++)
     {
      OrderSelect(z,SELECT_BY_POS);
      if(Bid-OrderOpenPrice()>Point*TrailingStop)
        {
         if(OrderStopLoss()<Bid-Point*TrailingStop)
           {
            bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
            if(!res) {
               Print("Error in OrderModify. Error code=",GetLastError());
               //if(Symbol()=="EURNZD")
              Alert("Error in OrderModify. Error code=",GetLastError()," ; ",OrderSymbol());
               }
            else {
               Print("Order modified successfully.");
               }
           }
        }
     }
}
}

Error being错误是

0 == ERR_NO_ERROR... No error returned

is rather ok, no need to "correct" it, it means there was nothing to actually improve.还可以,不需要“纠正”它,这意味着实际上没有什么可以改进的。

There are a few risky points to mention - prices move, STOPLEVEL parameter can either ( be careful on this - a hostile case if appears in live-trading ) and also be informed, that there are other levels, that may prohibit an OrderModify() -call from being Broker-T&C-compliant (and will get rejected for that reason)有几个风险点需要提及 - 价格变动, STOPLEVEL参数可以(对此要小心 - 如果出现在实时交易中是敌对的情况)并且还被告知,还有其他级别可能会禁止OrderModify() - 要求符合 Broker-T&C 标准(并因此而被拒绝)

Somehow improved code may work in this sense:从某种意义上说,改进后的代码可能会起作用:

void modifyTrade()
{
     TrailingStop = MarketInfo( Symbol(), MODE_STOPLEVEL );
     if ( TrailingStop >  0 )
     {
        for (  int z = 0; z <  OrdersTotal(); z++ )
        {
            if !OrderSelect( z, SELECT_BY_POS ) continue;
        // ______________________________________________________ CRITICAL
            RefreshRates();
        // ______________________________________________________ Bid + Ask valid...
            if ( TrailingStop != MarketInfo( OrderSymbol(), MODE_STOPLEVEL ) )
            { // TrailingStop CHANGED ... LOG, renegotiate such Broker policy 
                 TrailingStop  = MarketInfo( OrderSymbol(), MODE_STOPLEVEL
                 }
        // ______________________________________________________ STOPLEVEL valid...
            if (  Bid - OrderOpenPrice() >  Point * TrailingStop
               && OrderType() == OP_BUY )
            {
                if (  OrderStopLoss() <  Bid - Point * TrailingStop )
                {
                   bool res = OrderModify( OrderTicket(),
                                           OrderOpenPrice(),
                                           NormalizeDouble( Bid - Point * TrailingStop, Digits ),
                                           OrderTakeProfit(),
                                           0,
                                           Blue
                                           );
                   if ( !res )
                   {
                       Print( "Error in OrderModify. Error code=", _LastError );
                    // if (  Symbol() == "EURNZD" )
                       Alert( "Error in OrderModify. Error code=", _LastError, " ; ", OrderSymbol() );
                       }
                   else
                   {
                      Print( "Order modified successfully." );
                      }
                   }
                }
            if (  OrderOpenPrice() - Ask >  Point * TrailingStop
               && OrderType() == OP_SELL )
            {
                ...
                }
            }
        }
    }

There are a couple of errors in your code.您的代码中有几个错误。

  1. once called, GetLastError() is reset.一旦调用, GetLastError()将被重置。 If you are both printing and alerting the last error, you should store the error code prior to printing/alerting.如果您同时打印和警告最后一个错误,您应该在打印/警告之前存储错误代码。

  2. you should check OrderSelect() succeeds.您应该检查OrderSelect()是否成功。

  3. you should iterate over orders in the opposite direction to ensure your code carried on operating should an order be closed mid-check.您应该以相反的方向迭代订单,以确保您的代码在检查中途关闭订单时继续运行。

  4. You require different code for longs and shorts您需要不同的多头和空头代码

  5. Your code is prone to errors simply because you have no buffer and are trying to trail price at the minimum stop level.您的代码很容易出错,原因很简单,因为您没有缓冲并且试图在最低止损水平上跟踪价格。 By the time your order modification is received by the trading server it is highly likely price may have moved back inside the stop level area resulting in a modification error.当交易服务器收到您的订单修改时,价格很可能已经回到止损水平区域内,从而导致修改错误。

void modifyTrade()
{
   TrailingStop = MarketInfo(Symbol(), MODE_STOPLEVEL);
   if(TrailingStop>0)
   {
      for(int z=OrdersTotal()-1; z>=0; z--)
      {
         if(OrderSelect(z,SELECT_BY_POS))
         {
            if(OrderType()==OP_BUY)
            {
               if(Bid-OrderStopLoss()>TrailingStop*point)
               {
                  bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
                  if(!res)
                  {
                     int err=GetLastError();
                     Print("Error in OrderModify. Error code=",err);
                     Alert("Error in OrderModify. Error code=",err);
                  }
                  else Print("Order modified successfully.");
               }
            }
            if(OrderType()==OP_SELL)
            {
               if(OrderStopLoss()-Ask>TrailingStop*point)
               {
                  bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
                  if(!res)
                  {
                     int err=GetLastError();
                     Print("Error in OrderModify. Error code=",err);
                     Alert("Error in OrderModify. Error code=",err);
                  }
                  else Print("Order modified successfully.");
               }
            }
         }
      }
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM