Download MetaTrader 5

position close

To add comments, please log in or register
Find out the latest MQL5.community news!
dee544
119
dee544 2010.10.31 21:19 

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);
   }
}
Rashid Umarov
Admin
11928
Rashid Umarov 2010.11.01 08:20  
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)?
Samuel Olowoyo
2241
Samuel Olowoyo 2010.11.01 10:33  

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.

 

dee544
119
dee544 2010.11.01 11:30  

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.

To add comments, please log in or register