Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 708

 
Rustam Bikbulatov:
Hello guys. Can you please tell me why I have a couple dozen orders that are closed in staggered order or different order? How should I fix it? Is there a method of closing at once?

when closing - change the order of the orders

i.e. the loop should be

for(int i=OrdersTotal()-1;i>=0;i--){...}

at least :-)

PS. as long as one has stepped on "it" - closing orders in "staggered" order can sometimes be useful.
 
Maxim Kuznetsov:

when closing - change the order of the orders

i.e. there must be a cycle

at least :-)

PS. since you have stepped on it anyway - closing orders in a "staggered" order cansometimes be useful.

If it is a controlled closing ;)

In general (although your comment is correct) the question was about "method of closing at once", which cannot be done in a loop.

Here you need to open the opposite position with a lot equal to the sum of the lots of all open positions for locking, and then quietly close the opposite one.

 
Artyom Trishkin:

If it is a controlled closure ;)

And in general (although your comment is correct) the question was about the "method of closing at once", which cannot be done in a loop.

You need to open an opposite position with a lot equal to the sum of all open positions' lots for locking, and then quietly close the opposite one.

And what prevents you from closing orders one by one?

 
Rustam Bikbulatov:

and what prevents orders from closing in order?

Offsetting the indexes of orders in the market list when removing them in a direct loop from zero to OrdersTotal()-1.

 
Rustam Bikbulatov:

what prevents orders from being closed in order?

Either you close all open positions in the reverse cycle from OrdersTotal()-1 to >=0 in order of their order in the list, or you open an opposite position for locking, and then quietly close the required positions with an opposite one.

 
Thank you Artem!
 
Artyom Trishkin:

If it is a controlled closure ;)

In general (although your point is correct) the question was about "method of closing at once", which cannot be done in a loop.

Here you need to open the opposite position with a lot equal to the sum of lots of all open positions for locking, and then already quietly close with the counter

The most appropriate closing method is to call your agent: "John, close the position on General Electric, we agreed on everything".

:-)

It is not always possible to cover with the opposite volume. And it slides harder - the volume is large.

 
Artyom Trishkin:

Offset of indexes of orders in market list when deleting them in direct sequence from zero to OrdersTotal()-1.

I have seen the code for closing orders in a direct sequence and it worked without failure.

There it was just increasing the loop counter by one when an order was closed successfully. )))) Or was it decreased? I don't remember exactly and I can't strain my brain.

 
Maxim Kuznetsov:

The most correct method of closing is to call your agent, "John, close the position on General Electric, we agreed on everything".

:-)

It's not always possible to cover with counter volume. And it slides harder - the volume is high.

But all at once :) And in a loop, one at a time.

 
Alexey Viktorov:

I have seen the code to close orders in a direct sequence and it worked without any glitches.

It simply increased the loop counter by one when the order was closed successfully. )))) Or decreased it? I don't remember exactly, and I can't strain my brain.

To avoid straining your brain, you can gather all positions prepared for deletion into a list in the order you want (in any order you like), and delete them "name by name" looking at the list. That way, the correct deletion order will be maintained.

Reason: