# How to close by order sequence?

525

Closing by order sequence.

How to create a loop to check?

if

i.e.

order 1 + order 2 has profited - close
order 3 + order 4 has profited - close

5 + 6

7 + 8

9 + 10

and so on ...

Order 1 being having the smallest order number and was opened at the earliest time.

15725

In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Loops and Closing or Deleting Orders - MQL4 forum But since you want to close in order, you can't use a position loop.

Create an array of tickets in order. Then process the array in order.

If you are in the USA, FIFO rules mandate that you close #1 first.

525

WHRoeder:

In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Loops and Closing or Deleting Orders - MQL4 forum But since you want to close in order, you can't use a position loop.

Create an array of tickets in order. Then process the array in order.

If you are in the USA, FIFO rules mandate that you close #1 first.

How to code it?

Thanks!

15725

 johnnybegoode: How to code it? learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem. https://www.mql5.com/en/forum/139121
525

WHRoeder:
 johnnybegoode: How to code it? learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.https://www.mql5.com/en/forum/139121

```int slippage=10;
double minimum_profit=5.00;
int x;
int total=OrdersTotal();
for(x=total-1;x>=0;x--)
{
{
}
}

x=0;
while(x<total)
{
if(profit>=minimum_profit)
{
RefreshRates();
close_price=Bid;
Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());

x++;
RefreshRates();
close_price=Bid;
Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
x++;
}
}```

How do I rectify this error?
Error: incorrect start position 0 for ArraySort function

Thanks!

15725

johnnybegoode:
How do I rectify this error?
Error: incorrect start position 0 for ArraySort function
1. What error, you haven't stated one; there are no mind readers here.
2. `  ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND);`
How do you know there's a problem with sort? You never check for one.
3.  Don't use constants. ```  double trades[][4]; :         trades[x][0]=OrderTicket();         trades[x][1]=OrderProfit()+OrderCommission()+OrderSwap();         trades[x][2]=OrderLots();         trades[x][3]=OrderType();``` Use enumerations and document your code ```enum Trades {ticket, profit, lots, type, TradesSize}; double trades[][TradesSize]; :         trades[x][ticket]=OrderTicket();         trades[x][profit]=OrderProfit()+OrderCommission()+OrderSwap();         trades[x][lots]=OrderLots();         trades[x][type]=OrderType();```

Moderator
10644

That code was meant as an example and untested

This should work better.

WHRoeder is correct that using enums makes the code easier to read and will help the coder make less mistakes.

In future please do not represent code as your own when you have taken it from another forum.  I have no doubt that you definitely intended to give the impression that it was your code.

```  int slippage=10;
double minimum_profit=100.00;
int x;
int total=OrdersTotal();
if(total>1)
{
for(x=total-1;x>=0;x--)
{
{
}
}

x=0;
while(x<total-1)
{
if(profit>=minimum_profit)
{
RefreshRates();
close_price=Bid;