# Closing the order with the least profit

91

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
2646

```void KillLeastProfitable() {
int totalorders=OrdersTotal();
int num_array[99999];
int ctm,bbb;
for (bbb=totalorders-1;bbb>=0;bbb--) {
ctm=OrderCloseTime();
// if (ctm==0 && OrderMagicNumber() == uniqueMagic)
if (ctm==0) {
num_array[bbb]=OrderProfit();
}
}
}
bbb=num_array[ArrayMinimum(num_array, 0,0)];
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.
2646

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--){
if(OrderProfit() < minProfit  &&  OrderType() < 2){
minProfit = OrderProfit();
minTicket = OrderTicket();
}
}

if(minTicket > 0){
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 );
}
```
91

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
2646

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`
205

`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.
20208

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