How can i make my EA close Lossing position before the wining one?

To add comments, please log in or register
Juan Mata
57
Juan Mata  

With regard to the code attach below, How can i make my EA close Losing positions before the wining one?


int CloseAll(){
    
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         int MN = OrderMagicNumber();
            if(OrderSymbol()==_Symbol && (((MN==MagicNumber)))){
               if(OrderType()==OP_BUY)
                 {
                  if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID+OrderType()),closeSlippage,clrOrangeRed))
                     Print(__FUNCTION__+"Error closing buy order: #"+ErrorDescription(GetLastError()));
                 }
               if(OrderType()==OP_SELL)
                 {
                  if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID+OrderType()),closeSlippage,clrOrangeRed))
                     Print(__FUNCTION__+"Error closing sell order: #"+ErrorDescription(GetLastError()));
                 }
                 Sleep(200);  
            }
      }else{
         Print(__FUNCTION__+"Error selecting order: #"+IntegerToString(GetLastError()));
      }
     
   }
   return GetLastError();
}

Thank you in advance.

Seng Joo Thio
1149
Seng Joo Thio  
Juan Mata:

With regard to the code attach below, How can i make my EA close Losing positions before the wining one?

Thank you in advance.

One way is to call the CloseAll() function twice - first time to close losers, second time close the rest... (perhaps passing a parameter to differentiate the calls).

Another way is to store ticket number and float somewhere (e.g. arrays/structs) in this current loop (i.e. remove the OrderClose()) as "pre-processing"... afterwhich, you can even sort the ticket numbers based on their float, and close them in that sequence.

To add comments, please log in or register