Download MetaTrader 5
To add comments, please log in or register
Trade like successful traders - subscribe to their signals!
Sameer
792
Sameer 2012.11.11 20:33 

Hi all,

the following script closes all buy orders,

can someone help by modifying it to close .. the "oldest 5" only, instead of all orders.

thanks


//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}
whroeder1
13602
whroeder1 2012.11.12 15:17  
Modify my GetHistoryOrderByCloseTime and you'd get
int GetOpenOrdersByOpenTime(int& tickets[], int dsc=1){  #define ASCENDING -1
    /* https://forum.mql4.com/46182 zzuegg says history ordering "is not reliable
     * (as said in the doc)" [not in doc] dabbler says "the order of entries is
     * mysterious (by actual test)" */
    int nOrders = 0;    datetime OCTs[];
    for(int iPos=OrdersTotal()-1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                // Only orders w/
    &&  OrderMagicNumber()  == Magic.Number             // my magic number
    &&  OrderSymbol()       == chart.symbol             // and my pair.
    ){
        int nextTkt = OrderTicket();        datetime nextOCT = OrderOpenTime();
        nOrders++; ArrayResize(tickets,nOrders); ArrayResize(OCTs,nOrders);
        for (int iOrders=nOrders - 1; iOrders > 0; iOrders--){  // Insertn sort.
            datetime    prevOCT     = OCTs[iOrders-1];
            if ((prevOCT - nextOCT) * dsc >= 0)     break;
            int         prevTkt = tickets[iOrders-1];
            tickets[iOrders] = prevTkt;    OCTs[iOrders] = prevOCT;
        }
        tickets[iOrders] = nextTkt;    OCTs[iOrders] = nextOCT; // Insert.
    }
    return(nOrders);
}
:
    int     tickets[],      nTickets = GetOpenOrderByOpenTime(tickets, ASCENDING);
    if (nTickets > 5) nTickets = 5; // Only want first five.
    for(int iTicket = 0; iTicket < nTickets; iTicket++) if (
        OrderSelect(tickets[iTicket], SELECT_BY_TICKET) 
    ){  if (!OrderClose(...)) Alert(...);
Jeremy Roach
2277
Jeremy Roach 2016.05.18 09:55  

hi, what about closing by descending or ascending lot sizes?

and if you have two x 0.1 lot trades, it closes them both?

I am not sure what to pull? 

/
To add comments, please log in or register