How do I close a position without CTrade?

whoowl  

I am trying to learn how to close a position.
I found at least a dozen examples, more like two dozen, but it seems that ALL of them either require CTrade or present a solution that involves iterating over many open orders and selecting by position, which confuses me.

Can someone please provide an example of closing a position in MQL5 that does not involve CTrade (or any other include) and that only applies to ONE position by ticket number specifically? It's seems to be really hard to find.

Fernando Carreiro  
whoowl: I am trying to learn how to close a position. I found at least a dozen examples, more like two dozen, but it seems that ALL of them either require CTrade or present a solution that involves iterating over many open orders and selecting by position, which confuses me. Can someone please provide an example of closing a position in MQL5 that does not involve CTrade (or any other include) and that only applies to ONE position by ticket number specifically? It's seems to be really hard to find.

In MT5, closing a position is done by a trade deal in the opposite direction in reference to an open position. Here is a simple example (NB! position must be previously selected) ...

// Close selected position
   bool PositionClose( void )
   {
      MqlTradeRequest stCloseRequest = {};
      
      switch( (ENUM_POSITION_TYPE) PositionGetInteger( POSITION_TYPE ) )
      {
         case POSITION_TYPE_BUY:
            stCloseRequest.type  = ORDER_TYPE_SELL;
            stCloseRequest.price = SymbolInfoDouble( _Symbol, SYMBOL_BID );
            break;
         case POSITION_TYPE_SELL:
            stCloseRequest.type  = ORDER_TYPE_BUY;
            stCloseRequest.price = SymbolInfoDouble( _Symbol, SYMBOL_ASK );
            break;
         default:
            return false;
      };

      stCloseRequest.action       = TRADE_ACTION_DEAL;
      stCloseRequest.position     = PositionGetInteger( POSITION_TICKET );
      stCloseRequest.volume       = PositionGetDouble(  POSITION_VOLUME );
      stCloseRequest.symbol       = _Symbol;
      stCloseRequest.magic        = i_nMagicNumber;
      stCloseRequest.deviation    = i_nMaxDeviation;
      stCloseRequest.type_filling = g_eOrderFillType;

      MqlTradeResult stCloseResult = {};
      if( !OrderSend( stCloseRequest, stCloseResult ) )
      {
         PrintFormat( "Error %d closing selected position with return code %d (%s)",
            _LastError, stCloseResult.retcode, stCloseResult.comment );
         return false;
      };

      return true;
   };
fxsaber  
whoowl #:

How do I use this code to tell MT5 to please close position with order ticket number 12345678?

bool PositionCloseByTicket( const ulong Ticket )
{
  bool Res = false;
  
  if (PositionSelectByTicket(Ticket))
  {
    MqlTradeRequest Request = {};
    MqlTradeResult Result;
    
    Request.action = TRADE_ACTION_DEAL;
    Request.position = Ticket;

    Request.symbol = PositionGetString(POSITION_SYMBOL);

    Request.volume = PositionGetDouble(POSITION_VOLUME);
    Request.price = PositionGetDouble(POSITION_PRICE_CURRENT);

    Request.type = (ENUM_ORDER_TYPE)(1 - PositionGetInteger(POSITION_TYPE));
    
//    Request.type_filling = 
    
    Res = OrderSend(Request, Result);
  }
    
  return(Res);  
}

void OnStart()
{
  const ulong Ticket = PositionGetTicket(0); // 12345678
  
  PositionCloseByTicket(Ticket);
}


P.S.

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void OnStart()
{
  const ulong Ticket = PositionGetTicket(0); // 12345678

  if (OrderSelect(Ticket, SELECT_BY_TICKET))
    OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
}
Fernando Carreiro  
whoowl #: Thank you for the code, but I don't see how I can use it. Where and how is one supposed to use it? I need to be able to close an order by ticket. How do I use this code to tell MT5 to please close position with order ticket number 12345678?

Select the position by ticket first, then you can call the the function in my example.

PositionSelectByTicket( nTicket );
PositionClose();
Reason: