The code compiled correctly, but something when wrong

I have five orders on the chart, I am making a loop to select the 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 lines were here 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());
      }
   }

image
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
image
image
Please advise and thanks for your help

On the surface, it appears your TPLevel_BUY() & SLLevel_BUY() functions are messing with your program flow and “unselecting” your order. Perhaps post that code as well.

TPLevel_BUY() & SLLevel_BUY() are functions that return specific price value, and they contain a recall for another function that return also specific level price value to make a calculation that gets me TPLevel_BUY() & SLLevel_BUY(), and I print all the function to check the return value in the journal and all are correct, and all is fine except that the issue i mentioned above,
I do not know how these function would that mess my program

double TPLevel_BUY()
{

double TakeProfitLevel = 0;
if(OpenTrades_Counting_Current_Chart() == 1 && The_last_Trade()== OP_BUY)
{
TakeProfitLevel =NormalizeDouble((The_Trade_Best_Value()- Open_PriceOfPending_Order()) + (The_Trade_Best_Value() - AddingSwap()),Digits);
}
if(OpenTrades_Counting_Current_Chart() > 1)
{
TakeProfitLevel = NormalizeDouble((The_Trade_Best_Value()- First_open_Trade()) + (The_Trade_Best_Value() - AddingSwap()),Digits);
}
return (TakeProfitLevel);
}

You need to look at what the other functions are doing…do you have any orderselect functions there?

It is clear that the problem is created when you leave the current function to visit another function. In my experience, once you select an order, work with it straight away, or save it’s data into variable if you want to go do something else first.

They all have for loop and order select function to check for a required value or make some calculation to get a value(price) and when they get that value it’s assigned to a variable which is represented in the return of each one of the functions,
yes I may get a function to visit another function for rate comparison but by the end of the function the required value is saved to a variable so I can get the required return
I do not know if I got your comment correctly but can you provide me with a reference that I can get back to it regarding this issue that would help me understand fully the issue, where I messed up and how can I overcome it?

thanks

I don’t really have any reference but you cannot use orderselect in one function and then orderselect somewhere else and then come back and expect it to remember the original orderselect. When you use orderselect, it overides any other previous orderselect.

https://docs.mql4.com/trading/orderselect

Note this sentence:

It means that in some cases the order details (open price, SL/TP levels or expiration date) may change and the data become non-actual. It is strongly recommended to call the OrderSelect() function before request the order data.

Noted thank you :pray: