function to close open trades with current profit. - page 2

 
Frederic Metraux #:

you wrote

Where do you get total? Use the PositionsTotal() instead. I think this is where the problem is. Maybe total is smaller than the PositionsTotal()?

total is defined on the global like so

#define total PositionsTotal()

i will continue through your posts soon. thanks again.

 
Frederic Metraux #:

2. The worsTics loop

In the block:

You're looping i up to no2close , but still referencing the same currently selected position.Ticket() ( tk ) inside. That means the loop does not iterate through positions; it's just checking the same ticket repeatedly against multiple worsTics[i] .

So only if tk matches one of those entries will it close — but the others remain untouched, because you never advance position.SelectByIndex(...) inside that inner loop.

This creates a false impression that you’re checking multiple positions, when in fact you’re only checking the currently selected one repeatedly.

this makes me feel dumb now. Of course you are right.

I will try your suggested code tomoz and report closer to end of trading week.

Thanks again.

EDIT: The yellow is exactly how i wanted it to happen; however i like the way you did it in your code. I will modify my code to be almost all yours. THANKS AGAIN. and report near end of trading week with "how it goes".

EDIT II: worsTics is a list of losing trades listed in decreasing order; with the worst losing trade being worsTics[0]; If the profits from the closed "profit" trades is enuf, then, more than 1 losing trade will be closed; so the loop is not required to "advance position.Select(...); the loop is to match up the ticket in the worsTics array with the selected position.
 
Michael Charles Schefe #:


EDIT II: worsTics is a list of losing trades listed in decreasing order; with the worst losing trade being worsTics[0]; If the profits from the closed "profit" trades is enuf, then, more than 1 losing trade will be closed; so the loop is not required to "advance position.Select(...); the loop is to match up the ticket in the worsTics array with the selected position.

but the loop for worsTics will not be used "neg" (the total of the losing trades that has been closed) has exceeded the total number of losing trades to close ie no2close.

NOTE this part of the code is working fine. My issue was that profit trades were remaining open. But with your suggestions, i am sure that I have found the problem. THANKS AGAIN.

 
Frederic Metraux #:


Issue is fixed thanks to your suggestion regarding "continue". I have now seen groups of profit trades close; leaving 0 trades open with profit remaining.

so, despite not having any errors reporting in the journal or logs, it is apparent that there was some kind of server error or issue that caused the attempts to close the trades to return true,  and then to "continue" when they shouldnt have.

EDIT: but maybe this is just because i am using Asynchronous closing mode. This assumes that the result will be "true", therefore I should have forseen this issue may happen beforehand.