How to close by order sequence?

Back to topics list To post a new topic, please log in or register
avatar
102
johnnybegoode 2016.02.16 08:20
 

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.

avatar
12985
WHRoeder 2016.02.16 14:45 #
 

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.


avatar
102
johnnybegoode 2016.02.20 04:33 #
 
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!

avatar
12985
WHRoeder 2016.02.20 14:03 #
 
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://forum.mql4.com/47725#625194

avatar
102
johnnybegoode 2016.02.21 00:11 #
 
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://forum.mql4.com/47725#625194

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!


avatar
12985
WHRoeder 2016.02.21 02:31 #
 
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();

avatar
Moderator
3406
GumRai 2016.02.21 07:26 #
 

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;
     }
     }
Back to topics list  

To add comments, please log in or register