How to close a mt5 postion in hedging mode ?

 

0.01 buy  gbpusd    1.2700

0.01 buy  gbpusd    1.2710

How to close one of them using ticket of that position ?

 
Balachandran Chandrasekar:

0.01 buy  gbpusd    1.2700

0.01 buy  gbpusd    1.2710

How to close one of them using ticket of that position ?

This should work.

         MqlTradeResult result={0}; 
         MqlTradeRequest request={0}; 
         request.position = <Ticket>;
         request.action = TRADE_ACTION_DEAL;
         request.volume = <lot size to close>;
         if(type==POSITION_TYPE_BUY)
         {
            request.price=SymbolInfoDouble(<Symbol>,SYMBOL_BID);
            request.type =ORDER_TYPE_SELL;
         }
         else
         {
            request.price=SymbolInfoDouble(<Symbol>,SYMBOL_ASK);
            request.type =ORDER_TYPE_BUY;
         }
         OrderSend(request,result); 
 
Seng Joo Thio:

This should work.

Thank You Dear.

Will try that code on coming Monday.

 

Try this function

#include <Trade/Trade.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool ClosePosition(ulong pTicket)
  {
//--- Preliminary checks for trading functions
   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
     {
      Print("Error: automated trading is not allowed in the terminal settings, or 'AutoTrading' button is disabled.");
      return (false);
     }

   if(PositionSelectByTicket(pTicket)==false)
     {
      Print("Error: cannot select position #",pTicket);
      return (false);
     }

//--- Initialize trading object
   CTrade m_trade;
   m_trade.SetMarginMode();
   m_trade.SetDeviationInPoints(ULONG_MAX);
   m_trade.SetTypeFillingBySymbol(PositionGetString(POSITION_SYMBOL));

//--- Close position
   if(m_trade.PositionClose(pTicket) && m_trade.ResultRetcode()==TRADE_RETCODE_DONE)
     {
      Print("Position closed successfully #",pTicket);
      return (true);
     }
   else
     {
      PrintFormat("Failed to close position #%I64u (%s)",pTicket,m_trade.ResultComment());
      return (false);
     }
  }
 
amrali:

Try this function

I am looking for a non class solution.

 
#property script_show_inputs

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

input TICKET_TYPE PositionTicket = 0;

void OnStart()
{
  if (OrderSelect(PositionTicket, SELECT_BY_TICKET) && (OrderType() <= OP_SELL))
    OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100);    
}
 
fxsaber:

Good Work. Thanks.

Reason: