How to close a trade in EA

 

Hi All,

I did checked forum example and also this https://docs.mql4.com/trading/orderclose.

But, it doesn't close any of the open trade.

The first code I uses, it return a warning as "return value of 'OrderClose' should be checked"


 

OrderClose(tradeTicket,1,Ask,3);

 

So, I uses this. But, it doesn't close the trade.

int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
   OrderSelect(i, SELECT_BY_POS);
   int type   = OrderType();

   bool result = false;

   switch(type)
   {
     //Close opened long positions
     case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                         break;

     //Close opened short positions
     case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

   }

   if(result == false)
   {
     //Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
     Sleep(0);
   }
}


 If anyone can guide me on how to close a trade based on a ticket number in EA, that will be great.

 
bool result = false;

set it to true, not false

then

if(result == false)
   {
     //Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
     Sleep(0);
   }


un-comment the alert so that you can see what the error is.

Sleep(0) does nothing.

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );


use OrderClosePrice()

OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 5, Red );


.
 
Keith Watford:
bool result = false;

set it to true, not false

then

if(result == false)
   {
     //Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
     Sleep(0);
   }


un-comment the alert so that you can see what the error is.

Sleep(0) does nothing.

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );


use OrderClosePrice()

OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 5, Red );


.

Thank you for your help.

 

 I tried to simplify things just to close the trade and did try your advice.

 

But, doesn't seems to work.

bool result = true;
   result = OrderClose(44407768,1,Ask,3,Red);
   Alert(result);
   return(0);


 Alert return always return 0 regardless I changed to true or false.

As I did not set any condition, I expect it to close the trade that is running with the ticket number 44407768.

Any advice is appreciated. 

 
jeannie:

Thank you for your help.

 

 I tried to simplify things just to close the trade and did try your advice.

 

But, doesn't seems to work.

bool result = true;
   result = OrderClose(44407768,1,Ask,3,Red);
   Alert(result);
   return(0);


 Alert return always return 0 regardless I changed to true or false.

As I did not set any condition, I expect it to close the trade that is running with the ticket number 44407768.

Any advice is appreciated. 

I said to un-comment the alert so that you can see the error code, whyhave you changed the alert?
Reason: