OrderModify not working

 

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.


//+------------------------------------------------------------------+
//|                                                      testing.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int TrailingStop;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   modifyTrade();
   
  }
//+------------------------------------------------------------------+

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

Two things i noticed;

If you are looking for your EA to go through all the open trades in modifyTrade function, you should maybe try someting like this:

for (int z= OrdersTotal()-1; z>=0; z--)

Your OrderSelect seems to be missing the MODE_TRADES or MODE_HISTORY, so it should be like this:

OrderSelect(z, SELECT_BY_POS, MODE_TRADES)


Hope this helps.

 
SeferAmcaFTW #: Your OrderSelect seems to be missing the MODE_TRADES or MODE_HISTORY,

Trades is the default; not missing.