How to close an order at current price?

 

Hi, I want to close an order either BUY or SELL at current price, but I use the following function in vain. Please help.

OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE); // This is for SELL orders

OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE); // This is for BUY orders

When I ran this function, it returns a 129 errir which is wrong price.

Thanks.

 

Just found the problem. I am attaching the EA to an EUR/USD chart and the order that I want to close is a USD/CAD order. So the Ask returned is the Ask price for EUR/USD, not USD/CAD.

Is there anyway I can close an order at any price immediately even the EA is attached to another symbol?

Thanks!

 
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID+OrderType()),3,CLR_NONE); // for Buy or Sell orders
 
Oh, I like the MODE_BID+OrderType()... That hadn't occurred to me.
 
Thank you Ray, I wish I had your vast knowledge of MT4 and computing. Take care.
 

Learn by doing

Reason: