PositionsTotal() - page 2

 
Dominik Christian Egert #:

Thats not correct....

Here is why: (Has been explained multiple times)

When you have a list and you delete the record, you are looking at right now, then the next record will take the place in the list which you are looking at.

Since you are done with this line in your list, you will go to the next line, but the record from that line had been moved to the one you are now coming from... so you will miss that entry.


Clear?

EDIT: 

The solution is to start at the end of the list, so your "for()-loop" must count backwards...

Dominik

Many thanks for your response - very much appreciated

Apologies....but....

I had searched many times for a clue to my problem and not found anything - so "Has been explained multiple times" was irrelevant to me.

If, as you state items get replaced then will PositionsTotal() change following each delete?

"The solution is to start at the end of the list, so your "for()-loop" must count backwards..." looks like the 'proper solution'

Just thought I was trying to be helpful having spent 'ages' trying to find why items failed to delete.

 
Peter Williams #:

Dominik

Many thanks for your response - very much appreciated

Apologies....but....

I had searched many times for a clue to my problem and not found anything - so "Has been explained multiple times" was irrelevant to me.

If, as you state items get replaced then will PositionsTotal() change following each delete?

"The solution is to start at the end of the list, so your "for()-loop" must count backwards..." looks like the 'proper solution'

Just thought I was trying to be helpful having spent 'ages' trying to find why items failed to delete.



I think you were helpful. After all, it gives another opportunity to find this solution for others searching in a similar way, you did.

That's why I answered anyways....

Yes, PositionsTotal() will change every time an item gets deleted. - But that's no problem.

Haven't tested this, but maybe someone else could contribute on this:

In case code removes a position, another EA adds a position, PositionsTotal() would stay the same or even increment, right?

So is it required to take action on such situations?

A secondary question would be, by what criteria is the list sorted in the first place?

If it's sorted by time or ticket number, ascending, then no additional action is required.

Since this has never been discussed or mentioned anywhere, I would make the assumption the list is sorted in such way.
 

Thanks for you kind words

Yes my tests suggest items are sorted as you describe.  I came to this conclusion searching through HISTORY


if ( !myhistory.HistorySelect(History_Start_Time,TimeCurrent()))
   {  Report_StringError("Both", "408707 GHP History_Start_Time "+IntegerToString(History_Start_Time)+"("+TimeToString(History_Start_Time)+")"+", CHistoryPositionInfo::HistorySelect() failed!");
      //Alert("CHistoryPositionInfo::HistorySelect() failed!");
      return;
     }
   HistoryPositionsTotal = myhistory.PositionsTotal();


If I want to find all the positions in history for say - a MagicNumber - then I need the time the OLDEST trade was initiated to be the

History_Start_Time

This way I get all the trades now in HISTORY.

[You could of course, enter a really old date - say 1st Jan 2000 but that would probably mean HistoryPositionTotal is several thousands and means searching through each record is potentially too time consuming]