Why is the order being closed?

 

I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?

OrderSelect(OrderA,SELECT_BY_TICKET);
         Alert(OrderProfit());
// It does alert a negative profit
         
if(OrderProfit() > 0)
         {   
            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10);
         }
         else
         {
            TicketA = TicketA + 1;
         }
 
philipvln:

I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?

  if(OrderProfit()>0)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(OrderProfit()>0)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               Print("OrderClose error ",GetLastError());
           }
 
  1. philipvln: I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?
    The code looks fine.
    1. OrderSelect and OrderClose: Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
      Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.
    2. Use the debugger or print out your variables, including _LastError and find out why.

  2. Frank Vang:
    Use OrderClosePrice and you don't have to distinguish between buy/sells Bid/Ask.


 
philipvln:

I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?

Slippage? Commissions? OrderProfit doesn't take commissions or rollover into account. If you have $0.01 commission charge and close at the price where OrderProfit returns 0, then you'll see a $0.01 loss at least.

 
And in the case of sell orders, you pay the spread when you close.
Reason: