Closing the order with the least profit

To add comments, please log in or register
Levente Peres
91
Levente Peres  
Hi guys... I've been killing myself over this piece of script for the last 1 week (yes, one entrie week...) and I still can't find the problem... Can you please help me? I'm trying to write a small function to close up the one order that's currently in the biggest loss (or: the "Least pofitable open order"). The code as follows: [CODE] void KillLeastProfitable() { int totalorders=OrdersTotal(); int num_array[99999]; int ctm,bbb; for (bbb=totalorders-1;bbb>=0;bbb--) { if(OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) { ctm=OrderCloseTime(); // if (ctm==0 && OrderMagicNumber() == uniqueMagic) if (ctm==0) { num_array[bbb]=OrderProfit(); } } } bbb=num_array[ArrayMinimum(num_array, 0,0)]; if (OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) { if ( OrderType() == OP_BUY ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); if ( OrderType() > 1 ) OrderDelete( OrderTicket() ); } } [/CODE] As you can see, the idea is to round up all the orders into one array, then use ArrayMinimum() to select the one with the least profit (or rather: the biggest loss negative), then take that sucker and kill it. At least in theory. Reality is: Nothing happens. However, if I reverse the idea and replace ArrayMinimum with ArrayMaximum, it closes the most profitable order - as you would expect. So the problem - i figure - should be around ArrayMinimum... Can somebody please help me fix this code so that it will close the order with the biggest loss? :( Thank you sincerely... Levente
Ray
2646
Ray  
void KillLeastProfitable() { 
   int totalorders=OrdersTotal(); 
   int num_array[99999]; 
   int ctm,bbb; 
      for (bbb=totalorders-1;bbb>=0;bbb--) { 
         if(OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) { 
            ctm=OrderCloseTime(); 
            // if (ctm==0 && OrderMagicNumber() == uniqueMagic) 
               if (ctm==0) { 
                  num_array[bbb]=OrderProfit(); 
               } 
            } 
         } 
         bbb=num_array[ArrayMinimum(num_array, 0,0)]; 
         if (OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) {
            if ( OrderType() == OP_BUY ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); 
            if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); 
            if ( OrderType() > 1 ) OrderDelete( OrderTicket() ); 
         }
There, now it is readable, at least.
Ray
2646
Ray  

 I might approach it this way, to get the one with the lowest profit:

double minProfit = 100000000;
int minTicket = 0;
 
for(int i = OrdersTotal()-1; i >= 0; i--){
   OrderSelect(i, SEL_BY_POS, MODE_TRADES);
   if(OrderProfit() < minProfit  &&  OrderType() < 2){
      minProfit = OrderProfit();
      minTicket = OrderTicket();
   }
}
 
if(minTicket > 0){ 
    OrderSelect(minTicket, SEL_BY_TICKET, MODE_TRADES);
  if ( OrderType() == OP_BUY )  OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); 
  if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); 
}
Levente Peres
91
Levente Peres  
Many Hails, this is wonderful, now it works!!! Thank you so much! :))))) Btw just for future reference, I realize it's a stupid and redundant question but how do you make the code appear so well formatted in the post? :) Many thanks again, Levente
Ray
2646
Ray  

Select some text in your reply and click the little button in the toolbar as you are editting marked "MQL"

it will put a box like this around the selected text
Busingye Tusasirwe
205
Busingye Tusasirwe  
Thank_You_All();

Ray:

Select some text in your reply and click the little button in the toolbar as you are editting marked "MQL"

Thank you all.
William Roeder
20208
William Roeder  
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
To add comments, please log in or register