This function closes all orders on the currency pair and repeat as long orders are open.

Can be extended with Magic, Alerts, Stop trading and much more.

Dear Ixbone Customers, if you have any questions, please feel free to contact us!

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| CloseAllOrders repeat as long as orders are open                                                                                                                                                                          |
//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

#property copyright "Copyright © 2004- IXBONE/Michael Orth"
#property link "ixbone.com"
#property strict

    void CloseAllOrders() {
       bool res, result;
       bool repeat = true;
     
       while(repeat) {
          result = true;
          for(int i = OrdersTotal()-1; i >= 0; i--) {
             if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
                if (OrderSymbol() == Symbol() ) {
                   while (IsTradeContextBusy()) Sleep(100);
                   RefreshRates();
                   if (OrderType() == OP_BUY) {
                      res = OrderClose (OrderTicket(), OrderLots(), Bid, 0, Red);
                      if (!res) result = false;
                   }         
                   if (OrderType() == OP_SELL) {
                      res = OrderClose (OrderTicket(), OrderLots(), Ask, 0, Red);
                      if (!res) result = false;
                   }
                }       
             }
          }
          if (result) repeat = false;
       }
    }