position close

 

Hi,

I am a beginner with Metatrader. In the past I worked with Deal Book in the Forex market.

My question should be easy to answer for you:

I have a function "close position" created. I get when calling the function the following error message:

failed cancel order # 52 buy at 00:00 0.00000 [Invalid request]

Return: 10013; Last ticket: 52

Can someone help me? I set all the parameters right? The ticket number is generated on the purchase / sale of the order (ticket = mresult.order;).

Thank you!

dee544

 

// **************************************************************************
// *
// * ClosePosition
// *
// **************************************************************************
int ClosePosition(ulong ticket)
{
   MqlTradeRequest mrequest;
   MqlTradeResult mresult;
   
   mrequest.action      = TRADE_ACTION_REMOVE;
   mrequest.order       = ticket;

   OrderSend(mrequest,mresult);
   // get the result code
   Print("Return: ",mresult.retcode,"; Last Ticket: ",ticket);
   if (mresult.retcode == 10009 || mresult.retcode == 10008) {
      last_stop = 0;
      return(1);
   } else {
      ResetLastError();           
      return(0);
   }
}
 
Ticket should be an one of a pending (working) order. It might not be a ticket of triggered order. How do you get this ticket (show your code)?
 

Hi,

You specified the wrong action, 

 mrequest.action      = TRADE_ACTION_REMOVE;

This is used for deleting pending orders and not for closing opened positions.

To close an open position, you need to specify your action as:

 mrequest.action      = TRADE_ACTION_DEAL;

Also, you will close an opened position with a trade in the opposite position. For example, if you opened a BUY, you will close it when you a open a SELL and vice versa.

 

 

Thank you, so I have a misunderstanding .. 

once I place an order (buy or sell), I can conclude with the sale or purchase again.
"Position close" is only valid for orders not yet executed.

That was very helpful to me.

Reason: