Return value of 'OrderSelect' should be checked

 

I have no idea what i should do to fix this can someone help me out a little with my code and explain where and how i messed up it kind of work when i'm testing it but it's not working on every trade this is the code

PLEASE HELP going on 2 days i've been looking into this issue

   int TrailingStop=50;
   if(TrailingStop>0)
     {
      for(x=1; x<=OrdersTotal(); x++)
        {
         if(OrderSelect(x, SELECT_BY_TICKET)==true) 

            OrderSelect(x,SELECT_BY_TICKET);
   

         if(Bid-OrderOpenPrice()>Point*TrailingStop)
           {
            if(OrderStopLoss()<Bid-Point*TrailingStop)
              {
               bool res;
               res= OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid - Point*TrailingStop,Digits), OrderTakeProfit(),0,clrBeige);
               if(!res)
                  Print("Error in OrderModify. Error code=",GetLastError());
               else
                  Print("Order modified successfully.");
               if(Ask+OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Ask+Point*TrailingStop)
                    {
                     bool res2;
                     res2=OrderModify(OrderTicket(), OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,clrPaleTurquoise);
                     if(!res2)
                        Print("Error in OrderModify. Error code=",GetLastError());
                     else
                        Print("Order modified successfully.");
 
if(OrderSelect(x, SELECT_BY_TICKET)==true)

   OrderSelect(x,SELECT_BY_TICKET);

Return value of 'OrderSelect' should be checked

In the first line, you already checked OrderSelect to make the return value is true.

In the second line, you called it again, which is unnecessary and is what's giving you the error.

You only need to call OrderSelect one time via an if statement or store the value using a bool variable. Then, if it returns true, you can access information about that order using other functions such as OrderStoploss, OrderOpenPrice, OrderSwap, etc.

You should also place all of this inside the if statement that checks for the return value of OrderSelect

     if(Bid-OrderOpenPrice()>Point*TrailingStop)
        {
         if(Bid-OrderOpenPrice()>Point*TrailingStop)
           {
            if(OrderStopLoss()<Bid-Point*TrailingStop)
              {
               bool res;
               res= OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid - Point*TrailingStop,Digits), OrderTakeProfit(),0,clrBeige);
               if(!res)
                  Print("Error in OrderModify. Error code=",GetLastError());
               else
                  Print("Order modified successfully.");
               if(Ask+OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Ask+Point*TrailingStop)
                    {
                     bool res2;
                     res2=OrderModify(OrderTicket(), OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,clrPaleTurquoise);
                     if(!res2)
                        Print("Error in OrderModify. Error code=",GetLastError());
                     else
                        Print("Order modified successfully.");
 
      for(x=1; x<=OrdersTotal(); x++)
        {
         if(OrderSelect(x, SELECT_BY_TICKET)==true) 
  1. The for loop is by positions, not ticket numbers. Fix your Select.
  2. The position indexes are [zero … OrdersTotal()-1]. Fix your loop.
  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

 
for(int x=0; x<OrdersTotal(); x++)
     {
      if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
         break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==1234567)//replace 1234567 with your specific expert magic
        {
         //...
        }
     }

Here's one for you @xobeastmodeox

Reason: