Download MetaTrader 5

Help to Close order with profit

To add comments, please log in or register
Have you developed an application? Publish it in Code Base!
Daminda
110
Daminda 2015.05.29 17:19 

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()));
        }
    }
    
}
Osama Shaban
1448
Osama Shaban 2015.05.29 19:28  

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. 

Daminda
110
Daminda 2015.06.06 09:41  
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. 
To add comments, please log in or register