How to close by order sequence?

 

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.

 

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.

 
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!

 
johnnybegoode: How to code it?
  1. 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.
  2. https://www.mql5.com/en/forum/139121
 
WHRoeder:
johnnybegoode: How to code it?
  1. 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.
  2. https://www.mql5.com/en/forum/139121

int slippage=10;
  double minimum_profit=5.00;
  int x;
  double trades[][4];
  int total=OrdersTotal();
  ArrayResize(trades,total);
  for(x=total-1;x>=0;x--)
     {
     if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
        trades[x][0]=OrderTicket();
        trades[x][1]=OrderProfit()+OrderCommission()+OrderSwap();
        trades[x][2]=OrderLots();
        trades[x][3]=OrderType();
        }
     }
 
  ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND);
  x=0;
  while(x<total)
     {
     double profit=trades[x][1]+trades[x+1][1];
     if(profit>=minimum_profit)
        {
        RefreshRates();
        double close_price=Ask;
        if(trades[x][3]==OP_BUY)
           close_price=Bid;
        if(!OrderClose((int)trades[x][0],trades[x][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        
        x++;
        RefreshRates();
        close_price=Ask;
        if(trades[x][3]==OP_BUY)
           close_price=Bid;
        if(!OrderClose((int)trades[x][0],trades[x][2],close_price,slippage,clrNONE))
           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!

 
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();

 

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;
  double trades[][4];
  int total=OrdersTotal();
  if(total>1)
  {
  ArrayResize(trades,total);
  for(x=total-1;x>=0;x--)
     {
     if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
        trades[x][0]=OrderTicket();
        trades[x][1]=OrderProfit()+OrderCommission()+OrderSwap();
        trades[x][2]=OrderLots();
        trades[x][3]=OrderType();
        }
     }
  
  ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND);
  x=0;
  while(x<total-1)
     {
     double profit=trades[x][1]+trades[x+1][1];
     if(profit>=minimum_profit)
        {
        RefreshRates();
        double close_price=Ask;
        if(trades[x][3]==OP_BUY)
           close_price=Bid;
        if(!OrderClose((int)trades[x][0],trades[x][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        RefreshRates();
        close_price=Ask;
        if(trades[x+1][3]==OP_BUY)
           close_price=Bid;
        if(!OrderClose((int)trades[x+1][0],trades[x+1][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        }
        x+=2;
     }
     }
 
Keith Watford #:

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.

Hi, thank you so much for this code. I want to compare and close first and last order when they in profit. Meaning when EA opened 5 buy orders, compare 1st and 5th orders, close when they are in profit, likewise compare 4th and 2nd order, close when they are profit. Instead of closing all together, how can i close in this way? Can you please guide me how to do these? currently it checks for average of all the buy orders and closes them all at once.

 

Just4Reference

1.

bool getTicketInfo(double& MyTicInfo[][4],,)

MyTicInfo[i][4] :

MyTicInfo[i][0] <=OrderOpenPrice()//OrderOpenPrice is fixed so sort by it is might better than  OrderProfit

MyTicInfo[i][1] <=OrderProfit()

MyTicInfo[i][2] <=OrderType() 

MyTicInfo[i][3] <=OrderTicket() 


2.

ArraySort(,,)


3.

int N= ArrayRang(,,);

for(int i=0;i<N;i++)

{

if(i<=N-1-i && TheTicInfo[i][1]+ TheTicInfo[ N-1-i ][1] >0)//when i== N-1-i  ie. 2*OrderProfit()>0  <=>  OrderProfit()>0

{

...

}

}

Reason: