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

 

Hello, my first question so i may not get this right but i do have a problem i cannot seem to solve.

As a part of an EA i gather the information about the positions - and if a position is close or opened while in that information gathering loop i get an "Array out of range"; obviously.

Is there a way to circumvent this?

And is there a way to get notified if the EA crashes due to an error handling (ie array out of range)?

Thank you


int totpos=PositionsTotal();
if(totpos>0)
{                                                          //getpositions       
        for(int i=0;i<totpos;i++)
        { //pos
                ulong temp=PositionGetTicket(i);
                if(PositionSelectByTicket(temp))
                {
                        pos[i].p_symbol     =(string)PositionGetString(POSITION_SYMBOL);
                        /*more code*/
                }
        }
}
 

get rid of this condition

if(totpos>0)
{    
}


And is there a way to get notified if the EA crashes due to an error handling (ie array out of range)?

void OnDeinit(const int reason){

   SendNotification("EA closed down. the reason code : ", reason);
}
 
Conor Mcnamara #:

get rid of this condition


And is there a way to get notified if the EA crashes due to an error handling (ie array out of range)?

Thank you.

I will try this.

 
Sven Martin Zeke Persson:

Hello, my first question so i may not get this right but i do have a problem i cannot seem to solve.

As a part of an EA i gather the information about the positions - and if a position is close or opened while in that information gathering loop i get an "Array out of range"; obviously.

Is there a way to circumvent this?

And is there a way to get notified if the EA crashes due to an error handling (ie array out of range)?

Thank you


Loop down to 0 not up from 0
 
Paul Anscombe #:
Loop down to 0 not up from 0
Thank you - would someone mind explaining the reasoning behind as to why there would be a difference?
 
Sven Martin Zeke Persson #:
Thank you - would someone mind explaining the reasoning behind as to why there would be a difference?
Imagine an excel sheet. Fill a column and go through it from bottom to top, while doing so, delete a few rows here and there, and keep track of where you are with a counter until you hit 0.

Then you will see why it works.

EDIT: the only issue this approach has, what happens if the positions get removed faster than your loop runs.

You will additionally need some error handling to manage that (very unlikely) situation.
 
Sven Martin Zeke Persson:
Hello, my first question so i may not get this right but i do have a problem i cannot seem to solve.

The position can be closed manually or by another EA.

Therefore, you should first create your own array with tickets, and then iterate over your array, checking whether the position is open or closed.

That is, you need to work with your own array and not with the MetaTrader array.

 
Dominik Egert #:
Imagine an excel sheet. Fill a column and go through it from bottom to top, while doing so, delete a few rows here and there, and keep track of where you are with a counter until you hit 0.

Then you will see why it works.

EDIT: the only issue this approach has, what happens if the positions get removed faster than your loop runs.

You will additionally need some error handling to manage that (very unlikely) situation.

Thank you yes i do understand why the problem occurs - and it actually does so for me repeatedly.

If you've got the time would you mind expanding on the error handling?

 
Taras Slobodyanik #:

The position can be closed manually or by another EA.

Therefore, you should first create your own array with tickets, and then iterate over your array, checking whether the position is open or closed.

That is, you need to work with your own array and not with the MetaTrader array.

Thank you. Have you got an idea as to how i would do that? My worry is that even during the creation of my own array the same problem may occur, that the EA has closed one or more positions during the loop.
 
Sven Martin Zeke Persson #:
Thank you. Have you got an idea as to how i would do that? My worry is that even during the creation of my own array the same problem may occur, that the EA has closed one or more positions during the loop.

creating your own array will not stop a position being closed during your processing so will make no difference to the problem.

cycle through the positions in the reverse loop as I stated this should eliminate array out of range errors.

 
Sven Martin Zeke Persson #:
My worry is that even during the creation of my own array the same problem may occur, that the EA has closed one or more positions during the loop.

Tickets in your array are not changed by anyone except you, they will not disappear anywhere.
If the position for some ticket is already closed, you will not be able to select it using "PositionSelectByTicket".

And you will have tickets of your positions, which you do not need to look for somewhere in the history of deals.
If "PositionSelectByTicket" returns false - then the position is already closed and you can check how it was closed, with what profit, when, etc. (you already have a ticket)

You can also sort these tickets as you need, select tickets separately for closing and track your tickets specifically in "OnTradeTransaction".
Ignoring other deals.