The problem is that the PositionsTotal can change while this code is running. "Sleep" is there to fake some longer calculations. This is not necessary but increases the chances of getting false data.
Let's say we start with PositionsTotal=2 and the first position with index=0 is in the process of closing.
So, in the for loop's first cycle the closing position is still there at index=0 but after the first cycle's long calculation the PositionsTotal decreases and the position that was originally at index=1, now is at index=0 and the second cycle's SelectByIndex (at 1) will fail. Which means we end up with an array that stores the closed position and misses the valid open position.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
The code:
The problem is that the PositionsTotal can change while this code is running. "Sleep" is there to fake some longer calculations. This is not necessary but increases the chances of getting false data.
Let's say we start with PositionsTotal=2 and the first position with index=0 is in the process of closing.
So, in the for loop's first cycle the closing position is still there at index=0 but after the first cycle's long calculation the PositionsTotal decreases and the position that was originally at index=1, now is at index=0 and the second cycle's SelectByIndex (at 1) will fail. Which means we end up with an array that stores the closed position and misses the valid open position.