简体   繁体   中英

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. It's returning error code 0 (zero). 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)

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. 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.

  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.");
               }
            }
         }
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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