Array out of range while looping through positions due to one or more position closed or opened during the loop - page 2

 

Unforunately did reversed looping _not_ solve the problem. How do you guys work with positions?

Thank you,
Martin

 
Sven Martin Zeke Persson #:

Unforunately did reversed looping _not_ solve the problem. How do you guys work with positions?

Thank you,
Martin

The problem in your code is that you need a separate counter for your array of positions, since not all positions might be valid and added.

Try this:

const int posCount = PositionsTotal();
ArrayResize(pos, 0, posCount); // Initially empty, reserved memory for at most posCount positions

for (int i = posCount - 1; i >= 0; i--)
{
        const ulong ticket = PositionGetTicket(i);
        if (PositionSelectByTicket(ticket))
        {
                const int p = ArrayResize(pos, ArraySize(pos) + 1, posCount); // Add one item to the array, p is the new size
                if (p == -1) break;
                pos[p - 1].p_symbol = (string)PositionGetString(POSITION_SYMBOL);  // Access the last added item
                ...
        }
}
 
Francesco Baldi #:

The problem in your code is that you need a separate counter for your array of positions, since not all positions might be valid and added.

Try this:

Much obliged - will try.
 
Sven Martin Zeke Persson #:
Much obliged - will try.
It does seem to be working - big thanks.