OrderClose when at loss xx

 

Can anyone point me to an article or sample of code that loops through all open orders checks for loss of -10.00 Dollars or -xx.xx and the closes only that order? I searched but can't find anything ... I'm having a mental moment coding this... Just want to close the order when loss is -10.00 or more. Yes I know it's simple....

Thanks
 
n8937g:

Can anyone point me to an article or sample of code that loops through all open orders checks for loss of -10.00 Dollars or -xx.xx and the closes only that order? I searched but can't find anything ... I'm having a mental moment coding this... Just want to close the order when loss is -10.00 or more. Yes I know it's simple....


   // Must loop *down* when closing orders, because it affects OrdersTotal() and the 
   // position of each order 
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
      // Select the order 
      if (OrderSelect(i, SELECT_BY_POS)) {
         // Only look at open orders (not strictly necessary if the profit threshold is negative,
         // because a pending order will have profit of zero and therefore be excluded)
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
            // Optional check on a specific symbol and magic number 
            if (OrderSymbol() == ?? && OrderMagicNumber() == ??) {
               // Check profitability of order. Can use OrderProfit() + OrderSwap() + OrderCommission()
               // for total net profitability
               if (OrderProfit() < -10) {
                  // Order meets criteria. Try closing 
                  if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0)) {
                     // Succeeded
                  } else {
                     int err = GetLastError();
                     Print(StringConcatenate("OrderClose() of ticket #" , OrderTicket() , " failed with error code #" , err));
                  }
               }            
            }        
         }      
      }
   }
 
Hi JJC, Thank you for your help...exactly as needed. Sometimes it's the simple stuff LOL...
Reason: