Help to Close order with profit

 

i used following code to close only profit trades. but sometimes it close also loss trades. please some one help to resolve problem

if(OrderProfit()>0 && SellCondition==true)
  {
  int orderstotal = OrdersTotal();
    int orders = 0;
    int ordticket[30][2];
    for (int i = 0; i < orderstotal; i++)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if (OrderType() != OP_BUY || OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber)
        {
            continue;
        }
        ordticket[orders][0] = OrderOpenTime();
        ordticket[orders][1] = OrderTicket();
        orders++;
    }
    if (orders > 1)
    {
        ArrayResize(ordticket,orders);
        ArraySort(ordticket);
    }
    for (i = 0; i < orders; i++)
    {
        if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
        {
            bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
            if (ret == false)
            Print("OrderClose() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}
 

It could be because of Swap !

Try to consider profitable trade is the one like this ...

Profit = OrderProfit() + OrderSwap() + OrderCommission();

 so, in this case, if you have Swap in negative higher than profit itself, then the order will not be considered as a profitable trade.

It could be an other reason, but this for sure will enhance the result a little. 

 
Osama Shaban:

It could be because of Swap !

Try to consider profitable trade is the one like this ...

 so, in this case, if you have Swap in negative higher than profit itself, then the order will not be considered as a profitable trade.

It could be an other reason, but this for sure will enhance the result a little. 

thanks osama for your comment. 
Reason: