the code compiled correctly, but something when wrong

 

I have five orders on the chart, I am making a loop to select order to make some modification, it did not work so I print the order ticket along with its index to check, and I have noticed this

when these 2 line where here the it prints as follows

  for(int i=0; i<OrdersTotal(); i++)
       {

        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && Symbol() == OrderSymbol())
          {
                 double Old_TP = OrderTakeProfit();
                 double Old_SL = OrderStopLoss();
                 double New_TP = TPLevel_BUY();
                 double New_SL = SLLevel_BUY();
                 int Position_TicketNumber = 0;
                 Position_TicketNumber = OrderTicket();
                 
                 Print(i,"  ",Position_TicketNumber);
          }
        else
          {
           Print("error selecting Order","  ", GetLastError());
          }
       }

and that is the pending order only

while when I move these 2 lines to be as below

   for(int i=0; i<OrdersTotal(); i++)
     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && Symbol() == OrderSymbol())
        {
         int Position_TicketNumber = 0;
         Position_TicketNumber = OrderTicket();
         double Old_TP = OrderTakeProfit();
         double Old_SL = OrderStopLoss();
         double New_TP = TPLevel_BUY();
         double New_SL = SLLevel_BUY();


         Print(i,"  ",Position_TicketNumber);
        }
      else
        {
         Print("error selecting Order","  ", GetLastError());
        }
     }


it print the rest , noting that the print function line has not been changed and the code compiled correctly both times


Please advise and thanks for your help

 

There is only one possibility that I can guess.

         double Old_TP = OrderTakeProfit();
         double Old_SL = OrderStopLoss();

In either of these 2 functions, are you selecting another order?

 
Keith Watford:

There is only one possibility that I can guess.

In either of these 2 functions, are you selecting another order?

No these are predefined functions but the below 2 are user_defined function and there is aloop going on in each one of them with orderSelect function

         double New_TP = TPLevel_BUY();
         double New_SL = SLLevel_BUY();

would that affect the code?

 
Michael:

............the below 2 are user_defined function and there is aloop going on in each one of them with orderSelect function

would that affect the code?

Of course it will.

 
Keith Watford:

Of course it will.

So wont you tell me how ? :D as an example!

as this functions return a Price (value),?

 
Michael:

So wont you tell me how ? :D as an example!

as this functions return a Price (value),?

You are selecting an order, then calling a function that selects a different order.

What do you think OrderTicket() will return?

 
Keith Watford:

You are selecting an order, then calling a function that selects a different order.

What do you think OrderTicket() will return?

but these called function does not return the selected order, it return double price value,

the selection in these function just to make some calculation but it does not return different ticket number !!!

would you explain more ?

 
Michael:

but these called function does not return the selected order, it return double price value,

the selection in these function just to make some calculation but it does not return different ticket number !!!

would you explain more ?

OrderTicket() returns the ticket number of the last selected order.

 
Keith Watford:

OrderTicket() returns the ticket number of the last selected order.

but there shouldn't be any selected trade since the function should have been terminated by returning the double value which I print its output to make sure that it get the required value,

even if so in the called function there is a loop so all the trades are selected during the loop for checking and calculation, then why the pending order only is the one that kept selected even after the function exit and starting a new loop not any other trade ? 
 
Michael:

but there shouldn't be any selected trade since the function should have been terminated by returning the double value which I print its output to make sure that it get the required value,

even if so in the called function there is a loop so all the trades are selected during the loop for checking and calculation, then why the pending order only is the one that kept selected even after the function exit and starting a new loop not any other trade ? 

One thing does not have to with the other. Selection of an order is a globally scoped operation. If you call a function that changes the order selection it is not restored upon return. If you want that, then you will have to implement it by saving the context and restoring it before returning.

 
Fernando Carreiro:

One thing does not have to with the other. Selection of an order is a globally scoped operation. If you call a function that changes the order selection it is not restored upon return. If you want that, then you will have to implement it by saving the context and restoring it before returning.

can you provide me with a reference that I can read regarding this issue that would help me understand fully the issue and how can I overcome it in my future work?